I'm trying to do some simple piezo debouncing and the idea was to use timestamp variables to set a minimal delay between triggers.
Whats the best way to get timestamps (millisecond resolution) inside the Bela environment?
As of now I'm using a counter that is increased at every pass of the render function. This might prove very unreliable though, as the function grows in size and such.

Grateful for any suggestion!

The best way to do this is by counting audio samples, which will always give you a precise timestamp locked to when the events actually happen in the signal. There's a member called audioFramesElapsed in BelaContext which gives you an increasing counter of the number of frames as of the beginning of render(). From there you can get the exact count of any frame within the buffer.

To convert to seconds, of course, you would divide it by the audio sample rate.

Perfect! Thanks!

"beginning of render()" - You mean since it was called the first time, right?

audioFramesElapsed gives you the number of audio samples since the very first call to render(). But what I meant was that if you want to know the number of frame n within any call to render(), you can use (context->audioFramesElapsed + n). Or if you want to convert it to time:

(double)(context->audioFramesElapsed + n)/(double)context->audioSampleRate

In most cases I don't recommend using double for computations because it's less efficient than float on the BBB, but here you may need the extra precision when audioFramesElapsed grows very large.

    a year later

    andrew audioFramesElapsed gives you the number of audio samples since the very first call to render()

    Sorry for the possibly dull question, but I could really use some stone-cut clarification here: what is the difference from an audio sample and an audio frame in the Bela environment? I know what a sample is, and I understand samplerate - but the term "frame" is not quite clear to me. @andrew states here that frames and samples are the exact same thing, as far as I can tell. Is that really the case?

    EDIT: reading more up on this, I understand it this way: 'block' and 'frame' denotes the same concept, and they consist of audio samples. You set this in Bela under project settings "Block size (audio frames)", and it's usually 16 - which means one frame/block consists of 16 samples. Am I completely off here?

    EDIT II: Just went back here: https://github.com/BelaPlatform/Bela/wiki/Introduction-to-Bela-Code which explains it pretty dead-on I guess. So a frame is the exact same thing as a sample, except it's looking at ALL channels at once. So the concept of "buffer size" in a DAW is the same as block size here, I assume.

      soeren So a frame is the exact same thing as a sample, except it's looking at ALL channels at once

      We can phrase it in many different ways, all confusing in a different way. So let me try with a new one:
      a frame contains all the samples, one per each channel, corresponding at a given instant in time.