First of all: I really want to save the array as .txt
file. .plist
is out of the question.
The reason: Afterwards I want to save it to a vector using C++. Realizing this with a wrapper already drove me crazy.
Here is my code:
guard let buf = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: AVAudioFrameCount(file.length))
else{ throw NSError()}
try file.read(into: buf)
guard buf.floatChannelData != nil else{print("Channel Buffer konnte nicht erstellt werden")
throw NSError()}
let samples = Array(UnsafeBufferPointer(start:buf.floatChannelData![0],count:Int(buf.frameLength)))
var wData = Data(samples)
try! wData.write(to: outputURL())
But it is not possible to initialize Data with Float values, or is it?
How can I write an Array of float Values into a .txt
file?
The answer to the question itself (save it as txt), you can save your array of floats as a JSON string. It should be pretty straight forward to encode and decode it using JSONEncoder/JSONDecoder.
"But it is not possible to initialize Data with Float values, or is it?"
Yes it if definitely possible to convert an Array of Float to Data/Bytes and read it back. It is also preferred to preserve the exact values avoiding the conversion to string: