aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2020-08-28 18:01:12 +0100
committerdakkar <dakkar@thenautilus.net>2020-08-28 18:01:12 +0100
commitc0a3728db5f028f764b4887fb1ea8170de2bf1c3 (patch)
tree371f7762b2335d89dd20963e877780d92acb3e59
parentdump a soundfound as a C header (diff)
downloadlego-piano-c0a3728db5f028f764b4887fb1ea8170de2bf1c3.tar.gz
lego-piano-c0a3728db5f028f764b4887fb1ea8170de2bf1c3.tar.bz2
lego-piano-c0a3728db5f028f764b4887fb1ea8170de2bf1c3.zip
vaguely working
it takes too long to render the font, though
m---------ESP8266Audio0
-rw-r--r--esp32/lego-piano.ino50
2 files changed, 23 insertions, 27 deletions
diff --git a/ESP8266Audio b/ESP8266Audio
-Subproject cd42e28ca5ac09f5b6267ea2eb2b665c90cdb6b
+Subproject 9df0586bdf7252bf635ee41fe3778105c6fefa1
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<short, 1000> 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<toFill;++i) {
+ short sample;
+ tsf_render_short(g_TinySoundFont, &sample, 1, 0);
+ buffer.push(sample);
+ }
+ interrupts();
}
} \ No newline at end of file