AudioConverterFillComplexBuffer

AudioConverterFillComplexBuffer

嗨,我正在尝试使用AudioConverterFillComplexBuffer将AAC缓冲区转换为PCM。这是我的代码

 -(void)initDecoder{
         AudioStreamBasicDescription outAudioStreamBasicDescription;
        outAudioStreamBasicDescription.mSampleRate = 44100.0;
        outAudioStreamBasicDescription.mFormatID = kAudioFormatLinearPCM;
        outAudioStreamBasicDescription.mFormatFlags = 0xc;
        outAudioStreamBasicDescription.mBytesPerPacket = 2;
        outAudioStreamBasicDescription.mFramesPerPacket = 1;
        outAudioStreamBasicDescription.mBytesPerFrame = 2;
        outAudioStreamBasicDescription.mChannelsPerFrame = 1;
        outAudioStreamBasicDescription.mBitsPerChannel = 16;

        AudioStreamBasicDescription inAudioStreamBasicDescription;

        inAudioStreamBasicDescription.mSampleRate = 44100;
        inAudioStreamBasicDescription.mFormatID = kAudioFormatMPEG4AAC;
        inAudioStreamBasicDescription.mFormatFlags = kMPEG4Object_AAC_SSR;
        inAudioStreamBasicDescription.mBytesPerPacket = 0;
        inAudioStreamBasicDescription.mFramesPerPacket = 1024;
        inAudioStreamBasicDescription.mBytesPerFrame = 0;
        inAudioStreamBasicDescription.mChannelsPerFrame = 1;
        inAudioStreamBasicDescription.mBitsPerChannel = 0;
        inAudioStreamBasicDescription.mReserved = 0;


        AudioClassDescription audioClassDescription;
        memset(&audioClassDescription, 0, sizeof(audioClassDescription));


        audioClassDescription.mManufacturer = kAppleSoftwareAudioCodecManufacturer;
        audioClassDescription.mSubType = outAudioStreamBasicDescription.mFormatID;
        audioClassDescription.mType = kAudioFormatLinearPCM;
        NSAssert(audioClassDescription.mSubType == outAudioStreamBasicDescription.mFormatID && audioClassDescription.mManufacturer == kAppleSoftwareAudioCodecManufacturer, nil);


        NSAssert(AudioConverterNewSpecific(&inAudioStreamBasicDescription, &outAudioStreamBasicDescription, 1, &audioClassDescription, &audioConverterDecode) == 0, nil);

     }

    OSStatus inInputDataProc(AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets, AudioBufferList *ioData, AudioStreamPacketDescription **outDataPacketDescription, void *inUserData)
    {
        AudioBufferList audioBufferList = *(AudioBufferList *)inUserData;

        ioData->mBuffers[0].mData = audioBufferList.mBuffers[0].mData;
        ioData->mBuffers[0].mDataByteSize = audioBufferList.mBuffers[0].mDataByteSize;

        return  noErr;
    }

    -(void)decodeSample:(AudioBufferList)inAaudioBufferList{
        //inAaudioBufferList is the AAC buffer

        if (!audioConverterDecode) {

            [self initDecoder];
        }

         NSAssert(inAaudioBufferList.mNumberBuffers == 1, nil);

        uint32_t bufferSize = 1024*2;//inAaudioBufferList.mBuffers[0].mDataByteSize;
        uint8_t *buffer = (uint8_t *)malloc(1024*2);
        memset(buffer, 0, bufferSize);
        AudioBufferList outAudioBufferList;
        outAudioBufferList.mNumberBuffers = 1;
        outAudioBufferList.mBuffers[0].mNumberChannels = 1;
        outAudioBufferList.mBuffers[0].mDataByteSize = bufferSize;
        outAudioBufferList.mBuffers[0].mData = buffer;

        UInt32 ioOutputDataPacketSize = bufferSize;


       OSStatus ret = AudioConverterFillComplexBuffer(audioConverterDecode, inInputDataProc, &inAaudioBufferList, &ioOutputDataPacketSize, &outAudioBufferList, NULL) ;//== 0, nil);

        NSData *data = [NSData dataWithBytes:outAudioBufferList.mBuffers[0].mData length:outAudioBufferList.mBuffers[0].mDataByteSize];
        DLog(@"Rev Size = %d",(unsigned int)outAudioBufferList.mBuffers[0].mDataByteSize);
        free(buffer);

    }

解码后的输出长度为零,AudioConverterFillComplexBuffer的OSStatus代码为561015652

那怎么可能是错误的..?

最佳答案

这是在黑暗中拍摄的照片,您可能已经找到了解决方法,但是我相信您需要更改此方法
UInt32 ioOutputDataPacketSize = bufferSize;对此

UInt32 ioOutputDataPacketSize = bufferSize/2;

就我个人而言,我在inInputDataProc方法中执行此操作并始终发送
UInt32 ioOutputDataPacketSize = 1;

放入AudioConverterFillComplexBuffer方法,然后像这样在inInputDataProc中进行设置。
UInt32 maxPackets = audioBufferList.mBuffers[0].mDataByteSize / 2;
*ioNumberDataPackets = maxPackets;

希望这可以帮助。

10-08 17:49