CUGL 1.3
Cornell University Game Library
CUAudioNode.h
1 //
2 // CUAudioNode.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the base class for a node in the audio graph. While
6 // you will never use this class directly, it provides the base features that
7 // allow for a polymorphic audio graph.
8 //
9 // The audio graph and its nodes will always be accessed by two threads: the
10 // main thread and the audio thread. The audio graph is designed to safely
11 // coordinate between these two threads. However, it is minimizes locking
12 // and instead relies on a fail-fast model. If part of the audio graph is
13 // not in a state to be used by the audio thread, it will skip over that part
14 // of the graph until the next render frame. Hence some changes should only
15 // be made if the graph is paused. When there is some question about the
16 // thread safety, the methods are clearly marked.
17 //
18 // It is NEVER safe to access the audio graph outside of the main thread. The
19 // coordination algorithms only assume coordination between two threads.
20 //
21 // CUGL MIT License:
22 //
23 // This software is provided 'as-is', without any express or implied
24 // warranty. In no event will the authors be held liable for any damages
25 // arising from the use of this software.
26 //
27 // Permission is granted to anyone to use this software for any purpose,
28 // including commercial applications, and to alter it and redistribute it
29 // freely, subject to the following restrictions:
30 //
31 // 1. The origin of this software must not be misrepresented; you must not
32 // claim that you wrote the original software. If you use this software
33 // in a product, an acknowledgment in the product documentation would be
34 // appreciated but is not required.
35 //
36 // 2. Altered source versions must be plainly marked as such, and must not
37 // be misrepresented as being the original software.
38 //
39 // 3. This notice may not be removed or altered from any source distribution.
40 //
41 // Author: Walker White
42 // Version: 11/20/18
43 //
44 #ifndef __CU_AUDIO_NODE_H__
45 #define __CU_AUDIO_NODE_H__
46 #include <SDL/SDL.h>
47 #include <atomic>
48 #include <memory>
49 #include <functional>
50 #include <string>
51 
52 namespace cugl {
53 
54  namespace audio {
55 
93 class AudioNode : public std::enable_shared_from_this<AudioNode> {
94 public:
106  enum Action : int {
108  COMPLETE = 0,
112  FADE_OUT = 2,
114  FADE_IN = 3,
116  FADE_DIP = 4,
119  };
120 
133  typedef std::function<void (const std::shared_ptr<AudioNode>& node, Action type)> Callback;
134 
136  std::string _classname;
137 
138 #pragma mark Values
139 protected:
141  Uint8 _channels;
142 
144  Uint32 _sampling;
145 
147  bool _booted;
148 
150  std::atomic<float> _ndgain;
151 
153  std::atomic<bool> _paused;
154 
156  std::atomic<bool> _polling;
157 
161  std::atomic<bool> _calling;
162 
164  Sint32 _tag;
165 
171  std::string _localname;
172 
174  //std::string _classname;
175 
181  size_t _hashOfName;
182 
196  void notify(const std::shared_ptr<AudioNode>& node, Action action);
197 
198 #pragma mark -
199 #pragma mark Static Attributes
200 public:
202  const static Uint32 DEFAULT_CHANNELS;
203 
205  const static Uint32 DEFAULT_SAMPLING;
206 
207 #pragma mark -
208 #pragma mark Constructors
209 
218  AudioNode();
219 
223  virtual ~AudioNode();
224 
237  virtual bool init();
238 
251  virtual bool init(Uint8 channels, Uint32 rate);
252 
259  virtual void dispose();
260 
261 #pragma mark -
262 #pragma mark Node Attributes
263 
272  Uint8 getChannels() const { return _channels; }
273 
283  Uint32 getRate() const { return _sampling; }
284 
294  float getGain();
295 
305  virtual void setGain(float gain);
306 
315  const std::string& getName() const { return _localname; }
316 
325  void setName(const std::string& name);
326 
335  void setName(const char* name) {
336  setName(std::string(name));
337  }
338 
348  Sint32 getTag() const { return _tag; }
349 
359  void setTag(Sint32 tag) { _tag = tag; }
360 
371  virtual std::string toString(bool verbose = false) const;
372 
374  operator std::string() const { return toString(); }
375 
376 #pragma mark -
377 #pragma mark Playback Controls
378 
388 
398  void setCallback(Callback callback);
399 
405  virtual bool isPaused();
406 
415  virtual bool pause();
416 
424  virtual bool resume();
425 
437  virtual bool completed() { return false; }
438 
457  virtual Uint32 read(float* buffer, Uint32 frames);
458 
459 #pragma mark -
460 #pragma mark Optional Methods
461 
478  virtual bool mark() { return false; }
479 
494  virtual bool unmark() { return false; }
495 
511  virtual bool reset() { return false; }
512 
528  virtual Sint64 advance(Uint32 frames) { return -1; }
529 
544  virtual Sint64 getPosition() const { return -1; }
545 
562  virtual Sint64 setPosition(Uint32 position) { return -1; }
563 
578  virtual double getElapsed() const { return -1; }
579 
596  virtual double setElapsed(double time) { return -1; }
597 
614  virtual double getRemaining() const { return -1; }
615 
633  virtual double setRemaining(double time) { return -1; }
634 
635 };
636  }
637 }
638 
639 #endif /* __CU_AUDIO_NODE_H__ */
cugl::audio::AudioNode::_localname
std::string _localname
Definition: CUAudioNode.h:171
cugl::audio::AudioNode::setName
void setName(const char *name)
Definition: CUAudioNode.h:335
cugl::audio::AudioNode::_classname
std::string _classname
Definition: CUAudioNode.h:136
cugl::audio::AudioNode::_calling
std::atomic< bool > _calling
Definition: CUAudioNode.h:161
cugl::audio::AudioNode::_sampling
Uint32 _sampling
Definition: CUAudioNode.h:144
cugl::audio::AudioNode::FADE_OUT
Definition: CUAudioNode.h:112
cugl::audio::AudioNode::_polling
std::atomic< bool > _polling
Definition: CUAudioNode.h:156
cugl::audio::AudioNode::setCallback
void setCallback(Callback callback)
cugl::audio::AudioNode::Callback
std::function< void(const std::shared_ptr< AudioNode > &node, Action type)> Callback
Definition: CUAudioNode.h:133
cugl::audio::AudioNode::setGain
virtual void setGain(float gain)
cugl::audio::AudioNode::getChannels
Uint8 getChannels() const
Definition: CUAudioNode.h:272
cugl::audio::AudioNode::unmark
virtual bool unmark()
Definition: CUAudioNode.h:494
cugl::audio::AudioNode::reset
virtual bool reset()
Definition: CUAudioNode.h:511
cugl::audio::AudioNode::isPaused
virtual bool isPaused()
cugl::audio::AudioNode::setPosition
virtual Sint64 setPosition(Uint32 position)
Definition: CUAudioNode.h:562
cugl::audio::AudioNode::_hashOfName
size_t _hashOfName
Definition: CUAudioNode.h:181
cugl::audio::AudioNode::setName
void setName(const std::string &name)
cugl::audio::AudioNode::toString
virtual std::string toString(bool verbose=false) const
cugl::audio::AudioNode::pause
virtual bool pause()
cugl::audio::AudioNode::FADE_DIP
Definition: CUAudioNode.h:116
cugl::audio::AudioNode::COMPLETE
Definition: CUAudioNode.h:108
cugl::audio::AudioNode::AudioNode
AudioNode()
cugl::audio::AudioNode::DEFAULT_CHANNELS
const static Uint32 DEFAULT_CHANNELS
Definition: CUAudioNode.h:202
cugl::audio::AudioNode::advance
virtual Sint64 advance(Uint32 frames)
Definition: CUAudioNode.h:528
cugl::audio::AudioNode::_booted
bool _booted
Definition: CUAudioNode.h:147
cugl::audio::AudioNode::getName
const std::string & getName() const
Definition: CUAudioNode.h:315
cugl::audio::AudioNode::read
virtual Uint32 read(float *buffer, Uint32 frames)
cugl::audio::AudioNode::setRemaining
virtual double setRemaining(double time)
Definition: CUAudioNode.h:633
cugl::audio::AudioNode::getTag
Sint32 getTag() const
Definition: CUAudioNode.h:348
cugl::audio::AudioNode::LOOPBACK
Definition: CUAudioNode.h:118
cugl::audio::AudioNode::notify
void notify(const std::shared_ptr< AudioNode > &node, Action action)
cugl::audio::AudioNode::getPosition
virtual Sint64 getPosition() const
Definition: CUAudioNode.h:544
cugl::audio::AudioNode::getRemaining
virtual double getRemaining() const
Definition: CUAudioNode.h:614
cugl::audio::AudioNode::getRate
Uint32 getRate() const
Definition: CUAudioNode.h:283
cugl::audio::AudioNode::_ndgain
std::atomic< float > _ndgain
Definition: CUAudioNode.h:150
cugl::audio::AudioNode::_channels
Uint8 _channels
Definition: CUAudioNode.h:141
cugl::audio::AudioNode::setElapsed
virtual double setElapsed(double time)
Definition: CUAudioNode.h:596
cugl::audio::AudioNode::_tag
Sint32 _tag
Definition: CUAudioNode.h:164
cugl::audio::AudioNode::_callback
Callback _callback
Definition: CUAudioNode.h:159
cugl::audio::AudioNode::getCallback
Callback getCallback()
cugl::audio::AudioNode::_paused
std::atomic< bool > _paused
Definition: CUAudioNode.h:153
cugl::audio::AudioNode::resume
virtual bool resume()
cugl::audio::AudioNode::Action
Action
Definition: CUAudioNode.h:106
cugl::audio::AudioNode::DEFAULT_SAMPLING
const static Uint32 DEFAULT_SAMPLING
Definition: CUAudioNode.h:205
cugl::audio::AudioNode::init
virtual bool init()
cugl::audio::AudioNode::completed
virtual bool completed()
Definition: CUAudioNode.h:437
cugl::audio::AudioNode::INTERRUPT
Definition: CUAudioNode.h:110
cugl::audio::AudioNode::getGain
float getGain()
cugl::audio::AudioNode::~AudioNode
virtual ~AudioNode()
cugl::audio::AudioNode::setTag
void setTag(Sint32 tag)
Definition: CUAudioNode.h:359
cugl::audio::AudioNode
Definition: CUAudioNode.h:93
cugl::audio::AudioNode::dispose
virtual void dispose()
cugl::audio::AudioNode::FADE_IN
Definition: CUAudioNode.h:114
cugl::audio::AudioNode::mark
virtual bool mark()
Definition: CUAudioNode.h:478
cugl::audio::AudioNode::getElapsed
virtual double getElapsed() const
Definition: CUAudioNode.h:578