Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d28f421ea3 | ||
|
|
5aeefebc47 | ||
|
|
ca956f207b | ||
|
|
ad212e7416 |
+19
-10
@@ -4,15 +4,13 @@ using Serilog;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows.Threading;
|
|
||||||
|
|
||||||
namespace WorkIndicator;
|
namespace WorkIndicator;
|
||||||
|
|
||||||
internal class AudioDevice : IAudioSessionEventsHandler
|
internal class AudioDevice : IAudioSessionEventsHandler
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly MMDevice _device;
|
private readonly string _friendlyName;
|
||||||
private readonly Dispatcher _dispatcher;
|
|
||||||
private readonly Dictionary<string, AudioSessionControl> _sessions = new();
|
private readonly Dictionary<string, AudioSessionControl> _sessions = new();
|
||||||
|
|
||||||
public delegate void AudioSessionsChangedDelegate();
|
public delegate void AudioSessionsChangedDelegate();
|
||||||
@@ -23,15 +21,17 @@ internal class AudioDevice : IAudioSessionEventsHandler
|
|||||||
{
|
{
|
||||||
_logger = Log.ForContext<AudioDevice>();
|
_logger = Log.ForContext<AudioDevice>();
|
||||||
|
|
||||||
_dispatcher = Dispatcher.CurrentDispatcher;
|
_friendlyName = device.DeviceFriendlyName;
|
||||||
|
IsMuted = device.AudioEndpointVolume.Mute;
|
||||||
|
|
||||||
_device = device;
|
_logger.Information("AudioDevice: {name} {muted}", _friendlyName, IsMuted);
|
||||||
|
|
||||||
_device.AudioSessionManager.AudioSessionControl.RegisterEventClient(this);
|
device.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification;
|
||||||
_device.AudioSessionManager.OnSessionCreated += AudioSessionManager_OnSessionCreated;
|
device.AudioSessionManager.AudioSessionControl.RegisterEventClient(this);
|
||||||
|
device.AudioSessionManager.OnSessionCreated += AudioSessionManager_OnSessionCreated;
|
||||||
|
|
||||||
_device.AudioSessionManager.RefreshSessions();
|
device.AudioSessionManager.RefreshSessions();
|
||||||
var sessions = _device.AudioSessionManager.Sessions;
|
var sessions = device.AudioSessionManager.Sessions;
|
||||||
|
|
||||||
for (var i = 0; i < sessions.Count; i++)
|
for (var i = 0; i < sessions.Count; i++)
|
||||||
{
|
{
|
||||||
@@ -39,6 +39,13 @@ internal class AudioDevice : IAudioSessionEventsHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
|
||||||
|
{
|
||||||
|
_logger.Information("AudioEndpointVolume_OnVolumeNotification: {name} {muted}", _friendlyName, data.Muted);
|
||||||
|
|
||||||
|
IsMuted = data.Muted;
|
||||||
|
}
|
||||||
|
|
||||||
private void AudioSessionManager_OnSessionCreated(object sender, IAudioSessionControl newSession)
|
private void AudioSessionManager_OnSessionCreated(object sender, IAudioSessionControl newSession)
|
||||||
{
|
{
|
||||||
var session = new AudioSessionControl(newSession);
|
var session = new AudioSessionControl(newSession);
|
||||||
@@ -60,9 +67,11 @@ internal class AudioDevice : IAudioSessionEventsHandler
|
|||||||
|
|
||||||
public int ActiveSessionCount => _sessions.Values.Count(s => s.State == AudioSessionState.AudioSessionStateActive);
|
public int ActiveSessionCount => _sessions.Values.Count(s => s.State == AudioSessionState.AudioSessionStateActive);
|
||||||
|
|
||||||
|
public bool IsMuted { get; private set; }
|
||||||
|
|
||||||
void IAudioSessionEventsHandler.OnVolumeChanged(float volume, bool isMuted)
|
void IAudioSessionEventsHandler.OnVolumeChanged(float volume, bool isMuted)
|
||||||
{
|
{
|
||||||
_dispatcher.Invoke(() => { _logger.Information("{name}: {volume} {isMuted} {mute}", _device.DeviceFriendlyName, volume, isMuted, _device.AudioEndpointVolume.Mute); });
|
_logger.Information("OnVolumeChanged: {name} {volume} {isMuted}", _friendlyName, volume, isMuted);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IAudioSessionEventsHandler.OnDisplayNameChanged(string displayName)
|
void IAudioSessionEventsHandler.OnDisplayNameChanged(string displayName)
|
||||||
|
|||||||
+20
-10
@@ -1,20 +1,26 @@
|
|||||||
using NAudio.CoreAudioApi;
|
using NAudio.CoreAudioApi;
|
||||||
using NAudio.CoreAudioApi.Interfaces;
|
using NAudio.CoreAudioApi.Interfaces;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace WorkIndicator;
|
namespace WorkIndicator;
|
||||||
|
|
||||||
|
internal enum MicrophoneStatus
|
||||||
|
{
|
||||||
|
NotUsed,
|
||||||
|
UsedMuted,
|
||||||
|
UsedNotMuted
|
||||||
|
}
|
||||||
|
|
||||||
internal class AudioWatcher : IMMNotificationClient
|
internal class AudioWatcher : IMMNotificationClient
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger = Log.ForContext<AudioWatcher>();
|
private readonly ILogger _logger = Log.ForContext<AudioWatcher>();
|
||||||
private readonly MMDeviceEnumerator _deviceEnumerator = new();
|
private readonly MMDeviceEnumerator _deviceEnumerator = new();
|
||||||
private readonly Dictionary<string, AudioDevice> _inputDevices = new();
|
private readonly Dictionary<string, AudioDevice> _inputDevices = new();
|
||||||
|
|
||||||
public delegate void MicrophoneInUseChangedDelegate(bool microphoneInUse);
|
public delegate void MicrophoneStatusChangedDelegate(MicrophoneStatus microphoneStatus);
|
||||||
|
|
||||||
public event MicrophoneInUseChangedDelegate MicrophoneInUseChanged;
|
public event MicrophoneStatusChangedDelegate MicrophoneStatusChanged;
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
@@ -43,12 +49,12 @@ internal class AudioWatcher : IMMNotificationClient
|
|||||||
|
|
||||||
_inputDevices[captureDevice.ID] = audioDevice;
|
_inputDevices[captureDevice.ID] = audioDevice;
|
||||||
|
|
||||||
MicrophoneInUseChanged?.Invoke(MicrophoneInUse());
|
MicrophoneStatusChanged?.Invoke(GetMicrophoneStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AudioDevice_AudioSessionsChanged()
|
private void AudioDevice_AudioSessionsChanged()
|
||||||
{
|
{
|
||||||
MicrophoneInUseChanged?.Invoke(MicrophoneInUse());
|
MicrophoneStatusChanged?.Invoke(GetMicrophoneStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleRemovedDevice(string deviceId)
|
private void HandleRemovedDevice(string deviceId)
|
||||||
@@ -57,7 +63,7 @@ internal class AudioWatcher : IMMNotificationClient
|
|||||||
|
|
||||||
_inputDevices.Remove(deviceId);
|
_inputDevices.Remove(deviceId);
|
||||||
|
|
||||||
MicrophoneInUseChanged?.Invoke(MicrophoneInUse());
|
MicrophoneStatusChanged?.Invoke(GetMicrophoneStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
@@ -68,11 +74,15 @@ internal class AudioWatcher : IMMNotificationClient
|
|||||||
_deviceEnumerator.Dispose();
|
_deviceEnumerator.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool MicrophoneInUse()
|
public MicrophoneStatus GetMicrophoneStatus()
|
||||||
{
|
{
|
||||||
var totalActiveSessionCount = _inputDevices.Sum(i => i.Value.ActiveSessionCount);
|
foreach (var inputDevice in _inputDevices.Values)
|
||||||
|
{
|
||||||
|
if (inputDevice.ActiveSessionCount > 0)
|
||||||
|
return inputDevice.IsMuted ? MicrophoneStatus.UsedMuted : MicrophoneStatus.UsedNotMuted;
|
||||||
|
}
|
||||||
|
|
||||||
return totalActiveSessionCount > 0;
|
return MicrophoneStatus.NotUsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IMMNotificationClient.OnDeviceStateChanged(string deviceId, DeviceState newState)
|
void IMMNotificationClient.OnDeviceStateChanged(string deviceId, DeviceState newState)
|
||||||
|
|||||||
+6
-4
@@ -30,7 +30,7 @@ namespace WorkIndicator
|
|||||||
|
|
||||||
Properties.Settings.Default.PropertyChanged += HandleSettingChange;
|
Properties.Settings.Default.PropertyChanged += HandleSettingChange;
|
||||||
|
|
||||||
AudioWatcher.MicrophoneInUseChanged += AudioWatcher_MicrophoneInUseChanged;
|
AudioWatcher.MicrophoneStatusChanged += AudioWatcher_MicrophoneStatusChanged;
|
||||||
AudioWatcher.Start();
|
AudioWatcher.Start();
|
||||||
|
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
@@ -50,7 +50,7 @@ namespace WorkIndicator
|
|||||||
UpdateLights();
|
UpdateLights();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AudioWatcher_MicrophoneInUseChanged(bool microphoneInUse)
|
private static void AudioWatcher_MicrophoneStatusChanged(MicrophoneStatus microphoneStatus)
|
||||||
{
|
{
|
||||||
UpdateLights();
|
UpdateLights();
|
||||||
}
|
}
|
||||||
@@ -90,9 +90,11 @@ namespace WorkIndicator
|
|||||||
|
|
||||||
if (status == Status.Auto)
|
if (status == Status.Auto)
|
||||||
{
|
{
|
||||||
if (AudioWatcher.MicrophoneInUse())
|
var microphoneStatus = AudioWatcher.GetMicrophoneStatus();
|
||||||
|
|
||||||
|
if (microphoneStatus != MicrophoneStatus.NotUsed)
|
||||||
{
|
{
|
||||||
status = Status.OnPhone;
|
status = microphoneStatus == MicrophoneStatus.UsedMuted ? Status.OnPhone : Status.Talking;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user