简体   繁体   English

iOS Core Audio渲染回调只能在模拟器上使用,而不能在设备上使用

[英]iOS Core Audio render callback works on simulator, not on device

My callback looks like this: 我的回调如下所示:

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;
}

This is a super basic render callback that should just play a sine wave. 这是一个超级基本的渲染回调,应该只播放一个正弦波。 It does on the simulator, it does NOT on the device. 它在模拟器上执行,但不在设备上执行。 In fact, I can get no audio from the device. 实际上,我无法从设备获得音频。 Even if I add a printf to check outBuffer, it shows that outBuffer is filled with samples of a sine wave. 即使我添加了一个printf来检查outBuffer,它也表明outBuffer充满了正弦波的样本。

I'm setting the session type to Ambiet, but I've tried playAndRecord and MediaPlayback as well. 我将会话类型设置为Ambiet,但我也尝试了playAndRecord和MediaPlayback。 No luck with either. 都不运气。 My preferred framesPerBuffer is 1024 (which is what I get on the simulator and device). 我首选的framesPerBuffer是1024(这是我在模拟器和设备上得到的)。 My sample rate is 44100hz. 我的采样率为44100hz。 I've tried 48000 as well just in case. 我也尝试过48000,以防万一。 I've also tried changing the framesPerBuffer. 我也尝试过更改framesPerBuffer。

Are there any other reasons that the samples would not reach the hardware on the device? 还有其他原因导致样本无法到达设备上的硬件吗?

UPDATE: I just found out that if I plug my headphones into the device I hear what sounds like a sine wave that is clipping really horribly. 更新:我刚刚发现,如果我将耳机插入设备,我会听到听起来像是正弦波的声音,这确实令人震惊。 This made me think that possibly the device was expecting floating point instead of signed int, but when I changed the values to -1 to 1 there's just no audio (device or simulator, as expected since the engine is set to accept signed int, not floating point). 这使我认为设备可能期望浮点数而不是带符号的int,但是当我将值更改为-1到1时,就没有音频(设备或模拟器如预期那样,因为引擎设置为接受带符号的int,而不是浮点)。

I can't tell for sure without seeing more of your setup, but it sounds very much like you're getting bitten by the difference between AudioSampleType (SInt16 samples) and AudioUnitSampleType (fixed 8.24 samples inside of a SInt32 container). 我不能肯定地告诉没有看到您的更多的设置,但它听起来很像你要通过的区别咬伤AudioSampleType (SInt16样本)和AudioUnitSampleType (固定一个内部的8.24样品SInt32容器)。 It's almost certainly the case that AudioUnitSampleType is the format expected in your callback. 几乎可以肯定AudioUnitSampleType是回调中期望的格式。 This post on the Core Audio mailing list does a very good job explaining the difference between the two, and why they exist. Core Audio邮件列表上的这篇帖子很好地解释了两者之间的区别以及它们为什么存在。

Because I don't know how is your setup I suggest to read this: http://www.cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html 因为我不知道您的设置如何,所以建议您阅读以下内容: http : //www.cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html

The sample code is for a mono tone generator, if you want stereo fill the second channel too. 如果您也想立体声填充第二个通道,则该示例代码用于单声道音频发生器。

The pointer to second channel buffer is 指向第二个通道缓冲区的指针是

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

Hope this help 希望这个帮助

You may need to setup the audio session (initialize, set category and activate it) 您可能需要设置音频会话(初始化,设置类别并激活它)

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

More at: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html 有关更多信息,请访问: http : //developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM