I am trying to load a song, mix it with filters and save it to file. However I cannot manage to get it work with AudioKit.renderToFile. I always get "no such file" after rendering.
My code is very similar to this.
if let fileUrl = Bundle.main.path(forResource: "song", ofType: "wav") {
export()
}
Rendering:
func export(songUrl: String) {
do {
if let url = URL(string: songUrl) {
if let file = try? AKAudioFile(forReading: url) {
player = try AKAudioPlayer(file: file)
}
}
} catch {
fatalError("PLAYER URL ERROR")
}
mainMixer = AKMixer(player)
AudioKit.output = mainMixer
do {
try AudioKit.start()
self.outputFile = try AVAudioFile(forWriting: exportURL, settings: player.audioFile.fileFormat.settings)
try AudioKit.renderToFile(self.outputFile, duration: self.player.duration, prerender: {
self.player.play()
})
} catch {
fatalError("Unexpected error: \(error).")
}
self.showFileSize()
}
Export URL:
let exportURL: URL = {
let documentsURL = FileManager.default.temporaryDirectory
return documentsURL.appendingPathComponent("exported_song.wav")
}()
showFileSize uses FileManager.default.attributesOfItem(atPath: filePath) but here it throws an exception:
The file “exported_song.wav” couldn’t be opened because there is no such file.
What I am doing wrong here? Thanks in advance!