38 #include "freertos/FreeRTOS.h" 48 #define DEFAULT_SAMPLE_RATE 16000 52 #define I2S_SAMPLE_BUFFER_SIZE 200 // must be even 54 #define WAVEGENTASK_STACK_SIZE 1024 60 WaveformGenerator() : next(
nullptr), m_sampleRate(0), m_volume(100), m_enabled(
false), m_duration(-1), m_autoDestroy(
false), m_autoDetach(
false) { }
92 bool autoDetach() {
return m_autoDetach; }
99 void setAutoDestroy(
bool value) { m_autoDestroy = value; m_autoDetach |= value; }
101 bool autoDestroy() {
return m_autoDestroy; }
138 void enable(
bool value) { m_enabled = value; }
160 void decDuration() { --m_duration;
if (m_duration == 0) m_enabled =
false; }
163 uint16_t m_sampleRate;
184 uint16_t m_frequency;
185 int16_t m_lastSample;
208 uint16_t m_frequency;
226 uint16_t m_frequency;
243 uint16_t m_frequency;
276 uint16_t frequency() {
return m_frequency; }
281 static const uint16_t LFSRINIT = 0x0202;
282 static const int CLK = 4433618;
284 uint16_t m_frequency;
307 int8_t
const * m_data;
313 enum class SoundGeneratorState {
364 bool play(
bool value);
402 template <
typename T>
404 auto wf =
new T(waveform);
406 wf->setFrequency(frequency);
408 wf->setAutoDestroy(
true);
409 wf->setDuration(m_sampleRate / 1000 * durationMS);
435 void attach(WaveformGenerator * value);
442 void detach(WaveformGenerator * value);
461 void i2s_audio_init();
462 static void waveGenTask(
void * arg);
463 bool forcePlay(
bool value);
466 bool actualPlaying();
469 TaskHandle_t m_waveGenTaskHandle;
473 uint16_t * m_sampleBuffer;
477 uint16_t m_sampleRate;
480 SoundGeneratorState m_state;
481 SemaphoreHandle_t m_mutex;
SamplesGenerator * playSamples(int8_t const *data, int length, int volume=100, int durationMS=0)
Plays the specified samples.
void playSound(T const &waveform, int frequency, int durationMS, int volume=100)
Plays the specified waveform.
int getSample()
Gets next sample.
void setFrequency(int value)
Sets output frequency.
void setVolume(int value)
Sets the overall volume.
void attach(WaveformGenerator *value)
Attaches a waveform generator.
int getSample()
Gets next sample.
bool playing()
Determines whether sound generator is playing.
int volume()
Determines current overall volume.
This file contains some utility classes and functions.
void clear()
Stops playing and removes all attached waveform generators.
void detach(WaveformGenerator *value)
Detaches a waveform generator.
bool play(bool value)
Starts or stops playing.
SoundGenerator handles audio output.
Emulates VIC6561 (VIC20) noise generator.
void setFrequency(int value)
Sets output frequency.
This file contains FabGL library configuration settings, like number of supported colors...
SoundGenerator(int sampleRate=DEFAULT_SAMPLE_RATE)
Creates an instance of the sound generator. Only one instance is allowed.