From 89241b71e0fb7bb91758abfaf07ac120028c9018 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 23 Aug 2020 10:02:48 +0100 Subject: trying to use a soundfont I get very weird exceptions when calling `tsf_note_on`? --- esp32/lego-piano.ino | 164 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 58 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 575f55e..7826f31 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -17,9 +17,11 @@ */ #include +#define TSF_IMPLEMENTATION +#define TSF_NO_STDIO +#include "../ESP8266Audio/src/libtinysoundfont/tsf.h" int currentLed = 0; -int lastSeen = -1; #define DEBUG 0 @@ -33,6 +35,73 @@ const int ampEnable = 32; const int octave_shift = 2; +char pressed[row_count*col_count] = { 0 }; + +hw_timer_t * timer = NULL; +portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; +tsf* g_TinySoundFont = 0; +bool playing = false; + +//This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes) +const static unsigned char MinimalSoundFont[] PROGMEM = +{ + #define TEN0 0,0,0,0,0,0,0,0,0,0 + 'R','I','F','F',220,1,0,0,'s','f','b','k', + 'L','I','S','T',88,1,0,0,'p','d','t','a', + 'p','h','d','r',76,TEN0,TEN0,TEN0,TEN0,0,0,0,0,TEN0,0,0,0,0,0,0,0,255,0,255,0,1,TEN0,0,0,0, + 'p','b','a','g',8,0,0,0,0,0,0,0,1,0,0,0,'p','m','o','d',10,TEN0,0,0,0,'p','g','e','n',8,0,0,0,41,0,0,0,0,0,0,0, + 'i','n','s','t',44,TEN0,TEN0,0,0,0,0,0,0,0,0,TEN0,0,0,0,0,0,0,0,1,0, + 'i','b','a','g',8,0,0,0,0,0,0,0,2,0,0,0,'i','m','o','d',10,TEN0,0,0,0, + 'i','g','e','n',12,0,0,0,54,0,1,0,53,0,0,0,0,0,0,0, + 's','h','d','r',92,TEN0,TEN0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,49,0,0,0,34,86,0,0,60,0,0,0,1,TEN0,TEN0,TEN0,TEN0,0,0,0,0,0,0,0, + 'L','I','S','T',112,0,0,0,'s','d','t','a','s','m','p','l',100,0,0,0,86,0,119,3,31,7,147,10,43,14,169,17,58,21,189,24,73,28,204,31,73,35,249,38,46,42,71,46,250,48,150,53,242,55,126,60,151,63,108,66,126,72,207, + 70,86,83,100,72,74,100,163,39,241,163,59,175,59,179,9,179,134,187,6,186,2,194,5,194,15,200,6,202,96,206,159,209,35,213,213,216,45,220,221,223,76,227,221,230,91,234,242,237,105,241,8,245,118,248,32,252 +}; + +class DblBuffer { + const static size_t _size = 1000; + short samples[2][_size]; + size_t readPtr; + char which; + bool has_data[2]; + + public: + DblBuffer() :which(0), readPtr(0) { + has_data[0]=false; + has_data[1]=false; + } + + bool needsWriting() { + return !has_data[1-which]; + } + size_t size() { return _size; } + short* ptr() { return samples[1-which]; } + void writingDone() { + has_data[1-which]=true; + } + + short pop() { + if (readPtr >= _size) { + has_data[which]=false; + which=1-which;readPtr=0; + } + if (!has_data[which]) return 0; + + return samples[which][readPtr++]; + } +} buffer; + +void IRAM_ATTR onTimer() { + return; + if (!g_TinySoundFont) return; + + portENTER_CRITICAL_ISR(&timerMux); + + dac_output_voltage(DAC_CHANNEL_1, buffer.pop() >> 8); + + portEXIT_CRITICAL_ISR(&timerMux); +} + void setup() { Serial.begin(115200); @@ -51,27 +120,22 @@ void setup() { } currentLed = 0; - lastSeen = -1; -} - -void play(uint32_t freq) { - dac_cw_config_t wave_config = { - en_ch: DAC_CHANNEL_1, - scale: DAC_CW_SCALE_2, - phase: DAC_CW_PHASE_0, - freq: octave_shift*freq, - offset: 0, - }; - - dac_cw_generator_config(&wave_config); - dac_cw_generator_enable(); - - digitalWrite(ampEnable, HIGH); -} - -void mute() { - digitalWrite(ampEnable, LOW); - dac_cw_generator_disable(); + playing = false; + + g_TinySoundFont = tsf_load_memory(MinimalSoundFont, sizeof(MinimalSoundFont)); + if (g_TinySoundFont) { + // render at 20kHz + tsf_set_output(g_TinySoundFont, TSF_MONO, 20000, 0); + + // pre-scaling by 4k, then calling on every 1 tick, should give us 20kHz + timer = timerBegin(0, 1000, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmWrite(timer, 4, true); + timerAlarmEnable(timer); + } + else { + Serial.println("failed to start tsf"); + } } void tristate(int pin) { @@ -123,36 +187,6 @@ int sense(int led) { return value < 500; } -uint32_t notes[] = { - 261, // C4 - 277, - 294, // D4 - 311, - 330, // E4 - 349, // F4 - 370, - 392, // G4 - 415, - 440, // A4 - 466, - 494, // B4 - - 523, // C5 - 554, - 587, // D5 - 622, - 659, // E5 - 698, // F5 - 740, - 783, // G5 - 831, - 880, // A5 - 932, - 988, // B5 - - 1046, // C6 -}; - void loop() { #if DEBUG & 0x01 Serial.print("current led "); @@ -162,17 +196,31 @@ void loop() { enableLed(currentLed); delay(5); if (sense(currentLed)) { - if (lastSeen != currentLed) { - lastSeen = currentLed; + if (!pressed[currentLed]) { Serial.print(currentLed); Serial.println(" proximity!"); - play(notes[currentLed]); + + pressed[currentLed]=1; + if (g_TinySoundFont) { + portENTER_CRITICAL_ISR(&timerMux); + tsf_note_on(g_TinySoundFont, 0, 60 + currentLed, 1.0f); + portEXIT_CRITICAL_ISR(&timerMux); + } } } - else if (lastSeen == currentLed) { - lastSeen = -1; - mute(); + else if (pressed[currentLed]) { + pressed[currentLed]=0; + if (g_TinySoundFont) { + portENTER_CRITICAL_ISR(&timerMux); + tsf_note_off(g_TinySoundFont, 0, 60 + currentLed); + portEXIT_CRITICAL_ISR(&timerMux); + } } currentLed = (currentLed+1)%(row_count*col_count); + + if (buffer.needsWriting()) { + tsf_render_short(g_TinySoundFont, buffer.ptr(), buffer.size(), 0); + buffer.writingDone(); + } } \ No newline at end of file -- cgit v1.2.3 From f15331de57a4fde6088d7a1b47d933646e75151c Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 23 Aug 2020 10:45:54 +0100 Subject: soundfont works! it's pretty noisy, but might be good enough --- esp32/lego-piano.ino | 61 +++++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 44 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 7826f31..246794e 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -17,9 +17,12 @@ */ #include + #define TSF_IMPLEMENTATION #define TSF_NO_STDIO -#include "../ESP8266Audio/src/libtinysoundfont/tsf.h" +#include "../tsf/tsf.h" + +#include "../RingBuffer/src/RingBuf.h" int currentLed = 0; @@ -43,7 +46,7 @@ tsf* g_TinySoundFont = 0; bool playing = false; //This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes) -const static unsigned char MinimalSoundFont[] PROGMEM = +const static unsigned char MinimalSoundFont[] = { #define TEN0 0,0,0,0,0,0,0,0,0,0 'R','I','F','F',220,1,0,0,'s','f','b','k', @@ -58,46 +61,17 @@ const static unsigned char MinimalSoundFont[] PROGMEM = 70,86,83,100,72,74,100,163,39,241,163,59,175,59,179,9,179,134,187,6,186,2,194,5,194,15,200,6,202,96,206,159,209,35,213,213,216,45,220,221,223,76,227,221,230,91,234,242,237,105,241,8,245,118,248,32,252 }; -class DblBuffer { - const static size_t _size = 1000; - short samples[2][_size]; - size_t readPtr; - char which; - bool has_data[2]; - - public: - DblBuffer() :which(0), readPtr(0) { - has_data[0]=false; - has_data[1]=false; - } - - bool needsWriting() { - return !has_data[1-which]; - } - size_t size() { return _size; } - short* ptr() { return samples[1-which]; } - void writingDone() { - has_data[1-which]=true; - } - - short pop() { - if (readPtr >= _size) { - has_data[which]=false; - which=1-which;readPtr=0; - } - if (!has_data[which]) return 0; - - return samples[which][readPtr++]; - } -} buffer; +RingBuf buffer; void IRAM_ATTR onTimer() { - return; if (!g_TinySoundFont) return; portENTER_CRITICAL_ISR(&timerMux); - dac_output_voltage(DAC_CHANNEL_1, buffer.pop() >> 8); + short sample; + if (buffer.pop(sample)) { + dac_output_voltage(DAC_CHANNEL_1, sample >> 8); + } portEXIT_CRITICAL_ISR(&timerMux); } @@ -136,6 +110,8 @@ void setup() { else { Serial.println("failed to start tsf"); } + + digitalWrite(ampEnable, HIGH); } void tristate(int pin) { @@ -194,7 +170,7 @@ void loop() { #endif enableLed(currentLed); - delay(5); + delay(1); if (sense(currentLed)) { if (!pressed[currentLed]) { Serial.print(currentLed); @@ -202,25 +178,22 @@ void loop() { pressed[currentLed]=1; if (g_TinySoundFont) { - portENTER_CRITICAL_ISR(&timerMux); tsf_note_on(g_TinySoundFont, 0, 60 + currentLed, 1.0f); - portEXIT_CRITICAL_ISR(&timerMux); } } } else if (pressed[currentLed]) { pressed[currentLed]=0; if (g_TinySoundFont) { - portENTER_CRITICAL_ISR(&timerMux); tsf_note_off(g_TinySoundFont, 0, 60 + currentLed); - portEXIT_CRITICAL_ISR(&timerMux); } } currentLed = (currentLed+1)%(row_count*col_count); - if (buffer.needsWriting()) { - tsf_render_short(g_TinySoundFont, buffer.ptr(), buffer.size(), 0); - buffer.writingDone(); + while (!buffer.isFull()) { + short sample; + tsf_render_short(g_TinySoundFont, &sample, 1, 0); + buffer.lockedPush(sample); } } \ No newline at end of file -- cgit v1.2.3 From c0a3728db5f028f764b4887fb1ea8170de2bf1c3 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 28 Aug 2020 18:01:12 +0100 Subject: vaguely working it takes too long to render the font, though --- esp32/lego-piano.ino | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 246794e..88b6520 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -20,9 +20,10 @@ #define TSF_IMPLEMENTATION #define TSF_NO_STDIO -#include "../tsf/tsf.h" +#include "../ESP8266Audio/src/libtinysoundfont/tsf.h" #include "../RingBuffer/src/RingBuf.h" +#include "font.h" int currentLed = 0; @@ -45,22 +46,6 @@ portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; tsf* g_TinySoundFont = 0; bool playing = false; -//This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes) -const static unsigned char MinimalSoundFont[] = -{ - #define TEN0 0,0,0,0,0,0,0,0,0,0 - 'R','I','F','F',220,1,0,0,'s','f','b','k', - 'L','I','S','T',88,1,0,0,'p','d','t','a', - 'p','h','d','r',76,TEN0,TEN0,TEN0,TEN0,0,0,0,0,TEN0,0,0,0,0,0,0,0,255,0,255,0,1,TEN0,0,0,0, - 'p','b','a','g',8,0,0,0,0,0,0,0,1,0,0,0,'p','m','o','d',10,TEN0,0,0,0,'p','g','e','n',8,0,0,0,41,0,0,0,0,0,0,0, - 'i','n','s','t',44,TEN0,TEN0,0,0,0,0,0,0,0,0,TEN0,0,0,0,0,0,0,0,1,0, - 'i','b','a','g',8,0,0,0,0,0,0,0,2,0,0,0,'i','m','o','d',10,TEN0,0,0,0, - 'i','g','e','n',12,0,0,0,54,0,1,0,53,0,0,0,0,0,0,0, - 's','h','d','r',92,TEN0,TEN0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,49,0,0,0,34,86,0,0,60,0,0,0,1,TEN0,TEN0,TEN0,TEN0,0,0,0,0,0,0,0, - 'L','I','S','T',112,0,0,0,'s','d','t','a','s','m','p','l',100,0,0,0,86,0,119,3,31,7,147,10,43,14,169,17,58,21,189,24,73,28,204,31,73,35,249,38,46,42,71,46,250,48,150,53,242,55,126,60,151,63,108,66,126,72,207, - 70,86,83,100,72,74,100,163,39,241,163,59,175,59,179,9,179,134,187,6,186,2,194,5,194,15,200,6,202,96,206,159,209,35,213,213,216,45,220,221,223,76,227,221,230,91,234,242,237,105,241,8,245,118,248,32,252 -}; - RingBuf buffer; void IRAM_ATTR onTimer() { @@ -96,7 +81,7 @@ void setup() { currentLed = 0; playing = false; - g_TinySoundFont = tsf_load_memory(MinimalSoundFont, sizeof(MinimalSoundFont)); + g_TinySoundFont = tsf_load_memory(SoundFont, sizeof(SoundFont)); if (g_TinySoundFont) { // render at 20kHz tsf_set_output(g_TinySoundFont, TSF_MONO, 20000, 0); @@ -171,29 +156,40 @@ void loop() { enableLed(currentLed); delay(1); - if (sense(currentLed)) { - if (!pressed[currentLed]) { + if (sense(currentLed)) { // currently pressed + if (!pressed[currentLed]) { // was not pressed previously? Serial.print(currentLed); Serial.println(" proximity!"); - pressed[currentLed]=1; + pressed[currentLed]=1; // mark it pressed if (g_TinySoundFont) { + // start the note tsf_note_on(g_TinySoundFont, 0, 60 + currentLed, 1.0f); } } } - else if (pressed[currentLed]) { - pressed[currentLed]=0; + // not pressed currently + else if (pressed[currentLed]) { // was pressed previously? + Serial.print(currentLed); + Serial.println(" released!"); + + pressed[currentLed]=0; // mark it no longer pressed if (g_TinySoundFont) { + // stop the note tsf_note_off(g_TinySoundFont, 0, 60 + currentLed); } } currentLed = (currentLed+1)%(row_count*col_count); - while (!buffer.isFull()) { - short sample; - tsf_render_short(g_TinySoundFont, &sample, 1, 0); - buffer.lockedPush(sample); + int toFill = buffer.maxSize() - buffer.size(); + if (toFill) { + noInterrupts(); + for (int i=0;i Date: Fri, 28 Aug 2020 18:45:37 +0100 Subject: sounds not-completely terrible --- esp32/lego-piano.ino | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 88b6520..c6d33b8 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -46,7 +46,7 @@ portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; tsf* g_TinySoundFont = 0; bool playing = false; -RingBuf buffer; +RingBuf buffer; void IRAM_ATTR onTimer() { if (!g_TinySoundFont) return; @@ -83,13 +83,14 @@ void setup() { g_TinySoundFont = tsf_load_memory(SoundFont, sizeof(SoundFont)); if (g_TinySoundFont) { - // render at 20kHz - tsf_set_output(g_TinySoundFont, TSF_MONO, 20000, 0); + // render at 5kHz + tsf_set_output(g_TinySoundFont, TSF_MONO, 5000, 0); - // pre-scaling by 4k, then calling on every 1 tick, should give us 20kHz - timer = timerBegin(0, 1000, true); + // pre-scaling a 40MHz clock by 4k, then calling on every 2 tick, + // should give us 5kHz + timer = timerBegin(0, 4000, true); timerAttachInterrupt(timer, &onTimer, true); - timerAlarmWrite(timer, 4, true); + timerAlarmWrite(timer, 2, true); timerAlarmEnable(timer); } else { @@ -148,6 +149,8 @@ int sense(int led) { return value < 500; } +short intermediateBuffer[5000]; + void loop() { #if DEBUG & 0x01 Serial.print("current led "); @@ -164,7 +167,7 @@ void loop() { pressed[currentLed]=1; // mark it pressed if (g_TinySoundFont) { // start the note - tsf_note_on(g_TinySoundFont, 0, 60 + currentLed, 1.0f); + tsf_note_on(g_TinySoundFont, 0, 36 + currentLed, 1.0f); } } } @@ -176,19 +179,18 @@ void loop() { pressed[currentLed]=0; // mark it no longer pressed if (g_TinySoundFont) { // stop the note - tsf_note_off(g_TinySoundFont, 0, 60 + currentLed); + tsf_note_off(g_TinySoundFont, 0, 36 + currentLed); } } currentLed = (currentLed+1)%(row_count*col_count); int toFill = buffer.maxSize() - buffer.size(); - if (toFill) { + if (toFill > 100) { + tsf_render_short(g_TinySoundFont, intermediateBuffer, toFill, 0); noInterrupts(); for (int i=0;i Date: Fri, 28 Aug 2020 19:03:17 +0100 Subject: move sound rendering inside delay waiting for sensor --- esp32/lego-piano.ino | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index c6d33b8..7901279 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -158,7 +158,22 @@ void loop() { #endif enableLed(currentLed); - delay(1); + unsigned long t = millis(); + + int toFill = buffer.maxSize() - buffer.size(); + if (toFill > 100) { + tsf_render_short(g_TinySoundFont, intermediateBuffer, toFill, 0); + noInterrupts(); + for (int i=0;i 100) { - tsf_render_short(g_TinySoundFont, intermediateBuffer, toFill, 0); - noInterrupts(); - for (int i=0;i Date: Fri, 28 Aug 2020 19:42:12 +0100 Subject: using the i2c-to-internal-dac functions, it sounds a bit better --- esp32/lego-piano.ino | 63 +++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 7901279..8991f07 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -17,6 +17,7 @@ */ #include +#include #define TSF_IMPLEMENTATION #define TSF_NO_STDIO @@ -39,27 +40,12 @@ const int ampEnable = 32; const int octave_shift = 2; +const int sampleRate = 8000; + char pressed[row_count*col_count] = { 0 }; -hw_timer_t * timer = NULL; -portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; tsf* g_TinySoundFont = 0; -bool playing = false; - -RingBuf buffer; - -void IRAM_ATTR onTimer() { - if (!g_TinySoundFont) return; - - portENTER_CRITICAL_ISR(&timerMux); - - short sample; - if (buffer.pop(sample)) { - dac_output_voltage(DAC_CHANNEL_1, sample >> 8); - } - - portEXIT_CRITICAL_ISR(&timerMux); -} +const i2s_port_t i2sPort = I2S_NUM_0; void setup() { Serial.begin(115200); @@ -67,9 +53,24 @@ void setup() { pinMode(ampEnable, OUTPUT); digitalWrite(ampEnable, LOW); - dac_i2s_disable(); + dac_i2s_enable(); dac_output_enable(DAC_CHANNEL_1); + static const i2s_config_t i2s_config = { + .mode = (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN ), + .sample_rate = sampleRate, + .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, + .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, + .intr_alloc_flags = 0, // default interrupt priority + .dma_buf_count = 8, + .dma_buf_len = 64, + .use_apll = false + }; + + i2s_driver_install(i2sPort, &i2s_config, 0, NULL); + i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN); // only dac1 + i2s_set_sample_rates(i2sPort, sampleRate); // ?? + for (int i=0;i buffer; short intermediateBuffer[5000]; void loop() { @@ -159,15 +153,24 @@ void loop() { enableLed(currentLed); unsigned long t = millis(); + size_t written=100; + short value; + // fill the I2S buffer + while (written > 0) { + // we don't want to drop an element from the buffer if we then + // can't write it! so we peek at the buffer, try to write, and pop + // if successful + value=buffer[0]; + i2s_write(i2sPort, &value, sizeof(value), &written, 0); + if (written > 0) buffer.pop(value); + } int toFill = buffer.maxSize() - buffer.size(); if (toFill > 100) { tsf_render_short(g_TinySoundFont, intermediateBuffer, toFill, 0); - noInterrupts(); for (int i=0;i Date: Fri, 28 Aug 2020 19:49:18 +0100 Subject: change sample rate it still sounds pretty bad, though --- esp32/lego-piano.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 8991f07..73dce79 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -40,7 +40,7 @@ const int ampEnable = 32; const int octave_shift = 2; -const int sampleRate = 8000; +const int sampleRate = 11025; char pressed[row_count*col_count] = { 0 }; @@ -69,7 +69,6 @@ void setup() { i2s_driver_install(i2sPort, &i2s_config, 0, NULL); i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN); // only dac1 - i2s_set_sample_rates(i2sPort, sampleRate); // ?? for (int i=0;i Date: Fri, 28 Aug 2020 19:59:13 +0100 Subject: change int priority --- esp32/lego-piano.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 73dce79..c9a766c 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -61,7 +61,7 @@ void setup() { .sample_rate = sampleRate, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, - .intr_alloc_flags = 0, // default interrupt priority + .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority .dma_buf_count = 8, .dma_buf_len = 64, .use_apll = false -- cgit v1.2.3 From 5db683de46685373be71023162d2690ee9b9c96e Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 28 Aug 2020 19:59:21 +0100 Subject: better buffer sizing --- esp32/lego-piano.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index c9a766c..120792e 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -141,8 +141,9 @@ int sense(int led) { return value < 500; } -RingBuf buffer; -short intermediateBuffer[5000]; +const size_t bufferSize = 1000; +RingBuf buffer; +short intermediateBuffer[bufferSize]; void loop() { #if DEBUG & 0x01 -- cgit v1.2.3 From cc3e4431599de81f82d5f5039443825c071bc599 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 28 Aug 2020 19:59:28 +0100 Subject: IT WORKS NOW! it was (almost) all a problem of signed vs unsigned --- esp32/lego-piano.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'esp32/lego-piano.ino') diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 120792e..01b3bb7 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -160,7 +160,7 @@ void loop() { // we don't want to drop an element from the buffer if we then // can't write it! so we peek at the buffer, try to write, and pop // if successful - value=buffer[0]; + value=buffer[0] + 32768; // tsf produces *signed* shorts, i2s wants *unsigned* i2s_write(i2sPort, &value, sizeof(value), &written, 0); if (written > 0) buffer.pop(value); } -- cgit v1.2.3