Try hooking up talking state to mute state
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Successful in 34s

This commit is contained in:
2026-08-18 15:36:01 -04:00
parent 5aeefebc47
commit d28f421ea3
3 changed files with 31 additions and 19 deletions
+20 -10
View File
@@ -1,20 +1,26 @@
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using System.Collections.Generic;
using System.Linq;
using Serilog;
using System.Collections.Generic;
namespace WorkIndicator;
internal enum MicrophoneStatus
{
NotUsed,
UsedMuted,
UsedNotMuted
}
internal class AudioWatcher : IMMNotificationClient
{
private readonly ILogger _logger = Log.ForContext<AudioWatcher>();
private readonly MMDeviceEnumerator _deviceEnumerator = 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()
{
@@ -43,12 +49,12 @@ internal class AudioWatcher : IMMNotificationClient
_inputDevices[captureDevice.ID] = audioDevice;
MicrophoneInUseChanged?.Invoke(MicrophoneInUse());
MicrophoneStatusChanged?.Invoke(GetMicrophoneStatus());
}
private void AudioDevice_AudioSessionsChanged()
{
MicrophoneInUseChanged?.Invoke(MicrophoneInUse());
MicrophoneStatusChanged?.Invoke(GetMicrophoneStatus());
}
private void HandleRemovedDevice(string deviceId)
@@ -57,7 +63,7 @@ internal class AudioWatcher : IMMNotificationClient
_inputDevices.Remove(deviceId);
MicrophoneInUseChanged?.Invoke(MicrophoneInUse());
MicrophoneStatusChanged?.Invoke(GetMicrophoneStatus());
}
public void Stop()
@@ -68,11 +74,15 @@ internal class AudioWatcher : IMMNotificationClient
_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)