Cotton code
40 lines · 2.0 KB · SwiftRaw
import AVFoundationimport XCTest@testable import CottonUI/// The microphone opens at the tap and holds what it hears until the model/// can take it (lane voiceline, 16 Oct 2026, the owner's phone on 286: "Nothing/// came through the microphone, so there was nothing to keep." — the tap/// waited on a model built cold after his own model was compiled again, while/// the button already said it was listening, and the words said in that gap/// were never heard).final class DictationHeldAudioTests: XCTestCase {    private final class Received: RecognitionBuffering {        var first: [Float] = []        func append(_ buffer: AVAudioPCMBuffer) { first.append(buffer.floatChannelData![0][0]) }    }    private func buffer(_ value: Float) -> AVAudioPCMBuffer {        let format = AVAudioFormat(standardFormatWithSampleRate: 16000, channels: 1)!        let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 160)!        buffer.frameLength = 160        for frame in 0 ..< 160 { buffer.floatChannelData![0][frame] = value }        return buffer    }    /// RULE: every buffer the microphone hears between the tap and the model being ready reaches the model first, in the order it was heard, as it was heard (a copy — the engine reuses its own buffer), and every later buffer follows it straight through — a tap followed by speech never loses the speech to the model's warm-up (voiceline, 16 Oct 2026).    func testWhatIsHeardBeforeTheModelReachesItFirstInOrder() {        let held = HeldAudio()        let reused = buffer(1)        held.append(reused)        // The engine writes its next audio into the same buffer.        for frame in 0 ..< 160 { reused.floatChannelData![0][frame] = 9 }        held.append(buffer(2))        let model = Received()        let caught = held.attach(model)        XCTAssertEqual(caught.buffers, 2)        XCTAssertEqual(caught.seconds, 0.02, accuracy: 0.0001)        held.append(buffer(3))        XCTAssertEqual(model.first, [1, 2, 3], "held audio first, as heard, then the live audio")    }}