Cornell Cocos
Cornell Extensions to Cocos2d
CUSound.h
1 //
2 // CUSound.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This method provides a platform-independent way of refering to a (potentially)
6 // loaded sound asset. Technically, sounds are just referred to be file name, so
7 // this class seems unnecessary. However there is a lot of hidden functionality
8 // in the experimental AudioEngine for querying information like play duration or
9 // file format. We have made changes to the AudioEngine to expose this information.
10 // As AudioEngine is marked as experimental anyway, we felt this was okay.
11 //
12 // Author: Walker White
13 // Version: 12/10/15
14 //
15 #ifndef __CU_SOUND_H__
16 #define __CU_SOUND_H__
17 
18 #include <cocos2d.h>
19 #include <audio/include/AudioEngine.h>
20 
21 NS_CC_BEGIN
22 
23 #define AENG experimental::AudioEngine
24 
25 #pragma mark -
26 #pragma mark Sound Class
27 
28 class SoundLoader;
29 
42 class CC_DLL Sound : public Ref {
43 private:
45  CC_DISALLOW_COPY_AND_ASSIGN(Sound);
46 
47 protected:
49  std::string _source;
51  float _duration;
52 
53 public:
54 #pragma mark Attributes
55 
60  const std::string& getSource() const { return _source; }
61 
70  std::string getSuffix() const {
71  size_t pos = _source.rfind(".");
72  return (pos == std::string::npos ? "" : _source.substr(pos));
73  }
74 
83  float getDuration() const { return _duration; }
84 
92  bool isPreloaded() const { return _duration != AENG::TIME_UNKNOWN; }
93 
94 
95 CC_CONSTRUCTOR_ACCESS:
96 #pragma mark Initializers
97 
100  Sound() : Ref(), _source(""), _duration(0.0f) {}
101 
107  ~Sound() { CCASSERT(_source.empty(), "Sound asset was not unloaded"); }
108 
118  static Sound* create(std::string source);
119 
130  static Sound* create(std::string source, float duration);
131 
141  bool init(std::string source) {
142  _source = source; _duration = experimental::AudioEngine::TIME_UNKNOWN;
143  return true;
144  }
145 
156  bool init(std::string source, float duration) {
157  _source = source; _duration = duration;
158  return true;
159  }
160 
162  friend class SoundLoader;
163 };
164 
165 NS_CC_END
166 
167 #endif /* defined(__CU_SOUND_H__) */
Definition: CUSound.h:42
float getDuration() const
Definition: CUSound.h:83
~Sound()
Definition: CUSound.h:107
const std::string & getSource() const
Definition: CUSound.h:60
std::string _source
Definition: CUSound.h:49
std::string getSuffix() const
Definition: CUSound.h:70
bool isPreloaded() const
Definition: CUSound.h:92
Definition: CUSoundLoader.h:56
float _duration
Definition: CUSound.h:51
bool init(std::string source, float duration)
Definition: CUSound.h:156
bool init(std::string source)
Definition: CUSound.h:141