25 #include "freertos/FreeRTOS.h" 26 #include "freertos/timers.h" 32 #include "driver/i2s.h" 33 #include "driver/adc.h" 34 #include "esp_adc_cal.h" 44 #define FABGL_SAMPLE_BUFFER_SIZE 32 52 static const int8_t sinTable[257] = {
53 0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
54 49, 51, 54, 57, 60, 63, 65, 68, 71, 73, 76, 78, 81, 83, 85, 88,
55 90, 92, 94, 96, 98, 100, 102, 104, 106, 107, 109, 111, 112, 113, 115, 116,
56 117, 118, 120, 121, 122, 122, 123, 124, 125, 125, 126, 126, 126, 127, 127, 127,
57 127, 127, 127, 127, 126, 126, 126, 125, 125, 124, 123, 122, 122, 121, 120, 118,
58 117, 116, 115, 113, 112, 111, 109, 107, 106, 104, 102, 100, 98, 96, 94, 92,
59 90, 88, 85, 83, 81, 78, 76, 73, 71, 68, 65, 63, 60, 57, 54, 51,
60 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 12, 9, 6, 3,
61 0, -3, -6, -9, -12, -16, -19, -22, -25, -28, -31, -34, -37, -40, -43, -46,
62 -49, -51, -54, -57, -60, -63, -65, -68, -71, -73, -76, -78, -81, -83, -85, -88,
63 -90, -92, -94, -96, -98, -100, -102, -104, -106, -107, -109, -111, -112, -113, -115, -116,
64 -117, -118, -120, -121, -122, -122, -123, -124, -125, -125, -126, -126, -126, -127, -127, -127,
65 -127, -127, -127, -127, -126, -126, -126, -125, -125, -124, -123, -122, -122, -121, -120, -118,
66 -117, -116, -115, -113, -112, -111, -109, -107, -106, -104, -102, -100, -98, -96, -94, -92,
67 -90, -88, -85, -83, -81, -78, -76, -73, -71, -68, -65, -63, -60, -57, -54, -51,
68 -49, -46, -43, -40, -37, -34, -31, -28, -25, -22, -19, -16, -12, -9, -6, -3,
73 SineWaveformGenerator::SineWaveformGenerator()
83 if (m_frequency != value) {
85 m_phaseInc = (((uint32_t)m_frequency * 256) << 11) /
sampleRate();
91 if (m_frequency == 0 ||
duration() == 0) {
94 else if (m_lastSample < 0)
102 uint32_t index = m_phaseAcc >> 11;
103 double fmul = (double)(m_phaseAcc & 0x7ff) / 2048.0;
104 int sample = sinTable[index] + (sinTable[index + 1] - sinTable[index]) * fmul;
107 sample = sample *
volume() / 127;
109 m_lastSample = sample;
111 m_phaseAcc = (m_phaseAcc + m_phaseInc) & 0x7ffff;
128 SquareWaveformGenerator::SquareWaveformGenerator()
139 if (m_frequency != value) {
141 m_phaseInc = (((uint32_t)m_frequency * 256) << 11) /
sampleRate();
149 m_dutyCycle = dutyCycle;
154 if (m_frequency == 0 ||
duration() == 0) {
155 if (m_lastSample > 0)
157 else if (m_lastSample < 0)
164 uint32_t index = m_phaseAcc >> 11;
165 int sample = (index <= m_dutyCycle ? 127 : -127);
168 sample = sample *
volume() / 127;
170 m_lastSample = sample;
172 m_phaseAcc = (m_phaseAcc + m_phaseInc) & 0x7ffff;
187 TriangleWaveformGenerator::TriangleWaveformGenerator()
197 if (m_frequency != value) {
199 m_phaseInc = (((uint32_t)m_frequency * 256) << 11) /
sampleRate();
205 if (m_frequency == 0 ||
duration() == 0) {
206 if (m_lastSample > 0)
208 else if (m_lastSample < 0)
215 uint32_t index = m_phaseAcc >> 11;
216 int sample = (index & 0x80 ? -1 : 1) * ((index & 0x3F) * 2 - (index & 0x40 ? 0 : 127));
219 sample = sample *
volume() / 127;
221 m_lastSample = sample;
223 m_phaseAcc = (m_phaseAcc + m_phaseInc) & 0x7ffff;
239 SawtoothWaveformGenerator::SawtoothWaveformGenerator()
249 if (m_frequency != value) {
251 m_phaseInc = (((uint32_t)m_frequency * 256) << 11) /
sampleRate();
257 if (m_frequency == 0 ||
duration() == 0) {
258 if (m_lastSample > 0)
260 else if (m_lastSample < 0)
267 uint32_t index = m_phaseAcc >> 11;
268 int sample = index - 128;
271 sample = sample *
volume() / 127;
273 m_lastSample = sample;
275 m_phaseAcc = (m_phaseAcc + m_phaseInc) & 0x7ffff;
291 NoiseWaveformGenerator::NoiseWaveformGenerator()
309 m_noise = (m_noise >> 1) ^ (-(m_noise & 1) & 0xB400u);
310 int sample = 127 - (m_noise >> 8);
313 sample = sample *
volume() / 127;
332 VICNoiseGenerator::VICNoiseGenerator()
343 if (m_frequency != value) {
344 m_frequency = value >= 127 ? 0 : value;
362 for (
int i = 0; i < reduc; ++i) {
364 if (m_counter >= 127) {
367 m_counter = m_frequency;
370 m_outSR = ((m_outSR << 1) | ~(m_outSR >> 7));
373 int bit3 = (m_LFSR >> 3) & 1;
374 int bit12 = (m_LFSR >> 12) & 1;
375 int bit14 = (m_LFSR >> 14) & 1;
376 int bit15 = (m_LFSR >> 15) & 1;
377 m_LFSR |= (bit3 ^ bit12) ^ (bit14 ^ bit15);
381 sample += m_outSR & 1 ? 127 : -128;
386 sample = sample / reduc;
389 sample = sample *
volume() / 127;
406 SamplesGenerator::SamplesGenerator(int8_t
const *
data,
int length)
425 int sample = m_data[m_index++];
427 if (m_index == m_length)
431 sample = sample *
volume() / 127;
451 : m_waveGenTaskHandle(nullptr),
453 m_sampleBuffer(nullptr),
455 m_sampleRate(sampleRate),
457 m_state(SoundGeneratorState::Stop)
459 m_mutex = xSemaphoreCreateMutex();
464 SoundGenerator::~SoundGenerator()
467 vTaskDelete(m_waveGenTaskHandle);
468 heap_caps_free(m_sampleBuffer);
469 vSemaphoreDelete(m_mutex);
475 AutoSemaphore autoSemaphore(m_mutex);
477 m_channels =
nullptr;
481 void SoundGenerator::i2s_audio_init()
483 i2s_config_t i2s_config;
484 i2s_config.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
485 i2s_config.sample_rate = m_sampleRate;
486 i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
487 i2s_config.communication_format = (i2s_comm_format_t) I2S_COMM_FORMAT_I2S_MSB;
488 i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
489 i2s_config.intr_alloc_flags = 0;
490 i2s_config.dma_buf_count = 2;
491 i2s_config.dma_buf_len = FABGL_SAMPLE_BUFFER_SIZE *
sizeof(uint16_t);
492 i2s_config.use_apll = 0;
493 i2s_config.tx_desc_auto_clear = 0;
495 i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
497 i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);
499 m_sampleBuffer = (uint16_t*) heap_caps_malloc(FABGL_SAMPLE_BUFFER_SIZE *
sizeof(uint16_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
506 AutoSemaphore autoSemaphore(m_mutex);
508 if (actualPlaying() != value) {
509 bool r = forcePlay(value);
522 sgen->setAutoDestroy(
true);
524 sgen->setDuration(durationMS > 0 ? (m_sampleRate / 1000 * durationMS) : length );
532 bool SoundGenerator::forcePlay(
bool value)
534 bool isPlaying = actualPlaying();
538 if (!m_waveGenTaskHandle)
539 xTaskCreate(waveGenTask,
"", WAVEGENTASK_STACK_SIZE,
this, 5, &m_waveGenTaskHandle);
540 m_state = SoundGeneratorState::RequestToPlay;
541 xTaskNotifyGive(m_waveGenTaskHandle);
547 m_state = SoundGeneratorState::RequestToStop;
549 while (m_state != SoundGeneratorState::Stop)
557 bool SoundGenerator::actualPlaying()
559 return m_waveGenTaskHandle && m_state == SoundGeneratorState::Playing;
566 AutoSemaphore autoSemaphore(m_mutex);
568 bool isPlaying = forcePlay(
false);
570 value->setSampleRate(m_sampleRate);
572 value->next = m_channels;
575 forcePlay(isPlaying || m_play);
584 AutoSemaphore autoSemaphore(m_mutex);
586 bool isPlaying = forcePlay(
false);
587 detachNoSuspend(value);
588 forcePlay(isPlaying);
594 for (
WaveformGenerator * c = m_channels, * prev =
nullptr; c; prev = c, c = c->next) {
597 prev->next = c->next;
599 m_channels = c->next;
600 if (value->autoDestroy())
608 void SoundGenerator::waveGenTask(
void * arg)
612 i2s_set_clk(I2S_NUM_0, soundGenerator->m_sampleRate, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
614 uint16_t * buf = soundGenerator->m_sampleBuffer;
617 int muteCyclesCount = 0;
622 if (soundGenerator->m_state == SoundGeneratorState::RequestToStop || soundGenerator->m_state == SoundGeneratorState::Stop) {
623 soundGenerator->m_state = SoundGeneratorState::Stop;
624 while (soundGenerator->m_state == SoundGeneratorState::Stop)
625 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
629 if (soundGenerator->m_channels ==
nullptr && muteCyclesCount >= 8) {
630 soundGenerator->m_state = SoundGeneratorState::Stop;
631 while (soundGenerator->m_state == SoundGeneratorState::Stop)
632 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
635 soundGenerator->m_state = SoundGeneratorState::Playing;
637 int mainVolume = soundGenerator->volume();
639 for (
int i = 0; i < FABGL_SAMPLE_BUFFER_SIZE; ++i) {
640 int sample = 0, tvol = 0;
641 for (
auto g = soundGenerator->m_channels; g; ) {
643 sample += g->getSample();
645 }
else if (g->duration() == 0 && g->autoDetach()) {
648 soundGenerator->detachNoSuspend(curr);
654 int avol = tvol ? imin(127, 127 * 127 / tvol) : 127;
655 sample = sample * avol / 127;
656 sample = sample * mainVolume / 127;
658 buf[i + (i & 1 ? -1 : 1)] = (127 + sample) << 8;
661 size_t bytes_written;
662 i2s_write(I2S_NUM_0, buf, FABGL_SAMPLE_BUFFER_SIZE *
sizeof(uint16_t), &bytes_written, portMAX_DELAY);
664 muteCyclesCount = soundGenerator->m_channels ==
nullptr ? muteCyclesCount + 1 : 0;
669 void SoundGenerator::mutizeOutput()
671 for (
int i = 0; i < FABGL_SAMPLE_BUFFER_SIZE; ++i)
672 m_sampleBuffer[i] = 127 << 8;
673 size_t bytes_written;
674 for (
int i = 0; i < 4; ++i)
675 i2s_write(I2S_NUM_0, m_sampleBuffer, FABGL_SAMPLE_BUFFER_SIZE *
sizeof(uint16_t), &bytes_written, portMAX_DELAY);
SamplesGenerator * playSamples(int8_t const *data, int length, int volume=100, int durationMS=0)
Plays the specified samples.
int getSample()
Gets next sample.
void setFrequency(int value)
Sets output frequency.
void attach(WaveformGenerator *value)
Attaches a waveform generator.
int getSample()
Gets next sample.
int volume()
Determines current overall volume.
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.
void setFrequency(int value)
Sets output frequency.
This file contains all classes related to FabGL Sound System.
SoundGenerator(int sampleRate=DEFAULT_SAMPLE_RATE)
Creates an instance of the sound generator. Only one instance is allowed.