private void ProcessAdded(ObservableProcess process) { if (process == null) { throw new ArgumentNullException(nameof(process)); } while (applicationDevicePreferences == null) { Task.Delay(250); } SharedModels.ApplicationDevicePreference applicationDevicePreference = applicationDevicePreferences.Applications.FirstOrDefault(x => x.Name == process.ProcessName); if (applicationDevicePreference == null) { applicationDevicePreference = new SharedModels.ApplicationDevicePreference(); } AudioInterface preferredRender = GetApplicationDevicePreference(DataFlow.Render, applicationDevicePreference, process); if (preferredRender != null) { ChangeDefaultApplicationDevice(preferredRender, process); } AudioInterface preferredCapture = GetApplicationDevicePreference(DataFlow.Capture, applicationDevicePreference, process); if (preferredCapture != null) { ChangeDefaultApplicationDevice(preferredCapture, process); } }
public static AudioInterface GetDefaultApplicationDevice(DataFlow dataFlow, ObservableProcess process) { if (process == null) { throw new ArgumentNullException(nameof(process)); } EarTrumpet.DataModel.WindowsAudio.Internal.AudioPolicyConfig audioPolicyConfig = new EarTrumpet.DataModel.WindowsAudio.Internal.AudioPolicyConfig(dataFlow); return(GetAudioInterfaceById(audioPolicyConfig.GetDefaultEndPoint(process.Id))); }
private void AddDeviceApplicationPreference(AudioInterface audioInterface, ObservableProcess process) { SharedModels.DeviceApplicationPreference deviceApplicationPreference = deviceApplicationPreferences.Devices.FirstOrDefault(x => x.Id == audioInterface.ID); if (deviceApplicationPreference == default) { deviceApplicationPreference = new SharedModels.DeviceApplicationPreference() { Id = audioInterface.ID, Applications = new List <string>() }; deviceApplicationPreferences.Devices.Add(deviceApplicationPreference); } if (!deviceApplicationPreference.Applications.Contains(process.ProcessName)) { deviceApplicationPreference.Applications.Add(process.ProcessName); } jsonDataDirty = true; }
private void AddApplicationDevicePreference(ObservableProcess process, AudioInterface audioInterface) { SharedModels.ApplicationDevicePreference applicationDevicePreference = applicationDevicePreferences.Applications.FirstOrDefault(x => x.Name == process.ProcessName); if (applicationDevicePreference == default) { applicationDevicePreference = new SharedModels.ApplicationDevicePreference() { Name = process.ProcessName, Devices = new SharedModels.DefaultDevicePreference() }; applicationDevicePreferences.Applications.Add(applicationDevicePreference); } _ = audioInterface.DataFlow switch { DataFlow.Render => applicationDevicePreference.Devices.RenderDeviceId = audioInterface.ID, DataFlow.Capture => applicationDevicePreference.Devices.CaptureDeviceId = audioInterface.ID, _ => null }; jsonDataDirty = true; }
private void RemoveOldApplicationDevicePreference(ObservableProcess process, AudioInterface audioInterface) { SharedModels.ApplicationDevicePreference applicationDevicePreference = applicationDevicePreferences.Applications.FirstOrDefault(x => x.Name == process.ProcessName); if (applicationDevicePreference == default) { return; } string previousPreferredInterface = null; switch (audioInterface.DataFlow) { case DataFlow.Render: previousPreferredInterface = applicationDevicePreference.Devices.RenderDeviceId; applicationDevicePreference.Devices.RenderDeviceId = null; break; case DataFlow.Capture: previousPreferredInterface = applicationDevicePreference.Devices.CaptureDeviceId; applicationDevicePreference.Devices.CaptureDeviceId = null; break; default: return; } if (applicationDevicePreference.Devices.RenderDeviceId == null && applicationDevicePreference.Devices.CaptureDeviceId == null) { applicationDevicePreferences.Applications.Remove(applicationDevicePreference); } SharedModels.DeviceApplicationPreference deviceApplicationPreference = deviceApplicationPreferences.Devices.FirstOrDefault(x => x.Id == previousPreferredInterface); if (deviceApplicationPreference == default) { return; } deviceApplicationPreference.Applications.Remove(process.ProcessName); if (deviceApplicationPreference.Applications.Count == 0) { deviceApplicationPreferences.Devices.Remove(deviceApplicationPreference); } }
public static void ClearApplicationDefaultDevice(DataFlow dataFlow, ObservableProcess process) { if (process == null) { throw new ArgumentNullException(nameof(process)); } AudioInterface dataFlowInterface = dataFlow switch { DataFlow.Render => Instance.DefaultRender, DataFlow.Capture => Instance.DefaultCapture, _ => null }; if (dataFlowInterface == null) { throw new ArgumentException($"{Enum.GetName(typeof(DataFlow),dataFlow)} is not {Enum.GetName(typeof(DataFlow), DataFlow.Render)} or {Enum.GetName(typeof(DataFlow), DataFlow.Capture)}.", nameof(dataFlow)); } EarTrumpet.DataModel.WindowsAudio.Internal.AudioPolicyConfig audioPolicyConfig = new EarTrumpet.DataModel.WindowsAudio.Internal.AudioPolicyConfig(dataFlow); audioPolicyConfig.SetDefaultEndPoint(String.Empty, process.Id); Instance.RemoveOldApplicationDevicePreference(process, dataFlowInterface); }
public static void ToggleDefaultApplicationDevice(ObservableProcess process) { if (process == null) { throw new ArgumentNullException(nameof(process)); } AudioInterface renderDevice = GetDefaultApplicationDevice(DataFlow.Render, process); if (renderDevice != null) { Trace.WriteLine($"Toggling PID {process.Id} render device to default and back to {renderDevice.FriendlyName}."); ChangeDefaultApplicationDevice(Instance.DefaultRender, process); ChangeDefaultApplicationDevice(renderDevice, process); } AudioInterface captureDevice = GetDefaultApplicationDevice(DataFlow.Capture, process); if (captureDevice != null) { Trace.WriteLine($"Toggling PID {process.Id} capture device to default and back to {captureDevice.FriendlyName}."); ChangeDefaultApplicationDevice(Instance.DefaultCapture, process); ChangeDefaultApplicationDevice(captureDevice, process); } }
public static void ChangeDefaultApplicationDevice(AudioInterface audioInterface, ObservableProcess process) { if (audioInterface == null) { throw new ArgumentNullException(nameof(audioInterface)); } if (process == null) { throw new ArgumentNullException(nameof(process)); } EarTrumpet.DataModel.WindowsAudio.Internal.AudioPolicyConfig audioPolicyConfig = new EarTrumpet.DataModel.WindowsAudio.Internal.AudioPolicyConfig(audioInterface.DataFlow); audioPolicyConfig.SetDefaultEndPoint(audioInterface.ID, process.Id); Instance.RemoveOldApplicationDevicePreference(process, audioInterface); Instance.AddDeviceApplicationPreference(audioInterface, process); Instance.AddApplicationDevicePreference(process, audioInterface); }
private AudioInterface GetApplicationDevicePreference(DataFlow dataFlow, SharedModels.ApplicationDevicePreference applicationDevicePreference, ObservableProcess process) { string preferredInterfaceId = dataFlow switch { DataFlow.Render => applicationDevicePreference?.Devices?.RenderDeviceId, DataFlow.Capture => applicationDevicePreference?.Devices?.CaptureDeviceId, _ => null }; if (preferredInterfaceId != null) { return(GetAudioInterfaceById(preferredInterfaceId)); } AudioInterface audioInterface = GetDefaultApplicationDevice(dataFlow, process); if (audioInterface != null) { RemoveOldApplicationDevicePreference(process, audioInterface); AddApplicationDevicePreference(process, audioInterface); AddDeviceApplicationPreference(audioInterface, process); return(audioInterface); } return(null); }