-1

The question is how it is possible to save audio samples of an audio file into a text file. I have the special case that I have stored the samples in an array of UnsafeMutableRawBufferPointers and now I wonder how I can put them into a text file. In C++ I would have taken a stream operator here, however I am new to Swift and am wondering how to do this.

My code so far looks like this:

let Data = Array(UnsafeMutableRawBufferPointer(start:buffer.mData, count: Int(buffer.mDataByteSize))

3 Answers3

1

.... your buffer .....

let yourBuffer = Array(UnsafeMutableRawBufferPointer(start:buffer.mData, count: Int(buffer.mDataByteSize))
let dd = Data.withUnsafeBytes(yourBuffer)
let fileName = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!.appendingPathComponent("yourFile")
let result = try? dd.write(to: fileName)

This should create the file "yourFile" in your downloadsDirectory

Please let me know.

0

You already have code that creates an UnsafeMutableRawBufferPointer. Use that to create Data, and write that to disk:

let buffer = UnsafeRawBufferPointer(start:buffer.mData, count: Int(buffer.mDataByteSize)
let data = Data.withUnsafeBytes(buffer)

let fileURL = //the path to which you want to save

data.write(to: fileURL, options: []
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Then I get the error message: ´´´Cannot convert value of type `UnsafeMutableRawBufferPointer` to expected argument type `(UnsafeMutableRawBufferPointer) throws -> _`´´´ What does that mean? – Hannes Broschk Sep 16 '20 at 18:52
0

I solved the problem by refraining from the special case of using the array of pointers. If anyone else wants to write audio samples to a text file, this is how I approached the problem via AVAudioFile:

if FileManager.default.fileExists(atPath: getFileURL().path)
        {
            do{
                //Load Audio into Buffer and then write it down to a .txt
                let file = try AVAudioFile(forReading: getFileURL())
                let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
                
                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 was not able to be created")
                    throw NSError()}
                
                let arraySize = Int(buf.frameLength)
                print(arraySize, "Samples saved")
                let samples = Array(UnsafeBufferPointer(start:buf.floatChannelData![0],count:arraySize))
                //Array is going to be encoded and safe
                let encoded = try! JSONEncoder().encode(samples)
                try encoded.write(to: outputURL())
                print("Textfile created.\n")
             }catch {print(error)}
            
            
        }else {print("AudioFile is missing")}