summaryrefslogtreecommitdiff
path: root/Utils
diff options
context:
space:
mode:
authorArturs Artamonovs <arturs.artamonovs@protonmail.com>2025-02-03 22:31:45 +0000
committerArturs Artamonovs <arturs.artamonovs@protonmail.com>2025-02-03 22:31:45 +0000
commit972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c (patch)
treee96c4ce2d649558cc0b7b452d7a64fbe980b65c0 /Utils
parent1e096e55ca30dc80c3faa8d1ea36de13bc90cc6a (diff)
downloadPrySDR-972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c.tar.gz
PrySDR-972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c.zip
iqconvert: converts from u8 to f32 without issuesmain
Diffstat (limited to 'Utils')
-rw-r--r--Utils/Double+truncate.swift26
-rw-r--r--Utils/FileReader.swift2
-rw-r--r--Utils/FileWriter.swift91
-rw-r--r--Utils/Version.swift2
4 files changed, 120 insertions, 1 deletions
diff --git a/Utils/Double+truncate.swift b/Utils/Double+truncate.swift
new file mode 100644
index 0000000..b86d51d
--- /dev/null
+++ b/Utils/Double+truncate.swift
@@ -0,0 +1,26 @@
+//
+// Double+truncate.swift
+// PrySDR
+//
+// Created by Jacky Jack on 23/01/2025.
+//
+
+import Foundation
+
+extension Double {
+ func truncate(places : Int)-> Double {
+ return Double(floor(Foundation.pow(10.0, Double(places)) * self)/Foundation.pow(10.0, Double(places)))
+ }
+}
+
+extension Float32 {
+ func truncate(places : Int)-> Float32 {
+ return Float32(floor(Foundation.pow(10.0, Float32(places)) * self)/Foundation.pow(10.0, Float32(places)))
+ }
+}
+
+extension Float32 {
+ var bytes: [UInt8] {
+ withUnsafeBytes(of: self, Array.init)
+ }
+}
diff --git a/Utils/FileReader.swift b/Utils/FileReader.swift
index d4be92d..1f9c561 100644
--- a/Utils/FileReader.swift
+++ b/Utils/FileReader.swift
@@ -143,6 +143,8 @@ class FileReader {
}
}
+
+
func close() {
fileHandle?.closeFile()
}
diff --git a/Utils/FileWriter.swift b/Utils/FileWriter.swift
new file mode 100644
index 0000000..6330342
--- /dev/null
+++ b/Utils/FileWriter.swift
@@ -0,0 +1,91 @@
+//
+// FileWriter.swift
+// PrySDR
+//
+// Created by Jacky Jack on 21/01/2025.
+//
+
+import Foundation
+
+enum FileWriterError: Error {
+ case noFile
+ case writeError
+ case createError
+}
+
+class FileWriter {
+ var filepath: String?
+ let filemgr = FileManager.default
+ var fileHandle: FileHandle?
+
+ init() {
+
+ }
+
+ func openForRead(filename: String) throws {
+ let dir = getCurrentExecutableDir()
+ self.filepath = dir+"/"+filename
+ print(dir+"/"+filename)
+ if !filemgr.fileExists(atPath: filename) {
+ print("Cant find file \(filename)")
+ throw FileReaderError.noFile
+ }
+
+ fileHandle = FileHandle(forReadingAtPath: dir+"/"+filename)
+ if (fileHandle == nil) {
+ print("FileHandler failed")
+ }
+ }
+
+ func createForWrite(filename: String) throws {
+ let dir = getCurrentExecutableDir()
+
+ self.filepath = dir+"/"+filename
+
+ if !filemgr.fileExists(atPath: filename) {
+ filemgr.createFile(atPath: dir+"/"+filename, contents: nil)
+ }
+ fileHandle = FileHandle(forUpdatingAtPath: dir+"/"+filename)
+ if (fileHandle == nil) {
+ print("FileHandler failed")
+ throw FileWriterError.createError
+ }
+ }
+
+ func deleteForWrite(filename: String) throws {
+ let dir = getCurrentExecutableDir()
+
+ self.filepath = dir+"/"+filename
+
+ if filemgr.fileExists(atPath: filename) {
+ do {
+ try filemgr.removeItem(atPath: filename)
+ filemgr.createFile(atPath: dir+"/"+filename, contents: nil)
+ } catch {
+ print("couldnt delete file to write: \(error)")
+ }
+ }
+ fileHandle = FileHandle(forWritingAtPath: dir+"/"+filename)
+ if (fileHandle == nil) {
+ print("FileHandler failed")
+ throw FileWriterError.createError
+ }
+ }
+
+ func writeAll(_ arr: [Float32]) throws {
+ //convert float to data
+ let databuf = arr.withUnsafeBytes { Data($0) }
+ do {
+ print("try to write \(databuf.count) bytes")
+ try fileHandle?.write(contentsOf: databuf)
+ } catch {
+ print("Cant write to file \(self.filepath) : \(error)")
+ throw FileWriterError.writeError
+ }
+ }
+
+ func close() {
+ fileHandle?.closeFile()
+ }
+
+}
diff --git a/Utils/Version.swift b/Utils/Version.swift
index e9283d3..accda99 100644
--- a/Utils/Version.swift
+++ b/Utils/Version.swift
@@ -5,4 +5,4 @@
// Created by Jacky Jack on 02/12/2024.
//
-public let software_version = "2025.1-1"
+public let software_version = "2025.2-1"