From a0d12ecbac8fe327d1dcd4580fee594e24d4191b Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Mon, 6 Jan 2025 12:30:49 +0000 Subject: Waterfall: UI drawing from file --- WaterfallFile_UI/FileSpectrum.swift | 55 +++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) (limited to 'WaterfallFile_UI/FileSpectrum.swift') diff --git a/WaterfallFile_UI/FileSpectrum.swift b/WaterfallFile_UI/FileSpectrum.swift index 6415dfa..ce64836 100644 --- a/WaterfallFile_UI/FileSpectrum.swift +++ b/WaterfallFile_UI/FileSpectrum.swift @@ -13,8 +13,15 @@ import CoreImage class FileSpectrum: NSObject, ObservableObject { - let default_width = 512 - let default_height = 512 + static let default_width = 512 + static let default_height = 512 + static let defaultSleepTime:UInt32 = 1 + var offset:Int=0 + let fileReader = FileReader() + var dataBuffer:[Int8] = .init(repeating: 0, count: FileSpectrum.default_width) + var allData:[Int8]? = nil + let fft512 = NaiveFFT512() + var line_counter:Int = 0 let sessionQueue = DispatchQueue(label: "sessionQueue", attributes: [], @@ -22,7 +29,7 @@ class FileSpectrum: NSObject, ObservableObject { @Published var outputImage = emptyCGImage - let simpleImage = SimpleImage(width: 256, height: 256) + let simpleImage = SimpleImage(width: 512, height: 512) /// A 1x1 Core Graphics image. static var emptyCGImage: CGImage = { @@ -40,24 +47,62 @@ class FileSpectrum: NSObject, ObservableObject { return buffer.makeCGImage(cgImageFormat: fmt!)! }() + + init(_ input_filename: String) { + //get data from u8 file + + do { + try fileReader.open(filename: input_filename) + } catch { + print("Cant open file \(input_filename)") + exit(0) + } + var i8_arr:[Int8]? = nil + do { + try i8_arr = fileReader.readAll() + allData = i8_arr + } catch { + print("Got error \(error)") + } + + fileReader.close() + } func makeSpectrogramImage() -> NSImage { return NSImage(cgImage: FileSpectrum.emptyCGImage, size: .zero) } + + //just get data time to time from databuffer and pretend to be realtime data + func dataFileProcessor() { + //all now is about 512pixels + let processingSlice = allData![offset...offset+FileSpectrum.default_width-1] + let dataBuffer = Array(processingSlice) + var transform_result:[Float] = .init(repeating: 0.0, count: FileSpectrum.default_width) + //DispatchQueue.main.async { + transform_result = self.fft512.computeLine(dataBuffer) + self.simpleImage.drawPalletLine(line: self.line_counter, pixelLine: transform_result) + if self.offset+FileSpectrum.default_width < allData!.count { + self.offset += FileSpectrum.default_width + self.line_counter += 1 + } + //} + + } func startRunning() { sessionQueue.async { print("lets start the task for spectrum analysis") - for i in 0..<100 { + for i in 0..<512 { print("Process the task \(i)") self.simpleImage.drawPixel(i, i, PixelData(a: 255, r: 255, g: 0, b: 0)) DispatchQueue.main.async { + self.dataFileProcessor() self.outputImage = self.simpleImage.toCGImage()! } - sleep(1) + sleep(FileSpectrum.defaultSleepTime) } } } -- cgit v1.2.3