音频转换器新返回 -50

AudioConverterNew returned -50

本文关键字:返回 转换器 新返回 音频      更新时间:2023-10-16

我对使用AudioQueue服务有一点问题。我遵循了Apple网站上提供的指南,但是当我开始并运行音频队列时,我收到消息告诉我"AudioConverterNew返回-50"。现在,我知道 -50 错误代码意味着有一个错误的参数。但是,我不知道哪个参数是错误的(非常感谢苹果...


所以,这是我的代码。

这是我的类的参数,名为cPlayerCocoa

AudioQueueRef                   mQueue;
AudioQueueBufferRef             mBuffers[NUMBER_BUFFERS];    // NUMBER_BUFFERS = 3
uint32                          mBufferByteSize;
AudioStreamBasicDescription     mDataFormat;

这是第一个函数:

static void
BuildBuffer( void* iAQData, AudioQueueRef iAQ, AudioQueueBufferRef iBuffer )
{
    cPlayerCocoa* player = (cPlayerCocoa*) iAQData;
    player->HandleOutputBuffer( iAQ, iBuffer );
}

它从包含 AudioQueue 的结构创建一个 cPlayerCocoa,并调用 HandleOutputBuffer 函数,该函数分配音频缓冲区:

void
cPlayerCocoa::HandleOutputBuffer( AudioQueueRef iAQ, AudioQueueBufferRef iBuffer )
{
    if( mContinue )
    {
        xassert( iBuffer->mAudioDataByteSize == 32768 );
        int startSample = mPlaySampleCurrent;
        int result = 0;
        int samplecount = 32768 / ( mSoundData->BytesPerSample() );    // BytesPerSample, in my case, returns 4
        tErrorCode  error = mSoundData->ReadData( (int16*)(iBuffer->mAudioData), samplecount, &result, startSample );
        AudioQueueEnqueueBuffer( mQueue, iBuffer, 0, 0 );    // I'm using CBR data (PCM), hence the 0 passed into the AudioQueueEnqueueBuffer.
        if( result != samplecount )
            mContinue = false;
        startSample += result;
    }      
    else
    {
        AudioQueueStop( mQueue, false );
    }
}

在下一个函数中,将创建然后启动音频队列。我开始初始化数据格式的参数。然后我创建 AudioQueue,并分配 3 个缓冲区。分配缓冲区后,我启动 AudioQueue,然后运行循环。

void
cPlayerCocoa::ThreadEntry()
{
    int samplecount = 32768 / ( mSoundData->BytesPerSample() );
    mDataFormat.mSampleRate = mSoundData->SamplingRate();    // Returns 44100
    mDataFormat.mFormatID = kAudioFormatLinearPCM;
    mDataFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    mDataFormat.mBytesPerPacket = 32768;
    mDataFormat.mFramesPerPacket = samplecount;
    mDataFormat.mBytesPerFrame = mSoundData->BytesPerSample();    // BytesPerSample returns 4.
    mDataFormat.mChannelsPerFrame = 2;
    mDataFormat.mBitsPerChannel = uint32(mSoundData->BitsPerChannel());
    mDataFormat.mReserved = 0;
    AudioQueueNewOutput( &mDataFormat, BuildBuffer, this, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &mQueue );
    for( int i = 0; i < NUMBER_BUFFERS; ++i )
    {
        AudioQueueAllocateBuffer( mQueue, mBufferByteSize, &mBuffers[i] );
        HandleOutputBuffer( mQueue, mBuffers[i] );
    }
    AudioQueueStart( mQueue, NULL );    // I want the queue to start playing immediately, so I pass NULL
    do {
        CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0.25, false );
    } while ( !NeedStopASAP() );
    AudioQueueDispose( mQueue, true );
}

对 AudioQueueStart 的调用返回 -50(错误参数),我无法弄清楚出了什么问题......我真的很感激一些帮助,提前感谢:-)

我认为你的ASBD是可疑的。 PCM 格式具有可预测的 mBytesPerPacketmBytesPerFramemFramesPerPacket值。 对于普通的 16 位交错签名 44.1 立体声音频,ASBD 如下所示

AudioStreamBasicDescription asbd = {
  .mFormatID = kAudioFormatLinearPCM,
  .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
  .mSampleRate = 44100,
  .mChannelsPerFrame = 2,
  .mBitsPerChannel = 16,
  .mBytesPerPacket = 4,
  .mFramesPerPacket = 1,
  .mBytesPerFrame = 4,
  .mReserved = 0
};

当其中一个 ASBD 不受支持时,AudioConverterNew返回 -50。 没有 PCM 格式,mBytesPerPacket应该是 32768,这就是您收到错误的原因。