CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioResampler.h
1 //
2 // CUAudioResampler.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a graph node for converting from one sample rate to
6 // another. It uses SDL_AudioStream to perform continuous resampling on a
7 // potentially infinite audio stream. This is is necessary for cross-platform
8 // reasons as iPhones are very stubborn about delivering any requested sampling
9 // rates other than 48000.
10 //
11 // CUGL MIT License:
12 //
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 1/26/19
33 //
34 #ifndef __CU_AUDIO_RESAMPLER_H__
35 #define __CU_AUDIO_RESAMPLER_H__
36 #include <cugl/audio/graph/CUAudioNode.h>
37 #include <SDL/SDL.h>
38 #include <mutex>
39 #include <atomic>
40 
41 namespace cugl {
42 
43  namespace audio {
62 class AudioResampler : public AudioNode {
63 private:
65  mutable std::mutex _buffmtex;
66 
68  std::shared_ptr<AudioNode> _input;
69 
71  SDL_AudioStream* _resampler;
73  Uint32 _inputrate;
75  float* _cvtbuffer;
77  size_t _capacity;
79  std::atomic<float> _cvtratio;
80 
81 public:
82 #pragma mark -
83 #pragma mark Constructors
84 
94 
99 
109  virtual bool init() override;
110 
123  virtual bool init(Uint8 channels, Uint32 rate) override;
124 
131  virtual void dispose() override;
132 
133 
134 #pragma mark -
135 #pragma mark Static Constructors
136 
145  static std::shared_ptr<AudioResampler> alloc() {
146  std::shared_ptr<AudioResampler> result = std::make_shared<AudioResampler>();
147  return (result->init() ? result : nullptr);
148  }
149 
162  static std::shared_ptr<AudioResampler> alloc(Uint8 channels, Uint32 rate) {
163  std::shared_ptr<AudioResampler> result = std::make_shared<AudioResampler>();
164  return (result->init(channels,rate) ? result : nullptr);
165  }
166 
167 #pragma mark -
168 #pragma mark Audio Graph
169 
181  bool attach(const std::shared_ptr<AudioNode>& node);
182 
191  std::shared_ptr<AudioNode> detach();
192 
198  std::shared_ptr<AudioNode> getInput() const { return _input; }
199 
200 #pragma mark -
201 #pragma mark Playback Control
202 
213  virtual bool completed() override;
214 
232  virtual Uint32 read(float* buffer, Uint32 frames) override;
233 
234 #pragma mark -
235 #pragma mark Optional Methods
236 
253  virtual bool mark() override;
254 
269  virtual bool unmark() override;
270 
286  virtual bool reset() override;
287 
303  virtual Sint64 advance(Uint32 frames) override;
304 
319  virtual Sint64 getPosition() const override;
320 
337  virtual Sint64 setPosition(Uint32 position) override;
338 
353  virtual double getElapsed() const override;
354 
371  virtual double setElapsed(double time) override;
372 
389  virtual double getRemaining() const override;
390 
408  virtual double setRemaining(double time) override;
409 };
410  }
411 }
412 
413 #endif /* __CU_AUDIO_RESAMPLER_H__ */
cugl::audio::AudioResampler::mark
virtual bool mark() override
cugl::audio::AudioResampler::~AudioResampler
~AudioResampler()
Definition: CUAudioResampler.h:98
cugl::audio::AudioResampler::read
virtual Uint32 read(float *buffer, Uint32 frames) override
cugl::audio::AudioResampler::getElapsed
virtual double getElapsed() const override
cugl::audio::AudioResampler::setRemaining
virtual double setRemaining(double time) override
cugl::audio::AudioResampler::unmark
virtual bool unmark() override
cugl::audio::AudioResampler::advance
virtual Sint64 advance(Uint32 frames) override
cugl::audio::AudioResampler::getPosition
virtual Sint64 getPosition() const override
cugl::audio::AudioResampler::setPosition
virtual Sint64 setPosition(Uint32 position) override
cugl::audio::AudioResampler::alloc
static std::shared_ptr< AudioResampler > alloc()
Definition: CUAudioResampler.h:145
cugl::audio::AudioResampler::completed
virtual bool completed() override
cugl::audio::AudioResampler::getInput
std::shared_ptr< AudioNode > getInput() const
Definition: CUAudioResampler.h:198
cugl::audio::AudioResampler::setElapsed
virtual double setElapsed(double time) override
cugl::audio::AudioResampler::alloc
static std::shared_ptr< AudioResampler > alloc(Uint8 channels, Uint32 rate)
Definition: CUAudioResampler.h:162
cugl::audio::AudioResampler::init
virtual bool init() override
cugl::audio::AudioResampler::AudioResampler
AudioResampler()
cugl::audio::AudioResampler
Definition: CUAudioResampler.h:62
cugl::audio::AudioResampler::reset
virtual bool reset() override
cugl::audio::AudioResampler::dispose
virtual void dispose() override
cugl::audio::AudioResampler::detach
std::shared_ptr< AudioNode > detach()
cugl::audio::AudioResampler::getRemaining
virtual double getRemaining() const override
cugl::audio::AudioResampler::attach
bool attach(const std::shared_ptr< AudioNode > &node)
cugl::audio::AudioNode
Definition: CUAudioNode.h:93