CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioManager.h
1 //
2 // CUAudiomanager.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module is a singleton for managing audio in the game engine. This
6 // singleton can support multiple input and output devices for complex
7 // filter graphs. This class is for when developers needs direct access
8 // to the audio graph(s). In most cases, developers can use AudioChannels
9 // instead, which is built on top of this manager.
10 //
11 // Because this is a singleton, there are no publicly accessible constructors
12 // or intializers. Use the static methods instead.
13 //
14 // CUGL MIT License:
15 // This software is provided 'as-is', without any express or implied
16 // warranty. In no event will the authors be held liable for any damages
17 // arising from the use of this software.
18 //
19 // Permission is granted to anyone to use this software for any purpose,
20 // including commercial applications, and to alter it and redistribute it
21 // freely, subject to the following restrictions:
22 //
23 // 1. The origin of this software must not be misrepresented; you must not
24 // claim that you wrote the original software. If you use this software
25 // in a product, an acknowledgment in the product documentation would be
26 // appreciated but is not required.
27 //
28 // 2. Altered source versions must be plainly marked as such, and must not
29 // be misrepresented as being the original software.
30 //
31 // 3. This notice may not be removed or altered from any source distribution.
32 //
33 // Author: Walker White
34 // Version: 11/20/18
35 //
36 #ifndef __CU_AUDIO_MANAGER_H__
37 #define __CU_AUDIO_MANAGER_H__
38 #include <SDL/SDL.h>
39 #include <unordered_map>
40 #include <vector>
41 #include <memory>
42 #include <mutex>
43 
44 namespace cugl {
45 
47  namespace audio {
48  class AudioOutput;
49  class AudioInput;
50  }
51 
74 class AudioManager {
75 private:
77  static AudioManager* _gManager;
79  std::mutex _mutex;
81  bool _active;
83  Uint32 _output;
85  Uint32 _input;
86 
88  std::unordered_map<std::string, std::shared_ptr<audio::AudioOutput>> _outputs;
90  std::unordered_map<std::string, std::shared_ptr<audio::AudioInput>> _inputs;
91 
92 #pragma mark -
93 #pragma mark Constructors (Private)
94 
99  AudioManager();
100 
108  ~AudioManager() { dispose(); }
109 
124  bool init(Uint32 output, Uint32 input);
125 
133  void dispose();
134 
135 
136 #pragma mark -
137 #pragma mark Static Attributes
138 public:
140  static const Uint32 DEFAULT_OUTPUT_BUFFER;
141 
143  static const Uint32 DEFAULT_INPUT_BUFFER;
144 
145 #pragma mark -
146 #pragma mark Static Accessors
147 
155  static AudioManager* get() { return _gManager; }
156 
173  static void start();
174 
193  static void start(Uint32 frames);
194 
220  static void start(Uint32 output, Uint32 input);
221 
233  static void stop();
234 
246  static std::vector<std::string> devices(bool output);
247 
259  static std::vector<std::string> occupied(bool output);
260 
261 #pragma mark -
262 #pragma mark Manager Properties
263 
276  Uint32 getReadSize() const { return _output; }
277 
291  Uint32 getWriteSize() const { return _input; }
292 
301  bool isActive();
302 
313  void activate();
314 
326  void deactivate();
327 
338  void reset();
339 
340 
341 #pragma mark -
342 #pragma mark Output Devices
343 
356  std::shared_ptr<audio::AudioOutput> openOutput();
357 
374  std::shared_ptr<audio::AudioOutput> openOutput(Uint8 channels, Uint32 rate);
375 
389  std::shared_ptr<audio::AudioOutput> openOutput(const char* device);
390 
404  std::shared_ptr<audio::AudioOutput> openOutput(const std::string& device);
405 
421  std::shared_ptr<audio::AudioOutput> openOutput(const char* device, Uint8 channels, Uint32 rate);
422 
438  std::shared_ptr<audio::AudioOutput> openOutput(const std::string& device, Uint8 channels, Uint32 rate);
439 
450  bool closeOutput(const std::shared_ptr<audio::AudioOutput>& device);
451 
452 #pragma mark -
453 #pragma mark Input Devices
454 
473  std::shared_ptr<audio::AudioInput> openInput();
474 
501  std::shared_ptr<audio::AudioInput> openInput(Uint8 channels, Uint32 rate, Uint32 delay);
502 
522  std::shared_ptr<audio::AudioInput> openInput(const char* device);
523 
543  std::shared_ptr<audio::AudioInput> openInput(const std::string& device);
544 
570  std::shared_ptr<audio::AudioInput> openInput(const char* device, Uint8 channels, Uint32 rate, Uint32 delay);
571 
597  std::shared_ptr<audio::AudioInput> openInput(const std::string& device, Uint8 channels, Uint32 rate, Uint32 delay);
598 
609  bool closeInput(const std::shared_ptr<audio::AudioInput>& device);
610 
611 };
612 
613 }
614 
615 #endif /* __CU_AUDIO_MANAGER_H__ */
cugl::AudioManager::closeInput
bool closeInput(const std::shared_ptr< audio::AudioInput > &device)
cugl::AudioManager::stop
static void stop()
cugl::AudioManager::isActive
bool isActive()
cugl::AudioManager::devices
static std::vector< std::string > devices(bool output)
cugl::AudioManager::activate
void activate()
cugl::AudioManager::getReadSize
Uint32 getReadSize() const
Definition: CUAudioManager.h:276
cugl::AudioManager::start
static void start()
cugl::AudioManager::get
static AudioManager * get()
Definition: CUAudioManager.h:155
cugl::AudioManager
Definition: CUAudioManager.h:74
cugl::AudioManager::openInput
std::shared_ptr< audio::AudioInput > openInput()
cugl::AudioManager::deactivate
void deactivate()
cugl::AudioManager::DEFAULT_OUTPUT_BUFFER
static const Uint32 DEFAULT_OUTPUT_BUFFER
Definition: CUAudioManager.h:140
cugl::AudioManager::reset
void reset()
cugl::AudioManager::occupied
static std::vector< std::string > occupied(bool output)
cugl::AudioManager::getWriteSize
Uint32 getWriteSize() const
Definition: CUAudioManager.h:291
cugl::AudioManager::openOutput
std::shared_ptr< audio::AudioOutput > openOutput()
cugl::AudioManager::DEFAULT_INPUT_BUFFER
static const Uint32 DEFAULT_INPUT_BUFFER
Definition: CUAudioManager.h:143
cugl::AudioManager::closeOutput
bool closeOutput(const std::shared_ptr< audio::AudioOutput > &device)