Record audio input


#1

Hi everyone,

I want to record the audio input. I think I have to use the AudioIODevice but I’m not sure how to use it… I get the audio input with the getDeviceNames() method, then I create one audioIODevice object, using the device name, and after that I don’t know how to use the “open” method" and the AudioIODeviceCallback class… Can somebody help me?
Thanx

Leskimo


#2

I wrote quite a lot of comments for those classes - which bits are you finding confusing?


#3

In fact, I don’t know how to find the two first parameters:

If I can make the “open” method work, do these next steps are good to record input?

  • Call the “start” method of AudioIODevice, with a subclass of AudioIODeviceCallback for parameter.

  • then I will be able to get the input in the "audioDeviceIOCallBack method of my AudioIODeviceCallBack class.

Is that Right?

Thanx for your help

Leskimo


#4

Yes, that’s it.


#5

great, so how do I set the first and second parameters of the “open” method?
My input has two channels (L and R), and I want to enable both channels, so how does the BitArray must be to do it? Sorry if I’m a bit slow to understand…

Leskimo


#6

ok, that was a dumb question… I suppose If I want to enable the channel 0 and the channel 1, I have to set my BitArray like this: 0000 0011

Leskimo


#7

Hi again,

seems like the opening is working, but the audioDeviceIOCallback is not called… Here’s my code:

[code] StringArray tab = AudioIODevice::getDeviceNames();

AudioIODevice device(tab[0]);  // 0 is for the Mic input

StringArray chanTab = device.getInputChannelNames();
double SampleRate = device.getSampleRate(0);   // 44100
int bufferSize = device.getDefaultBufferSize();

String error = device.open(BitArray(3),BitArray(3),device.getSampleRate(0),device.getDefaultBufferSize());  // BitArray(3), 3 because I want the first and the second channels (0000 0011)

if (error.isEmpty())
	printf("device opening was succesfull\n\n");
else
	printf("device opening failed. Return value: %s\n\n", toCString(error));

device.start(mpCallBack);  // mpCallBack is created in the constructor of this class[/code]

The audioDeviceAboutToStart method is called nut not he callback method… Does anyone see what I did wrong? Thanx in advance.

Leskimo


#8

Looks ok, but what does your app do after this code? What’s the object that’s getting called back?


#9

Oh - I just noticed that your AudioDevice object is on the stack… doh!

I’d also recommend the AudioDeviceManager class as a better option than using a device directly. And always put a spaces after your commas.


#10

[code]AudioStuff::AudioStuff()
{
mpData = new tThreadData();
mpData->mGoOn = true;
mpCallBack = new AudioCallBack();
}

AudioStuff::~AudioStuff()
{
}

char* toCString(String str)
{
int length = str.length();
char* res = new char[length+1];

int i;
for (i = 0; i < length; i++)
	res[i] = str[i];

res[i] = '\0';
return res;

}

void AudioStuff::LaunchProcess()
{
StringArray tab = AudioIODevice::getDeviceNames();

AudioIODevice device(tab[0]);  // 0 is for the Mic input

StringArray chanTab = device.getInputChannelNames();
double SampleRate = device.getSampleRate(0);   // 44100
int bufferSize = device.getDefaultBufferSize();

String error = device.open(BitArray(3),BitArray(3),device.getSampleRate(0),device.getDefaultBufferSize());  // BitArray(3), 3 because I want the first and the second channels (0000 0011)

if (error.isEmpty())
	printf("device opening was succesfull\n\n");
else
	printf("device opening failed. Return value: %s\n\n", toCString(error));

device.start(mpCallBack);  // mpCallBack is created in the constructor of this class

}[/code]

here is the code of the class which create and open a device.
My callback class is like this:

[code]void AudioCallBack::audioDeviceIOCallback(const float **inputChannelData, int totalNumInputChannels, float **outputChannelData, int totalNumOutputChannels, int numSamples)
{
printf(“callback\n”);
}

void AudioCallBack::audioDeviceAboutToStart(double sampleRate, int numSamplesPerBlock)
{
printf(“about to start, sampleRate:%f, nbSamplePerBlock:%d\n”, sampleRate, numSamplesPerBlock);
}

void AudioCallBack::audioDeviceStopped()
{
}[/code]

I thought that when I called the “start” method, it called automatically my callback function, but It seems that it’s not the case, or maybe I’m missing something…

Leskimo


#11

start() doesn’t block - callbacks are asynchronous. As soon as your start() call returns, your audio object goes out of scope, gets deleted and hence no callbacks…


#12

thank you so much for your help. It works perfectly.

Leskimo


#13