IOS how to play midi notes?
I searched and already made an OS X app that can play MIDI notes, but when I tried on iOS, nothing happened. Here's the main code:
AUGraph graph;
AudioUnit synthUnit;
AUNode synthNode, outNode;
NewAUGraph(&graph);
AudioComponentDescription cd;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
cd.componentType = kAudioUnitType_MusicDevice;
cd.componentSubType = kAudioUnitSubType_MIDISynth;
AUGraphAddNode(graph, &cd, &synthNode);
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_GenericOutput;
AUGraphAddNode(graph, &cd, &outNode), "AUGraphAddNode");
CheckError(AUGraphOpen(graph), "AUGraphOpen");
AUGraphConnectNodeInput(graph, synthNode, 0, outNode, 0);
AUGraphNodeInfo(graph, synthNode, 0, &synthUnit);
AUGraphInitialize(graph);
CAShow(graph);
AUGraphStart(graph);
CFURLRef bankURL = ... //gs_instruments.dls
AudioUnitSetProperty(synthUnit,
kMusicDeviceProperty_SoundBankURL,
kAudioUnitScope_Global,
0,
&bankURL,
sizeof(bankURL));
static UInt32 kChannelMessage_NoteOn = 0x90;
UInt8 channel = 0;
UInt8 note = 60;
UInt32 velocity = 127;
MusicDeviceMIDIEvent(synthUnit,
kChannelMessage_NoteOn | channel,
note,
velocity,
0);
AUGraphStop(graph);
DisposeAUGraph(graph);
I know the settings cd.componentType
and cd.componentSubType
might be wrong because the difference between an iOS app and OS X is simple. On OS X:
AudioComponentDescription cd;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
cd.componentType = kAudioUnitType_MusicDevice;
cd.componentSubType = kAudioUnitSubType_DLSSynth;
AUGraphAddNode(graph, &cd, &synthNode);
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_DefaultOutput;
AUGraphAddNode(graph, &cd, &outNode);
How to set up cd
and correctly play a MIDI note in iOS? I do not have a clear or deep understanding on Audio Unit
/ AUGraph
...
UPDATED: Usage cd.componentSubType = kAudioUnitSubType_RemoteIO;
on add outNode
will be correct and will play soft MIDI notes successfully.
kAudioUnitSubType_GenericOutput must be kAudioUnitSubType_RemoteIO for iOS.
You have to let the graphics run for the audio to play, so don't call AUGraphStop () and DisposeAUGraph () right away.
For testing the graph, I would use kAudioUnitSubType_Sampler instead of kAudioUnitSubType_MIDISynth because it has a built-in waveform generator if you didn't specify one. Then, when you get the graph, try your synthesizer.
link the AVFoundation and AudioToolbox framework to your project, then use this code:
#import <AVFoundation/AVFoundation.h>
NSURL *midiUrl = [[NSBundle mainBundle] URLForResource:@"even voor mij" withExtension:@"mid"];
MusicPlayer player = NULL;
NewMusicPlayer(&player);
MusicSequence sequence = NULL;
NewMusicSequence(&sequence);
MusicSequenceFileLoad(sequence, (__bridge CFURLRef)midiUrl, NULL, NULL);
MusicPlayerSetSequence(player, sequence);
MusicPlayerStart(player);