arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
FastIntSineGenerator< T > Class Template Reference

Sine wave generator that does not use any floating point operations in the readSample() hot path: it advances a 32 bit phase accumulator with an integer increment and looks up the result in a Q15 fixed point table. All floating point math (converting frequency/sample_rate into the phase increment) is done once in setFrequency()/setAudioInfo(), never per sample. This makes it a good fit for MCUs without a hardware FPU - e.g. the RP2040 - where sinf() is comparatively expensive. More...

#include <SoundGenerator.h>

Inheritance diagram for FastIntSineGenerator< T >:
SoundGenerator< T >

Public Member Functions

 FastIntSineGenerator (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 ()
 Ends the processing.
 
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 or sin() calls!
 
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.
 
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.
 

Protected Member Functions

applyRamp (T sample)
 
size_t readBytesFrames (uint8_t *buffer, size_t lengthBytes, int frames, int channels)
 
size_t readBytesFromBuffer (uint8_t *buffer, size_t lengthBytes, int frame_size, int channels)
 
void recalculatePlayTime ()
 
void updatePhaseIncrement ()
 

Protected Attributes

bool active = false
 
bool activeWarningIssued = false
 
uint32_t currentSample = 0
 
uint8_t downPercent = 40
 
float factor = 1.0f
 
AudioInfo info
 
float m_amplitude = 1.0f
 
int32_t m_amplitude_i = 32767
 
volatile float m_frequency = 0.0f
 
uint32_t m_phase_acc = 0
 
uint32_t m_phase_increment = 0
 
uint32_t m_phase_offset = 0
 
uint32_t playMs = 0
 
uint32_t playSamples = 0
 
float rampDownDec = 0.0
 
uint32_t rampDownSamples = 0
 
float rampUpInc = 0.0
 
RingBuffer< uint8_t > ring_buffer {0}
 
uint8_t upPercent = 5
 
uint32_t upSamples = 0
 

Static Protected Attributes

static const int kIndexShift = 32 - kTableBits
 
static const int kTableBits = 8
 
static const int kTableSize = 1 << kTableBits
 
static constexpr int16_t sine_table [kTableSize]
 

Detailed Description

template<class T = int16_t>
class audio_tools::FastIntSineGenerator< T >

Sine wave generator that does not use any floating point operations in the readSample() hot path: it advances a 32 bit phase accumulator with an integer increment and looks up the result in a Q15 fixed point table. All floating point math (converting frequency/sample_rate into the phase increment) is done once in setFrequency()/setAudioInfo(), never per sample. This makes it a good fit for MCUs without a hardware FPU - e.g. the RP2040 - where sinf() is comparatively expensive.

Author
Phil Schatzmann
Template Parameters
T

Constructor & Destructor Documentation

◆ FastIntSineGenerator()

template<class T = int16_t>
FastIntSineGenerator ( float  amplitude = NumberConverter::maxValueT<T>(),
float  phase = 0.0f 
)
inline

Member Function Documentation

◆ applyRamp()

template<class T = int16_t>
T applyRamp ( sample)
inlineprotectedinherited

◆ audioInfo()

template<class T = int16_t>
virtual AudioInfo audioInfo ( )
inlinevirtualinherited

Provides the AudioInfo.

◆ begin() [1/4]

template<class T = int16_t>
bool begin ( )
inlineoverridevirtual

Starts the processing.

Reimplemented from SoundGenerator< T >.

◆ begin() [2/4]

template<class T = int16_t>
bool begin ( AudioInfo  info)
inlineoverridevirtual

Starts the processing with the provided AudioInfo.

Reimplemented from SoundGenerator< 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>
virtual AudioInfo defaultConfig ( )
inlinevirtualinherited

◆ end()

template<class T = int16_t>
virtual void end ( )
inlinevirtualinherited

Ends the processing.

Reimplemented in GeneratorFromArray< T >.

◆ 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.

◆ 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>
virtual T readSample ( )
inlineoverridevirtual

Provides a single sample - integer only, no float ops or sin() calls!

Implements SoundGenerator< T >.

◆ recalculatePlayTime()

template<class T = int16_t>
void recalculatePlayTime ( )
inlineprotectedinherited

◆ setAmplitude()

template<class T = int16_t>
void setAmplitude ( float  amp)
inline

◆ setAudioInfo()

template<class T = int16_t>
virtual void setAudioInfo ( AudioInfo  info)
inlineoverridevirtual

Defines/updates the AudioInfo.

Reimplemented from SoundGenerator< 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>
void setPlayTime ( uint32_t  playMs,
uint8_t  upPercent = 20,
uint8_t  downPercent = 30 
)
inlineinherited

Defines the play time in ms and the ramp up and ramp down time in percent.

◆ updatePhaseIncrement()

template<class T = int16_t>
void updatePhaseIncrement ( )
inlineprotected

Member Data Documentation

◆ active

template<class T = int16_t>
bool active = false
protectedinherited

◆ activeWarningIssued

template<class T = int16_t>
bool activeWarningIssued = false
protectedinherited

◆ currentSample

template<class T = int16_t>
uint32_t currentSample = 0
protectedinherited

◆ downPercent

template<class T = int16_t>
uint8_t downPercent = 40
protectedinherited

◆ factor

template<class T = int16_t>
float factor = 1.0f
protectedinherited

◆ info

template<class T = int16_t>
AudioInfo info
protectedinherited

◆ kIndexShift

template<class T = int16_t>
const int kIndexShift = 32 - kTableBits
staticprotected

◆ kTableBits

template<class T = int16_t>
const int kTableBits = 8
staticprotected

◆ kTableSize

template<class T = int16_t>
const int kTableSize = 1 << kTableBits
staticprotected

◆ m_amplitude

template<class T = int16_t>
float m_amplitude = 1.0f
protected

◆ 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>
uint32_t m_phase_acc = 0
protected

◆ 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>
uint32_t playMs = 0
protectedinherited

◆ playSamples

template<class T = int16_t>
uint32_t playSamples = 0
protectedinherited

◆ rampDownDec

template<class T = int16_t>
float rampDownDec = 0.0
protectedinherited

◆ rampDownSamples

template<class T = int16_t>
uint32_t rampDownSamples = 0
protectedinherited

◆ rampUpInc

template<class T = int16_t>
float rampUpInc = 0.0
protectedinherited

◆ ring_buffer

template<class T = int16_t>
RingBuffer<uint8_t> ring_buffer {0}
protectedinherited

◆ sine_table

template<class T = int16_t>
constexpr int16_t sine_table[kTableSize]
staticconstexprprotected
Initial value:
= {
0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179,
7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732,
15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403,
22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571,
30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521,
32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521,
32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790,
26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403,
20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732,
14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804,
-1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739,
-9512, -10278,-11039,-11793,-12539,-13279,-14010,-14732,-15446,-16151,
-16846,-17530,-18204,-18868,-19519,-20159,-20787,-21403,-22005,-22594,
-23170,-23731,-24279,-24811,-25329,-25832,-26319,-26790,-27245,-27683,
-28105,-28510,-28898,-29268,-29621,-29956,-30273,-30571,-30852,-31113,
-31356,-31580,-31785,-31971,-32137,-32285,-32412,-32521,-32609,-32678,
-32728,-32757,-32767,-32757,-32728,-32678,-32609,-32521,-32412,-32285,
-32137,-31971,-31785,-31580,-31356,-31113,-30852,-30571,-30273,-29956,
-29621,-29268,-28898,-28510,-28105,-27683,-27245,-26790,-26319,-25832,
-25329,-24811,-24279,-23731,-23170,-22594,-22005,-21403,-20787,-20159,
-19519,-18868,-18204,-17530,-16846,-16151,-15446,-14732,-14010,-13279,
-12539,-11793,-11039,-10278,-9512, -8739, -7962, -7179, -6393, -5602,
-4808, -4011, -3212, -2410, -1608, -804}

◆ upPercent

template<class T = int16_t>
uint8_t upPercent = 5
protectedinherited

◆ upSamples

template<class T = int16_t>
uint32_t upSamples = 0
protectedinherited

The documentation for this class was generated from the following file: