It looks like you missed out the f.close I'd suggested, so the file will be left open which will use memory?
f.close
Try:
function _playSoundFile(file) { LED3.write(1); var filename = "/audio/" + file; f = E.openFile(filename,"r"); w = undefined; w = new Waveform(2048, {doubleBuffer:true}); w.buffers = [ w.buffer, w.buffer2 ]; w.buffer.set(f.read(w.buffer.length)); w.buffer2.set(f.read(w.buffer.length)); var fileBuf = f.read(w.buffer.length); w.on("buffer", function(buf) { buf.set(fileBuf); fileBuf = f.read(buf.length); if (fileBuf===undefined) { _stopSoundFile(); } }); analogWrite(AUDIOUT, 0.5, {freq:20000}); w.startOutput(AUDIOUT,8000,{repeat:true}); } function _stopSoundFile() { LED3.write(0); if (w && w.running) w.stop(); AUDIOUT.write(0); w = undefined; if (f) f.close(); f = undefined; }
Another option is just to leave the w object defined... Something like below should work:
w
var w = new Waveform(2048, {doubleBuffer:true}); var fileBuf, f; w.buffers = [ w.buffer, w.buffer2 ]; w.on("buffer", function(buf) { buf.set(fileBuf); fileBuf = f.read(buf.length); if (fileBuf===undefined) { _stopSoundFile(); } }); function _playSoundFile(file) { LED3.write(1); var filename = "/audio/" + file; f = E.openFile(filename,"r"); w.buffer.set(f.read(w.buffer.length)); w.buffer2.set(f.read(w.buffer.length)); fileBuf = f.read(w.buffer.length); analogWrite(AUDIOUT, 0.5, {freq:20000}); w.startOutput(AUDIOUT,8000,{repeat:true}); } function _stopSoundFile() { LED3.write(0); if (w.running) w.stop(); AUDIOUT.write(0); if (f) f.close(); f = undefined; }
@Gordon started
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.
It looks like you missed out the
f.close
I'd suggested, so the file will be left open which will use memory?Try:
Another option is just to leave the
w
object defined... Something like below should work: