开发者

record sounds played by my iPhone app with audio units

I have red a lot of interesting stuff today about iOS & Audio Units and have found a lot of usefull resources (SO included).

First of all, i am confused with something : Is it really necessary to create an audio graph with mixer unit to record sounds played by an app ?

Or is it sufficient to play sounds with ObjectAL (or more simply AVAudioPlayer calls) and create a single remote io unit adressed on the correct bus with a recording callback ?

Second, a more programmatically issue ! As i'm not already comfortable with Audio Units concepts, i try to adapt the apple Mixer Host project with the ability to record the resulting mix. Obviously, i try to do this with the Michael Tyson RemoteIO post.

And I get a EXC_BAD_ACCESS on my callback function :

static OSStatus recordingCallback       (void *inRefCon,
                                  AudioUnitRenderActionFlags *ioActionFlags,
                                  const AudioTimeStamp *inTimeStamp,
                                  UInt32        inBusNumber,
                                  UInt32        inNumberFrames,
                                  AudioBufferList *ioData) {

    AudioBufferList *bufferList; // <- Fill this up with buffers (you will want to malloc it, as it's a dynamic-length list)

    EffectState *effectState = (EffectState *)inRefCon;
    AudioUnit rioUnit = effectState->rioUnit;

    OSStatus status;

    // BELOW I GET THE ERROR
    status = AudioUnitRender(rioUnit,      
                             ioActionFlags, 
                             inTimeStamp, 
                             inBusNumber, 
                             inNumberFrames, 
                             bufferList);

    if (noErr != status) { NSLog(@"AudioUnitRender error"); return noErr;}

    // Now, we have the samples we just read sitting in buffers in bufferList
    //ExtAudioFileWriteAsync(effectState->audioFileRef, inNumberFrames, bufferList);

    return noErr;
}

Before using the callback function i did in MixerHostAudio.h

typedef struct {
    AudioUnit rioUnit;
    ExtAudioFileRef audioFileRef;
} EffectState;

And create in the interface :

AudioUnit                       iOUnit;
EffectState effectState;
AudioStreamBasicDescription     iOStreamFormat;
...
@property                       AudioUnit           iOUnit;
@property (readwrite)           AudioStreamBasicDescription iOStreamFormat;

Then in the implementation file MixerHostAudio.h :

#define kOutputBus 0
#define kInputBus 1
...
@synthesize iOUnit;                    // the Remote IO unit
...
result =    AUGraphNodeInfo (
                             processingGraph,
                             iONode,
                             NULL,
                             &iOUnit
                             );

if (noErr != result) {[self printErrorMessage: @"AUGraphNodeInfo" withStatus: result]; return;}

// Enable IO for recording
UInt32 flag = 1;
result = AudioUnitSetProperty(iOUnit, 
                              kAudioOutputUnitProperty_EnableIO, 
                              kAudioUnitScope_Input, 
                              kInputBus,
                              &flag, 
                              sizeof(flag));

if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}
// Describe format
iOStreamFormat.mSampleRate          = 44100.00;
iOStreamFormat.mFormatID            = kAudioFormatLinearPCM;
iOStreamFormat.mFormatFlags     = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
iOStreamFormat.mFramesPerPacket = 1;
iOStreamFormat.mChannelsPerFrame    = 1;
iOStreamFormat.mBitsPerChannel      = 16;
iOStreamFormat.mBytesPerPacket      = 2;
iOStreamFormat.mB开发者_运维技巧ytesPerFrame       = 2;

// Apply format
result = AudioUnitSetProperty(iOUnit, 
                              kAudioUnitProperty_StreamFormat, 
                              kAudioUnitScope_Output, 
                              kInputBus, 
                              &iOStreamFormat, 
                              sizeof(iOStreamFormat));

if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}

result = AudioUnitSetProperty(iOUnit, 
                              kAudioUnitProperty_StreamFormat, 
                              kAudioUnitScope_Input, 
                              kOutputBus, 
                              &iOStreamFormat, 
                              sizeof(iOStreamFormat));

if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}

effectState.rioUnit = iOUnit;

// Set input callback ----> RECORDING
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = recordingCallback;
callbackStruct.inputProcRefCon = self;
result = AudioUnitSetProperty(iOUnit, 
                              kAudioOutputUnitProperty_SetInputCallback, 
                              kAudioUnitScope_Global, 
                              kInputBus, 
                              &callbackStruct, 
                              sizeof(callbackStruct));

if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}

But I don't know what's wrong and don't know how to digg. Note : The EffectState struct is present because I also try to integrate the BioAudio project ability to write file from buffers.

And Third, I wonder if there something easier to do to record sounds played by my iPhone app (ie microphone excluded) ?


Found by myself. I forgot to chain like this :

callbackStruct.inputProcRefCon = &effectState;

This is the code part. Now I have again conceptual issues ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜