3

I'm building a project about music and I am using the AudioKit framework. AudioKit is being very useful, but I need now to export sounds created in the app.

I'm using AKSequencer to play notes in sequence and I'm also applying some filters on the sound, like reverb.

I found a example code here that exports an audio from another audio file, but it is not what I need. Actually, I need to render the notes sequence, including the filters, and export it to the user.

My code:

class Sequencer {

    let oscBank = AKOscillatorBank(waveform: AKTable(AKTableType.positiveReverseSawtooth))//1
    let sequencer = AKSequencer()

    init() {
        setup()
    }

    func setup() {

        // I instantiate a MIDI node with the oscillator bank to put it in the track output.
        // It makes the oscillator's filters are recognized in the track output.
        let midiNode = AKMIDINode(node: oscBank)
        _ = sequencer.newTrack()
        sequencer.tracks[0].setMIDIOutput(midiNode.midiIn)

        generateSequence()

        // Here I'm applying some filter, a reverb in this case.
        let reverb = AKReverb(oscBank)
        reverb.loadFactoryPreset(.plate)

        // The Audiokit's output may be the last filter applyed. It is working to play.
        AudioKit.output = AKMixer(reverb)
        try? AudioKit.start()
    }

    func play() {
        sequencer.play()
    }

    func stop() {
        sequencer.stop()
    }


    /// Generates some melody (Sweet Child of Mine)
    func generateSequence() {
        for _ in 0..<2 {
            sequencer.tracks[0].add(noteNumber: 62, velocity: 127, position: AKDuration(beats: 0), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 74, velocity: 127, position: AKDuration(beats: 0.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 1), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 67, velocity: 127, position: AKDuration(beats: 1.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 79, velocity: 127, position: AKDuration(beats: 2), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 2.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 78, velocity: 127, position: AKDuration(beats: 3), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 3.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 62, velocity: 127, position: AKDuration(beats: 4), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 74, velocity: 127, position: AKDuration(beats: 4.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 67, velocity: 127, position: AKDuration(beats: 5.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 79, velocity: 127, position: AKDuration(beats: 6), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 6.5), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 78, velocity: 127, position: AKDuration(beats: 7), duration: AKDuration(beats: 0.5))
            sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 7.5), duration: AKDuration(beats: 0.5))
        }
    }


    // Here is my problem. I want to get the song and export it with the .wav extension exactly as it was played.
    // This code is just one of my many attempts and it also does not works.
    func saveFile() {
        guard let auxiliarPlayer = try? AKAudioPlayer(file: AKAudioFile(readFileName: "bass.wav")) else { return }
        guard let outputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("audio_file.m4a") else { return }
        guard let output = try? AKAudioFile(forWriting: outputURL, settings: auxiliarPlayer.audioFile.fileFormat.settings) else { return }

        try? AudioKit.renderToFile(output, seconds: sequencer.tracks[0].length, prerender: {
            self.sequencer.play()
        })
    }
}

Can someone please help?

1 Answer 1

1

As far as I know, offline rendering does not work with MIDI due to timing issues. Basically, only things that process audio can be rendered, not things that rely on MIDI. I believe it has to do with the MIDI clock remaining constant and not being able to be virtually "sped up" to match the audio rendering speed.

4
  • Aurelius, what do you suggest we do? This feature is essential in our project. Aug 28, 2018 at 18:00
  • Do not you have any suggestions? Sep 17, 2018 at 0:48
  • Someone will have to create a sequencer that is based on audio samples rather than midi timestamps for this to work. Sep 18, 2018 at 16:58
  • 1
    Does this problem remain unsolved? We need offline rendering of MIDI sequences in our project as well.
    – jbm
    Jul 16, 2019 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.