问题
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