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

Use CoreAudio system volume for independent volume and add warning footnote

Bezhana4b2bc4d51f8
diff --git a/Sources/Fluid/Services/TranscriptionSoundPlayer.swift b/Sources/Fluid/Services/TranscriptionSoundPlayer.swift
index b0476a3f..5debb1f2 100644
--- a/Sources/Fluid/Services/TranscriptionSoundPlayer.swift
+++ b/Sources/Fluid/Services/TranscriptionSoundPlayer.swift
@@ -7,6 +7,7 @@ final class TranscriptionSoundPlayer {
static let shared = TranscriptionSoundPlayer()
private var players: [String: AVAudioPlayer] = [:]
+ private var savedSystemVolume: Float?
private init() {}
@@ -38,15 +39,12 @@ final class TranscriptionSoundPlayer {
let settings = SettingsStore.shared
let desiredVolume = overrideVolume ?? settings.transcriptionSoundVolume
- let playerVolume: Float
if settings.transcriptionSoundIndependentVolume {
- let systemVol = Self.getSystemVolume()
- // If muted, don't play
- guard systemVol > 0.001 else { return }
- // Compensate for system volume so the sound is always at the desired absolute level
- playerVolume = min(1.0, desiredVolume / systemVol)
- } else {
- playerVolume = desiredVolume
+ let currentSystemVol = Self.getSystemVolume()
+ guard currentSystemVol > 0.001 else { return }
+ // Save current system volume and temporarily set it to desired level
+ self.savedSystemVolume = currentSystemVol
+ Self.setSystemVolume(desiredVolume)
}
do {
@@ -60,9 +58,27 @@ final class TranscriptionSoundPlayer {
}
player.currentTime = 0
- player.volume = playerVolume
+ if settings.transcriptionSoundIndependentVolume {
+ player.volume = 1.0
+ } else {
+ player.volume = desiredVolume
+ }
player.play()
+
+ // Restore system volume after the sound finishes
+ if settings.transcriptionSoundIndependentVolume, let saved = self.savedSystemVolume {
+ let duration = player.duration
+ DispatchQueue.main.asyncAfter(deadline: .now() + duration + 0.05) { [weak self] in
+ Self.setSystemVolume(saved)
+ self?.savedSystemVolume = nil
+ }
+ }
} catch {
+ // Restore system volume on error
+ if let saved = self.savedSystemVolume {
+ Self.setSystemVolume(saved)
+ self.savedSystemVolume = nil
+ }
DebugLogger.shared.error(
"Failed to play sound \(soundName).m4a: \(error.localizedDescription)",
source: "TranscriptionSoundPlayer"
@@ -70,7 +86,7 @@ final class TranscriptionSoundPlayer {
}
}
- // MARK: - System Volume Reading via CoreAudio
+ // MARK: - System Volume via CoreAudio
private static func getDefaultOutputDeviceID() -> AudioObjectID? {
var address = AudioObjectPropertyAddress(
@@ -101,4 +117,19 @@ final class TranscriptionSoundPlayer {
guard status == noErr else { return 1.0 }
return volume
}
+
+ private static func setSystemVolume(_ volume: Float) {
+ guard let deviceID = getDefaultOutputDeviceID() else { return }
+ var address = AudioObjectPropertyAddress(
+ mSelector: kAudioHardwareServiceDeviceProperty_VirtualMainVolume,
+ mScope: kAudioDevicePropertyScopeOutput,
+ mElement: kAudioObjectPropertyElementMain
+ )
+ var vol = Float32(max(0, min(1, volume)))
+ let size = UInt32(MemoryLayout<Float32>.size)
+ let status = AudioObjectSetPropertyData(deviceID, &address, 0, nil, size, &vol)
+ if status != noErr {
+ DebugLogger.shared.error("Failed to set system volume: OSStatus \(status)", source: "TranscriptionSoundPlayer")
+ }
+ }
}
diff --git a/Sources/Fluid/UI/SettingsView.swift b/Sources/Fluid/UI/SettingsView.swift
index 2544f983..7ac02639 100644
--- a/Sources/Fluid/UI/SettingsView.swift
+++ b/Sources/Fluid/UI/SettingsView.swift
@@ -244,6 +244,7 @@ struct SettingsView: View {
self.settingsToggleRow(
title: "Independent Volume",
description: "Sound volume stays constant regardless of system volume. Mute is still respected.",
+ footnote: "Temporarily changes system volume during playback, which may briefly affect other audio.",
isOn: Binding(
get: { SettingsStore.shared.transcriptionSoundIndependentVolume },
set: { SettingsStore.shared.transcriptionSoundIndependentVolume = $0 }