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 --- .gitmodules | 6 ++++++ RingBuffer | 1 + esp32/lego-piano.ino | 61 +++++++++++++++------------------------------------- tsf | 1 + 4 files changed, 25 insertions(+), 44 deletions(-) create mode 160000 RingBuffer create mode 160000 tsf diff --git a/.gitmodules b/.gitmodules index a863510..129140f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,9 @@ [submodule "ESP8266Audio"] path = ESP8266Audio url = https://github.com/earlephilhower/ESP8266Audio +[submodule "tsf"] + path = tsf + url = https://github.com/schellingb/TinySoundFont.git +[submodule "RingBuffer"] + path = RingBuffer + url = https://github.com/Locoduino/RingBuffer.git diff --git a/RingBuffer b/RingBuffer new file mode 160000 index 0000000..06e8bf1 --- /dev/null +++ b/RingBuffer @@ -0,0 +1 @@ +Subproject commit 06e8bf18ba98ad421ea1dd12e06b90f7071237ac 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 diff --git a/tsf b/tsf new file mode 160000 index 0000000..bf57451 --- /dev/null +++ b/tsf @@ -0,0 +1 @@ +Subproject commit bf574519e601202c3a9d27a74f345921277eed39 -- cgit v1.2.3