I try to create single WAV file by portions.
Sapmles array with Floats generated by algorithm, and I can’t write as single file because too much RAM is used for Floats array.
I have not idea how to do that. Need just how I can write WAV by parts as AVAudioFile
buf is array with floats.
func saveWav(_ buf: [[Float]]) {
if let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48000, channels: 1, interleaved: false) {
let pcmBuf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(buf[0].count))
memcpy(pcmBuf?.floatChannelData?[0], buf[0], 4 * buf[0].count)
pcmBuf?.frameLength = UInt32(buf[0].count)
let fileManager = FileManager.default
do {
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
try FileManager.default.createDirectory(atPath: documentDirectory.path, withIntermediateDirectories: true, attributes: nil)
let fileURL = documentDirectory.appendingPathComponent("out0.wav")
print(fileURL.path)
let audioFile = try AVAudioFile(forWriting: fileURL, settings: format.settings)
try audioFile.write(from: pcmBuf!)
print("SAVE FILE DONE")
} catch {
print(error)
}
}
New contributor
D P is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.