3

I'm trying to play a midi file as a wav audio is being played.

I've tried with

let akSequencer = AKSequencer(filename: "melody")
akSequencer.enableLooping()
akSequencer.play()

But it only plays the first note and nothing else. Also, I'm not being able to use a mixer to mix it with the wav file that it's being loaded like:

let akMidiSampler = AKMIDISampler()
let akSampler = AKSampler()
let wavFile = try AKAudioFile(forReading: wavUrl!)
try akSampler.loadAudioFile(wavFile)

let mixer = AKMixer(akSampler, akMidiSampler)
mixer.volume = 1

AudioKit.output = mixer
AudioKit.start()
akSampler.play()

The wav audio is being played, but the midi just the first note.

Any help is very welcome!

Juan Giorello
  • 333
  • 2
  • 13
  • I'd love to be able to answer this question, but I don't understand it well enough and now enough information is given. For instance, we don't know what is contained in melody.mid and a variable named "akMidiSampler" is referred to but its declaration is not show. – Aurelius Prochazka Oct 08 '17 at 00:27
  • @AureliusProchazka I've just edited adding the definition of akMidiSampler. melody.mid is just a midi file that it's added to the project. Here you have the link to it https://drive.google.com/file/d/0B0-hfmdUuApIUGp2cmQwUm5SWWM/view?usp=sharing. But basically, I'm not sure if the approach I used is the correct or if I'm completely wrong. – Juan Giorello Oct 10 '17 at 17:29

1 Answers1

2

I believe you don't have properly hooked up your sequencer to your MIDI Sampler. If you look at some of the AudioKit example playgrounds here:

http://audiokit.io/playgrounds/Playback/Sequencer/

or

http://audiokit.io/playgrounds/Playback/Drum%20Sequencer/

You'll notice there's a line that connects the sequencers to the samplers like:

sequencer.setGlobalMIDIOutput(piano.midiIn)

or

sequencer.setGlobalMIDIOutput(drums.midiIn)

Eventually you could have different samplers for different tracks of the sequencer, but I think this global method is what will work best for you now. Remember that you may have to enable audio background modes on your iOS for MIDI to work. HTH!

Aurelius Prochazka
  • 4,510
  • 2
  • 11
  • 34
  • So, I tried that code and still was not working. But the main issue was because I was using local variables so after a few milliseconds, the sound stops. The fix is as it's stated here: http://audiokit.io/playgrounds/Basics/Playgrounds%20to%20Production/ Thanks Aurelius! – Juan Giorello Oct 16 '17 at 17:34
  • This is the code I used: https://stackoverflow.com/questions/46777602/save-aksequencer-output-to-local-file – Juan Giorello Oct 16 '17 at 19:22