如何将AUGraph的输出写入文件? [英] How to write output of AUGraph to a file?

查看:387
本文介绍了如何将AUGraph的输出写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写(应该是)一个简单的应用程序,它在AUGraph中按顺序包含一堆音频单元,然后将输出写入文件。我使用AUGraphAddRenderNotify添加了回调。这是我的回调函数:

I am trying to write (what should be) a simple app that has a bunch of audio units in sequence in an AUGraph and then writes the output to a file. I added a callback using AUGraphAddRenderNotify. Here is my callback function:

OSStatus MyAURenderCallback(void *inRefCon,
                        AudioUnitRenderActionFlags *actionFlags,
                        const AudioTimeStamp *inTimeStamp,
                        UInt32 inBusNumber,
                        UInt32 inNumberFrames,
                        AudioBufferList *ioData) {
    if (*actionFlags & kAudioUnitRenderAction_PostRender) {
        ExtAudioFileRef outputFile = (ExtAudioFileRef)inRefCon;
        ExtAudioFileWriteAsync(outputFile, inNumberFrames, ioData);
    }
}

这种作品。该文件是可播放的,我可以听到我录制的内容,但是有大量静电使其几乎听不见。

This sort of works. The file is playable and I can hear what I recorded but there is horrible amounts of static that makes it barely audible.

有人知道这有什么问题吗?或者有没有人知道将AUGraph输出记录到文件的更好方法?

Does anybody know what is wrong with this? Or does anyone know of a better way to record the AUGraph output to a file?

感谢您的帮助。

推荐答案

我刚才有关于音频单元的顿悟,这帮助我解决了自己的问题。我对音频单元连接和渲染回调如何工作有误解。我认为它们是完全不同的东西,但事实证明连接只是渲染回调的简写。

I had a epiphany with regards to Audio Units just now which helped me solve my own problem. I had a misconception about how audio unit connections and render callbacks work. I thought they were completely separate things but it turns out that a connection is just short hand for a render callback.

从音频单元A的输出到音频单元B的输入执行kAudioUnitProperty_MakeConnection与在单元B的输入上执行kAudioUnitProperty_SetRenderCallback并且具有回调函数调用相同AudioUnitRender在音频单元A的输出上。

Doing an kAudioUnitProperty_MakeConnection from the output of audio unit A to the input of audio unit B is the same as doing kAudioUnitProperty_SetRenderCallback on the input of unit B and having the callback function call AudioUnitRender on the output of audio unit A.

我在设置渲染回调后进行了make连接测试,并且不再调用渲染回调。

I tested this by doing a make connection after setting my render callback and the render callback was no longer invoked.

因此,我能够通过以下方式解决我的问题:

Therefore, I was able to solve my problem by doing the following:

AURenderCallbackStruct callbackStruct = {0};
callbackStruct.inputProc = MyAURenderCallback;
callbackStruct.inputProcRefCon = mixerUnit;

AudioUnitSetProperty(ioUnit,
                     kAudioUnitProperty_SetRenderCallback,
                     kAudioUnitScope_Input,
                     0,
                     &callbackStruct,
                     sizeof(callbackStruct));

他们的回调函数是这样的:

And them my callback function did something like this:

OSStatus MyAURenderCallback(void *inRefCon,
                        AudioUnitRenderActionFlags *actionFlags,
                        const AudioTimeStamp *inTimeStamp,
                        UInt32 inBusNumber,
                        UInt32 inNumberFrames,
                        AudioBufferList *ioData) {

    AudioUnit mixerUnit = (AudioUnit)inRefCon;

    AudioUnitRender(mixerUnit,
                    actionFlags,
                    inTimeStamp,
                    0,
                    inNumberFrames,
                    ioData);

    ExtAudioFileWriteAsync(outputFile, 
                           inNumberFrames, 
                           ioData);

    return noErr;
}

这对我来说应该是显而易见的,但因为它不是我打赌有其他人以同样的方式感到困惑,所以希望这对他们也有帮助。

This probably should have been obvious to me but since it wasn't I'll bet there are others that were confused in the same way so hopefully this is helpful to them too.

我仍然不确定为什么我遇到AUGraphAddRenderNotify回调有问题。我稍后会深入研究,但现在我找到了一个似乎有用的解决方案。

I'm still not sure why I had trouble with the AUGraphAddRenderNotify callback. I will dig deeper into this later but for now I found a solution that seems to work.

这篇关于如何将AUGraph的输出写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆