CUGL
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 <unordered_map>
45 #include <vector>
46 #include <deque>
47 
49 #define AUDIO_INPUT_CHANNELS 24
50 
51 #define AUDIO_OUTPUT_CHANNELS 2
52 
53 #define AUDIO_FREQUENCY 44100
54 
56 #if defined (__MACOSX__) || defined (__IPHONEOS__)
57  #define CU_AUDIO_AVFOUNDATION 1
58 #endif
59 
60 
61 namespace cugl {
62 
64 class SoundChannel;
66 class MusicQueue;
67 
91 class AudioEngine {
92 #pragma mark -
93 #pragma mark Sound State
94 public:
98  enum class State {
100  INACTIVE,
102  PLAYING,
104  PAUSED
105  };
106 
107 
108 private:
110  static AudioEngine* _gEngine;
111 
113  std::shared_ptr<MusicQueue> _mqueue;
114 
116  unsigned int _capacity;
118  std::vector<std::shared_ptr<SoundChannel>> _channels;
120  std::unordered_map<std::string,int> _effects;
122  std::deque<std::string> _equeue;
123 
135  std::function<void(Music* asset, bool status)> _musicCB;
136 
148  std::function<void(const std::string& key , bool status)> _soundCB;
149 
150 #pragma mark -
151 #pragma mark Constructors (Private)
152 
157  AudioEngine() : _capacity(0) {}
158 
165  ~AudioEngine() { dispose(); }
166 
178  bool init(unsigned int channels=AUDIO_INPUT_CHANNELS);
179 
186  void dispose();
187 
188 
189 #pragma mark -
190 #pragma mark Internal Helpers
191 
199  void removeKey(std::string key);
200 
201 
202 #pragma mark -
203 #pragma mark Static Accessors
204 public:
213  static AudioEngine* get() { return _gEngine; }
214 
230  static void start(unsigned int channels=AUDIO_INPUT_CHANNELS);
231 
242  static void stop();
243 
244 
245 #pragma mark -
246 #pragma mark Music Management
247 
271  void playMusic(const std::shared_ptr<Music>& music, bool loop=false, float volume=-1.0f, float fade=0.0f);
272 
280  const Music* currentMusic() const;
281 
287  State getMusicState() const;
288 
296  bool isMusicLoop() const;
297 
309  void setMusicLoop(bool loop); // Clears the queue
310 
318  float getMusicVolume() const;
319 
327  void setMusicVolume(float volume);
328 
341  float getMusicDuration() const;
342 
358  double getMusicElapsed() const;
359 
376  double getMusicRemaining() const;
377 
394  void setMusicElapsed(double time);
395 
413  void setMusicRemaining(double time);
414 
427  void stopMusic(float fade=0.0f);
428 
435  void clearMusicQueue();
436 
442  void pauseMusic();
443 
449  void resumeMusic();
450 
461  void setMusicListener(std::function<void(Music*,bool)> callback) {
462  _musicCB = callback;
463  }
464 
475  std::function<void(Music*,bool)> getMusicListener() const {
476  return _musicCB;
477  }
478 
492  void gcMusic(bool status);
493 
494 
495 #pragma mark -
496 #pragma mark Music Queue
497 
521  void queueMusic(const std::shared_ptr<Music>& music, bool loop=false, float volume=-1.0f, float fade=0.0f);
522 
528  const std::vector<const Music*> getMusicQueue() const;
529 
535  size_t getMusicQueueSize() const;
536 
547  void advanceMusicQueue(unsigned int steps=0);
548 
549 
550 #pragma mark -
551 #pragma mark Sound Effect Management
552 
576  bool playEffect(const std::string& key, const std::shared_ptr<Sound>& sound,
577  bool loop=false, float volume=-1.0f, bool force=false);
578 
603  bool playEffect(const char* key, const std::shared_ptr<Sound>& sound,
604  bool loop=false, float volume=-1.0f, bool force=false) {
605  return playEffect(std::string(key),sound,loop,volume,force);
606  }
607 
618  size_t getAvailableChannels() const {
619  return (size_t)_capacity-_effects.size();
620  }
621 
632  State getEffectState(const std::string& key) const;
633 
644  State getEffectState(const char* key) const {
645  return getEffectState(std::string(key));
646  }
647 
655  bool isActiveEffect(const std::string& key) const {
656  return _effects.find(key) != _effects.end();
657  }
658 
666  bool isActiveEffect(const char* key) const {
667  return isActiveEffect(std::string(key));
668  }
669 
680  const Sound* currentEffect(const std::string& key) const;
681 
692  const Sound* currentEffect(const char* key) const {
693  return currentEffect(std::string(key));
694  }
695 
705  bool isEffectLoop(const std::string& key) const;
706 
716  bool isEffectLoop(const char* key) const {
717  return isEffectLoop(std::string(key));
718  }
719 
728  void setEffectLoop(const std::string& key, bool loop);
729 
738  void setEffectLoop(const char* key, bool loop) {
739  setEffectLoop(std::string(key),loop);
740  }
741 
751  float getEffectVolume(const std::string& key) const;
752 
762  float getEffectVolume(const char* key) const {
763  return getEffectVolume(std::string(key));
764  }
765 
774  void setEffectVolume(const std::string& key, float volume);
775 
784  void setEffectVolume(const char* key, float volume) {
785  setEffectVolume(std::string(key),volume);
786  }
787 
800  float getEffectDuration(const std::string& key) const;
801 
814  float getEffectDuration(const char* key) const {
815  return getEffectDuration(std::string(key));
816  }
817 
832  float getEffectElapsed(const std::string& key) const;
833 
848  float getEffectElapsed(const char* key) const {
849  return getEffectElapsed(std::string(key));
850  }
851 
866  float getEffectRemaining(const std::string& key) const;
867 
882  float getEffectRemaining(const char* key) const {
883  return getEffectRemaining(std::string(key));
884  }
885 
899  void setEffectElapsed(const std::string& key, float time);
900 
914  void setEffectElapsed(const char* key, float time) {
915  setEffectElapsed(std::string(key),time);
916  }
917 
931  void setEffectRemaining(const std::string& key, float time);
932 
946  void setEffectRemaining(const char* key, float time) {
947  setEffectRemaining(std::string(key),time);
948  }
949 
959  void stopEffect(const std::string& key);
960 
970  void stopEffect(const char* key) {
971  stopEffect(std::string(key));
972  }
973 
981  void pauseEffect(const std::string& key);
982 
990  void pauseEffect(const char* key) {
991  pauseEffect(std::string(key));
992  }
993 
1001  void resumeEffect(std::string key);
1002 
1010  void resumeEffect(const char* key) {
1011  resumeEffect(std::string(key));
1012  }
1013 
1019  void stopAllEffects();
1020 
1026  void pauseAllEffects();
1027 
1031  void resumeAllEffects();
1032 
1043  void setEffectListener(std::function<void(const std::string& key,bool)> callback) {
1044  _soundCB = callback;
1045  }
1046 
1057  std::function<void(const std::string& key,bool)> getEffectListener() const {
1058  return _soundCB;
1059  }
1060 
1073  void gcEffect(int id, bool status);
1074 
1075 
1076 #pragma mark -
1077 #pragma mark Global Management
1078 
1083  void stopAll();
1084 
1091  void pauseAll();
1092 
1099  void resumeAll();
1100 };
1101 
1102 }
1103 
1104 
1105 #endif /* __CU_AUDIO_ENGINE_H__ */
Definition: CUSound.h:84
Definition: CUMusic.h:81
float getMusicVolume() const
void resumeEffect(const char *key)
Definition: CUAudioEngine.h:1010
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:762
size_t getMusicQueueSize() const
std::function< void(const std::string &key, bool)> getEffectListener() const
Definition: CUAudioEngine.h:1057
void setEffectLoop(const char *key, bool loop)
Definition: CUAudioEngine.h:738
bool isActiveEffect(const std::string &key) const
Definition: CUAudioEngine.h:655
std::function< void(Music *, bool)> getMusicListener() const
Definition: CUAudioEngine.h:475
void setMusicVolume(float volume)
const std::vector< const Music * > getMusicQueue() const
void stopEffect(const char *key)
Definition: CUAudioEngine.h:970
Definition: CUAudioEngine.h:91
size_t getAvailableChannels() const
Definition: CUAudioEngine.h:618
const Sound * currentEffect(const std::string &key) const
void pauseEffect(const char *key)
Definition: CUAudioEngine.h:990
State getEffectState(const char *key) const
Definition: CUAudioEngine.h:644
void setEffectListener(std::function< void(const std::string &key, bool)> callback)
Definition: CUAudioEngine.h:1043
void setMusicRemaining(double time)
void stopEffect(const std::string &key)
void setMusicListener(std::function< void(Music *, bool)> callback)
Definition: CUAudioEngine.h:461
void setEffectRemaining(const char *key, float time)
Definition: CUAudioEngine.h:946
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 getEffectElapsed(const char *key) const
Definition: CUAudioEngine.h:848
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:692
float getMusicDuration() const
State getMusicState() const
double getMusicRemaining() 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:603
float getEffectElapsed(const std::string &key) const
void setMusicLoop(bool loop)
void pauseEffect(const std::string &key)
void setMusicElapsed(double time)
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:784
bool isEffectLoop(const char *key) const
Definition: CUAudioEngine.h:716
void gcMusic(bool status)
Definition: CUAnimationNode.h:52
float getEffectVolume(const std::string &key) const
void setEffectElapsed(const char *key, float time)
Definition: CUAudioEngine.h:914
State
Definition: CUAudioEngine.h:98
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)
double getMusicElapsed() const
void resumeEffect(std::string key)
float getEffectRemaining(const std::string &key) const
float getEffectDuration(const char *key) const
Definition: CUAudioEngine.h:814
float getEffectRemaining(const char *key) const
Definition: CUAudioEngine.h:882
bool isActiveEffect(const char *key) const
Definition: CUAudioEngine.h:666