【问题标题】:How to play a signal with AudioUnit (iOS)?如何使用 AudioUnit (iOS) 播放信号?
【发布时间】:2016-10-26 08:59:40
【问题描述】:

我需要生成信号并使用 iPhone 的扬声器或耳机播放。

为此,我生成了一个交错信号。然后我需要用下一个信息实例化一个 AudioUnit 继承的类对象:2 个通道,44100 kHz 采样率,一些缓冲区大小来存储几帧。

然后我需要编写一个回调方法,它将接收我的信号并将其放入 iPhone 的输出缓冲区。

问题是我不知道如何编写 AudioUnit 继承类。我无法理解 Apple 关于它的文档,我能找到的所有示例要么从文件中读取并以巨大的延迟播放,要么使用贬低的结构。

我开始认为我很愚蠢之类的。请帮忙...

【问题讨论】:

标签: ios objective-c iphone core-audio audiounit


【解决方案1】:

要使用AudioUnit 向iPhone 硬件播放音频,您不能从AudioUnit 派生,因为CoreAudio 是一个c 框架 - 而是给它一个渲染回调,您可以在其中向该单元提供音频样本。以下代码示例向您展示了如何操作。您需要用真正的错误处理替换asserts,并且您可能想要更改或至少使用kAudioUnitProperty_StreamFormat 选择器检查音频单元的样本格式。我的格式恰好是 48kHz 浮点交错立体声。

static OSStatus
renderCallback(
               void* inRefCon,
               AudioUnitRenderActionFlags* ioActionFlags,
               const AudioTimeStamp* inTimeStamp,
               UInt32 inBusNumber,
               UInt32 inNumberFrames,
               AudioBufferList* ioData)
{
    // inRefCon contains your cookie

    // write inNumberFrames to ioData->mBuffers[i].mData here

    return noErr;
}

AudioUnit
createAudioUnit() {
    AudioUnit   au;
    OSStatus err;

    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;

    AudioComponent comp = AudioComponentFindNext(NULL, &desc);
    assert(0 != comp);

    err = AudioComponentInstanceNew(comp, &au);
    assert(0 == err);


    AURenderCallbackStruct input;
    input.inputProc = renderCallback;
    input.inputProcRefCon = 0;  // put your cookie here

    err = AudioUnitSetProperty(au, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, sizeof(input));
    assert(0 == err);

    err = AudioUnitInitialize(au);
    assert(0 == err);

    err = AudioOutputUnitStart(au);
    assert(0 == err);

    return au;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 2012-11-08
相关资源
最近更新 更多