I am sampling from a microphone using the sampler class for as long as a pushbutton is held down. When I release the pushbutton, I call sampler.stop(). After that, at some time in the future, I stop getting callbacks with sampler data. I send the data to the server as I get it, but I need to know when I have the last buffer. How long after sampler.stop should I wait for that last buffer?
I’m using a-law compression, so my previous sampler question just posted is relevant.
If you set an imp.onidle() handler when you call sampler.stop(), it will be called immediately after the final buffer is delivered. You can also use this mechanism to do something special with, as opposed to after, the final buffer if that’s what’s needed. I haven’t tested this, but these modifications to the example on the devwiki http://devwiki.electricimp.com/doku.php?id=electricimpapi:sampler should get you on your way:
`stopping <- false;
lastBuffer <- null;
function samplesReady(buffer, length)
{
if (length > 0) {
if (stopping) {
if (lastBuffer) { // oops it wasn’t quite the last one, send normally
output.set(lastBuffer);
}
lastBuffer = buffer;
} else {
output.set(buffer); // normal operation
server.log(length);
}
} else {
server.log(“Overrun”);
}
}
function stopSampler()
{
server.log(“stop”);
stopping = true;
imp.onidle(sendLastBuffer);
hardware.sampler.stop();
}
function sendLastBuffer()
{
output.set(lastBuffer); // do the special stuff for last buffer here
stopping = false;
}`
Peter, thank you, I never thought of using onidle() this way. I’ll try it out. This implies that as long as the sampler is running, we are never idle, which makes sense. I tend to forget the things that I set up for the system to run, as I don’t have full visibility into how the are executed.