iOS AudioConverter将AAC转换为PCM失败并返回-50

时间:2018-07-31 12:17:46

标签: ios iphone core-audio audiotoolbox audio-converter

我正在尝试通过音频转换器服务将AAC转换为PCM。但是AudioConverterFillComplexBuffer()函数始终返回 -50

我正在查看 -50 的意思是 kAudio_ParamError ,这是该函数的参数列表中的错误。

我一次又一次地检查了每个参数,但仍然感到困惑。这是我的代码:

在此处设置转换器

AudioStreamBasicDescription inFormat;

    memset(&inFormat, 0, sizeof(inFormat));
    inFormat.mSampleRate        = 44100;
    inFormat.mFormatID          = kAudioFormatMPEG4AAC;
    inFormat.mFormatFlags       = kMPEG4Object_AAC_LC;
    inFormat.mBytesPerPacket    = 0;
    inFormat.mFramesPerPacket   = 1024;
    inFormat.mBytesPerFrame     = 0;
    inFormat.mChannelsPerFrame  = 2;
    inFormat.mBitsPerChannel    = 0;
    inFormat.mReserved          = 0;

    AudioStreamBasicDescription outFormat;
    memset(&outFormat, 0, sizeof(outFormat));
    outFormat.mSampleRate       = 44100;
    outFormat.mFormatID         = kAudioFormatLinearPCM;
    outFormat.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    outFormat.mBytesPerPacket   = 4;
    outFormat.mFramesPerPacket  = 1;
    outFormat.mBytesPerFrame    = 4;
    outFormat.mChannelsPerFrame = 2;
    outFormat.mBitsPerChannel   = 16;
    outFormat.mReserved         = 0;

    OSStatus err =  AudioConverterNew(&inFormat, &outFormat, &_audioConverter);

    if (err != noErr) {
        printf("setup converter error, status: %i", (int)err);
    }

在此处将AAC转换为PCM:

NSData *data;                                   // data is AAC data from TCP stream
uint8_t *buffer = (uint8_t *)[data bytes];
int kAdtsHeaderLength = 7;                      // means ADTS header is 7 Bytes
size_t mediaSize = aacSize - kAdtsHeaderLength; // skip ADTS header
size_t maxBufferSize = 10000;

//input
AudioBufferList inAudioBufferList = {0};
inAudioBufferList.mNumberBuffers = 1;
inAudioBufferList.mBuffers[0].mNumberChannels = 2;
inAudioBufferList.mBuffers[0].mDataByteSize = (UInt32)mediaSize;
inAudioBufferList.mBuffers[0].mData = buffer + kAdtsHeaderLength;

//output
uint8_t *outBuf = (uint8_t *)malloc(maxBufferSize);
memset(outBuf, 0, maxBufferSize);
AudioBufferList outAudioBufferList = {0};
outAudioBufferList.mNumberBuffers = 1;
outAudioBufferList.mBuffers[0].mNumberChannels = inAudioBufferList.mBuffers[0].mNumberChannels;
outAudioBufferList.mBuffers[0].mDataByteSize = inAudioBufferList.mBuffers[0].mDataByteSize;
outAudioBufferList.mBuffers[0].mData = outBuf;

UInt32 ioOutputDataPacketSize = (UInt32)mediaSize;

OSStatus err = AudioConverterFillComplexBuffer(self->_audioConverter,
                                               inInputDataProc,
                                               &inAudioBufferList,
                                               &ioOutputDataPacketSize,
                                               &outAudioBufferList,
                                               NULL);
if (err != noErr) {
    NSLog(@"convert fail error: %d", err);
}

转换器回调在这里:

OSStatus inInputDataProc(AudioConverterRef inAudioConverter, 
                         UInt32 *ioNumberDataPackets, 
                         AudioBufferList *ioData, 
                         AudioStreamPacketDescription **outDataPacketDescription, 
                         void *inUserData)
{
    AudioBufferList inAudioBufferList = *(AudioBufferList *)inUserData;
    if (inAudioBufferList.mNumberBuffers == 0) {
        *ioNumberDataPackets = 0;
        return -1;
    }

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

    if (outDataPacketDescription) {
        AudioStreamPacketDescription packetDescription;
        packetDescription.mDataByteSize = ioData->mBuffers[0].mDataByteSize;
        packetDescription.mStartOffset = 0;
        packetDescription.mVariableFramesInPacket = 0;
        *outDataPacketDescription = &packetDescription;
    }

    *ioNumberDataPackets = 1;

    return  noErr;
}

非常感谢您的帮助!

0 个答案:

没有答案