2

AudioKit用来允许用户选择 MIDI 和 WAV 文件的多种组合。对于用户选择的每个组合,我使用AKSequencerAKMIDISampler为每对文件播放 MIDI 和 WAV。

结果是所有曲目都按照我想要的方式一起播放。

问题是我想将该混合保存为文件。

我试过使用AKOfflineRenderNode,但我只得到第一个音符,之后它只会发出哔哔声,直到指定的持续时间结束。

这是我到目前为止的示例代码:

func saveMixToDisk() {
    do {
        AudioKit.output = offlineRender

        for track in tracks {
            let trackSampler = AKMIDISampler()
            try trackSampler.loadWav(track.instrument.fileName)
            trackSampler.connect(to: mixer)

            let sequencer = AKSequencer(filename: track.midi.fileName)
            sequencer.setTempo(Double(tempo))
            sequencer.setRate(rate)
            sequencer.setGlobalMIDIOutput(trackSampler.midiIn)
            sequencer.enableLooping()
            offlineSequencers.append(sequencer)

            trackSampler >>> offlineRender
        }

        AudioKit.start()

        let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let destinationAudioFile = docs.appendingPathComponent("\(UUID().uuidString).m4a")

        offlineRender.internalRenderEnabled = false
        for sequencer in offlineSequencers {
            sequencer.play()
        }

        try offlineRender.renderToURL(destinationAudioFile, seconds: offlineSequencers.first!.length.seconds)
        for sequencer in offlineSequencers {
            sequencer.stop()
        }
        offlineRender.internalRenderEnabled = true
    } catch let error {
        print(error)
    }
}

非常感谢任何帮助!

谢谢

4

0 回答 0