From 5aa1435739fa1cd150dd8cd8fb2fee5473d5ed3f Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Thu, 20 Mar 2025 11:55:33 +0000 Subject: gensin: generate sinusoid in u8,s8,u16,s16 formats --- Utils/FileWriter.swift | 112 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 5 deletions(-) (limited to 'Utils/FileWriter.swift') diff --git a/Utils/FileWriter.swift b/Utils/FileWriter.swift index 6330342..e4c3b7a 100644 --- a/Utils/FileWriter.swift +++ b/Utils/FileWriter.swift @@ -28,7 +28,7 @@ class FileWriter { print(dir+"/"+filename) if !filemgr.fileExists(atPath: filename) { print("Cant find file \(filename)") - throw FileReaderError.noFile + throw FileWriterError.noFile } fileHandle = FileHandle(forReadingAtPath: dir+"/"+filename) @@ -73,11 +73,113 @@ class FileWriter { } func writeAll(_ arr: [Float32]) throws { - //convert float to data - let databuf = arr.withUnsafeBytes { Data($0) } + + var byteArray = [UInt8](repeating: 0, count: 0) + var b1:Data = Data() + + byteArray.append(contentsOf: [0x11,0x22,0x33,0x44]) + + arr.withUnsafeBytes { + byteArray.append(contentsOf: $0) + } + + for i in 0...arr.count { + b1.append(contentsOf: [byteArray[i*4+0],byteArray[i*4+1],byteArray[i*4+2],byteArray[i*4+3]]) + } + + do { + print("try to write \(b1.count) bytes") + try fileHandle?.write(contentsOf: b1) + } catch { + print("Cant write to file \(self.filepath) : \(error)") + throw FileWriterError.writeError + } + } + + func writeAll(_ arr: [Int8]) throws { + var byteArray = [UInt8](repeating: 0, count: 0) + var b1:Data = Data() + + arr.withUnsafeBytes { + byteArray.append(contentsOf: $0) + } + + for i in 0..