From d6231ece77e0b4f29037927d30646eeaef33f813 Mon Sep 17 00:00:00 2001 From: dakkar 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(-) 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