Я строю проект о музыке и использую каркас AudioKit.AudioKit очень полезен, но теперь мне нужно экспортировать звуки, созданные в приложении.
Я использую AKSequencer для последовательного воспроизведения нот, а также применяю некоторые фильтры к звуку, например, реверберацию.
Я нашел пример кода здесь , который экспортирует аудио из другого аудиофайла, но это не то, что мне нужно.На самом деле мне нужно визуализировать последовательность заметок, включая фильтры, и экспортировать ее пользователю.
Мой код:
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()
})
}
}
Может кто-нибудь помочь, пожалуйста?