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.
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