I use Audiokit successfully to play back a Midi sequence using multiple MidiSamplers -> Single Mixer. It normally goes to the audio output of the device, and that works just fine. Now, what I would like is to record the sound into files. By using NodeRecorder I was able to make a recording, figured out how to start/stop etc.. so that is all fine. It works real time, so I run the midi sequence down once, close the resulting single .caf file and that is done. What I cannot figure out is how to separate the instruments (the MidiSampler outputs) into separate files in a single pass. Otherwise I would have to run the sequence down with a single instrument soloing and all others muted, and that is obviously very cumbersome to do for all instruments. Any ideas, or sample anybody can point me to?
I tried allocating multiple recorders, it did not work. The resulting files had audio garbage in them.
Here is what I tried:
var drumKitSampler: MIDISampler!
var bassSampler: MIDISampler!
var pianoChordsSampler: MIDISampler!
// the samplers are loaded with sounds
do {
try audioEngine?.start()
} catch {
SLOG("AudioKit did not start!")
}
let recorder1 = try! AudioKit.NodeRecorder.init(node: drumKitSampler, fileDirectoryURL: outputURL, bus: 0, shouldCleanupRecordings: false)
let recorder2 = try! AudioKit.NodeRecorder.init(node: bassSampler, fileDirectoryURL: outputURL, bus: 0, shouldCleanupRecordings: false)
let recorder3 = try! AudioKit.NodeRecorder.init(node: pianoChordsSampler, fileDirectoryURL: outputURL, bus: 0, shouldCleanupRecordings: false)
//
try! recorder1.record()
try! recorder2.record()
try! recorder3.record()
// I start the playback here, record 20 seconds of it
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 20) {
recorder1.stop(); recorder1.audioFile?.close()
recorder2.stop(); recorder2.audioFile?.close()
recorder3.stop(); recorder3.audioFile?.close()
}
// RESULT: two .caf files instead of three, and the files have bass and piano mixed up