512 isn't the sample rate, it's the size of the buffer. Using 1024 could be a good idea though.
The sample rate is in w.startInput - you're using 2000 currently (and the code you posted up is playing back at 8000). It'd be worth trying to use 4000 for both as a starting point (8000 might be possible, but for now let's not push it)
You don't need analogWrite - you're trying to record so you want to input, not output.
You definitely don't need E.FFT or any of that code with the FOR loops. FFT is something totally different to recording sound - and on Espruino at the moment you're going to struggle to record proper audio and do an FFT to pick out frequencies.
Also appendFileSync has to find the file, open it, append, and close it - which can be quite slow. E.openFile is probably better.
Something like this should work:
var spi = new SPI();
spi.setup({mosi:B15, miso:B14, sck:B10});
E.connectSDCard(spi, B1);
console.log(require("fs").readdirSync());
var f = E.openFile("X.raw","w");
var w = new Waveform(1024,{doubleBuffer:true,bits:16});
w.on("buffer", function(buf) {
f.write(buf);
});
w.startInput(B0,4000,{repeat:true});
setTimeout(function() {
w.stop();
f.close();
},10000);
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.
Ok, I think there are a few things here...
w.startInput
- you're using 2000 currently (and the code you posted up is playing back at 8000). It'd be worth trying to use 4000 for both as a starting point (8000 might be possible, but for now let's not push it)analogWrite
- you're trying to record so you want to input, not output.E.FFT
or any of that code with the FOR loops. FFT is something totally different to recording sound - and on Espruino at the moment you're going to struggle to record proper audio and do an FFT to pick out frequencies.appendFileSync
has to find the file, open it, append, and close it - which can be quite slow.E.openFile
is probably better.Something like this should work: