Play and Record audio Iphone, AudioUnit - ios

I want to add to this http://www.cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html
example the recordCallBack:
i add:
recordingCallback:
static OSStatus recordingCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
NSlog(#"recordingCallback");
...
return noErr;
}
and add to the output audio unit (toneUnit) config.
// Set input callback
input.inputProc = recordingCallback;
input.inputProcRefCon = self;
err = AudioUnitSetProperty(toneUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global,
1,
&input,
sizeof(input));
...
err = AudioUnitSetProperty (toneUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
1,
&streamFormat,
sizeof(AudioStreamBasicDescription));
iphone asked for permission to use microphone, but my recording callback is never called
update SOLUTION
AudioUnitAddRenderNotify(
iOUnit,
&recordingCallback,
self
);

Related

How to merge two Audio Units into AudioBufferList for AURenderCallback

Please see code below. All I want to, is to merge bufflist1 and bufflist2, then inset to ioData. But I don't know how.
OSStatus PlayCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
ALNPlayer *player = (__bridge ALNPlayer *)inRefCon;
OSStatus status;
player->buffList->mBuffers[0].mDataByteSize = CONST_BUFFER_SIZEV3;
//read audio1 data to bufflist1
status = AudioConverterFillComplexBuffer(player->audioConverter1, lyInInputDataProcV1, inRefCon, &inNumberFrames, player->buffList1, NULL);
//read audio2 data to bufflist2
status = AudioConverterFillComplexBuffer(player->audioConverter2, lyInInputDataProcV2, inRefCon, &inNumberFrames, player->buffList2, NULL);
//below is copy bufferlist1 to ioData
//and now i want to merge bufflist1 and buflist2,then inset to iodata. But I don't know how.
memcpy(ioData->mBuffers[0].mData, player->buffList->mBuffers[0].mData, player->buffList->mBuffers[0].mDataByteSize);
}

AURemoteIO::IOThread(18):EXE_BAD_ACCESS (Code1, address 0X20)

I am using following function to render the incoming audio.
static OSStatus audioUnitRenderCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberOfFrames,
AudioBufferList *ioData{
// App is crashing at this :
AudioUnitIO *SELF = (__bridge AudioUnitIO *)inRefCon;
// MICROPHONE: take the sound received, render it into bytes and hand them to the delegate
AudioUnitRender(SELF->g_audioUnit,
ioActionFlags,
inTimeStamp,
g_inputBus,
inNumberOfFrames,
ioData);
if (!SELF->recordingAudio) {
memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize);
// Some other code goes here
}
I am getting the crash as mentioned in question. Am I missing something?

How to get the volume of an AudioUnit

I am using AudioUnit to play input from the microphone to the earphones.
It's working great. Now I need to increase the volume of weak sounds and decrease strong ones.
I found a way to increase the sound:
static OSStatus performRender (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
OSStatus err = noErr;
if (*cd.audioChainIsBeingReconstructed == NO)
{
// we are calling AudioUnitRender on the input bus of AURemoteIO
// this will store the audio data captured by the microphone in ioData
err = AudioUnitRender(cd.rioUnit, ioActionFlags, inTimeStamp, 1, inNumberFrames, ioData);
// filter out the DC component of the signal
cd.dcRejectionFilter->ProcessInplace((Float32*) ioData->mBuffers[0].mData, inNumberFrames);
//Add Volume
float desiredGain = 2.0f;
for(UInt32 bufferIndex = 0; bufferIndex < ioData->mNumberBuffers; ++bufferIndex) {
float *rawBuffer = (float *)ioData->mBuffers[bufferIndex].mData;
vDSP_vsmul(rawBuffer, 1, &desiredGain, rawBuffer, 1, inNumberFrames);
}
// mute audio if needed
if (*cd.muteAudio)
{
for (UInt32 i=0; i<ioData->mNumberBuffers; ++i)
memset(ioData->mBuffers[i].mData, 0, ioData->mBuffers[i].mDataByteSize);
}
}
return err;
}
My question is how to I get what is the current volume so I would know how much to gain it and vice versa
Thanks!
Getting the "volume" depends on the type of AudioUnit. Some audio units have input levels, output levels, and "global" volume levels.
// MatrixMixer
Float32 volume = 0;
OSStatus result = AudioUnitGetParameter(mxmx_unit, kMatrixMixerParam_Volume, kAudioUnitScope_Global, 0, &volume);
// MultiChannelMixer
Float32 volume = 0;
OSStatus result = AudioUnitGetParameter(mcmx_unit, kMultiChannelMixerParam_Volume, kAudioUnitScope_Global, 0, &volume);

Using multiple AudioUnits with MTAudioProcessingTap on iOS

I'm attempting to play audio files from the users' iPod library on an iOS device, while using AudioUnit to apply a parametric EQ effect. I have been using this sample as a guide: https://developer.apple.com/library/ios/samplecode/AudioTapProcessor/Introduction/Intro.html
I have the EQ effect working, but I need to add multiple EQ effects.
In my 'process' callback, I tried running AudioUnitRender multiple times, on multiple AudioUnit effects (all of type Parametric EQ).
status = AudioUnitRender(audioUnit, 0, &audioTimeStamp, 0, (UInt32)numberFrames, bufferListInOut);
With any more than 1 AudioUnitRender call, the audio skips and cuts out.
How can I use multiple Parametric EQ effects at once?
Thanks
I'm still not sure what the reason of the glitch is, but I've found the solution and it's probably what AUGraph does internally.
The trick is to call AudioUnitRender() of the next AU from within the render callback function of the previous AU. The last render callback calls MTAudioProcessingTapGetSourceAudio(). Assuming you have a single render callback for all your AU's and assuming you have an array with all the AU's you created:
UInt64 processedFrames;
UInt32 curAudioUnit;
UInt32 audioUnitCount;
AudioUnit audioUnits[MAX_AUDIO_UNITS];
OSStatus AU_RenderCallback(void *tap, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
curAudioUnit++;
if (curAudioUnit == audioUnitCount)
return MTAudioProcessingTapGetSourceAudio(tap, inNumberFrames, ioData, NULL, NULL, NULL);
AudioTimeStamp audioTimeStamp;
audioTimeStamp.mSampleTime = processedFrames;
audioTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
return AudioUnitRender(audioUnits[curAudioUnit], 0, &audioTimeStamp, 0, inNumberFrames, ioData);
}
void tap_ProcessCallback(MTAudioProcessingTapRef tap, CMItemCount inNumberFrames, MTAudioProcessingTapFlags flags, AudioBufferList *bufferListInOut, CMItemCount *numberFramesOut, MTAudioProcessingTapFlags *flagsOut)
{
if (audioUnitCount)
{
curAudioUnit = 0;
AudioTimeStamp audioTimeStamp;
audioTimeStamp.mSampleTime = processedFrames;
audioTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
AudioUnitRender(audioUnits[curAudioUnit], 0, &audioTimeStamp, 0, inNumberFrames, bufferListInOut);
}
else
MTAudioProcessingTapGetSourceAudio(tap, inNumberFrames, bufferListInOut, flagsOut, NULL, numberFramesOut);
processedFrames += inNumberFrames;
}

How to write output of AUGraph to a file?

I am trying to write (what should be) a simple app that has a bunch of audio units in sequence in an AUGraph and then writes the output to a file. I added a callback using AUGraphAddRenderNotify. Here is my callback function:
OSStatus MyAURenderCallback(void *inRefCon,
AudioUnitRenderActionFlags *actionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
if (*actionFlags & kAudioUnitRenderAction_PostRender) {
ExtAudioFileRef outputFile = (ExtAudioFileRef)inRefCon;
ExtAudioFileWriteAsync(outputFile, inNumberFrames, ioData);
}
}
This sort of works. The file is playable and I can hear what I recorded but there is horrible amounts of static that makes it barely audible.
Does anybody know what is wrong with this? Or does anyone know of a better way to record the AUGraph output to a file?
Thanks for the help.
I had a epiphany with regards to Audio Units just now which helped me solve my own problem. I had a misconception about how audio unit connections and render callbacks work. I thought they were completely separate things but it turns out that a connection is just short hand for a render callback.
Doing an kAudioUnitProperty_MakeConnection from the output of audio unit A to the input of audio unit B is the same as doing kAudioUnitProperty_SetRenderCallback on the input of unit B and having the callback function call AudioUnitRender on the output of audio unit A.
I tested this by doing a make connection after setting my render callback and the render callback was no longer invoked.
Therefore, I was able to solve my problem by doing the following:
AURenderCallbackStruct callbackStruct = {0};
callbackStruct.inputProc = MyAURenderCallback;
callbackStruct.inputProcRefCon = mixerUnit;
AudioUnitSetProperty(ioUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&callbackStruct,
sizeof(callbackStruct));
And them my callback function did something like this:
OSStatus MyAURenderCallback(void *inRefCon,
AudioUnitRenderActionFlags *actionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
AudioUnit mixerUnit = (AudioUnit)inRefCon;
AudioUnitRender(mixerUnit,
actionFlags,
inTimeStamp,
0,
inNumberFrames,
ioData);
ExtAudioFileWriteAsync(outputFile,
inNumberFrames,
ioData);
return noErr;
}
This probably should have been obvious to me but since it wasn't I'll bet there are others that were confused in the same way so hopefully this is helpful to them too.
I'm still not sure why I had trouble with the AUGraphAddRenderNotify callback. I will dig deeper into this later but for now I found a solution that seems to work.
Here is some sample code from Apple (the project is PlaySequence, but it isn't MIDI specific) that might help:
{
CAStreamBasicDescription clientFormat = CAStreamBasicDescription();
ca_require_noerr (result = AudioUnitGetProperty(outputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, 0,
&clientFormat, &size), fail);
size = sizeof(clientFormat);
ca_require_noerr (result = ExtAudioFileSetProperty(outfile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat), fail);
{
MusicTimeStamp currentTime;
AUOutputBL outputBuffer (clientFormat, numFrames);
AudioTimeStamp tStamp;
memset (&tStamp, 0, sizeof(AudioTimeStamp));
tStamp.mFlags = kAudioTimeStampSampleTimeValid;
int i = 0;
int numTimesFor10Secs = (int)(10. / (numFrames / srate));
do {
outputBuffer.Prepare();
AudioUnitRenderActionFlags actionFlags = 0;
ca_require_noerr (result = AudioUnitRender (outputUnit, &actionFlags, &tStamp, 0, numFrames, outputBuffer.ABL()), fail);
tStamp.mSampleTime += numFrames;
ca_require_noerr (result = ExtAudioFileWrite(outfile, numFrames, outputBuffer.ABL()), fail);
ca_require_noerr (result = MusicPlayerGetTime (player, &currentTime), fail);
if (shouldPrint && (++i % numTimesFor10Secs == 0))
printf ("current time: %6.2f beats\n", currentTime);
} while (currentTime < sequenceLength);
}
}
Maybe try this. Copy the data from the audio unit callback to a long buffer. Play the buffer to test it, then write the entire buffer to a file after you have verified that the whole buffer is OK.

Resources