CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAudioWaveform.h
1 //
2 // CUAudioWaveform.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the interface for creating a single-frequency waveform.
6 // Examples include sine waves, square waves, sawtooth waves, and triangle
7 // waves. This waveform may serve as the source node in an audio graph.
8 //
9 // This module is not intended to be "music quality". The audio waveforms
10 // are good enough for procedural sound generation in most games. In
11 // particular, we use the PolyBLEP technique for bandwidth-limiting our
12 // various waveforms. This technique is known to have audible aliasing near
13 // the Nyquist frequency and overly attenuate higher frequencies. However,
14 // it is compact and ideal for real-time sound generation. Because the
15 // problems are less of an issue for sound-effect generation, this the
16 // approach that we have chosen.
17 //
18 // CUGL MIT License:
19 //
20 // This software is provided 'as-is', without any express or implied
21 // warranty. In no event will the authors be held liable for any damages
22 // arising from the use of this software.
23 //
24 // Permission is granted to anyone to use this software for any purpose,
25 // including commercial applications, and to alter it and redistribute it
26 // freely, subject to the following restrictions:
27 //
28 // 1. The origin of this software must not be misrepresented; you must not
29 // claim that you wrote the original software. If you use this software
30 // in a product, an acknowledgment in the product documentation would be
31 // appreciated but is not required.
32 //
33 // 2. Altered source versions must be plainly marked as such, and must not
34 // be misrepresented as being the original software.
35 //
36 // 3. This notice may not be removed or altered from any source distribution.
37 //
38 // Author: Walker White
39 // Version: 12/20/18
40 //
41 #ifndef __CU_AUDIO_WAVE_FORM_H__
42 #define __CU_AUDIO_WAVE_FORM_H__
43 #include <SDL/SDL.h>
44 #include <cugl/assets/CUJsonValue.h>
45 #include "CUSound.h"
46 #include <random>
47 
48 namespace cugl {
78 class AudioWaveform : public Sound {
79 public:
81  const static float DEFAULT_FREQUENCY;
82 
91  enum class Type : int {
96  NOISE = 0,
100  SINE = 1,
108  NAIVE_TRIANG = 2,
115  NAIVE_SQUARE = 3,
122  NAIVE_TOOTH = 4,
129  NAIVE_TRAIN = 5,
142  POLY_TRIANG = 6,
152  POLY_SQUARE = 7,
162  POLY_TOOTH = 8,
172  BLIT_TRAIN = 9,
176  UNKNOWN = 10
177  };
178 
179 protected:
181  std::atomic<int> _type;
183  std::atomic<bool> _upper;
185  std::atomic<float> _frequency;
187  std::atomic<bool> _newfreq;
189  std::atomic<double> _duration;
190 
192  std::minstd_rand0 _random;
193 
194 public:
195 #pragma mark Constructors
196 
202  AudioWaveform();
203 
208 
217  bool init();
218 
230  bool init(Uint8 channels, Uint32 rate);
231 
250  bool init(Uint8 channels, Uint32 rate, Type type, float frequency);
251 
258  virtual void dispose() override;
259 
260 #pragma mark Static Constructors
261 
269  static std::shared_ptr<AudioWaveform> alloc() {
270  std::shared_ptr<AudioWaveform> result = std::make_shared<AudioWaveform>();
271  return (result->init() ? result : nullptr);
272  }
273 
285  static std::shared_ptr<AudioWaveform> alloc(Uint8 channels, Uint32 rate) {
286  std::shared_ptr<AudioWaveform> result = std::make_shared<AudioWaveform>();
287  return (result->init(channels,rate) ? result : nullptr);
288  }
289 
308  static std::shared_ptr<AudioWaveform> alloc(Uint8 channels, Uint32 rate, Type type, float frequency) {
309  std::shared_ptr<AudioWaveform> result = std::make_shared<AudioWaveform>();
310  return (result->init(channels,rate,type,frequency) ? result : nullptr);
311  }
312 
336  static std::shared_ptr<AudioWaveform> allocWithData(const std::shared_ptr<JsonValue>& data);
337 
338 #pragma mark Generator Attributes
339 
344  Type getType() const;
345 
351  void setType(Type type);
352 
365  bool isUpper() const;
366 
379  void setUpper(bool upper);
380 
386  float getFrequency() const;
387 
393  void setFrequency(float frequency);
394 
404  virtual Sint64 getLength() const override;
405 
415  virtual double getDuration() const override;
416 
426  void setDuration(double time);
427 
447  Uint32 generate(float* buffer, Uint32 frames, Uint64 offset, float last);
448 
458  virtual std::shared_ptr<audio::AudioNode> createNode() override;
459 };
460 }
461 
462 #endif /* __CU_AUDIO_WAVE_FORM_H__ */
cugl::AudioWaveform::dispose
virtual void dispose() override
cugl::AudioWaveform::Type::SINE
cugl::AudioWaveform::getDuration
virtual double getDuration() const override
cugl::AudioWaveform::createNode
virtual std::shared_ptr< audio::AudioNode > createNode() override
cugl::AudioWaveform::generate
Uint32 generate(float *buffer, Uint32 frames, Uint64 offset, float last)
cugl::AudioWaveform::init
bool init()
cugl::AudioWaveform::getFrequency
float getFrequency() const
cugl::AudioWaveform::setFrequency
void setFrequency(float frequency)
cugl::AudioWaveform::Type
Type
Definition: CUAudioWaveform.h:91
cugl::AudioWaveform
Definition: CUAudioWaveform.h:78
cugl::AudioWaveform::Type::POLY_TOOTH
cugl::AudioWaveform::_newfreq
std::atomic< bool > _newfreq
Definition: CUAudioWaveform.h:187
cugl::Sound
Definition: CUSound.h:69
cugl::AudioWaveform::Type::POLY_TRIANG
cugl::AudioWaveform::setDuration
void setDuration(double time)
cugl::AudioWaveform::_random
std::minstd_rand0 _random
Definition: CUAudioWaveform.h:192
cugl::AudioWaveform::alloc
static std::shared_ptr< AudioWaveform > alloc(Uint8 channels, Uint32 rate, Type type, float frequency)
Definition: CUAudioWaveform.h:308
cugl::AudioWaveform::allocWithData
static std::shared_ptr< AudioWaveform > allocWithData(const std::shared_ptr< JsonValue > &data)
cugl::AudioWaveform::isUpper
bool isUpper() const
cugl::AudioWaveform::getType
Type getType() const
cugl::AudioWaveform::setType
void setType(Type type)
cugl::AudioWaveform::~AudioWaveform
~AudioWaveform()
Definition: CUAudioWaveform.h:207
cugl::AudioWaveform::Type::NOISE
cugl::AudioWaveform::AudioWaveform
AudioWaveform()
cugl::AudioWaveform::DEFAULT_FREQUENCY
const static float DEFAULT_FREQUENCY
Definition: CUAudioWaveform.h:81
cugl::AudioWaveform::Type::NAIVE_TRIANG
cugl::AudioWaveform::_type
std::atomic< int > _type
Definition: CUAudioWaveform.h:181
cugl::AudioWaveform::setUpper
void setUpper(bool upper)
cugl::AudioWaveform::_upper
std::atomic< bool > _upper
Definition: CUAudioWaveform.h:183
cugl::AudioWaveform::Type::POLY_SQUARE
cugl::AudioWaveform::_duration
std::atomic< double > _duration
Definition: CUAudioWaveform.h:189
cugl::AudioWaveform::Type::NAIVE_SQUARE
cugl::AudioWaveform::Type::NAIVE_TRAIN
cugl::AudioWaveform::alloc
static std::shared_ptr< AudioWaveform > alloc(Uint8 channels, Uint32 rate)
Definition: CUAudioWaveform.h:285
cugl::AudioWaveform::alloc
static std::shared_ptr< AudioWaveform > alloc()
Definition: CUAudioWaveform.h:269
cugl::AudioWaveform::_frequency
std::atomic< float > _frequency
Definition: CUAudioWaveform.h:185
cugl::AudioWaveform::Type::NAIVE_TOOTH
cugl::AudioWaveform::Type::UNKNOWN
cugl::AudioWaveform::Type::BLIT_TRAIN
cugl::AudioWaveform::getLength
virtual Sint64 getLength() const override