For convenience I added a watch to Button 1. When the button is pressed, the play function is run, which illuminates LED1 and streams the raw file from the SD card to analog pin A4 as a waveform. When playing is done, it turns off the LED.
function play() {
var f = E.openFile("test16ku.raw","r");
var w = new Waveform(2048, {doubleBuffer:true});
// load first bits of sound file
w.buffer.set(f.read(w.buffer.length));
w.buffer2.set(f.read(w.buffer.length));
var fileBuf = f.read(w.buffer.length);
// when one buffer finishes playing, load the next one
w.on("buffer", function(buf) {
buf.set(fileBuf);
fileBuf = f.read(buf.length);
if (fileBuf===undefined) {
w.stop(); // end of file
LED1.write(0);
}
});
LED1.write(1); //turn on led1
analogWrite(A4, 0.5);
w.startOutput(A4,16000,{repeat:true});
}
function btnEvent() {
if (digitalRead(BTN1) == 1)
play();
}
setWatch(btnEvent, BTN1, true);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
For convenience I added a watch to Button 1. When the button is pressed, the play function is run, which illuminates LED1 and streams the raw file from the SD card to analog pin A4 as a waveform. When playing is done, it turns off the LED.