AudioOutputUnitStart很慢 [英] AudioOutputUnitStart very slow

查看:1641
本文介绍了AudioOutputUnitStart很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码播放单声道音频事件(在各种频率的短哔哔声)。
我创建一个AudioOutputUnit,停止它,并且每当我需要播放音频。我开始吧。

I have a code that plays mono audio event (short beeps at various frequencies). I create an AudioOutputUnit, stop it, and whenever I need to play the audio. I start it. When I've played it for the required time, I stop it.

听起来很简单。

但是, ,AudioOutputUnitStart通常需要180ms才能返回我的iPhone 4S(用iOS 5.1),这是太多了。

However, AudioOutputUnitStart will take usually 180ms to return on my iPhone 4S (with iOS 5.1), this is way too much.

这里是AudioOutputUnit的创建/初始化



Here is the creation/initialisation of the AudioOutputUnit

void createAOU()
{
    m_init = false;
    // find the default playback output unit
    AudioComponentDescription defaultOutputDescription;
    defaultOutputDescription.componentType = kAudioUnitType_Output;
    defaultOutputDescription.componentSubType = kAudioUnitSubType_RemoteIO;
    defaultOutputDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
    defaultOutputDescription.componentFlags = 0;
    defaultOutputDescription.componentFlagsMask = 0;

    // Get the default playback output unit
    AudioComponent defaultOutput = AudioComponentFindNext(NULL, &defaultOutputDescription);
    if (defaultOutput == NULL)
        return;

    OSErr err = AudioComponentInstanceNew(defaultOutput, &m_toneUnit);
    if (err != noErr)
        return;

    // single channel, floating point, linear PCM
    AudioStreamBasicDescription streamFormat;
    streamFormat.mSampleRate = m_framerate;
    streamFormat.mFormatID = kAudioFormatLinearPCM;
    streamFormat.mFormatFlags =
    kLinearPCMFormatFlagIsFloat |
    kAudioFormatFlagsNativeEndian | kLinearPCMFormatFlagIsPacked;
    streamFormat.mBytesPerPacket = sizeof(float);
    streamFormat.mFramesPerPacket = 1;
    streamFormat.mBytesPerFrame = sizeof(float);
    streamFormat.mChannelsPerFrame = 1;
    streamFormat.mBitsPerChannel = sizeof(float) * 8;
    err = AudioUnitSetProperty (m_toneUnit,
                                kAudioUnitProperty_StreamFormat,
                                kAudioUnitScope_Input,
                                0,
                                &streamFormat,
                                sizeof(AudioStreamBasicDescription));
    if (err != noErr)
        return;

    // Attach callback to default output
    AURenderCallbackStruct input;
    input.inputProc = RenderTone;
    input.inputProcRefCon = this;
    err = AudioUnitSetProperty(m_toneUnit, 
                               kAudioUnitProperty_SetRenderCallback, 
                               kAudioUnitScope_Input,
                               0, 
                               &input, 
                               sizeof(input));
    if (err != noErr)
        return;

    float aBufferLength = 0.001; // In seconds
    AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, 
                            sizeof(aBufferLength), &aBufferLength);

    err = AudioUnitInitialize(m_toneUnit);
    if (err != noErr)
        return;
    reset();
    m_init = true;
}

要开始,我只需调用:

OSErr err = AudioOutputUnitStart(m_toneUnit);

完成后:

OSErr err = AudioOutputUnitStop(m_toneUnit);

我尝试了各种各样的东西:
使kAudioSessionProperty_PreferredHardwareIOBufferDuration属性为一个非常小的值像5ms)
降低帧率(尝试48kHz,44.1kHz,8kHz)

I've tried various things: making the kAudioSessionProperty_PreferredHardwareIOBufferDuration property to a very small value (like 5ms) Reduce the framerate (tried 48kHz, 44.1kHz, 8kHz)

无论我做什么...
第一次音频单元开始,AudioOutputUnitStart只在160-180ms后返回。如果我立即停止并立即启动它,那么它只需要大约5米,这是更容易接受。

No matter what I do... First time the audiounit is started, AudioOutputUnitStart only returns after 160-180ms. If I stop and start it right away, then it only takes about 5m which is much more acceptable.

我的应用程序的延迟是相当重要的,180毫秒是绝对不能接受的。

Latency in my application is rather important, and 180ms is definitely not acceptable.

有任何建议吗?
我见过一些人问类似的问题,但他们通常从来没有得到答案。

Any suggestions? I've seen a few people asking similar questions, but they usually never got an answer.

我希望这个时间的东西会不同:)

I'm hoping this time things will be different :)

推荐答案

处理音频单元启动延迟的方法是在所需时间之前启动音频单元,而不是停下来。相反,只需配置音频会话为短缓冲区,然后填充音频单元渲染回调缓冲区沉默,直到它的时间,以填充他们所需的声音。然后在你的声音后,回到填充缓冲区沉默,而不是停止。

The way to deal with Audio Unit start-up latency is to start the Audio Unit ahead of the time needed, and not stop it. Instead, just configure the audio session for short buffers, and then fill the Audio Unit render callback buffers with silence until it's time to fill them with your desired sound. Then after your sound, go back to filling the buffers with silence instead of stopping.

这篇关于AudioOutputUnitStart很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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