Generates a square wave sound. Uses the same 32 bit phase accumulator as SawToothGenerator/FastIntSineGenerator: no floating point operations or sin() calls in the readSample() hot path. A square wave needs neither a sine approximation nor a lookup table - just the sign bit of the ramp - so readSample() is a single comparison, making this a good fit for MCUs without a hardware FPU (e.g. RP2040), where the previous FastSineGenerator based implementation still did a float polynomial evaluation per sample just to keep its sign.
More...
#include <SoundGenerator.h>
|
| | SquareWaveGenerator (float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f) |
| |
| virtual AudioInfo | audioInfo () |
| | Provides the AudioInfo.
|
| |
| bool | begin () override |
| | Starts the processing.
|
| |
| bool | begin (AudioInfo info) override |
| | Starts the processing with the provided AudioInfo.
|
| |
| bool | begin (AudioInfo info, float frequency) |
| |
| bool | begin (int channels, int sample_rate, float frequency) |
| |
| virtual AudioInfo | defaultConfig () |
| | Provides the default configuration.
|
| |
| virtual void | end () |
| |
| virtual void | end (bool allow_rampdown) |
| |
| virtual bool | isActive () |
| | Checks if the begin method has been called - after end() isActive is false.
|
| |
| virtual size_t | readBytes (uint8_t *data, size_t len) |
| | Provides the data as byte array with the requested number of channels.
|
| |
| virtual T | readSample () override |
| | Provides a single sample - integer only, no float ops!
|
| |
| virtual size_t | readSamples (T *data, size_t len) |
| |
| virtual void | restart () |
| | Restarts the generator, e.g. after a play time has been defined and reached.
|
| |
| void | setAmplitude (float amp) |
| |
| virtual void | setAudioInfo (AudioInfo info) override |
| | Defines/updates the AudioInfo.
|
| |
| void | setFrequency (float frequency) override |
| | Defines the frequency - the only place where float math is used.
|
| |
| void | setPhase (float phase) |
| | Defines the starting phase in radians.
|
| |
| virtual void | setPlayTime (uint32_t playMs, uint8_t upPercent=20, uint8_t downPercent=30) |
| | Defines the play time in ms and the ramp up and ramp down time in percent.
|
| |
| virtual void | setRampTimes (uint32_t playMs, uint32_t upMs=5, uint32_t downMs=5) |
| |
template<class T = int16_t>
class audio_tools::SquareWaveGenerator< T >
Generates a square wave sound. Uses the same 32 bit phase accumulator as SawToothGenerator/FastIntSineGenerator: no floating point operations or sin() calls in the readSample() hot path. A square wave needs neither a sine approximation nor a lookup table - just the sign bit of the ramp - so readSample() is a single comparison, making this a good fit for MCUs without a hardware FPU (e.g. RP2040), where the previous FastSineGenerator based implementation still did a float polynomial evaluation per sample just to keep its sign.
- Author
- Phil Schatzmann
- Copyright
- GPLv3
◆ SquareWaveGenerator()
template<class T = int16_t>
◆ applyRamp()
template<class T = int16_t>
◆ audioInfo()
template<class T = int16_t>
◆ begin() [1/4]
template<class T = int16_t>
◆ begin() [2/4]
template<class T = int16_t>
◆ begin() [3/4]
template<class T = int16_t>
| bool begin |
( |
AudioInfo |
info, |
|
|
float |
frequency |
|
) |
| |
|
inline |
◆ begin() [4/4]
template<class T = int16_t>
| bool begin |
( |
int |
channels, |
|
|
int |
sample_rate, |
|
|
float |
frequency |
|
) |
| |
|
inline |
◆ defaultConfig()
template<class T = int16_t>
◆ end() [1/2]
template<class T = int16_t>
◆ end() [2/2]
template<class T = int16_t>
| virtual void end |
( |
bool |
allow_rampdown | ) |
|
|
inlinevirtualinherited |
Ends the processing. If either setPlayTime() or setRampTimes() have been set, then allow_rampdown=true will start a ramp-down instead of stopping immediately.
◆ isActive()
template<class T = int16_t>
| virtual bool isActive |
( |
| ) |
|
|
inlinevirtualinherited |
Checks if the begin method has been called - after end() isActive is false.
◆ readBytes()
template<class T = int16_t>
| virtual size_t readBytes |
( |
uint8_t * |
data, |
|
|
size_t |
len |
|
) |
| |
|
inlinevirtualinherited |
Provides the data as byte array with the requested number of channels.
Reimplemented in GeneratorQueue< T >.
◆ readBytesFrames()
template<class T = int16_t>
| size_t readBytesFrames |
( |
uint8_t * |
buffer, |
|
|
size_t |
lengthBytes, |
|
|
int |
frames, |
|
|
int |
channels |
|
) |
| |
|
inlineprotectedinherited |
◆ readBytesFromBuffer()
template<class T = int16_t>
| size_t readBytesFromBuffer |
( |
uint8_t * |
buffer, |
|
|
size_t |
lengthBytes, |
|
|
int |
frame_size, |
|
|
int |
channels |
|
) |
| |
|
inlineprotectedinherited |
◆ readSample()
template<class T = int16_t>
◆ readSamples()
template<class T = int16_t>
| virtual size_t readSamples |
( |
T * |
data, |
|
|
size_t |
len |
|
) |
| |
|
inlinevirtualinherited |
Provides the data as T array (interleaved by channel) with the requested number of samples (values, not frames)
Reimplemented in GeneratorQueue< T >.
◆ recalculatePlayTime()
template<class T = int16_t>
| void recalculatePlayTime |
( |
| ) |
|
|
inlineprotectedinherited |
◆ restart()
template<class T = int16_t>
Restarts the generator, e.g. after a play time has been defined and reached.
◆ setAmplitude()
template<class T = int16_t>
| void setAmplitude |
( |
float |
amp | ) |
|
|
inline |
◆ setAudioInfo()
template<class T = int16_t>
◆ setFrequency()
template<class T = int16_t>
| void setFrequency |
( |
float |
frequency | ) |
|
|
inlineoverridevirtual |
Defines the frequency - the only place where float math is used.
Reimplemented from SoundGenerator< T >.
◆ setPhase()
template<class T = int16_t>
| void setPhase |
( |
float |
phase | ) |
|
|
inline |
Defines the starting phase in radians.
◆ setPlayTime()
template<class T = int16_t>
| virtual void setPlayTime |
( |
uint32_t |
playMs, |
|
|
uint8_t |
upPercent = 20, |
|
|
uint8_t |
downPercent = 30 |
|
) |
| |
|
inlinevirtualinherited |
Defines the play time in ms and the ramp up and ramp down time in percent.
Reimplemented in GeneratorQueue< T >.
◆ setRampTimes()
template<class T = int16_t>
| virtual void setRampTimes |
( |
uint32_t |
playMs, |
|
|
uint32_t |
upMs = 5, |
|
|
uint32_t |
downMs = 5 |
|
) |
| |
|
inlinevirtualinherited |
Alternative to setPlayTime. It sets ramp-up and ramp-down times in ms instead of percentages. If playMs == 0, it will play indefinitely. restart();begin(); starts the ramp-up and sustain as normal, but it will sustain forever. end(true) triggers a ramp-down then inactive.
◆ updatePhaseIncrement()
template<class T = int16_t>
| void updatePhaseIncrement |
( |
| ) |
|
|
inlineprotected |
◆ active
template<class T = int16_t>
◆ activeWarningIssued
template<class T = int16_t>
| bool activeWarningIssued = false |
|
protectedinherited |
◆ currentSample
template<class T = int16_t>
| uint32_t currentSample = 0 |
|
protectedinherited |
◆ downMs
template<class T = int16_t>
◆ downPercent
template<class T = int16_t>
◆ downSamples
template<class T = int16_t>
◆ factor
template<class T = int16_t>
◆ info
template<class T = int16_t>
◆ m_amplitude
template<class T = int16_t>
◆ m_amplitude_i
template<class T = int16_t>
| int32_t m_amplitude_i = 32767 |
|
protected |
◆ m_frequency
template<class T = int16_t>
| volatile float m_frequency = 0.0f |
|
protected |
◆ m_phase_acc
template<class T = int16_t>
◆ m_phase_increment
template<class T = int16_t>
| uint32_t m_phase_increment = 0 |
|
protected |
◆ m_phase_offset
template<class T = int16_t>
| uint32_t m_phase_offset = 0 |
|
protected |
◆ playMs
template<class T = int16_t>
◆ playSamples
template<class T = int16_t>
◆ rampDownDec
template<class T = int16_t>
◆ rampUpInc
template<class T = int16_t>
◆ ring_buffer
template<class T = int16_t>
◆ upMs
template<class T = int16_t>
◆ upPercent
template<class T = int16_t>
◆ upSamples
template<class T = int16_t>
The documentation for this class was generated from the following file: