1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
//
// 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)
}
}
}
}
|