CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioPlayer.h
1 //
2 // CUAudioPlayer.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the interface for representing an playback instance
6 // of an audio sample. A player is attached to a single sound asset, though
7 // it may be disposed and reinitialized to contain another asset (in order to
8 // to limit object creation). To rapidly swap between sounds, or to play them
9 // in order, this node should be combined with the AudioScheduler node.
10 //
11 // This class is necessary bcause samples may have multiple instances,
12 // particularly if they are playing simultaneously. The complexity of stream
13 // decoding forces us to put decoding state in these classes and not in the
14 // asset file (particularly when there are multiple streams).
15 //
16 // CUGL MIT License:
17 //
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: 11/20/18
38 //
39 #ifndef __CU_AUDIO_PLAYER_H__
40 #define __CU_AUDIO_PLAYER_H__
41 #include <SDL/SDL.h>
42 #include <cugl/audio/CUAudioSample.h>
43 #include "CUAudioNode.h"
44 #include <functional>
45 #include <string>
46 #include <atomic>
47 
48 // TODO: Move fade-in/fade-out support to new class
49 namespace cugl {
50  namespace audio {
51 
52 #pragma mark -
53 #pragma mark Base Player
54 
80 class AudioPlayer : public AudioNode {
81 protected:
83  std::shared_ptr<AudioSample> _source;
85  std::shared_ptr<AudioDecoder> _decoder;
86 
88  std::atomic<Uint64> _offset;
90  std::atomic<Uint64> _marked;
91 
93  float* _buffer;
94 
95  // Streaming support
97  float* _chunker;
99  Uint32 _chksize;
101  Uint32 _chklimt;
103  Uint32 _chklast;
104 
106  std::atomic<bool> _dirty;
107 
108 public:
109 #pragma mark Constructors
110 
116  AudioPlayer();
117 
122 
133  bool init(const std::shared_ptr<AudioSample>& source);
134 
141  virtual void dispose() override;
142 
153  static std::shared_ptr<AudioPlayer> alloc(const std::shared_ptr<AudioSample>& sample) {
154  std::shared_ptr<AudioPlayer> result = std::make_shared<AudioPlayer>();
155  return (result->init(sample) ? result : nullptr);
156  }
157 
163  std::shared_ptr<AudioSample> getSource() { return _source; }
164 
165 #pragma mark Overriden Methods
166 
184  virtual Uint32 read(float* buffer, Uint32 frames) override;
185 
194  virtual bool completed() override;
195 
204  virtual bool mark() override;
205 
215  virtual bool unmark() override;
216 
225  virtual bool reset() override;
226 
237  virtual Sint64 advance(Uint32 frames) override;
238 
247  virtual Sint64 getPosition() const override;
248 
259  virtual Sint64 setPosition(Uint32 position) override;
260 
269  virtual double getElapsed() const override;
270 
281  virtual double setElapsed(double time) override;
282 
291  virtual double getRemaining() const override;
292 
303  virtual double setRemaining(double time) override;
304 
305 private:
306 #pragma mark Stream Decoding
307 
319  void scan(Uint64 frame);
320 };
321 
322  }
323 }
324 
325 #endif /* __CU_AUDIO_PLAYER_H__ */
cugl::audio::AudioPlayer::_marked
std::atomic< Uint64 > _marked
Definition: CUAudioPlayer.h:90
cugl::audio::AudioPlayer::_decoder
std::shared_ptr< AudioDecoder > _decoder
Definition: CUAudioPlayer.h:85
cugl::audio::AudioPlayer::unmark
virtual bool unmark() override
cugl::audio::AudioPlayer::_chklimt
Uint32 _chklimt
Definition: CUAudioPlayer.h:101
cugl::audio::AudioPlayer::_buffer
float * _buffer
Definition: CUAudioPlayer.h:93
cugl::audio::AudioPlayer::advance
virtual Sint64 advance(Uint32 frames) override
cugl::audio::AudioPlayer::getSource
std::shared_ptr< AudioSample > getSource()
Definition: CUAudioPlayer.h:163
cugl::audio::AudioPlayer::setElapsed
virtual double setElapsed(double time) override
cugl::audio::AudioPlayer::_chklast
Uint32 _chklast
Definition: CUAudioPlayer.h:103
cugl::audio::AudioPlayer::alloc
static std::shared_ptr< AudioPlayer > alloc(const std::shared_ptr< AudioSample > &sample)
Definition: CUAudioPlayer.h:153
cugl::audio::AudioPlayer::mark
virtual bool mark() override
cugl::audio::AudioPlayer::read
virtual Uint32 read(float *buffer, Uint32 frames) override
cugl::audio::AudioPlayer::_source
std::shared_ptr< AudioSample > _source
Definition: CUAudioPlayer.h:83
cugl::audio::AudioPlayer::_dirty
std::atomic< bool > _dirty
Definition: CUAudioPlayer.h:106
cugl::audio::AudioPlayer::getElapsed
virtual double getElapsed() const override
cugl::audio::AudioPlayer::getRemaining
virtual double getRemaining() const override
cugl::audio::AudioPlayer::~AudioPlayer
~AudioPlayer()
Definition: CUAudioPlayer.h:121
cugl::audio::AudioPlayer::setRemaining
virtual double setRemaining(double time) override
cugl::audio::AudioPlayer::reset
virtual bool reset() override
cugl::audio::AudioPlayer::_chksize
Uint32 _chksize
Definition: CUAudioPlayer.h:99
cugl::audio::AudioPlayer::dispose
virtual void dispose() override
cugl::audio::AudioPlayer::AudioPlayer
AudioPlayer()
cugl::audio::AudioNode::init
virtual bool init()
cugl::audio::AudioPlayer::_chunker
float * _chunker
Definition: CUAudioPlayer.h:97
cugl::audio::AudioPlayer::setPosition
virtual Sint64 setPosition(Uint32 position) override
cugl::audio::AudioPlayer
Definition: CUAudioPlayer.h:80
cugl::audio::AudioNode
Definition: CUAudioNode.h:93
cugl::audio::AudioPlayer::_offset
std::atomic< Uint64 > _offset
Definition: CUAudioPlayer.h:88
cugl::audio::AudioPlayer::completed
virtual bool completed() override
cugl::audio::AudioPlayer::getPosition
virtual Sint64 getPosition() const override