@@ -103,9 +103,16 @@ final class AudioEngine: @unchecked Sendable {
103103 case . setSystemVolume ( let value ) :
104104 let device = DeviceCatalog . defaultOutput ( )
105105 DeviceCatalog . setVolume ( value , of : device , scope : kAudioObjectPropertyScopeOutput )
106- case . setSystemMuted ( let muted ) :
107- let device = DeviceCatalog . defaultOutput ( )
108- DeviceCatalog . setMuted ( muted , of : device , scope : kAudioObjectPropertyScopeOutput )
106+ case . setOutputMuted ( let uid , let muted ) :
107+ // Mix mutes a device by silencing the audio it sends there, never
108+ // with the device's own mute: that one silenced every device at
109+ // once. Unmuting still clears a hardware mute, so the button always
110+ // brings the sound back whoever muted it.
111+ persistence . setOutputMuted ( muted , uid : uid )
112+ if ! muted , let device = DeviceCatalog . device ( uid : uid ) ,
113+ DeviceCatalog . isMuted ( device , scope : kAudioObjectPropertyScopeOutput ) {
114+ DeviceCatalog . setMuted ( false , of : device , scope : kAudioObjectPropertyScopeOutput )
115+ }
109116 case . setDefaultOutput ( let uid ) :
110117 outputArrivals . removeValue ( forKey : uid )
111118 try? DeviceCatalog . setDefaultOutput ( uid : uid )
@@ -191,7 +198,7 @@ final class AudioEngine: @unchecked Sendable {
191198 for ( bundleID , processes ) in grouped where routes [ bundleID ] == nil && ! MixIdentity . isCallApp ( bundleID ) {
192199 guard let playing = processes . first ( where : \. isPlayingOutput ) else { continue }
193200 let route = TapRoute ( bundleID : bundleID , volume : 1 , muted : false , outputUID : nil )
194- route . duckUID = ProcessCatalog . outputDeviceUID ( of : playing )
201+ route . heldUID = ProcessCatalog . outputDeviceUID ( of : playing )
195202 routes [ bundleID ] = route
196203 }
197204 for route in routes . values where ! MixIdentity . isCallApp ( route . bundleID ) {
@@ -200,6 +207,9 @@ final class AudioEngine: @unchecked Sendable {
200207 }
201208 }
202209
210+ let mutedUIDs = persistence . mutedOutputUIDs
211+ applyOutputMutes ( mutedUIDs , grouped : grouped , systemUID : systemUID )
212+
203213 for route in routes . values {
204214 let processes = grouped [ route . bundleID ] ?? [ ]
205215 rebuild ( route : route , processes : processes , systemUID : systemUID )
@@ -221,13 +231,15 @@ final class AudioEngine: @unchecked Sendable {
221231 systemOutput : systemOut ,
222232 systemInput : systemIn ,
223233 systemVolume : DeviceCatalog . volume ( of : systemOutID , scope : kAudioObjectPropertyScopeOutput ) ,
224- systemMuted : DeviceCatalog . isMuted ( systemOutID , scope : kAudioObjectPropertyScopeOutput ) ,
234+ systemMuted : mutedUIDs . contains ( systemUID )
235+ || DeviceCatalog . isMuted ( systemOutID , scope : kAudioObjectPropertyScopeOutput ) ,
236+ mutedOutputUIDs : mutedUIDs ,
225237 systemInputVolume : DeviceCatalog . hasInput ( systemInID )
226238 ? DeviceCatalog . volume ( of : systemInID , scope : kAudioObjectPropertyScopeInput )
227239 : nil ,
228240 outputs : DeviceCatalog . outputsForPicker ( ) ,
229241 inputs : DeviceCatalog . inputs ( ) ,
230- apps : makeRows ( grouped : grouped , systemOut : systemOut ) ,
242+ apps : makeRows ( grouped : grouped , systemOut : systemOut , mutedUIDs : mutedUIDs ) ,
231243 permissionGranted : ! tapDenied
232244 )
233245 }
@@ -269,6 +281,42 @@ final class AudioEngine: @unchecked Sendable {
269281 arrivals . removeValue ( forKey : uid )
270282 }
271283
284+ /// Muting a device taps every app playing to it and plays it back at zero,
285+ /// the same way ducking works, so apps on other devices never notice.
286+ /// An app already routed by Mix just has its gain dropped. Routes that
287+ /// exist only for the mute go away once their device is unmuted, or once
288+ /// the app has no audio processes left.
289+ private func applyOutputMutes ( _ mutedUIDs : Set < String > , grouped : [ String : [ AudioProcess ] ] , systemUID : String ) {
290+ if ! mutedUIDs . isEmpty {
291+ for ( bundleID , processes ) in grouped where routes [ bundleID ] == nil {
292+ guard let playing = processes . first ( where : \. isPlayingOutput ) else { continue }
293+ let playingUID = ProcessCatalog . outputDeviceUID ( of : playing ) ?? systemUID
294+ guard mutedUIDs . contains ( playingUID ) else { continue }
295+ let route = TapRoute ( bundleID : bundleID , volume : 1 , muted : false , outputUID : nil )
296+ // An app on the system output keeps following it, so switching
297+ // the system output away from a muted device lets it play.
298+ if playingUID != systemUID { route . heldUID = playingUID }
299+ route . muteHold = true
300+ routes [ bundleID ] = route
301+ }
302+ }
303+ for ( bundleID , route ) in routes {
304+ let destination = resolvedUID ( for : route , systemUID : systemUID )
305+ let muted = mutedUIDs . contains ( destination )
306+ route . setDeviceMuted ( muted )
307+ guard route . muteHold else { continue }
308+ let alive = ! ( grouped [ bundleID ] ?? [ ] ) . isEmpty
309+ if ! muted || ! alive {
310+ route . muteHold = false
311+ if ! route . duckHold { route . heldUID = nil }
312+ if ! route . needsAudio {
313+ route . teardown ( )
314+ routes . removeValue ( forKey : bundleID )
315+ }
316+ }
317+ }
318+ }
319+
272320 private func rebuildRoute ( bundleID : String ) {
273321 let grouped = ProcessCatalog . grouped ( )
274322 guard let route = routes [ bundleID ] else { return }
@@ -295,20 +343,22 @@ final class AudioEngine: @unchecked Sendable {
295343 }
296344
297345 private func resolvedUID ( for route : TapRoute , systemUID : String ) -> String {
298- guard let wanted = route . outputUID ?? route . duckUID else { return systemUID }
346+ guard let wanted = route . outputUID ?? route . heldUID else { return systemUID }
299347 if let device = DeviceCatalog . device ( uid : wanted ) , DeviceCatalog . isAlive ( device ) {
300348 return wanted
301349 }
302350 return systemUID
303351 }
304352
305- private func makeRows ( grouped : [ String : [ AudioProcess ] ] , systemOut : DeviceInfo ) -> [ AppRow ] {
353+ private func makeRows ( grouped : [ String : [ AudioProcess ] ] , systemOut : DeviceInfo , mutedUIDs : Set < String > ) -> [ AppRow ] {
306354 let saved = persistence . snapshot . apps
307355 // Apps that actually have audio right now, plus anything Mix is already
308356 // routing. A saved route for an app that is not running stays on disk
309357 // and comes back with the app; it never gets a row of its own here.
310358 var bundleIDs = Set ( grouped . filter { $0 . value . contains ( where : \. isPlayingOutput ) } . keys )
311- bundleIDs . formUnion ( routes . keys )
359+ // A route held only for a device mute is not something the user set,
360+ // so a paused app does not linger in the list because of it.
361+ bundleIDs . formUnion ( routes . filter { ! $0 . value . isMuteHoldOnly } . keys )
312362
313363 return bundleIDs . sorted { ProcessCatalog . displayName ( bundleID : $0 ) < ProcessCatalog . displayName ( bundleID : $1 ) } . map { bundleID in
314364 let route = routes [ bundleID ]
@@ -337,6 +387,7 @@ final class AudioEngine: @unchecked Sendable {
337387 outputUID : outputUID ,
338388 destinationName : dest . name ,
339389 destinationIsFallback : fallback ,
390+ destinationMuted : mutedUIDs . contains ( dest . uid ) ,
340391 error : route ? . error
341392 )
342393 }
@@ -369,7 +420,7 @@ final class AudioEngine: @unchecked Sendable {
369420 private func releaseDuck ( ) {
370421 for ( bundleID , route ) in routes where route . duckHold {
371422 route . duckHold = false
372- route . duckUID = nil
423+ if ! route . muteHold { route . heldUID = nil }
373424 if ! route . needsAudio {
374425 route . teardown ( )
375426 routes . removeValue ( forKey : bundleID )
0 commit comments