aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2020-08-28 18:45:37 +0100
committerdakkar <dakkar@thenautilus.net>2020-08-28 18:45:37 +0100
commitd6231ece77e0b4f29037927d30646eeaef33f813 (patch)
tree58953aa91d50d387def15bcca7fc5fb86e568be7
parentvaguely working (diff)
downloadlego-piano-d6231ece77e0b4f29037927d30646eeaef33f813.tar.gz
lego-piano-d6231ece77e0b4f29037927d30646eeaef33f813.tar.bz2
lego-piano-d6231ece77e0b4f29037927d30646eeaef33f813.zip
sounds not-completely terrible
-rw-r--r--esp32/lego-piano.ino26
1 files 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<short, 1000> buffer;
+RingBuf<short, 500> 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<toFill;++i) {
- short sample;
- tsf_render_short(g_TinySoundFont, &sample, 1, 0);
- buffer.push(sample);
+ buffer.push(intermediateBuffer[i]);
}
interrupts();
}