VoiceInput/Sources/Core/AudioLevelMonitor.swift
JOJO 1d9c9d5fa0 feat: macOS 菜单栏语音输入应用初始版本
- CGEvent cghidEventTap 全局监听 Fn 键
- Apple Speech 流式语音识别(zh-CN 默认)
- 实时音频 RMS 电平驱动波形动画
- NSPanel 胶囊浮动窗口
- 剪贴板 + Cmd+V 文本注入,CJK 输入法自动切换
- OpenAI 兼容 LLM 纠错(DeepSeek 预设)
- LSUIElement 菜单栏运行
- SPM + Makefile 构建
2026-04-24 16:17:57 +08:00

67 lines
2.0 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AVFAudio
import Foundation
/// AVAudioEngine RMS
final class AudioLevelMonitor {
private let engine = AVAudioEngine()
private var isRunning = false
/// RMS 0.0 ~ 1.0 1.0
var onLevelUpdate: ((Float) -> Void)?
func start() -> Bool {
guard !isRunning else { return true }
let inputNode = engine.inputNode
let format = inputNode.outputFormat(forBus: 0)
// tap
inputNode.installTap(onBus: 0, bufferSize: 1024, format: format) { [weak self] buffer, _ in
self?.processAudioBuffer(buffer)
}
do {
try engine.start()
isRunning = true
print("[AudioLevel] Started")
return true
} catch {
print("[AudioLevel] Failed to start engine: \(error)")
return false
}
}
func stop() {
guard isRunning else { return }
engine.inputNode.removeTap(onBus: 0)
engine.stop()
isRunning = false
print("[AudioLevel] Stopped")
}
private func processAudioBuffer(_ buffer: AVAudioPCMBuffer) {
guard let channelData = buffer.floatChannelData else { return }
let frameLength = Int(buffer.frameLength)
let channelCount = Int(buffer.format.channelCount)
// RMS
var sumSquares: Float = 0
for channel in 0..<channelCount {
let samples = channelData[channel]
for i in 0..<frameLength {
let sample = samples[i]
sumSquares += sample * sample
}
}
let rms = sqrt(sumSquares / Float(frameLength * channelCount))
// 15 使 1.0
let scaled = min(rms * 15.0, 1.5)
DispatchQueue.main.async { [weak self] in
self?.onLevelUpdate?(scaled)
}
}
}