CUGL 1.2
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_AudioCVT _audiocvt;
129 
130 #pragma mark -
131 #pragma mark AudioManager Methods
132 
152  virtual bool init() override;
153 
177  virtual bool init(Uint8 channels, Uint32 rate) override;
178 
211  bool init(Uint8 channels, Uint32 rate, Uint32 buffer);
212 
233  bool init(const std::string& device);
234 
266  bool init(const std::string& device, Uint8 channels, Uint32 rate, Uint32 buffer);
267 
274  virtual void dispose() override;
275 
285  void setActive(bool active);
286 
288  friend class cugl::AudioManager;
289 
290 public:
300  AudioOutput();
301 
305  ~AudioOutput();
306 
307 #pragma mark -
308 #pragma mark Data Access
309 
314  const SDL_AudioDeviceID getAUID() const { return _device; }
315 
321  const std::string& getDevice() const;
322 
331  bool isDefault() const { return _dvname == ""; }
332 
353  Uint16 getCapacity() const { return _audiospec.samples; }
354 
355 
356 #pragma mark -
357 #pragma mark Audio Graph
358 
368  bool attach(const std::shared_ptr<AudioNode>& node);
369 
377  std::shared_ptr<AudioNode> detach();
378 
384  std::shared_ptr<AudioNode> getInput() { return _input; }
385 
386 #pragma mark -
387 #pragma mark Playback Control
388 
396  virtual bool pause() override;
397 
407  virtual bool resume() override;
408 
420  virtual bool completed() override;
421 
439  virtual Uint32 read(float* buffer, Uint32 frames) override;
440 
452  void reboot();
453 
461  Uint64 getOverhead() const;
462 
463 #pragma mark -
464 #pragma mark Optional Methods
465 
482  virtual bool mark() override;
483 
498  virtual bool unmark() override;
499 
515  virtual bool reset() override;
516 
532  virtual Sint64 advance(Uint32 frames) override;
533 
548  virtual Sint64 getPosition() const override;
549 
566  virtual Sint64 setPosition(Uint32 position) override;
567 
582  virtual double getElapsed() const override;
583 
600  virtual double setElapsed(double time) override;
601 
618  virtual double getRemaining() const override;
619 
637  virtual double setRemaining(double time) override;
638 
639  // TODO: May need to add a push option.
640 };
641  }
642 }
643 
644 #endif /* __CU_AUDIO_OUTPUT_H__ */
std::shared_ptr< AudioNode > detach()
Definition: CUAudioManager.h:74
bool attach(const std::shared_ptr< AudioNode > &node)
virtual Sint64 advance(Uint32 frames) override
virtual Sint64 getPosition() const override
bool isDefault() const
Definition: CUAudioOutput.h:331
Uint16 getCapacity() const
Definition: CUAudioOutput.h:353
virtual Sint64 setPosition(Uint32 position) override
Definition: CUAudioNode.h:93
virtual bool completed() override
Definition: CUAudioOutput.h:108
virtual double setElapsed(double time) override
std::shared_ptr< AudioNode > getInput()
Definition: CUAudioOutput.h:384
virtual bool unmark() override
virtual bool reset() override
virtual Uint32 read(float *buffer, Uint32 frames) override
virtual double getElapsed() const override
const std::string & getDevice() const
virtual double setRemaining(double time) override
Uint64 getOverhead() const
virtual double getRemaining() const override
virtual bool pause() override
const SDL_AudioDeviceID getAUID() const
Definition: CUAudioOutput.h:314
Definition: CUAction.h:51
virtual bool mark() override
virtual bool resume() override