Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to play midi notes?

I have searched and already have done an OS X app that can play MIDI notes, but when i tried in iOS, nothing happened. Here is the core 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 cd.componentType and cd.componentSubType settings may be not correct, because the difference between iOS app and OS X app is just it. In 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 cd and play MIDI note correctly in iOS? I don't have clearly or deeply understanding on Audio Unit / AUGraph...

UPDATED: Using cd.componentSubType = kAudioUnitSubType_RemoteIO; when adding outNode will be right and play soft MIDI notes successfully.

like image 569
Smeegol Avatar asked May 29 '15 04:05

Smeegol


1 Answers

kAudioUnitSubType_GenericOutput should be kAudioUnitSubType_RemoteIO for iOS.

You need to let the graph run for audio to play, so don't immediately call AUGraphStop() and DisposeAUGraph().

For testing the graph, I would use kAudioUnitSubType_Sampler instead of kAudioUnitSubType_MIDISynth because it has a built in wave generator if you don't set it's preset. Then when you get the graph working, try out your synth.

like image 134
dave234 Avatar answered Oct 22 '22 01:10

dave234