How long to wait after sampler.stop() to ensure you have all callback data?

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.

That’s a good point; maybe we ought to be calling the callback in some way to indicate “last buffer”… have filed a task to look at that.

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 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
lastBuffer = buffer;
} else {
output.set(buffer); // normal operation
} else {

function stopSampler()
stopping = true;

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.

You can be idle while the sampler is running – it’s just that once you stop it, you next go idle once all the buffers have been processed.


Minor notes for others who use this:

  • Don’t forget to clear lastBuffer each time you start the sampler
  • The code above does not keep track of the final buffer’s length, which you will probably want

Overall, this solution worked well for me.