- Edited
Hi Guilio,
Is it possible to utilise more than one pipe so that I can record both the audio and analogue inputs. So I adapted the code (which was originally for recording the audio input) to work with the analogue inputs to use the 8 channels (audio expander capelet) but would like to also be able to record the audio input. As using the same pipe causes issues but attempting to use two pipes (one for audio, one for analogue) causes errors when the code is running.
I assume it is probably best to record to two separate files (one for recording analogue, one for recording audio and fairly straightforward to implement the code) because of the different sampling rates, due to the analogue channels being sampled half as often as the audio channels etc.
The current setup is that I’m recording four lapel microphones plugged into the audio expander capelet but would be great to record a fifth lapel that is plugged into the audio input.
I’ve pasted the current code for reference, most of the audio input related code is commented out. So this is working for analogue at the moment. The code also works for audio input when the analogue related code is commented out.
#include <Bela.h>
#include <libraries/Pipe/Pipe.h>
#include <libraries/sndfile/sndfile.h>
const char* path = "./recording.wav";
AuxiliaryTask gFillBufferTask;
SNDFILE * outfile;
unsigned int gAudioFrames;
unsigned int gAudioInChannels;
float gAudioSampleRate;
Pipe gPipe;
//analog
unsigned int numAnalogFrames;
unsigned int numAnalogChannels;
float numAnalogSampleRate;
void openFile() {
SF_INFO sfinfo;
//sfinfo.channels = gAudioInChannels;
//sfinfo.samplerate = gAudioSampleRate;
sfinfo.channels = numAnalogChannels;
sfinfo.samplerate = numAnalogSampleRate;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
outfile = sf_open(path, SFM_WRITE, &sfinfo);
}
/*void openFileAnalog() {
SF_INFO sfinfo;
sfinfo.channels = numAnalogChannels;
sfinfo.samplerate = numAnalogSampleRate;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
outfile = sf_open(path, SFM_WRITE, &sfinfo);
}*/
void closeFile() {
sf_write_sync(outfile);
sf_close(outfile);
printf(".wav file written and closed\n");
}
void writeBuffer(void*) {
/*
unsigned int numItems = gAudioFrames * gAudioInChannels;
float buf[numItems];
int ret;
while((ret = gPipe.readNonRt(buf, numItems) ) > 0)
{
sf_write_float(outfile, &buf[0], ret);
}*/
unsigned int numItemsAn = numAnalogFrames * numAnalogChannels;
float bufAnalog[numItemsAn];
int retAn;
while ((retAn = gPipe.readNonRt(bufAnalog, numItemsAn)) > 0)
{
sf_write_float(outfile, &bufAnalog[0], retAn);
}
}
bool setup(BelaContext* context, void* arg)
{
//setup audio frames and channels
gAudioSampleRate = context->audioSampleRate;
gAudioFrames = context->audioFrames;
gAudioInChannels = context->audioInChannels;
//setup analog frames and channels
numAnalogSampleRate = context->analogSampleRate;
numAnalogFrames = context->analogFrames;
numAnalogChannels = context->analogInChannels;
gPipe.setup("sndfile-write", 65536, false, false);
openFile();
if((gFillBufferTask = Bela_createAuxiliaryTask(&writeBuffer, 90, "writeBuffer")) == 0) {
return false;
}
return true;
}
void render(BelaContext* context, void* arg)
{
// context->audioIn is the float* that points to all the input samples, stored as interleaved channels)
// audioIn is an array of 4 frames * 2 channels = 8 audio input samples.
//gPipe.writeRt(context->audioIn, context->audioFrames * context->audioInChannels);
// analogIn is an array of 2 frames * 8 channels = 16 analog input samples.
// (2 frames because analog I/O runs at half the sample rate of audio I/O.)
gPipe.writeRt(context->analogIn, context->analogFrames * context->analogInChannels);
Bela_scheduleAuxiliaryTask(gFillBufferTask);
}
void cleanup(BelaContext* context, void* arg)
{
closeFile();
}