gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:00:48 24 4
gpt4 key购买 nike

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

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

时间表如下:

  1. 启动应用程序。 CPU 使用率低至零。
  2. 播放音符,中等 CPU 使用率。
  3. 笔记完成且没有声音播放,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);
}

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

谢谢。

最佳答案

好吧,我明白了!

基本上,我只是忘记停止演奏音符,尽管它们逐渐变得安静,但它们仍在演奏并使计算机正常工作。

可以通过将力度设置为 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

关于cocoa - 无声音播放时核心音频 CPU 使用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com