【问题标题】:iOS Core Audio render callback works on simulator, not on deviceiOS Core Audio 渲染回调适用于模拟器,而不是设备
【发布时间】:2013-01-18 02:08:14
【问题描述】:

我的回调如下所示:

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags,                          const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
    AudioSampleType *outBuffer = (AudioSampleType *)ioData->mBuffers[0].mData;
    memset(outBuffer, 0, sizeof(AudioSampleType)*inNumberFrames*kNumChannels);      

    //copy a sine wave into outBuffer
    double max_aust = pow(2.f, (float)(sizeof(AudioSampleType)*8.0 - 1.f)) - 1.0;
    for(int i = 0; i < inNumberFrames; i++) {
        SInt16 val = (SInt16) (gSine2_[(int)phase2_] * max_aust);
        outBuffer[2*i] = outBuffer[2*i+1] = (AudioSampleType)val;
        phase2_ += inc2_;
        if(phase2_ > 1024) phase2_ -= 1024;
    }

    return noErr;
}

这是一个超级基本的渲染回调,应该只是播放一个正弦波。它在模拟器上,它不在设备上。事实上,我无法从设备中获得任何音频。即使我添加了一个 printf 来检查 outBuffer,它也表明 outBuffer 充满了正弦波的样本。

我将会话类型设置为 Ambiet,但我也尝试过 playAndRecord 和 MediaPlayback。也没有运气。我首选的 framesPerBuffer 是 1024(这是我在模拟器和设备上得到的)。我的采样率为 44100hz。为了以防万一,我也尝试了48000。我也尝试过更改 framesPerBuffer。

是否还有其他原因导致样本无法到达设备上的硬件?

更新: 我刚刚发现,如果我将耳机插入设备,我会听到听起来像是正弦波的声音,而且削波非常可怕。这让我认为设备可能需要浮点而不是带符号的 int,但是当我将值更改为 -1 到 1 时,就没有音频(设备或模拟器,正如预期的那样,因为引擎设置为接受带符号的 int,而不是浮点数)。

【问题讨论】:

    标签: iphone ios ipad core-audio


    【解决方案1】:

    如果没有看到您的更多设置,我无法确定,但这听起来很像您被AudioSampleType(SInt16 样本)和AudioUnitSampleType(固定 8.24 样本内的SInt32 容器)。几乎可以肯定,AudioUnitSampleType 是回调中预期的格式。 This post on the Core Audio mailing list does a very good job explaining the difference between the two, and why they exist.

    【讨论】:

      【解决方案2】:

      因为我不知道您的设置如何,我建议您阅读以下内容:http://www.cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html

      示例代码适用于单音发生器,如果您也想要立体声填充第二个通道。

      指向第二个通道缓冲区的指针是

      const int secondChannel = 1;
      Float32 *bufferSecondChannel = (Float32 *)ioData->mBuffers[secondChannel].mData;
      

      希望有帮助

      【讨论】:

        【解决方案3】:

        您可能需要设置音频会话(初始化、设置类别并激活它)

        OSStatus activationResult = NULL;
        result = AudioSessionSetActive (true);
        

        更多信息: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html

        【讨论】:

          猜你喜欢
          • 2012-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-15
          相关资源
          最近更新 更多