使用AudioConverterFillComplexBuffer转换AudioBuffer时出错

时间:2018-09-13 13:34:01

标签: macos core-audio audiounit

我正在尝试使用AudioConverterFillComplexBuffer减小记录的音频数据的缓冲区大小(后来我想将其转换回并使用Audiounit播放),我使用的代码如下。

 - (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection
    {
        SInt16 buffer[length];
        CMBlockBufferCopyDataBytes(blockBufferRef, 0, length, buffer);
        NSData * data = [[NSData alloc] initWithBytes:buffer length:length];
        if(!fConverter)
        {
            [self ConverterSetup:asbd1];
        }
        [self Convert:data];
    }

    -(void)ConverterSetup:(AudioStreamBasicDescription)sourceDesc
    {
        AudioStreamBasicDescription fOutputFormat = {0};
        memset(&fOutputFormat, 0, sizeof(fOutputFormat));
        fOutputFormat.mSampleRate = 32000;
        fOutputFormat.mFormatID = kAudioFormatLinearPCM;
        fOutputFormat.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
        fOutputFormat.mBytesPerPacket = fOutputFormat.mBytesPerFrame =4;//4 * sizeof(SInt16);
        fOutputFormat.mFramesPerPacket = 1;
        fOutputFormat.mChannelsPerFrame = 1;
        fOutputFormat.mBitsPerChannel = 32;

        OSStatus err = AudioConverterNew(&sourceDesc, &fOutputFormat,&fConverter);  //converts mFormat -> mFileStreamFormat
        if(err != noErr)
        {
            NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
                    NSLog(@"Error = %@", error);
            return ;
        }

        SInt32 channelMap[] = { 0, 0 };
        err = AudioConverterSetProperty(fConverter, kAudioConverterChannelMap, 2*sizeof(SInt32), channelMap);

        UInt32 quality = kAudioConverterQuality_Medium;
        err = AudioConverterSetProperty(fConverter,
                                        kAudioConverterSampleRateConverterQuality,
                                        sizeof(UInt32),
                                        &quality);
    }



    -(void)Convert:(NSData*)data
    {

        AudioBufferList  inBufferList_new;
        inBufferList_new.mNumberBuffers = 1;
        inBufferList_new.mBuffers[0].mNumberChannels = 1;
        inBufferList_new.mBuffers[0].mData = (void *)data.bytes;
        inBufferList_new.mBuffers[0].mDataByteSize = [data length];

        char szBuf[1024];
        int  nSize = sizeof(szBuf);
        AudioBufferList fAudioOutputBuffer;
        fAudioOutputBuffer.mNumberBuffers              = 1;
        fAudioOutputBuffer.mBuffers[0].mNumberChannels = 1;
        fAudioOutputBuffer.mBuffers[0].mDataByteSize   = nSize;
        fAudioOutputBuffer.mBuffers[0].mData           = szBuf;
        UInt32 outputDataPacketSize               = nSize;

        UInt numPackets = nSize;//inBufferList_new.mBuffers[0].mDataByteSize;

       OSStatus err = AudioConverterFillComplexBuffer(fConverter, ConverterProc, &inBufferList_new, &numPackets, &fAudioOutputBuffer, NULL);
        if(err != noErr)
        {
            NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
            NSLog(@"%@", error);

        }
        else
        {
            NSData *data=[NSData dataWithBytes:(UInt8*)fAudioOutputBuffer.mBuffers[0].mData length:fAudioOutputBuffer.mBuffers[0].mDataByteSize];
            NSLog(@"Converted data length = %d", [data length]);
        }

    }


OSStatus ConverterProc(AudioConverterRef inAudioConverter,
                                        UInt32* ioNumberDataPackets,
                                        AudioBufferList* ioData,
                                        AudioStreamPacketDescription** outDataPacketDescription,
                                        void* inUserData)
{
    OSStatus err = kAudioUnitErr_InvalidPropertyValue;


    AudioBufferList bufferList = *(AudioBufferList*)inUserData;
    ioData->mBuffers[0].mNumberChannels = 1;
    ioData->mBuffers[0].mData           = bufferList.mBuffers[0].mData;
    ioData->mBuffers[0].mDataByteSize   = bufferList.mBuffers[0].mDataByteSize;

    return err;
}

我得到的错误是Error Domain = NSOSStatusErrorDomain Code = -10851“(null)”。 任何人都可以找出此代码的问题。

0 个答案:

没有答案