无声音播放时核心音频 CPU 使用率高
我刚刚开始学习核心音频并制作了一个简单的测试应用程序来播放三个不同的钢琴音符。除了一件小事之外,看起来还不错。
起初,该应用程序使用了大约 7% 左右的 CPU(在活动监视器中可见),我认为这是正常的,因为它正在运行实时 AUGraph。然而,随着演奏更多音符,CPU 使用率不断增加,即使当时可能没有播放声音。
下面是一个时间表:
- 启动应用程序。 CPU 使用率低甚至无。
- 播放音符,中等 CPU 使用率。
- 注释完成&没有声音播放,CPU 使用率中等。
代码:
- (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);
}
这是怎么回事?我该如何解决这个问题?
谢谢。
I am just starting to learn Core Audio and made a simple test application the plays three different piano notes. It seems pretty good except for one small thing.
At first, the app uses about 7% or so of the CPU (visible in activity monitor), which I assume is normal because it is running a live AUGraph
. However, as more notes are played the CPU usage keeps increasing, even though no sound may be playing at the time.
A timeline of this below:
- Starts app. Low to none CPU usage.
- Plays notes, medium CPU usage.
- Notes finish & no sound playing, medium CPU usage.
The code:
- (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);
}
What's going on? How can I fix this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想通了!
基本上,我只是忘记停止演奏音符,尽管它们逐渐变得安静,但它们仍在演奏并使计算机工作。
可以通过将力度设置为
0
再次播放音符来解决此问题。所以,代码看起来像这样:我想如果我想让音符播放更长时间,我可以在开始和停止音符之间等待。
感谢您的所有帮助:@user757808 @Peter Hosey @mbykov
Well, I figured it out!
Basically, I was just forgetting to stop playing the notes, and although they tapered off into silence, they were still playing and making the computer work.
This can be fixed by playing the note again with velocity set to
0
. So, the code would look like this:I guess if I wanted the note to play longer I could wait between starting and stopping the note.
Thanks for all your help: @user757808 @Peter Hosey @mbykov
代码中 CPU 最重的部分是 AudioUnit 处理过程。尝试为用于播放媒体的硬件设置相同的采样率,以减少 CPU 负载。
只有一种方法可以阻止 AudioUnit 处理 - AUGraphStop。
我尝试了一切,退出活动监视器,关闭所有系统声音,唯一有帮助的是:
或者完全重新格式化 HD 并重新安装 Lion(如果有时间)。
The most CPU heavy part in your code is AudioUnit processing procedure. Try set up same sample rate for hardware that is used for media played to reduce CPU loading.
And there only one way to stop AudioUnit from processing - AUGraphStop.
I tried everything, quit from activity monitor, close all system sounds, only what had helped was:
Or completely reformat the HD and reinstall Lion if you have the time.