CUGL 1.2
Cornell University Game Library
CUAudioMixer.h
1 //
2 // CUAudioMixer.h
3 // Cornell University Game Library (CUGL)
4 //
5 // Thie module provides an audio graph node for mixing together several input
6 // streams into a single output stream. The input nodes must all have the same
7 // same number of channels and sampling rate.
8 //
9 // Mixing works by adding together all of the streams. This means that the
10 // results may exceed the range [-1,1], causing clipping. The mixer provides
11 // a "soft-knee" option for confining the results to the range [-1,1].
12 //
13 // CUGL MIT License:
14 //
15 // This software is provided 'as-is', without any express or implied
16 // warranty. In no event will the authors be held liable for any damages
17 // arising from the use of this software.
18 //
19 // Permission is granted to anyone to use this software for any purpose,
20 // including commercial applications, and to alter it and redistribute it
21 // freely, subject to the following restrictions:
22 //
23 // 1. The origin of this software must not be misrepresented; you must not
24 // claim that you wrote the original software. If you use this software
25 // in a product, an acknowledgment in the product documentation would be
26 // appreciated but is not required.
27 //
28 // 2. Altered source versions must be plainly marked as such, and must not
29 // be misrepresented as being the original software.
30 //
31 // 3. This notice may not be removed or altered from any source distribution.
32 //
33 // Author: Walker White
34 // Version: 11/7/18
35 //
36 #ifndef __CU_AUDIO_MIXER_H__
37 #define __CU_AUDIO_MIXER_H__
38 #include "CUAudioNode.h"
39 
40 namespace cugl {
41  namespace audio {
61 class AudioMixer : public AudioNode {
62 private:
64  std::shared_ptr<AudioNode>* _inputs;
66  Uint8 _width;
67 
69  float* _buffer;
71  Uint32 _capacity;
72 
74  std::atomic<float> _knee;
75 
76 public:
77 #pragma mark Constructors
78 
79  static const Uint8 DEFAULT_WIDTH;
81  static const float DEFAULT_KNEE;
82 
89  AudioMixer();
90 
95 
111  virtual bool init() override;
112 
128  bool init(Uint8 width);
129 
144  virtual bool init(Uint8 channels, Uint32 rate) override;
145 
160  bool init(Uint8 width, Uint8 channels, Uint32 rate);
161 
168  virtual void dispose() override;
169 
170 #pragma mark Static Constructors
171 
186  static std::shared_ptr<AudioMixer> alloc(Uint8 width=8) {
187  std::shared_ptr<AudioMixer> result = std::make_shared<AudioMixer>();
188  return (result->init(width) ? result : nullptr);
189  }
190 
205  static std::shared_ptr<AudioMixer> alloc(Uint8 width, Uint8 channels, Uint32 rate) {
206  std::shared_ptr<AudioMixer> result = std::make_shared<AudioMixer>();
207  return (result->init(width,channels, rate) ? result : nullptr);
208  }
209 
210 #pragma mark Audio Graph Methods
211 
222  std::shared_ptr<AudioNode> attach(Uint8 slot, const std::shared_ptr<AudioNode>& input);
223 
233  std::shared_ptr<AudioNode> detach(Uint8 slot);
234 
253  virtual Uint32 read(float* buffer, Uint32 frames) override;
254 
255 #pragma mark Anticlipping Methods
256 
269  float getKnee() const;
270 
284  void setKnee(float knee);
285 
286 };
287  }
288 }
289 
290 #endif /* __CU_AUDIO_MIXER_H__ */
void setKnee(float knee)
virtual bool init() override
static std::shared_ptr< AudioMixer > alloc(Uint8 width, Uint8 channels, Uint32 rate)
Definition: CUAudioMixer.h:205
static std::shared_ptr< AudioMixer > alloc(Uint8 width=8)
Definition: CUAudioMixer.h:186
static const Uint8 DEFAULT_WIDTH
Definition: CUAudioMixer.h:79
Definition: CUAudioMixer.h:61
std::shared_ptr< AudioNode > attach(Uint8 slot, const std::shared_ptr< AudioNode > &input)
virtual Uint32 read(float *buffer, Uint32 frames) override
Definition: CUAudioNode.h:93
~AudioMixer()
Definition: CUAudioMixer.h:94
virtual void dispose() override
std::shared_ptr< AudioNode > detach(Uint8 slot)
Definition: CUAction.h:51
static const float DEFAULT_KNEE
Definition: CUAudioMixer.h:81
float getKnee() const