How to save audio file with AudioKit?

牧云@^-^@ 提交于 2019-12-06 00:03:44

问题


I have audio file. I made some effects on it

let pitchshifter = AKPitchShifter(self.audioPlayer)
pitchshifter.shift = 10
AudioKit.output = pitchshifter

It works if I play it in app, but I want to save it as a new file to use it as avasset later. How it can be implemented?

I tried using AKNodeRecorder but this produces empty audio track:

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("recorded")
let format = AVAudioFormat(commonFormat: .pcmFormatFloat64, sampleRate: 44100, channels: 2, interleaved: false)!
let tape = try! AKAudioFile(forWriting: url, settings: format.settings)
let mixer = AKMixer(self.audioPlayer!, pitchshifter)
AudioKit.output = mixer
self.recorder = try! AKNodeRecorder(node: mixer, file: tape)

try? AudioKit.start()
self.audioPlayer?.play()
self.audioPlayer?.completionHandler = {
    self.recorder?.stop()
    self.selectedAudioURL = tape.url
}

Also I tried renderToFile method - it is also not working and I got this error

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("recorded.m4a")
let format = AVAudioFormat(commonFormat: .pcmFormatFloat64, sampleRate: 44100, channels: 2, interleaved: false)!
let tape = try! AKAudioFile(forWriting: url, settings: format.settings)
try! AudioKit.renderToFile(tape, seconds: Double(CMTimeGetSeconds(self.selectedAudioAsset!.duration)))

That's how I add initial file to AudioKit:

let audioFile = try AKAudioFile(forReading: audioURL, commonFormat: .pcmFormatFloat32, interleaved: true)
let player = AKPlayer(audioFile: audioFile)
self.audioFile = audioFile
self.audioPlayer = player
player.startTime = self.startTime
AKSettings.defaultToSpeaker = true

Thanks in advance for the answer!


回答1:


From what I found, you have to use the prerenderer to "play" what you want to record:

try AudioKit.renderToFile(outputFileForWriting, duration: sequencerTotalSeconds, prerender: {
    self.sequencer.play()
})

But, for me.. Rendering out a single track sequence produces a sinus sound. :-/

Hope this helps!



来源:https://stackoverflow.com/questions/50590012/how-to-save-audio-file-with-audiokit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!