mpc but also thinking there might need to be something in the writeBuffer within the while loop
yes correct.
Something like this should do:
void writeBuffer(void*) {
unsigned int numItems = gAudioFrames * gAudioInChannels;
float buf[numItems];
int ret;
while((ret = gPipe.readNonRt(buf, numItems) ) > 0)
{
sf_write_float(outfile2, &buf[0], ret);
audio_client->send((void*)buf, sizeof(buf));
}
}
However, just in case you are going to drop packets or receive them out-of-order (any of these could happen with UDP), it'd be good to prepend a timestamp or counter of some sort as part of the packet. To avoid further memory copies, this should do the trick:
void writeBuffer(void*) {
static float count = 0;
unsigned int numItems = gAudioFrames * gAudioInChannels;
float buf[numItems + 1];
int ret;
while((ret = gPipe.readNonRt(buf + 1, numItems) ) > 0) // start writing audio from buf[1]
{
sf_write_float(outfile2, buf + 1, ret); // again, audio starts from buf[1]
buf[0] = count; // buf[0] for the counter;
audio_client->send((void*)buf, sizeof(buf));
count++;
}
}
obviously, as that's a float
counter, you'll end up losing some precision, namely as you go beyond 16777216
, count++
will become ineffective. At 16 samples per block that's about 1.7 hours, so probably fine for most applications? You can always wrap back to 0 when you reach that if you need longer times, or a number of other tricks 🙂.
mpc void cleanup(BelaContext context, void userData)
remember to delete audio_client
here.