Repository Public License
1//2// ORIX Audio3// File: Core/Sources/ORIXAudioCore/Analysis/VoiceActivity.swift4// Description: Heuristic speech-activity detection (energy in the speech band over an adaptive noise floor) and pause statistics.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9import Foundation1011/// Everything in this type is a *heuristic estimate*. The detector reacts to energy in the 300–3400 Hz band; loud12/// non-speech sounds in that band are counted as activity. It is labelled as an estimate everywhere it is shown.13public struct SpeechActivityMetrics: Codable, Sendable, Equatable {14 public var segments: [TimeInterval2] = []15 public var pauses: [TimeInterval2] = []16 public var speakingTime: Double = 017 public var totalTime: Double = 018 public var speechRatio: Double { totalTime > 0 ? speakingTime / totalTime : 0 }19 public var meanPause: Double = 020 public var longestPause: Double = 021 /// Mean level of active segments, dBFS RMS (speech band).22 public var meanSegmentLevelDBFS: Float = Decibels.floor23 /// Estimated syllable-rate proxy: energy-envelope peaks per second of speaking time. Not a word count.24 public var envelopePeaksPerSecond: Double = 025 public init() {}2627 /// Words per minute of speaking time, when a transcript provides the word count. Still an estimate.28 public func wordsPerMinute(wordCount: Int) -> Double? { speakingTime >= 5 ? Double(wordCount) / speakingTime * 60 : nil }29}3031public struct VoiceActivityDetector {32 public var frameDuration = 0.0233 /// A frame is active when its speech-band level exceeds the noise floor by this margin…34 public var marginDB: Float = 935 /// …and is above this absolute level.36 public var absoluteFloorDBFS: Float = -5537 public var hangover = 0.2538 public var minimumSegment = 0.239 public var minimumPause = 0.34041 public let sampleRate: Double42 private var filter: BiquadCascade43 private let frameSize: Int44 private var acc = 0.0, accCount = 045 private var levels: [Float] = []4647 public init(sampleRate: Double) {48 self.sampleRate = sampleRate49 filter = BiquadCascade([.highPass(frequency: 300, sampleRate: sampleRate), .lowPass(frequency: 3400, sampleRate: sampleRate)])50 frameSize = max(1, Int(sampleRate * 0.02))51 }5253 public mutating func push(_ s: UnsafeBufferPointer<Float>) {54 for v in s {55 let y = filter.processSample(Double(v))56 acc += y * y; accCount += 157 if accCount == frameSize { levels.append(Decibels.fromPower(Float(acc / Double(frameSize)))); acc = 0; accCount = 0 }58 }59 }6061 public mutating func push(_ s: [Float]) { s.withUnsafeBufferPointer { push($0) } }6263 public func finish() -> SpeechActivityMetrics {64 var m = SpeechActivityMetrics()65 m.totalTime = Double(levels.count) * frameDuration66 guard levels.count > 5 else { return m }67 // Noise floor: 10th percentile of frame levels (robust to long silences and to continuous speech alike).68 let sorted = levels.sorted()69 let floorLevel = max(-100, sorted[sorted.count / 10])70 let threshold = max(absoluteFloorDBFS, floorLevel + marginDB)71 let hang = Int(hangover / frameDuration)72 var active = [Bool](repeating: false, count: levels.count)73 var countdown = 074 for i in levels.indices {75 if levels[i] > threshold { active[i] = true; countdown = hang }76 else if countdown > 0 { active[i] = true; countdown -= 1 }77 }78 var segments: [TimeInterval2] = []79 var start: Int?80 for i in 0...levels.count {81 let on = i < levels.count && active[i]82 if on, start == nil { start = i }83 if !on, let s = start {84 let seg = TimeInterval2(start: Double(s) * frameDuration, end: Double(i) * frameDuration)85 if seg.duration >= minimumSegment { segments.append(seg) }86 start = nil87 }88 }89 // Merge segments separated by less than the minimum pause.90 var merged: [TimeInterval2] = []91 for seg in segments {92 if let last = merged.last, seg.start - last.end < minimumPause { merged[merged.count - 1].end = seg.end } else { merged.append(seg) }93 }94 m.segments = merged95 m.speakingTime = merged.reduce(0) { $0 + $1.duration }96 if merged.count > 1 {97 m.pauses = zip(merged, merged.dropFirst()).map { TimeInterval2(start: $0.end, end: $1.start) }98 m.meanPause = m.pauses.reduce(0) { $0 + $1.duration } / Double(m.pauses.count)99 m.longestPause = m.pauses.map(\.duration).max() ?? 0100 }101 // Level and envelope peaks over active frames.102 var power = 0.0, frames = 0, peaks = 0103 for seg in merged {104 let a = Int(seg.start / frameDuration), b = min(levels.count, Int(seg.end / frameDuration))105 guard b > a else { continue }106 for i in a..<b { power += pow(10, Double(levels[i]) / 10); frames += 1 }107 // Smooth over 3 frames (60 ms) and count local maxima at least 3 dB above both neighbours 80 ms away.108 if b - a > 8 {109 for i in (a + 4)..<(b - 4) {110 let c = (levels[i - 1] + levels[i] + levels[i + 1]) / 3111 let l = (levels[i - 5 + 1] + levels[i - 4] + levels[i - 3]) / 3, r = (levels[i + 3] + levels[i + 4] + levels[i + 5 - 1]) / 3112 if c > l + 3 && c > r + 3 && levels[i] >= levels[i - 1] && levels[i] > levels[i + 1] { peaks += 1 }113 }114 }115 }116 if frames > 0 { m.meanSegmentLevelDBFS = Decibels.fromPower(Float(power / Double(frames))) }117 if m.speakingTime > 0 { m.envelopePeaksPerSecond = Double(peaks) / m.speakingTime }118 return m119 }120}121