CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUMusic.h
1 //
2 // CUMusic.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a stream music asset. While we support a wide range of
6 // formats, only WAV, MP3, OGG are guaranteed to be supported on all CUGL
7 // platforms (Android and iOS have very little overlap here). MP3 files are
8 // under patent until April 2017. OGG is a popular alternative in games, but
9 // it is still a lossy encoding. WAV files are extremely large and are not
10 // suitable for streaming. If anything, you want want to create platform
11 // specific assets and differentiate them in your asset directory.
12 //
13 // We have factored out the platform-dependent code and have hidden it
14 // behind this class as an abstraction.
15 //
16 // This class uses our standard shared-pointer architecture.
17 //
18 // 1. The constructor does not perform any initialization; it just sets all
19 // attributes to their defaults.
20 //
21 // 2. All initialization takes place via init methods, which can fail if an
22 // object is initialized more than once.
23 //
24 // 3. All allocation takes place via static constructors which return a shared
25 // pointer.
26 //
27 //
28 // CUGL zlib License:
29 // This software is provided 'as-is', without any express or implied
30 // warranty. In no event will the authors be held liable for any damages
31 // arising from the use of this software.
32 //
33 // Permission is granted to anyone to use this software for any purpose,
34 // including commercial applications, and to alter it and redistribute it
35 // freely, subject to the following restrictions:
36 //
37 // 1. The origin of this software must not be misrepresented; you must not
38 // claim that you wrote the original software. If you use this software
39 // in a product, an acknowledgment in the product documentation would be
40 // appreciated but is not required.
41 //
42 // 2. Altered source versions must be plainly marked as such, and must not
43 // be misrepresented as being the original software.
44 //
45 // 3. This notice may not be removed or altered from any source distribution.
46 //
47 // Author: Walker White
48 // Version: 12/10/16
49 //
50 #ifndef __CU_MUSIC_H__
51 #define __CU_MUSIC_H__
52 #include <cugl/base/CUBase.h>
53 
54 namespace cugl {
55 
56 // We use the impl namespace for platform-dependent data.
57 namespace impl {
65  struct AudioStream;
66 }
67 
81 class Music {
82 private:
84  CU_DISALLOW_COPY_AND_ASSIGN(Music);
85 
87  std::string _source;
89  impl::AudioStream* _buffer;
91  float _volume;
92 
93 public:
101  enum class Type : int {
103  WAV = 0,
105  MP3 = 1,
107  AAC = 2,
109  OGG = 3,
111  M4A = 4,
113  FLAC = 5,
115  UNSUPPORTED = 6
116  };
117 
118 
119 #pragma mark -
120 #pragma mark Constructors
121 
127  Music() : _source(""), _buffer(nullptr) {}
128 
132  ~Music() { dispose(); }
133 
140  void dispose();
141 
158  bool init(const std::string& source);
159 
176  bool init(const char* source) {
177  return init(std::string(source));
178  }
179 
180 #pragma mark -
181 #pragma mark Static Constructors
182 
198  static std::shared_ptr<Music> alloc(const std::string& source) {
199  std::shared_ptr<Music> result = std::make_shared<Music>();
200  return (result->init(source) ? result : nullptr);
201  }
202 
219  static std::shared_ptr<Music> alloc(const char* source) {
220  std::shared_ptr<Music> result = std::make_shared<Music>();
221  return (result->init(source) ? result : nullptr);
222  }
223 
224 #pragma mark Attributes
225 
230  const std::string& getSource() const { return _source; }
231 
240  std::string getSuffix() const {
241  size_t pos = _source.rfind(".");
242  return (pos == std::string::npos ? "" : _source.substr(pos));
243  }
244 
253  double getDuration() const;
254 
260  Type getType() const;
261 
271  float getVolume() const { return _volume; }
272 
282  void setVolume(float volume);
283 
285  friend class MusicQueue;
286 };
287 
288 }
289 
290 #endif /* __CU_MUSIC_H__ */
Definition: CUMusic.h:81
Type
Definition: CUMusic.h:101
Music()
Definition: CUMusic.h:127
static std::shared_ptr< Music > alloc(const std::string &source)
Definition: CUMusic.h:198
double getDuration() const
std::string getSuffix() const
Definition: CUMusic.h:240
bool init(const std::string &source)
bool init(const char *source)
Definition: CUMusic.h:176
friend class MusicQueue
Definition: CUMusic.h:285
void dispose()
static std::shared_ptr< Music > alloc(const char *source)
Definition: CUMusic.h:219
float getVolume() const
Definition: CUMusic.h:271
Type getType() const
Definition: CUAnimationNode.h:52
~Music()
Definition: CUMusic.h:132
const std::string & getSource() const
Definition: CUMusic.h:230
void setVolume(float volume)