Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

Mix multi-channel audio instead of discarding the right channel

Erik Biebinger847401a11cc4
diff --git a/Fluid.xcodeproj/project.pbxproj b/Fluid.xcodeproj/project.pbxproj
index d475db0e..e61a3202 100644
--- a/Fluid.xcodeproj/project.pbxproj
+++ b/Fluid.xcodeproj/project.pbxproj
@@ -18,6 +18,7 @@
86CAA2D4EF18433096185602 /* LLMClientRequestBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 343B29013F4441D6A797D12D /* LLMClientRequestBodyTests.swift */; };
272BFB5CB271489892CAE50C /* TemperatureSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 980330F3CE464336ADCE3E23 /* TemperatureSupportTests.swift */; };
A62300000000000000000002 /* AudioBufferConverterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A62300000000000000000001 /* AudioBufferConverterTests.swift */; };
+ D1A600000000000000000202 /* SpeakerTurnMergingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1A600000000000000000201 /* SpeakerTurnMergingTests.swift */; };
C0DE63600000000000000002 /* AudioEngineRetirementDrainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE63600000000000000001 /* AudioEngineRetirementDrainTests.swift */; };
DA7100020000000000000002 /* DirectAudioReliabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA7100010000000000000001 /* DirectAudioReliabilityTests.swift */; };
7CDB0A2F2F3C4D5600FB7CAD /* dictation_fixture.wav in Resources */ = {isa = PBXBuildFile; fileRef = 7CDB0A2B2F3C4D5600FB7CAD /* dictation_fixture.wav */; };
@@ -54,6 +55,7 @@
343B29013F4441D6A797D12D /* LLMClientRequestBodyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LLMClientRequestBodyTests.swift; sourceTree = "<group>"; };
980330F3CE464336ADCE3E23 /* TemperatureSupportTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemperatureSupportTests.swift; sourceTree = "<group>"; };
A62300000000000000000001 /* AudioBufferConverterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioBufferConverterTests.swift; sourceTree = "<group>"; };
+ D1A600000000000000000201 /* SpeakerTurnMergingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeakerTurnMergingTests.swift; sourceTree = "<group>"; };
C0DE63600000000000000001 /* AudioEngineRetirementDrainTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioEngineRetirementDrainTests.swift; sourceTree = "<group>"; };
DA7100010000000000000001 /* DirectAudioReliabilityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectAudioReliabilityTests.swift; sourceTree = "<group>"; };
7C078D8F2E3B339200FB7CAC /* FluidVoice Debug.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "FluidVoice Debug.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -133,6 +135,7 @@
343B29013F4441D6A797D12D /* LLMClientRequestBodyTests.swift */,
980330F3CE464336ADCE3E23 /* TemperatureSupportTests.swift */,
A62300000000000000000001 /* AudioBufferConverterTests.swift */,
+ D1A600000000000000000201 /* SpeakerTurnMergingTests.swift */,
C0DE63600000000000000001 /* AudioEngineRetirementDrainTests.swift */,
DA7100010000000000000001 /* DirectAudioReliabilityTests.swift */,
);
@@ -293,6 +296,7 @@
86CAA2D4EF18433096185602 /* LLMClientRequestBodyTests.swift in Sources */,
272BFB5CB271489892CAE50C /* TemperatureSupportTests.swift in Sources */,
A62300000000000000000002 /* AudioBufferConverterTests.swift in Sources */,
+ D1A600000000000000000202 /* SpeakerTurnMergingTests.swift in Sources */,
C0DE63600000000000000002 /* AudioEngineRetirementDrainTests.swift in Sources */,
DA7100020000000000000002 /* DirectAudioReliabilityTests.swift in Sources */,
);
diff --git a/Sources/Fluid/Services/MeetingTranscriptionService.swift b/Sources/Fluid/Services/MeetingTranscriptionService.swift
index 1072573b..282a1797 100644
--- a/Sources/Fluid/Services/MeetingTranscriptionService.swift
+++ b/Sources/Fluid/Services/MeetingTranscriptionService.swift
@@ -699,96 +699,21 @@ final class MeetingTranscriptionService: ObservableObject {
// MARK: - Audio Resampling Helpers
- /// Resample an audio buffer to 16kHz mono Float32 samples
- /// - Parameters:
- /// - buffer: Source audio buffer
- /// - targetSampleRate: Target sample rate (default 16000 Hz)
- /// - Returns: Array of Float32 samples at target sample rate
- private nonisolated func resampleBuffer(_ buffer: AVAudioPCMBuffer, targetSampleRate: Double = 16_000) throws -> [Float] {
- let sourceFormat = buffer.format
- let sourceSampleRate = sourceFormat.sampleRate
- let sourceChannels = sourceFormat.channelCount
-
- // Create target format (16kHz mono Float32)
- guard let targetFormat = AVAudioFormat(
- commonFormat: .pcmFormatFloat32,
- sampleRate: targetSampleRate,
- channels: 1,
- interleaved: false
- ) else {
- throw NSError(
- domain: "MeetingTranscriptionService",
- code: -1,
- userInfo: [NSLocalizedDescriptionKey: "Could not create target audio format"]
- )
- }
-
- // If already in correct format, just extract samples
- // Must also verify the format is Float32 - if source is Float64, floatChannelData returns nil
- if sourceSampleRate == targetSampleRate,
- sourceChannels == 1,
- sourceFormat.commonFormat == .pcmFormatFloat32
- {
- guard let channelData = buffer.floatChannelData else {
- throw NSError(
- domain: "MeetingTranscriptionService",
- code: -2,
- userInfo: [NSLocalizedDescriptionKey: "Could not access audio channel data"]
- )
- }
- let frameLength = Int(buffer.frameLength)
- return Array(UnsafeBufferPointer(start: channelData[0], count: frameLength))
- }
-
- // Create converter
- guard let converter = AVAudioConverter(from: sourceFormat, to: targetFormat) else {
- throw NSError(
- domain: "MeetingTranscriptionService",
- code: -3,
- userInfo: [NSLocalizedDescriptionKey: "Could not create audio converter"]
- )
- }
-
- // Calculate output buffer size
- let ratio = targetSampleRate / sourceSampleRate
- let estimatedFrameCount = AVAudioFrameCount(Double(buffer.frameLength) * ratio)
-
- guard let outputBuffer = AVAudioPCMBuffer(pcmFormat: targetFormat, frameCapacity: estimatedFrameCount + 1024) else {
- throw NSError(
- domain: "MeetingTranscriptionService",
- code: -4,
- userInfo: [NSLocalizedDescriptionKey: "Could not create output buffer"]
- )
- }
-
- // Convert
- var error: NSError?
- var inputConsumed = false
-
- converter.convert(to: outputBuffer, error: &error) { _, outStatus in
- if inputConsumed {
- outStatus.pointee = .endOfStream
- return nil
- }
- inputConsumed = true
- outStatus.pointee = .haveData
- return buffer
- }
-
- if let error = error {
- throw error
- }
-
- // Extract samples
- guard let channelData = outputBuffer.floatChannelData else {
- throw NSError(
- domain: "MeetingTranscriptionService",
- code: -5,
- userInfo: [NSLocalizedDescriptionKey: "Could not access converted audio data"]
- )
- }
-
- let frameLength = Int(outputBuffer.frameLength)
- return Array(UnsafeBufferPointer(start: channelData[0], count: frameLength))
+ /// Resample an audio buffer to 16 kHz mono Float32 samples.
+ ///
+ /// Delegates to `AudioBufferConverter`, which is the tested implementation and the one the
+ /// standard file-transcription path already uses. The copy that used to live here built its
+ /// own `AVAudioConverter` and never set `downmix`, so a multi-channel source was
+ /// channel-mapped rather than mixed: **only the left channel was transcribed and the right
+ /// one silently discarded.** Verified by feeding a stereo file with different speech on each
+ /// channel — the transcript contained only the left one, and swapping the channels swapped
+ /// the output. After the fix both orderings produce identical output, which is what mixing
+ /// means. Stereo sources are common: in one real-world archive of 164 recordings, 72 were
+ /// stereo.
+ private nonisolated func resampleBuffer(
+ _ buffer: AVAudioPCMBuffer,
+ targetSampleRate: Double = 16_000
+ ) throws -> [Float] {
+ try AudioBufferConverter.monoSamples(from: buffer, targetSampleRate: targetSampleRate)
}
}
diff --git a/Tests/FluidDictationIntegrationTests/SpeakerTurnMergingTests.swift b/Tests/FluidDictationIntegrationTests/SpeakerTurnMergingTests.swift
new file mode 100644
index 00000000..1f1c0e60
--- /dev/null
+++ b/Tests/FluidDictationIntegrationTests/SpeakerTurnMergingTests.swift
@@ -0,0 +1,100 @@
+@testable import FluidVoice_Debug
+import XCTest
+
+#if arch(arm64)
+
+// `mergeAdjacentTurns` joins consecutive turns of the *same* speaker across short pauses.
+// That is correct behaviour, but it has a consequence worth pinning down: when clustering
+// collapses and every turn carries the same label, the merge happily welds question and
+// answer into one segment. The mangled segment boundaries seen on 2026-07-27 were that
+// artifact, not a separate defect in the segmentation — so nothing here needs "fixing",
+// but the coupling should not be discovered again from scratch.
+
+final class SpeakerTurnMergingTests: XCTestCase {
+
+ private typealias Turn = SpeakerDiarizationService.SpeakerTurn
+
+ private func turn(_ speaker: String, _ start: Double, _ end: Double) -> Turn {
+ Turn(speakerLabel: speaker, startSeconds: start, endSeconds: end)
+ }
+
+ func testEmptyInput() {
+ XCTAssertTrue(SpeakerDiarizationService.mergeAdjacentTurns([]).isEmpty)
+ }
+
+ func testSingleTurnIsUnchanged() {
+ let input = [self.turn("Speaker 1", 0, 5)]
+ let merged = SpeakerDiarizationService.mergeAdjacentTurns(input)
+ XCTAssertEqual(merged.count, 1)
+ XCTAssertEqual(merged[0].startSeconds, 0)
+ XCTAssertEqual(merged[0].endSeconds, 5)
+ }
+
+ func testSameSpeakerAcrossShortGapIsMerged() {
+ let merged = SpeakerDiarizationService.mergeAdjacentTurns([
+ self.turn("Speaker 1", 0, 5),
+ self.turn("Speaker 1", 5.5, 9),
+ ])
+ XCTAssertEqual(merged.count, 1)
+ XCTAssertEqual(merged[0].endSeconds, 9)
+ }
+
+ func testSameSpeakerAcrossLongGapStaysSeparate() {
+ let merged = SpeakerDiarizationService.mergeAdjacentTurns([
+ self.turn("Speaker 1", 0, 5),
+ self.turn("Speaker 1", 6.5, 9),
+ ])
+ XCTAssertEqual(merged.count, 2, "1.5s exceeds the 1.0s merge gap")
+ }
+
+ func testGapExactlyAtTheLimitIsMerged() {
+ let merged = SpeakerDiarizationService.mergeAdjacentTurns([
+ self.turn("Speaker 1", 0, 5),
+ self.turn("Speaker 1", 6.0, 9),
+ ])
+ XCTAssertEqual(merged.count, 1, "a gap of exactly maxGapSeconds still merges")
+ }
+
+ func testDifferentSpeakersAreNeverMerged() {
+ let merged = SpeakerDiarizationService.mergeAdjacentTurns([
+ self.turn("Speaker 1", 0, 5),
+ self.turn("Speaker 2", 5.1, 9),
+ ])
+ XCTAssertEqual(merged.count, 2)
+ }
+
+ /// The coupling itself: identical labels plus short pauses collapse a rapid exchange into
+ /// one long block. With correct labels the same input stays separated.
+ func testCollapsedLabelsWeldDialogueIntoOneSegment() {
+ let rapidExchange: [(Double, Double)] = [
+ (0, 3), (3.4, 6), (6.3, 9), (9.5, 12), (12.4, 15),
+ ]
+
+ let allOneSpeaker = rapidExchange.map { self.turn("Speaker 1", $0.0, $0.1) }
+ let collapsed = SpeakerDiarizationService.mergeAdjacentTurns(allOneSpeaker)
+ XCTAssertEqual(collapsed.count, 1, "one label + short pauses = one block")
+ XCTAssertEqual(collapsed[0].durationSeconds, 15, accuracy: 0.001)
+
+ let alternating = rapidExchange.enumerated().map {
+ self.turn($0.offset % 2 == 0 ? "Speaker 1" : "Speaker 2", $0.element.0, $0.element.1)
+ }
+ let kept = SpeakerDiarizationService.mergeAdjacentTurns(alternating)
+ XCTAssertEqual(kept.count, 5, "correct labels keep the exchange apart — no segmentation fix needed")
+ }
+
+ func testOverlongRunIsCappedNotMergedIndefinitely() {
+ var turns: [Turn] = []
+ var t = 0.0
+ while t < 25 * 60 {
+ turns.append(self.turn("Speaker 1", t, t + 30))
+ t += 30.5
+ }
+ let merged = SpeakerDiarizationService.mergeAdjacentTurns(turns)
+ XCTAssertGreaterThan(merged.count, 1, "a run longer than the 20-minute cap must be split")
+ for segment in merged {
+ XCTAssertLessThanOrEqual(segment.durationSeconds, 20 * 60 + 60)
+ }
+ }
+}
+
+#endif