Files
sst/Sources/SstProbe/TranscribeProbe.swift

39 lines
1.5 KiB
Swift

import Foundation
import SstWhisper
func runTranscribeProbe() async {
print("Model laden (eerste keer: download ≈1.5 GB)…")
let transcriber = WhisperKitTranscriber()
do {
try await transcriber.load()
} catch {
print("mislukt:", error.localizedDescription)
return
}
print("Model klaar.")
for i in [3, 2, 1] {
print("\(i)...")
try? await Task.sleep(for: .seconds(1))
}
guard let samples = await recordSamples(seconds: 8) else { return }
let peak = samples.map { abs($0) }.max() ?? 0
print("samples: \(samples.count), piek: \(peak)")
let perSecond = stride(from: 0, to: samples.count, by: 16_000).map { start -> String in
let chunk = samples[start..<min(start + 16_000, samples.count)]
let rms = (chunk.reduce(0) { $0 + $1 * $1 } / Float(chunk.count)).squareRoot()
return String(format: "%.3f", rms)
}
print("RMS per seconde:", perSecond.joined(separator: " "))
do {
let vocabulary = (ProcessInfo.processInfo.environment["SST_VOCAB"] ?? "")
.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }
if !vocabulary.isEmpty { print("voorrang: \(vocabulary.joined(separator: ", "))") }
let started = Date.now
let text = try await transcriber.transcribe(samples, vocabulary: vocabulary)
let elapsed = Date.now.timeIntervalSince(started)
print(String(format: "(%.2f s) ", elapsed) + text)
} catch {
print("mislukt:", error.localizedDescription)
}
}