Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing and encoding remote I/0 output to file - Core Audio

I'm coding a music recording app using Audio Units and I'm having some issues getting my resulting M4A file to play anything other than a not so awesome buzzing noise. I've used these SO sources as references, and I've tried everything to troubleshoot.

I've got an AUGraph with 2 nodes: a multi channel mixer and a remote I/O. I've got two input callbacks on my mixer: one that pulls input from the mic, and one that pulls from an audio file. The mixer output is connected to the input element of the output scope on the I/O unit. This enables simultaneous I/O.

To capture the output I've added a callback and two methods:

The callback

static OSStatus recordAndSaveCallback (void *                inRefCon,
                                       AudioUnitRenderActionFlags * ioActionFlags,
                                       const AudioTimeStamp *       inTimeStamp,
                                       UInt32                       inBusNumber,
                                       UInt32                       inNumberFrames,
                                       AudioBufferList *            ioData) 
{
    Mixer* THIS = (__bridge Mixer*)inRefCon;
    AudioBufferList bufferList;

    OSStatus status;
    status = AudioUnitRender(THIS.ioUnit,    
                             ioActionFlags,
                             inTimeStamp,
                             0,
                             inNumberFrames,
                             &bufferList);

    SInt16 samples[inNumberFrames]; // A large enough size to not have to worry about buffer overrun
    memset (&samples, 0, sizeof (samples));

    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0].mData = samples;
    bufferList.mBuffers[0].mNumberChannels = 1;
    bufferList.mBuffers[0].mDataByteSize = inNumberFrames*sizeof(SInt16);

    OSStatus result;
    if (*ioActionFlags == kAudioUnitRenderAction_PostRender) {
        result =  ExtAudioFileWriteAsync(THIS.extAudioFileRef, inNumberFrames, &bufferList);
        if(result) printf("ExtAudioFileWriteAsync %ld \n", result);}
    return noErr; 
}

Recording Method:

- (void)recordFile
{    
    OSStatus result;

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *recordFile = [documentsDirectory stringByAppendingPathComponent: @"audio.m4a"];

    CFURLRef destinationURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, 
                                                            (__bridge   CFStringRef)recordFile, 
                                                            kCFURLPOSIXPathStyle, 
                                                            false);    

    AudioStreamBasicDescription destinationFormat;
    memset(&destinationFormat, 0, sizeof(destinationFormat));
    destinationFormat.mChannelsPerFrame = 1;
    destinationFormat.mFormatID = kAudioFormatMPEG4AAC;
    UInt32 size = sizeof(destinationFormat);
    result = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &destinationFormat);        
    if(result) printf("AudioFormatGetProperty %ld \n", result);    


    result = ExtAudioFileCreateWithURL(destinationURL, 
                                       kAudioFileM4AType, 
                                       &destinationFormat, 
                                       NULL, 
                                       kAudioFileFlags_EraseFile, 
                                       &extAudioFileRef);
    if(result) printf("ExtAudioFileCreateWithURL %ld \n", result);


    AudioStreamBasicDescription clientFormat;
    memset(&clientFormat, 0, sizeof(clientFormat));


    UInt32 clientsize = sizeof(clientFormat);   
    result = AudioUnitGetProperty(ioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &clientFormat, &clientsize);
    if(result) printf("AudioUnitGetProperty %ld \n", result);

    UInt32 codec = kAppleHardwareAudioCodecManufacturer;

    result = ExtAudioFileSetProperty(extAudioFileRef, 
                                     kExtAudioFileProperty_CodecManufacturer, 
                                     sizeof(codec), 
                                     &codec);

    if(result) printf("ExtAudioFileSetProperty %ld \n", result);


    result = ExtAudioFileSetProperty(extAudioFileRef,kExtAudioFileProperty_ClientDataFormat,sizeof(clientFormat), &clientFormat);
    if(result) printf("ExtAudioFileSetProperty %ld \n", result);


    result =  ExtAudioFileWriteAsync(extAudioFileRef, 0, NULL);
    if (result) {[self printErrorMessage: @"ExtAudioFileWriteAsync error" withStatus: result];}

   result = AudioUnitAddRenderNotify(ioUnit, recordAndSaveCallback, (__bridge void*)self);
    if (result) {[self printErrorMessage: @"AudioUnitAddRenderNotify" withStatus: result];}     
}

Saving Method:

- (void) saveFile {
    OSStatus status = ExtAudioFileDispose(extAudioFileRef);
    NSLog(@"OSStatus(ExtAudioFileDispose): %ld\n", status);

}

This is what I see in my console:

Stopping audio processing graph
OSStatus(ExtAudioFileDispose): 0
ExtAudioFileWriteAsync -50 
ExtAudioFileWriteAsync -50 
ExtAudioFileWriteAsync -50 

It seems to me that my code is very similar to that of people who have gotten this to work, but clearly I've made a crucial error. I'm sure there must be others struggling with this.

Does anyone have any insight?

Thanks.

like image 324
Orpheus Mercury Avatar asked May 07 '12 11:05

Orpheus Mercury


1 Answers

I know the question has been asked a long time ago and you probably found out the error by now, I'm just answering for other that might have the same issue.

I might be wrong but I think the problem is coming from the fact that you are doing a in-scope variable declaration for the buffer.

I would recommend that you change

SInt16 samples[inNumberFrames];

into

SInt16* samples = malloc(inNumberFrames * sizeof(SInt16));

Since the recordAndSaveCallback is meant to fill the buffer list, if you do an in-scope declaration, datas will be destroyed as soon as the scope is ended.

like image 87
TheSquad Avatar answered Nov 15 '22 08:11

TheSquad