// // FileSpectrum.swift // PrySDR // // Created by Jacky Jack on 01/01/2025. // import Accelerate import AppKit import Combine import CoreImage class FileSpectrum: NSObject, ObservableObject { let default_width = 512 let default_height = 512 let sessionQueue = DispatchQueue(label: "sessionQueue", attributes: [], autoreleaseFrequency: .workItem) @Published var outputImage = emptyCGImage let simpleImage = SimpleImage(width: 256, height: 256) /// A 1x1 Core Graphics image. static var emptyCGImage: CGImage = { let buffer = vImage.PixelBuffer( pixelValues: [0], size: .init(width: 1, height: 1), pixelFormat: vImage.Planar8.self) let fmt = vImage_CGImageFormat( bitsPerComponent: 8, bitsPerPixel: 8 , colorSpace: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue), renderingIntent: .defaultIntent) return buffer.makeCGImage(cgImageFormat: fmt!)! }() func makeSpectrogramImage() -> NSImage { return NSImage(cgImage: FileSpectrum.emptyCGImage, size: .zero) } func startRunning() { sessionQueue.async { print("lets start the task for spectrum analysis") for i in 0..<100 { print("Process the task \(i)") self.simpleImage.drawPixel(i, i, PixelData(a: 255, r: 255, g: 0, b: 0)) DispatchQueue.main.async { self.outputImage = self.simpleImage.toCGImage()! } sleep(1) } } } }