Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the same microphone mute logic as ASUSOptimization #3361

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions app/Helpers/Audio.cs

This file was deleted.

127 changes: 127 additions & 0 deletions app/Helpers/AudioIndicatorApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using System.Diagnostics;
using System.Windows.Forms;

namespace GHelper.Helpers
{
internal class AudioIndicatorApplicationContext : ApplicationContext, IMMNotificationClient, IDisposable
{
private MMDeviceEnumerator deviceEnumerator;
private object lockObj = new();
private MMDevice? microphone;
private bool isMuted;
public AudioIndicatorApplicationContext()
{
deviceEnumerator = new MMDeviceEnumerator();
var result=deviceEnumerator.RegisterEndpointNotificationCallback(this);
if(result!=0)
{
Logger.WriteLine($"RegisterEndpointNotificationCallback Failed, Code: {result}");
return;
}
SetDefaultMicrophone();
}
private void SetDefaultMicrophone()
{
lock(lockObj)
{
var newmicrophone = deviceEnumerator.HasDefaultAudioEndpoint(DataFlow.Capture, Role.Communications) ? deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications) : null;
bool hasChangedMic = newmicrophone?.ID != microphone?.ID;
if (microphone != null && hasChangedMic)
{
microphone.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
microphone = null;
}

if (newmicrophone != null && hasChangedMic)
{
newmicrophone.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification;
}

microphone = newmicrophone;
UpdateMuteStatus();
}
}

private void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
if (data.Muted != isMuted)
{
UpdateMuteStatus(data.Muted);
}
}

private void UpdateMuteStatus(bool muted)
{
isMuted = muted;
if (!OptimizationService.IsRunning() && AppConfig.IsVivoZenbook())
{
Program.acpi.DeviceSet(AsusACPI.MicMuteLed, muted ? 1 : 0, "MicmuteLed");
}
}

private void UpdateMuteStatus()
{
if (microphone != null)
{
UpdateMuteStatus(microphone.AudioEndpointVolume.Mute);
}
else
{
UpdateMuteStatus(false);
}
}

public bool? ToggleMute()
{
if (microphone != null)
{
bool result;
result = microphone.AudioEndpointVolume.Mute = !microphone.AudioEndpointVolume.Mute;
return result;
}
else
{
return null;
}
}

public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
SetDefaultMicrophone();
}

public void OnDeviceAdded(string pwstrDeviceId)
{
SetDefaultMicrophone();
}

public void OnDeviceRemoved(string deviceId)
{
SetDefaultMicrophone();
}

public void OnDefaultDeviceChanged(DataFlow flow, Role role, string defaultDeviceId)
{
SetDefaultMicrophone();
}

public void OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key)
{
SetDefaultMicrophone();
}

protected override void Dispose(bool disposing)
{
if (microphone != null)
{
microphone.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
microphone = null;
}
deviceEnumerator.UnregisterEndpointNotificationCallback(this);
deviceEnumerator.Dispose();
base.Dispose(disposing);
}
}
}
9 changes: 5 additions & 4 deletions app/Helpers/OptimizationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Linq;

namespace GHelper.Helpers
{
Expand Down Expand Up @@ -36,12 +37,12 @@ public static class OptimizationService

public static bool IsRunning()
{
return Process.GetProcessesByName("AsusOptimization").Count() > 0;
return Process.GetProcessesByName("AsusOptimization").Any();
}

public static bool IsOSDRunning()
{
return Process.GetProcessesByName("AsusOSD").Count() > 0;
return Process.GetProcessesByName("AsusOSD").Any();
}


Expand All @@ -50,13 +51,13 @@ public static int GetRunningCount()
int count = 0;
foreach (string service in services)
{
if (Process.GetProcessesByName(service).Count() > 0) count++;
if (Process.GetProcessesByName(service).Any()) count++;
}

if (AppConfig.IsStopAC())
foreach (string service in processesAC)
{
if (Process.GetProcessesByName(service).Count() > 0)
if (Process.GetProcessesByName(service).Any())
{
count++;
Logger.WriteLine(service);
Expand Down
12 changes: 9 additions & 3 deletions app/Input/InputDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class InputDispatcher
KeyboardListener listener;
KeyboardHook hook = new KeyboardHook();

static AudioIndicatorApplicationContext? audioIndicatorApplicationContext = null;

public InputDispatcher()
{

Expand All @@ -48,6 +50,8 @@ public InputDispatcher()

timer.Elapsed += Timer_Elapsed;

audioIndicatorApplicationContext = new AudioIndicatorApplicationContext();

}

private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
Expand Down Expand Up @@ -613,9 +617,11 @@ public static void KeyProcess(string name = "m3")

static void ToggleMic()
{
bool muteStatus = Audio.ToggleMute();
Program.toast.RunToast(muteStatus ? Properties.Strings.Muted : Properties.Strings.Unmuted, muteStatus ? ToastIcon.MicrophoneMute : ToastIcon.Microphone);
if (AppConfig.IsVivoZenbook()) Program.acpi.DeviceSet(AsusACPI.MicMuteLed, muteStatus ? 1 : 0, "MicmuteLed");
var muteStatus = audioIndicatorApplicationContext?.ToggleMute();
if (muteStatus != null)
{
Program.toast.RunToast(muteStatus.Value ? Properties.Strings.Muted : Properties.Strings.Unmuted, muteStatus.Value ? ToastIcon.MicrophoneMute : ToastIcon.Microphone);
}
}

static bool GetTouchpadState()
Expand Down
Loading