CUGL 1.1
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioEngine.h
1 //
2 // CUAudioEngine.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module is a singleton providing a simple 2000-era audio engine. Like
6 // all engines of this era, it provides a flat channel structure for playing
7 // sounds as well as a single channel for background music. This is much
8 // more primitive than modern sound engines, with the advantage that it is
9 // much simpler to use.
10 //
11 // Because this is a singleton, there are no publicly accessible constructors
12 // or intializers. Use the static methods instead.
13 //
14 // We have factored out the platform-dependent code and have hidden it
15 // behind this class as an abstraction.
16 //
17 // CUGL zlib License:
18 // This software is provided 'as-is', without any express or implied
19 // warranty. In no event will the authors be held liable for any damages
20 // arising from the use of this software.
21 //
22 // Permission is granted to anyone to use this software for any purpose,
23 // including commercial applications, and to alter it and redistribute it
24 // freely, subject to the following restrictions:
25 //
26 // 1. The origin of this software must not be misrepresented; you must not
27 // claim that you wrote the original software. If you use this software
28 // in a product, an acknowledgment in the product documentation would be
29 // appreciated but is not required.
30 //
31 // 2. Altered source versions must be plainly marked as such, and must not
32 // be misrepresented as being the original software.
33 //
34 // 3. This notice may not be removed or altered from any source distribution.
35 //
36 // Author: Walker White
37 // Version: 12/10/16
38 //
39 #ifndef __CU_AUDIO_ENGINE_H__
40 #define __CU_AUDIO_ENGINE_H__
41 #include <cugl/audio/CUSound.h>
42 #include <cugl/audio/CUMusic.h>
43 #include <cugl/util/CUTimestamp.h>
44 #include <functional>
45 #include <unordered_map>
46 #include <vector>
47 #include <deque>
48 
50 #define AUDIO_INPUT_CHANNELS 24
51 
52 #define AUDIO_OUTPUT_CHANNELS 2
53 
54 #define AUDIO_FREQUENCY 44100
55 
57 #if defined (__MACOSX__) || defined (__IPHONEOS__)
58  #define CU_AUDIO_AVFOUNDATION 1
59 #endif
60 
61 
62 namespace cugl {
63 
65 class SoundChannel;
67 class MusicQueue;
68 
92 class AudioEngine {
93 #pragma mark -
94 #pragma mark Sound State
95 public:
99  enum class State {
101  INACTIVE,
103  PLAYING,
105  PAUSED
106  };
107 
108 
109 private:
111  static AudioEngine* _gEngine;
112 
114  std::shared_ptr<MusicQueue> _mqueue;
115 
117  unsigned int _capacity;
119  std::vector<std::shared_ptr<SoundChannel>> _channels;
121  std::unordered_map<std::string,int> _effects;
123  std::deque<std::string> _equeue;
124 
136  std::function<void(Music* asset, bool status)> _musicCB;
137 
149  std::function<void(const std::string& key , bool status)> _soundCB;
150 
151 #pragma mark -
152 #pragma mark Constructors (Private)
153 
158  AudioEngine() : _capacity(0) {}
159 
166  ~AudioEngine() { dispose(); }
167 
179  bool init(unsigned int channels=AUDIO_INPUT_CHANNELS);
180 
187  void dispose();
188 
189 
190 #pragma mark -
191 #pragma mark Internal Helpers
192 
200  void removeKey(std::string key);
201 
202 
203 #pragma mark -
204 #pragma mark Static Accessors
205 public:
214  static AudioEngine* get() { return _gEngine; }
215 
231  static void start(unsigned int channels=AUDIO_INPUT_CHANNELS);
232 
243  static void stop();
244 
245 
246 #pragma mark -
247 #pragma mark Music Management
248 
272  void playMusic(const std::shared_ptr<Music>& music, bool loop=false, float volume=-1.0f, float fade=0.0f);
273 
281  const Music* currentMusic() const;
282 
288  State getMusicState() const;
289 
297  bool isMusicLoop() const;
298 
310  void setMusicLoop(bool loop); // Clears the queue
311 
319  float getMusicVolume() const;
320 
328  void setMusicVolume(float volume);
329 
342  float getMusicDuration() const;
343 
359  float getMusicElapsed() const;
360 
377  float getMusicRemaining() const;
378 
395  void setMusicElapsed(float time);
396 
414  void setMusicRemaining(float time);
415 
428  void stopMusic(float fade=0.0f);
429 
436  void clearMusicQueue();
437 
443  void pauseMusic();
444 
450  void resumeMusic();
451 
462  void setMusicListener(std::function<void(Music*,bool)> callback) {
463  _musicCB = callback;
464  }
465 
476  std::function<void(Music*,bool)> getMusicListener() const {
477  return _musicCB;
478  }
479 
493  void gcMusic(bool status);
494 
495 
496 #pragma mark -
497 #pragma mark Music Queue
498 
522  void queueMusic(const std::shared_ptr<Music>& music, bool loop=false, float volume=-1.0f, float fade=0.0f);
523 
529  const std::vector<const Music*> getMusicQueue() const;
530 
536  size_t getMusicQueueSize() const;
537 
548  void advanceMusicQueue(unsigned int steps=0);
549 
550 
551 #pragma mark -
552 #pragma mark Sound Effect Management
553 
577  bool playEffect(const std::string& key, const std::shared_ptr<Sound>& sound,
578  bool loop=false, float volume=-1.0f, bool force=false);
579 
604  bool playEffect(const char* key, const std::shared_ptr<Sound>& sound,
605  bool loop=false, float volume=-1.0f, bool force=false) {
606  return playEffect(std::string(key),sound,loop,volume,force);
607  }
608 
619  size_t getAvailableChannels() const {
620  return (size_t)_capacity-_effects.size();
621  }
622 
633  State getEffectState(const std::string& key) const;
634 
645  State getEffectState(const char* key) const {
646  return getEffectState(std::string(key));
647  }
648 
656  bool isActiveEffect(const std::string& key) const {
657  return _effects.find(key) != _effects.end();
658  }
659 
667  bool isActiveEffect(const char* key) const {
668  return isActiveEffect(std::string(key));
669  }
670 
681  const Sound* currentEffect(const std::string& key) const;
682 
693  const Sound* currentEffect(const char* key) const {
694  return currentEffect(std::string(key));
695  }
696 
706  bool isEffectLoop(const std::string& key) const;
707 
717  bool isEffectLoop(const char* key) const {
718  return isEffectLoop(std::string(key));
719  }
720 
729  void setEffectLoop(const std::string& key, bool loop);
730 
739  void setEffectLoop(const char* key, bool loop) {
740  setEffectLoop(std::string(key),loop);
741  }
742 
752  float getEffectVolume(const std::string& key) const;
753 
763  float getEffectVolume(const char* key) const {
764  return getEffectVolume(std::string(key));
765  }
766 
775  void setEffectVolume(const std::string& key, float volume);
776 
785  void setEffectVolume(const char* key, float volume) {
786  setEffectVolume(std::string(key),volume);
787  }
788 
801  float getEffectDuration(const std::string& key) const;
802 
815  float getEffectDuration(const char* key) const {
816  return getEffectDuration(std::string(key));
817  }
818 
833  float getEffectElapsed(const std::string& key) const;
834 
849  float getEffectElapsed(const char* key) const {
850  return getEffectElapsed(std::string(key));
851  }
852 
867  float getEffectRemaining(const std::string& key) const;
868 
883  float getEffectRemaining(const char* key) const {
884  return getEffectRemaining(std::string(key));
885  }
886 
900  void setEffectElapsed(const std::string& key, float time);
901 
915  void setEffectElapsed(const char* key, float time) {
916  setEffectElapsed(std::string(key),time);
917  }
918 
932  void setEffectRemaining(const std::string& key, float time);
933 
947  void setEffectRemaining(const char* key, float time) {
948  setEffectRemaining(std::string(key),time);
949  }
950 
960  void stopEffect(const std::string& key);
961 
971  void stopEffect(const char* key) {
972  stopEffect(std::string(key));
973  }
974 
982  void pauseEffect(const std::string& key);
983 
991  void pauseEffect(const char* key) {
992  pauseEffect(std::string(key));
993  }
994 
1002  void resumeEffect(std::string key);
1003 
1011  void resumeEffect(const char* key) {
1012  resumeEffect(std::string(key));
1013  }
1014 
1020  void stopAllEffects();
1021 
1027  void pauseAllEffects();
1028 
1032  void resumeAllEffects();
1033 
1044  void setEffectListener(std::function<void(const std::string& key,bool)> callback) {
1045  _soundCB = callback;
1046  }
1047 
1058  std::function<void(const std::string& key,bool)> getEffectListener() const {
1059  return _soundCB;
1060  }
1061 
1074  void gcEffect(int id, bool status);
1075 
1076 
1077 #pragma mark -
1078 #pragma mark Global Management
1079 
1084  void stopAll();
1085 
1092  void pauseAll();
1093 
1100  void resumeAll();
1101 };
1102 
1103 }
1104 
1105 
1106 #endif /* __CU_AUDIO_ENGINE_H__ */
Definition: CUSound.h:84
Definition: CUMusic.h:81
float getMusicVolume() const
void resumeEffect(const char *key)
Definition: CUAudioEngine.h:1011
static void stop()
const Music * currentMusic() const
static void start(unsigned int channels=AUDIO_INPUT_CHANNELS)
float getEffectVolume(const char *key) const
Definition: CUAudioEngine.h:763
size_t getMusicQueueSize() const
std::function< void(const std::string &key, bool)> getEffectListener() const
Definition: CUAudioEngine.h:1058
void setEffectLoop(const char *key, bool loop)
Definition: CUAudioEngine.h:739
bool isActiveEffect(const std::string &key) const
Definition: CUAudioEngine.h:656
std::function< void(Music *, bool)> getMusicListener() const
Definition: CUAudioEngine.h:476
void setMusicVolume(float volume)
const std::vector< const Music * > getMusicQueue() const
void stopEffect(const char *key)
Definition: CUAudioEngine.h:971
Definition: CUAudioEngine.h:92
size_t getAvailableChannels() const
Definition: CUAudioEngine.h:619
const Sound * currentEffect(const std::string &key) const
void pauseEffect(const char *key)
Definition: CUAudioEngine.h:991
State getEffectState(const char *key) const
Definition: CUAudioEngine.h:645
float getMusicRemaining() const
void setEffectListener(std::function< void(const std::string &key, bool)> callback)
Definition: CUAudioEngine.h:1044
void stopEffect(const std::string &key)
void setMusicListener(std::function< void(Music *, bool)> callback)
Definition: CUAudioEngine.h:462
void setEffectRemaining(const char *key, float time)
Definition: CUAudioEngine.h:947
void setEffectLoop(const std::string &key, bool loop)
bool isEffectLoop(const std::string &key) const
State getEffectState(const std::string &key) const
bool isMusicLoop() const
float getMusicElapsed() const
float getEffectElapsed(const char *key) const
Definition: CUAudioEngine.h:849
void setMusicElapsed(float time)
float getEffectDuration(const std::string &key) const
void gcEffect(int id, bool status)
void setEffectElapsed(const std::string &key, float time)
void advanceMusicQueue(unsigned int steps=0)
void setEffectRemaining(const std::string &key, float time)
void stopMusic(float fade=0.0f)
const Sound * currentEffect(const char *key) const
Definition: CUAudioEngine.h:693
float getMusicDuration() const
State getMusicState() const
void setEffectVolume(const std::string &key, float volume)
bool playEffect(const char *key, const std::shared_ptr< Sound > &sound, bool loop=false, float volume=-1.0f, bool force=false)
Definition: CUAudioEngine.h:604
float getEffectElapsed(const std::string &key) const
void setMusicLoop(bool loop)
void pauseEffect(const std::string &key)
void queueMusic(const std::shared_ptr< Music > &music, bool loop=false, float volume=-1.0f, float fade=0.0f)
void setEffectVolume(const char *key, float volume)
Definition: CUAudioEngine.h:785
bool isEffectLoop(const char *key) const
Definition: CUAudioEngine.h:717
void gcMusic(bool status)
Definition: CUAction.h:51
float getEffectVolume(const std::string &key) const
void setEffectElapsed(const char *key, float time)
Definition: CUAudioEngine.h:915
State
Definition: CUAudioEngine.h:99
void playMusic(const std::shared_ptr< Music > &music, bool loop=false, float volume=-1.0f, float fade=0.0f)
bool playEffect(const std::string &key, const std::shared_ptr< Sound > &sound, bool loop=false, float volume=-1.0f, bool force=false)
void setMusicRemaining(float time)
void resumeEffect(std::string key)
float getEffectRemaining(const std::string &key) const
float getEffectDuration(const char *key) const
Definition: CUAudioEngine.h:815
float getEffectRemaining(const char *key) const
Definition: CUAudioEngine.h:883
bool isActiveEffect(const char *key) const
Definition: CUAudioEngine.h:667