7

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.

Smeegol
  • 2,014
  • 4
  • 29
  • 44
  • 2
    Here's a blog post on how to use an AUGraph with a MID MusicSequence. There is also a github repo with a working example. http://www.rockhoppertech.com/blog/swift-augraph-and-musicsequence/ – Gene De Lisa May 30 '15 at 16:47
  • 3
    @GeneDeLisa this is a good tutorial about AUGraph, thanks – Smeegol Jun 01 '15 at 02:38

2 Answers2

2

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.

dave234
  • 4,793
  • 1
  • 14
  • 29
0

link the AVFoundation, and AudioToolbox frameworks 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);
lzam
  • 185
  • 16
Jay
  • 7
  • 1
  • 2
    You here are trying to play a MIDI file, which is not the answer to the question: Just playing MIDI notes on iOS. – Smeegol May 29 '15 at 05:46