c++ - AudioConverterNew 返回 -50

标签 c++ objective-c macos core-audio

我对使用 AudioQueue 服务有一点疑问。 我已经按照 Apple 网站上提供的指南进行操作,但是当我开始并运行音频队列时,我收到消息告诉我“AudioConverterNew 返回 -50”。 现在,我知道 -50 错误代码意味着存在错误参数。但是,我不知道哪个参数是错误的(非常感谢 Apple...)!


所以,这是我的代码。

这是我的类的参数,名为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 创建然后启动。 我开始初始化数据格式的参数。然后我创建 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。不存在 mBytesPerPacket 应为 32768 的 PCM 格式,这就是您收到错误的原因。

关于c++ - AudioConverterNew 返回 -50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18631134/

相关文章:

c++ - C++ std::hash 实现总是确定性的吗?

c++ - const 转换运算符的行为

iphone - 断言失败错误

c++ - Qt5 OpenGL GLSL 版本错误

macos - 在 Mac OS-X 中将 tail -f 与多个 Grep 命令一起使用时无输出

c++ - 如何根据圆上中点和其他点之间的角度计算圆上的点

ios - NSPersistentContainer 的核心数据并发

ios - 如何在 UICollectionView 的节标题中动态添加标签和按钮?

macos - 在 AppleScript 中从 PDF 文档到 JPEG 图像

c++ - 在读取之前预填充 cin