CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioOutput.h
1 //
2 // CUAudioOutput.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides interface to an audio output device. As such, it is
6 // often the final node in an audio stream DAG. It is analogous to AVAudioEngine
7 // in Apple's AVFoundation API. The main differences is that it does not have
8 // a dedicated mixer node. Instead, you attach the single terminal node of the
9 // audio graph. In addition, it is possible to have a distinct audio graph for
10 // each output device.
11 //
12 // The audio graph and its nodes will always be accessed by two threads: the
13 // main thread and the audio thread. The audio graph is designed to safely
14 // coordinate between these two threads. However, it is minimizes locking
15 // and instead relies on a fail-fast model. If part of the audio graph is
16 // not in a state to be used by the audio thread, it will skip over that part
17 // of the graph until the next render frame. Hence some changes should only
18 // be made if the graph is paused. When there is some question about the
19 // thread safety, the methods are clearly marked.
20 //
21 // It is NEVER safe to access the audio graph outside of the main thread. The
22 // coordination algorithms only assume coordination between two threads.
23 //
24 // CUGL MIT License:
25 //
26 // This software is provided 'as-is', without any express or implied
27 // warranty. In no event will the authors be held liable for any damages
28 // arising from the use of this software.
29 //
30 // Permission is granted to anyone to use this software for any purpose,
31 // including commercial applications, and to alter it and redistribute it
32 // freely, subject to the following restrictions:
33 //
34 // 1. The origin of this software must not be misrepresented; you must not
35 // claim that you wrote the original software. If you use this software
36 // in a product, an acknowledgment in the product documentation would be
37 // appreciated but is not required.
38 //
39 // 2. Altered source versions must be plainly marked as such, and must not
40 // be misrepresented as being the original software.
41 //
42 // 3. This notice may not be removed or altered from any source distribution.
43 //
44 // Author: Walker White
45 // Version: 8/20/18
46 //
47 #ifndef __CU_AUDIO_OUTPUT_H__
48 #define __CU_AUDIO_OUTPUT_H__
49 #include <SDL/SDL.h>
50 #include "CUAudioNode.h"
51 #include <cugl/math/dsp/CUBiquadIIR.h>
52 #include <unordered_set>
53 #include <string>
54 #include <memory>
55 #include <vector>
56 
57 namespace cugl {
58 
60  class AudioManager;
61 
62  namespace audio {
108 class AudioOutput : public AudioNode {
109 private:
111  std::string _dvname;
112 
114  std::atomic<Uint64> _overhd;
115 
117  SDL_AudioDeviceID _device;
119  SDL_AudioSpec _audiospec;
120 
122  std::atomic<bool> _active;
123 
125  std::shared_ptr<AudioNode> _input;
126 
128  SDL_AudioStream* _resampler;
130  float* _cvtbuffer;
132  float _cvtratio;
134  size_t _bitrate;
135 
136 #pragma mark -
137 #pragma mark AudioManager Methods
138 
158  virtual bool init() override;
159 
183  virtual bool init(Uint8 channels, Uint32 rate) override;
184 
217  bool init(Uint8 channels, Uint32 rate, Uint32 buffer);
218 
239  bool init(const std::string& device);
240 
272  bool init(const std::string& device, Uint8 channels, Uint32 rate, Uint32 buffer);
273 
280  virtual void dispose() override;
281 
291  void setActive(bool active);
292 
294  friend class cugl::AudioManager;
295 
296 public:
306  AudioOutput();
307 
311  ~AudioOutput();
312 
313 #pragma mark -
314 #pragma mark Data Access
315 
320  const SDL_AudioDeviceID getAUID() const { return _device; }
321 
327  const std::string& getDevice() const;
328 
337  bool isDefault() const { return _dvname == ""; }
338 
359  Uint16 getCapacity() const { return _audiospec.samples; }
360 
371  size_t getBitRate() const { return _bitrate; }
372 
373 #pragma mark -
374 #pragma mark Audio Graph
375 
385  bool attach(const std::shared_ptr<AudioNode>& node);
386 
394  std::shared_ptr<AudioNode> detach();
395 
401  std::shared_ptr<AudioNode> getInput() { return _input; }
402 
403 #pragma mark -
404 #pragma mark Playback Control
405 
413  virtual bool pause() override;
414 
424  virtual bool resume() override;
425 
437  virtual bool completed() override;
438 
456  virtual Uint32 read(float* buffer, Uint32 frames) override;
457 
469  void reboot();
470 
478  Uint64 getOverhead() const;
479 
480 #pragma mark -
481 #pragma mark Optional Methods
482 
499  virtual bool mark() override;
500 
515  virtual bool unmark() override;
516 
532  virtual bool reset() override;
533 
549  virtual Sint64 advance(Uint32 frames) override;
550 
565  virtual Sint64 getPosition() const override;
566 
583  virtual Sint64 setPosition(Uint32 position) override;
584 
599  virtual double getElapsed() const override;
600 
617  virtual double setElapsed(double time) override;
618 
635  virtual double getRemaining() const override;
636 
654  virtual double setRemaining(double time) override;
655 
656  // TODO: May need to add a push option.
657 };
658  }
659 }
660 
661 #endif /* __CU_AUDIO_OUTPUT_H__ */
cugl::audio::AudioOutput::resume
virtual bool resume() override
cugl::audio::AudioOutput::reboot
void reboot()
cugl::audio::AudioOutput::getRemaining
virtual double getRemaining() const override
cugl::audio::AudioOutput::setPosition
virtual Sint64 setPosition(Uint32 position) override
cugl::audio::AudioOutput::getDevice
const std::string & getDevice() const
cugl::audio::AudioOutput::getBitRate
size_t getBitRate() const
Definition: CUAudioOutput.h:371
cugl::audio::AudioOutput::getOverhead
Uint64 getOverhead() const
cugl::audio::AudioOutput::reset
virtual bool reset() override
cugl::audio::AudioOutput::getPosition
virtual Sint64 getPosition() const override
cugl::audio::AudioOutput::getInput
std::shared_ptr< AudioNode > getInput()
Definition: CUAudioOutput.h:401
cugl::audio::AudioOutput::pause
virtual bool pause() override
cugl::audio::AudioOutput::read
virtual Uint32 read(float *buffer, Uint32 frames) override
cugl::audio::AudioOutput::unmark
virtual bool unmark() override
cugl::audio::AudioOutput::getAUID
const SDL_AudioDeviceID getAUID() const
Definition: CUAudioOutput.h:320
cugl::AudioManager
Definition: CUAudioManager.h:74
cugl::audio::AudioOutput::setRemaining
virtual double setRemaining(double time) override
cugl::audio::AudioOutput::AudioOutput
AudioOutput()
cugl::audio::AudioOutput::getElapsed
virtual double getElapsed() const override
cugl::audio::AudioOutput
Definition: CUAudioOutput.h:108
cugl::audio::AudioOutput::completed
virtual bool completed() override
cugl::audio::AudioOutput::getCapacity
Uint16 getCapacity() const
Definition: CUAudioOutput.h:359
cugl::audio::AudioOutput::mark
virtual bool mark() override
cugl::audio::AudioOutput::setElapsed
virtual double setElapsed(double time) override
cugl::audio::AudioOutput::isDefault
bool isDefault() const
Definition: CUAudioOutput.h:337
cugl::audio::AudioOutput::detach
std::shared_ptr< AudioNode > detach()
cugl::audio::AudioOutput::attach
bool attach(const std::shared_ptr< AudioNode > &node)
cugl::audio::AudioOutput::advance
virtual Sint64 advance(Uint32 frames) override
cugl::audio::AudioNode
Definition: CUAudioNode.h:93
cugl::audio::AudioOutput::~AudioOutput
~AudioOutput()