无声播放时核心音频CPU使用率高

时间:2011-07-08 23:42:28

标签: cocoa core-audio cpu-usage

我刚刚开始学习Core Audio并制作了一个简单的测试应用程序,可以播放三种不同的钢琴音符。除了一件小事之外,它看起来还不错。

首先,应用程序使用大约7%左右的CPU(在活动监视器中可见),我认为这是正常的,因为它正在运行实时AUGraph。但是,随着播放更多音符,CPU使用率会不断增加,即使当时没有声音播放。

以下时间表:

  1. 启动应用。 CPU使用率低至零。
  2. 播放笔记,中等CPU使用率。
  3. 笔记完成&没有声音播放,CPU使用率中等。
  4. 代码:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
    
        NewAUGraph(&audioGraph);
    
        AudioComponentDescription cd;
        AUNode outputNode;
        AudioUnit outputUnit;
    
        cd.componentManufacturer = kAudioUnitManufacturer_Apple;
        cd.componentFlags = 0;
        cd.componentFlagsMask = 0;
        cd.componentType = kAudioUnitType_Output;
        cd.componentSubType = kAudioUnitSubType_DefaultOutput;
    
        //AUGraphNewNode(audioGraph, &cd, 0, NULL, &outputNode);
        AUGraphAddNode(audioGraph, &cd, &outputNode);
        //AUGraphGetNodeInfo(audioGraph, outputNode, 0, 0, 0, &outputUnit);
        AUGraphNodeInfo(audioGraph, outputNode, &cd, &outputUnit);
    
        AUNode mixerNode;
        AudioUnit mixerUnit;
    
        cd.componentManufacturer = kAudioUnitManufacturer_Apple;
        cd.componentFlags = 0;
        cd.componentFlagsMask = 0;
        cd.componentType = kAudioUnitType_Mixer;
        cd.componentSubType = kAudioUnitSubType_StereoMixer;
    
        AUGraphAddNode(audioGraph, &cd, &mixerNode);
        AUGraphNodeInfo(audioGraph, mixerNode, &cd, &mixerUnit);
    
        AUGraphConnectNodeInput(audioGraph, mixerNode, 0, outputNode, 0);
    
        AUGraphOpen(audioGraph);
        AUGraphInitialize(audioGraph);
        AUGraphStart(audioGraph);
    
        AUNode synthNode;
    
    
        cd.componentManufacturer = kAudioUnitManufacturer_Apple;
        cd.componentFlags = 0;
        cd.componentFlagsMask = 0;
        cd.componentType = kAudioUnitType_MusicDevice;
        cd.componentSubType = kAudioUnitSubType_DLSSynth;
    
        AUGraphAddNode(audioGraph, &cd, &synthNode);
        AUGraphNodeInfo(audioGraph, synthNode, &cd, &synthUnit);
    
        AUGraphConnectNodeInput(audioGraph, synthNode, 0, mixerNode, 0);
    
        AUGraphUpdate(audioGraph, NULL);
        CAShow(audioGraph);
    
    }
    
    - (IBAction)playMusic:(id)sender {
        MusicDeviceMIDIEvent(synthUnit, 0x90, 60, 127, 0);
        sleep(1);
        MusicDeviceMIDIEvent(synthUnit, 0x90, 62, 127, 0);
        sleep(1);
        MusicDeviceMIDIEvent(synthUnit, 0x90, 64, 127, 0);
    }
    
    - (void)one:(id)sender {
        MusicDeviceMIDIEvent(synthUnit, 0x90, 60, 127, 0);
    }
    
    - (void)two:(id)sender {
        MusicDeviceMIDIEvent(synthUnit, 0x90, 62, 127, 0);
    }
    
    - (void)three:(id)sender {
        MusicDeviceMIDIEvent(synthUnit, 0x90, 64, 127, 0);
    }
    

    发生了什么事?我该如何解决这个问题?

    感谢。

2 个答案:

答案 0 :(得分:1)

好吧,我明白了!

基本上,我只是忘记停止播放音符,尽管它们逐渐变为沉默,但它们仍然在播放并使计算机正常工作。

可以通过将速度设置为0再次播放音符来解决此问题。所以,代码看起来像这样:

- (IBAction)playMusic:(id)sender {
    MusicDeviceMIDIEvent(synthUnit, 0x90, 60, 127, 0);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 60, 0, 0);
    sleep(1);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 62, 127, 0);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 62, 0, 0);
    sleep(1);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 64, 127, 0);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 64, 0, 0);
}

- (void)one:(id)sender {
    MusicDeviceMIDIEvent(synthUnit, 0x90, 60, 127, 0);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 60, 0, 0);
}

- (void)two:(id)sender {
    MusicDeviceMIDIEvent(synthUnit, 0x90, 62, 127, 0);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 62, 0, 0);
}

- (void)three:(id)sender {
    MusicDeviceMIDIEvent(synthUnit, 0x90, 64, 127, 0);
    MusicDeviceMIDIEvent(synthUnit, 0x90, 64, 0, 0);
}

我想如果我希望音符播放时间更长,我可以在开始和停止音符之间等待。

感谢您的帮助:@ user757808 @Peter Hosey @mbykov

答案 1 :(得分:0)

代码中CPU最重的部分是AudioUnit处理程序。尝试为用于播放媒体的硬件设置相同的采样率,以减少CPU负载。

AudioSessionSetProperty (kAudioSessionProperty_PreferredHardwareSampleRate, size, &float64);

只有一种方法可以阻止AudioUnit处理--AUGraphStop。

我尝试了一切,从活动监视器退出,关闭所有系统声音,只有那些有帮助的是:

  1. 启动时启动(安全启动)
  2. 打开iTunes
  3. 或完全重新格式化HD并在有时间的情况下重新安装Lion。

相关问题