@@ -16,6 +16,8 @@ public class AudioHandler
16
16
private readonly Dictionary < IDisposable , CoreAudioDevice > audioDeviceSubscriptions = new Dictionary < IDisposable , CoreAudioDevice > ( ) ;
17
17
public CoreAudioController AudioController { get => audioController ; set => audioController = value ; }
18
18
public int FriendlyVolume { get => audioController . DefaultPlaybackDevice != null ? ( int ) Math . Round ( audioController . DefaultPlaybackDevice . Volume ) : - 1 ; }
19
+ public List < CoreAudioDevice > AudioDevices { get => AudioController . GetPlaybackDevicesAsync ( DeviceState . Active ) . Result . ToList ( ) ; }
20
+
19
21
public bool IsAudioAvailable ( )
20
22
{
21
23
if ( AudioController == null )
@@ -56,7 +58,7 @@ public async Task AdjustVolume(MouseWheelDirection direction, ActionTriggerType
56
58
if ( type == ActionTriggerType . PreciseVolumeScroll )
57
59
newVolume += Settings . Default . PreciseVolumeStep ;
58
60
if ( audioController . DefaultPlaybackDevice . IsMuted )
59
- await Task . Run ( ( ) => Globals . AudioHandler . SetDeviceMute ( Globals . AudioHandler . AudioController . DefaultPlaybackDevice , false ) ) ;
61
+ await Globals . AudioHandler . SetDeviceMute ( Globals . AudioHandler . AudioController . DefaultPlaybackDevice , false ) ;
60
62
break ;
61
63
case MouseWheelDirection . Down :
62
64
if ( type == ActionTriggerType . RegularVolumeScroll )
@@ -71,57 +73,57 @@ public async Task AdjustVolume(MouseWheelDirection direction, ActionTriggerType
71
73
if ( newVolume < 0 )
72
74
newVolume = 0 ;
73
75
74
- await Task . Run ( ( ) => SetDeviceVolume ( audioController . DefaultPlaybackDevice , newVolume ) ) ;
76
+ await SetDeviceVolume ( audioController . DefaultPlaybackDevice , newVolume ) ;
75
77
if ( newVolume == 0 )
76
- await Task . Run ( ( ) => Globals . AudioHandler . SetDeviceMute ( Globals . AudioHandler . AudioController . DefaultPlaybackDevice , true ) ) ;
78
+ await Globals . AudioHandler . SetDeviceMute ( Globals . AudioHandler . AudioController . DefaultPlaybackDevice , true ) ;
77
79
}
78
80
79
81
80
82
81
83
public async Task ChangePlaybackDevice ( MouseWheelDirection direction )
82
84
{
83
- List < CoreAudioDevice > audioDevices = ( await Task . Run ( ( ) => AudioController . GetPlaybackDevicesAsync ( DeviceState . Active ) ) ) . ToList ( ) ;
84
- if ( audioDevices . Count != 0 )
85
+ if ( AudioDevices . Count != 0 )
85
86
{
86
- int curDeviceIndex = audioDevices . IndexOf ( AudioController . DefaultPlaybackDevice ) ;
87
+ int curDeviceIndex = AudioDevices . IndexOf ( AudioController . DefaultPlaybackDevice ) ;
87
88
CoreAudioDevice newAudioDevice = null ;
88
89
switch ( direction )
89
90
{
90
91
case MouseWheelDirection . Up :
91
- if ( curDeviceIndex != audioDevices . Count )
92
- newAudioDevice = audioDevices . ElementAt ( curDeviceIndex + 1 ) ;
92
+ if ( curDeviceIndex != AudioDevices . Count )
93
+ newAudioDevice = AudioDevices . ElementAt ( curDeviceIndex + 1 ) ;
93
94
break ;
94
95
case MouseWheelDirection . Down :
95
96
if ( curDeviceIndex != 0 )
96
- newAudioDevice = audioDevices . ElementAt ( curDeviceIndex - 1 ) ;
97
+ newAudioDevice = AudioDevices . ElementAt ( curDeviceIndex - 1 ) ;
97
98
break ;
98
99
}
99
100
if ( newAudioDevice != null )
100
- await Task . Run ( ( ) => SetPlaybackDevice ( newAudioDevice ) ) ;
101
+ await SetPlaybackDevice ( newAudioDevice ) ;
101
102
}
102
103
}
103
104
104
105
public async void DeviceStateChanged ( DeviceChangedArgs value )
105
106
{
107
+ Console . WriteLine ( $ "{ DateTime . Now . ToString ( "H:mm:ss.FFF" ) } : { value . Device . FullName } triggered { value . ChangedType } ") ;
108
+ CoreAudioDevice audioDevice = null ;
106
109
if ( value . Device . InterfaceName != "Unknown" && value . Device . IsPlaybackDevice )
107
110
{
108
- CoreAudioDevice audioDevice = audioController . GetDevice ( value . Device . Id , DeviceState . All ) ;
109
- Console . WriteLine ( value . ChangedType ) ;
110
111
switch ( value . ChangedType )
111
112
{
112
- //case DeviceChangedType.DeviceRemoved:
113
- // foreach (var item in audioDeviceSubscriptions.Where(x => x.Value.Id == audioDevice.Id).ToList())
114
- // {
115
- // audioDeviceSubscriptions.Remove(item.Key);
116
- // item.Key.Dispose();
117
- // }
118
- // if (!Globals.AudioAvailable && Globals.InputAvailable)
119
- // Globals.MainForm.SetAppAppearances(ActionTriggerType.AudioDisabled);
120
-
121
- // break;
122
- case DeviceChangedType . StateChanged :
113
+ case DeviceChangedType . DeviceRemoved :
114
+ foreach ( var item in audioDeviceSubscriptions . Where ( x => x . Value . Id == value . Device . Id ) . ToList ( ) )
115
+ {
116
+ audioDeviceSubscriptions . Remove ( item . Key ) ;
117
+ item . Key . Dispose ( ) ;
118
+ }
119
+ if ( ! Globals . AudioAvailable && Globals . InputAvailable )
120
+ Globals . MainForm . SetAppAppearances ( ActionTriggerType . AudioDisabled ) ;
121
+
122
+ break ;
123
123
case DeviceChangedType . DeviceAdded :
124
- if ( audioDevice != null && ! audioDeviceSubscriptions . ContainsValue ( audioDevice ) )
124
+ Console . WriteLine ( "State: " + value . Device . State ) ;
125
+ audioDevice = audioController . GetDevice ( value . Device . Id , DeviceState . All ) ;
126
+ if ( audioDevice != null && ! audioDeviceSubscriptions . ContainsValue ( audioDevice ) && value . Device . State == DeviceState . Active )
125
127
{
126
128
audioDeviceSubscriptions . Add ( audioDevice . PeakValueChanged . Subscribe ( DeviceStateChanged ) , audioDevice ) ;
127
129
audioDeviceSubscriptions . Add ( audioDevice . VolumeChanged . Subscribe ( DeviceStateChanged ) , audioDevice ) ;
@@ -132,22 +134,30 @@ public async void DeviceStateChanged(DeviceChangedArgs value)
132
134
Globals . MainForm . SetAppAppearances ( ActionTriggerType . AudioEnabled ) ;
133
135
break ;
134
136
case DeviceChangedType . VolumeChanged :
137
+ Console . WriteLine ( "Volume: " + value . Device . Volume ) ;
138
+
135
139
Globals . MainForm . SetAppAppearances ( ActionTriggerType . InternalEvent ) ;
136
140
if ( Globals . VolumeSliderControlForm != null )
137
141
Globals . VolumeSliderControlForm . UpdateVolumeState ( ) ;
138
142
break ;
139
143
case DeviceChangedType . MuteChanged :
144
+ Console . WriteLine ( "Mute: " + value . Device . IsMuted ) ;
145
+
140
146
Globals . MainForm . SetAppAppearances ( ActionTriggerType . InternalEvent ) ;
141
147
if ( Globals . VolumeSliderControlForm != null )
142
148
Globals . VolumeSliderControlForm . UpdateVolumeState ( ) ;
143
149
break ;
144
150
case DeviceChangedType . DefaultChanged :
151
+ Console . WriteLine ( "Default: " + value . Device . IsPlaybackDevice ) ;
152
+
145
153
if ( Globals . AudioAvailable && Globals . InputAvailable )
146
154
Globals . MainForm . SetAppAppearances ( ActionTriggerType . InternalEvent ) ;
147
155
if ( Globals . VolumeSliderControlForm != null )
148
156
Globals . VolumeSliderControlForm . UpdateDeviceState ( ) ;
149
157
break ;
150
158
case DeviceChangedType . PeakValueChanged :
159
+ Console . WriteLine ( "PeakValue: " + value . Device . PeakValue ) ;
160
+ audioDevice = audioController . GetDevice ( value . Device . Id , DeviceState . All ) ;
151
161
if ( Globals . VolumeSliderControlForm != null && ! AudioController . DefaultPlaybackDevice . IsMuted )
152
162
{
153
163
audioDevice . PeakValueTimer . PeakValueInterval = 10 ;
0 commit comments