CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUSound.h
1 //
2 // CUSound.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a preloaded sound asset. In general, these assets
6 // should be WAV files (as there is no benefit to compression once they are
7 // loaded into memory), but we do have support for other file types. However,
8 // at this time, only WAV, MP3, and OGG are guaranteed to be supported on all
9 // CUGL platforms (Android and iOS have very little overlap here).
10 //
11 // We have factored out the platform-dependent code and have hidden it
12 // behind this class as an abstraction.
13 //
14 // This class uses our standard shared-pointer architecture.
15 //
16 // 1. The constructor does not perform any initialization; it just sets all
17 // attributes to their defaults.
18 //
19 // 2. All initialization takes place via init methods, which can fail if an
20 // object is initialized more than once.
21 //
22 // 3. All allocation takes place via static constructors which return a shared
23 // pointer.
24 //
25 //
26 // CUGL zlib License:
27 // This software is provided 'as-is', without any express or implied
28 // warranty. In no event will the authors be held liable for any damages
29 // arising from the use of this software.
30 //
31 // Permission is granted to anyone to use this software for any purpose,
32 // including commercial applications, and to alter it and redistribute it
33 // freely, subject to the following restrictions:
34 //
35 // 1. The origin of this software must not be misrepresented; you must not
36 // claim that you wrote the original software. If you use this software
37 // in a product, an acknowledgment in the product documentation would be
38 // appreciated but is not required.
39 //
40 // 2. Altered source versions must be plainly marked as such, and must not
41 // be misrepresented as being the original software.
42 //
43 // 3. This notice may not be removed or altered from any source distribution.
44 //
45 // Author: Walker White
46 // Version: 12/10/16
47 //
48 #ifndef __CU_SOUND_H__
49 #define __CU_SOUND_H__
50 #include <cugl/base/CUBase.h>
51 
52 #pragma mark -
53 #pragma mark Sound Class
54 
55 namespace cugl {
56 
57 // We use the impl namespace for platform-dependent data.
58 namespace impl {
66  struct AudioBuffer;
67 }
68 
84 class Sound {
85 private:
87  CU_DISALLOW_COPY_AND_ASSIGN(Sound);
88 
90  std::string _source;
92  impl::AudioBuffer* _buffer;
94  float _volume;
95 
96 #pragma mark -
97 #pragma mark Constructors
98 public:
105  Sound() : _source(""), _buffer(nullptr), _volume(1) {}
106 
110  ~Sound() { dispose(); }
111 
118  void dispose();
119 
130  bool init(const std::string& source);
131 
142  bool init(const char* source) {
143  return init(std::string(source));
144  }
145 
146 #pragma mark -
147 #pragma mark Static Constructors
148 
158  static std::shared_ptr<Sound> alloc(const std::string& source) {
159  std::shared_ptr<Sound> result = std::make_shared<Sound>();
160  return (result->init(source) ? result : nullptr);
161  }
162 
173  static std::shared_ptr<Sound> alloc(const char* source) {
174  std::shared_ptr<Sound> result = std::make_shared<Sound>();
175  return (result->init(source) ? result : nullptr);
176  }
177 
178 #pragma mark Attributes
179 
184  const std::string& getSource() const { return _source; }
185 
194  std::string getSuffix() const {
195  size_t pos = _source.rfind(".");
196  return (pos == std::string::npos ? "" : _source.substr(pos));
197  }
198 
207  double getDuration() const;
208 
214  double getSampleRate() const;
215 
223  Uint64 getLength() const;
224 
234  Uint32 getChannels() const;
235 
245  float getVolume() const { return _volume; }
246 
256  void setVolume(float volume);
257 
259  friend class SoundChannel;
260 };
261 
262 }
263 
264 #endif /* __CU_SOUND_H__ */
Definition: CUSound.h:84
std::string getSuffix() const
Definition: CUSound.h:194
Uint64 getLength() const
double getSampleRate() const
Sound()
Definition: CUSound.h:105
const std::string & getSource() const
Definition: CUSound.h:184
friend class SoundChannel
Definition: CUSound.h:259
bool init(const std::string &source)
void setVolume(float volume)
void dispose()
double getDuration() const
~Sound()
Definition: CUSound.h:110
static std::shared_ptr< Sound > alloc(const char *source)
Definition: CUSound.h:173
static std::shared_ptr< Sound > alloc(const std::string &source)
Definition: CUSound.h:158
Uint32 getChannels() const
bool init(const char *source)
Definition: CUSound.h:142
float getVolume() const
Definition: CUSound.h:245
Definition: CUAnimationNode.h:52