42 #include "freertos/FreeRTOS.h" 52 #define DEFAULT_SAMPLE_RATE 16000 56 #define I2S_SAMPLE_BUFFER_SIZE 200 // must be even 58 #define WAVEGENTASK_STACK_SIZE 2000 64 WaveformGenerator() : next(
nullptr), m_sampleRate(0), m_volume(100), m_enabled(
false), m_duration(-1), m_autoDestroy(
false), m_autoDetach(
false) { }
96 bool autoDetach() {
return m_autoDetach; }
103 void setAutoDestroy(
bool value) { m_autoDestroy = value; m_autoDetach |= value; }
105 bool autoDestroy() {
return m_autoDestroy; }
142 void enable(
bool value) { m_enabled = value; }
164 void decDuration() { --m_duration;
if (m_duration == 0) m_enabled =
false; }
167 uint16_t m_sampleRate;
188 uint16_t m_frequency;
189 int16_t m_lastSample;
212 uint16_t m_frequency;
230 uint16_t m_frequency;
247 uint16_t m_frequency;
280 uint16_t frequency() {
return m_frequency; }
285 static const uint16_t LFSRINIT = 0x0202;
286 static const int CLK = 4433618;
288 uint16_t m_frequency;
311 int8_t
const * m_data;
320 enum class SoundGeneratorState {
371 bool play(
bool value);
409 template <
typename T>
411 auto wf =
new T(waveform);
413 wf->setFrequency(frequency);
415 wf->setAutoDestroy(
true);
416 wf->setDuration(m_sampleRate / 1000 * durationMS);
442 void attach(WaveformGenerator * value);
449 void detach(WaveformGenerator * value);
468 void i2s_audio_init();
469 static void waveGenTask(
void * arg);
470 bool forcePlay(
bool value);
473 bool actualPlaying();
476 TaskHandle_t m_waveGenTaskHandle;
480 uint16_t * m_sampleBuffer;
484 uint16_t m_sampleRate;
487 SoundGeneratorState m_state;
488 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=16000)
Creates an instance of the sound generator. Only one instance is allowed.