0

I have an AUGraph with AudioUnits. My MIDI Synth AudioUnit was created by this:

    AudioComponentDescription midiSynthDesc;
    midiSynthDesc.componentType = kAudioUnitType_MusicDevice;
    midiSynthDesc.componentSubType = kAudioUnitSubType_MIDISynth;

Then I load the SF2 File into the MIDI AudioUnit:

    NSURL *bankURL = [[NSBundle mainBundle] URLForResource:@"Combined" withExtension:@"sf2"];
    AudioUnitSetProperty(midiSynthUnit,
                         kMusicDeviceProperty_SoundBankURL,
                         kAudioUnitScope_Global,
                         0,
                         &bankURL,
                         sizeof(bankURL));

I have this SF2 loaded MIDI-Synth AudioUnit connected to Remote-IO AudioUnit, then I added MusicPlayer to this AUGraph to play:

    MusicPlayerSetSequence(musicPlayer, musicSequence);
    NewMusicSequence(&musicSequence);
    MusicSequenceFileLoad(musicSequence, (__bridge CFURLRef)midiFileURL, 0, 0)
    MusicSequenceSetAUGraph(musicSequence, processingGraph);
    MusicPlayerPreroll(musicPlayer);
    MusicPlayerStart(musicPlayer);

And it works with the Remote-IO, I can hear the MIDI file playing.

However, if I pull the MIDI Synth Audio Unit programmatically, trying to render the MIDI File into Raw Audio Data and then write into m4a file:

    OSStatus err = noErr;
    err = AudioUnitRender(midiSynthUnit, &flags, &inTimeStamp, busNumber, numberFrames, bufferList);

No audio data can be read ( bufferList's mData == 0 Always). No matter how many times I call AudioUnitRender, MusicPlayerGetTime always tells me that the current beat of the Music Player is 0:

    Float64 currentBeat;
    MusicPlayerGetTime(musicPlayer, &currentBeat);

Can anybody tell me why? Any suggestion how to read the MIDI File with SF2 soundfont file and convert into raw audio data / render MIDI into m4a file?

Thanks

John
  • 2,672
  • 2
  • 23
  • 29
  • Did you try calling render no faster (more often) than the real time audio sample rate would require? – hotpaw2 Sep 14 '19 at 16:44

1 Answers1

0

I've not tested this, but maybe you can try another SO question & answer as inspiration.

Anyway, what I know about AudioToolbox's MusicPlayer is that it works in real time only. If the MIDI sequence is two minutes long, it will take two minutes at least to render the audio file. If you prefer batch processing, where the rendering speed is only limited by the speed of your system, CPU, memory and IO, then I recommend you to use an alternative like FLuidSynth

Former contributor
  • 2,466
  • 2
  • 10
  • 15