Fixing ShortcutForm audio config

Added more robustness and error checks
to handle AudioSwitcher not being able to
detect chipsets, and to cope with situations
with no active audio or capture devices.
Should be much more robust as it also
handles exceptions better, and has better
error logging.
This commit is contained in:
Terry MacDonald 2021-04-10 10:51:20 +12:00
parent c799121a77
commit 07f328b890
4 changed files with 713 additions and 427 deletions

View File

@ -14,6 +14,7 @@ using System.Windows.Forms;
using System.Text.RegularExpressions;
using IWshRuntimeLibrary;
using AudioSwitcher.AudioApi.CoreAudio;
using AudioSwitcher.AudioApi;
namespace DisplayMagician
{
@ -2090,87 +2091,135 @@ namespace DisplayMagician
}
}
// Check the Audio Device is still valid (if one is specified)
CoreAudioController audioController = ShortcutRepository.AudioController;
if (ChangeAudioDevice)
{
CoreAudioController audioController = ShortcutRepository.AudioController;
IEnumerable<CoreAudioDevice> audioDevices = audioController.GetPlaybackDevices();
foreach (CoreAudioDevice audioDevice in audioDevices)
IEnumerable<CoreAudioDevice> audioDevices = null;
if (audioController != null)
{
if (audioDevice.FullName.Equals(AudioDevice))
try
{
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Disabled)
{
ShortcutError error = new ShortcutError();
error.Name = "AudioDeviceDisabled";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Audio Device { AudioDevice} is disabled, so the shortcut '{Name}' cannot be used.You need to enable the audio device to use this shortcut, or edit the shortcut to change the audio device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.NotPresent)
{
ShortcutError error = new ShortcutError();
error.Name = "AudioDeviceNotPresent";
error.Validity = ShortcutValidity.Error;
error.Message = $"The Audio Device {AudioDevice} is not present, so the shortcut '{Name}' cannot be used.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Error;
}
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Unplugged)
{
ShortcutError error = new ShortcutError();
error.Name = "AudioDeviceUnplugged";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Audio Device {AudioDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the audio device to use this shortcut, or edit the shortcut to change the audio device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
audioDevices = audioController.GetPlaybackDevices();
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutRepository/RefreshValidity: Exception trying to get all playback devices!");
}
if (audioDevices != null)
{
foreach (CoreAudioDevice audioDevice in audioDevices)
{
if (audioDevice.FullName.Equals(AudioDevice))
{
if (audioDevice.State == DeviceState.Disabled)
{
ShortcutError error = new ShortcutError();
error.Name = "AudioDeviceDisabled";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Audio Device { AudioDevice} is disabled, so the shortcut '{Name}' cannot be used.You need to enable the audio device to use this shortcut, or edit the shortcut to change the audio device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
if (audioDevice.State == DeviceState.NotPresent)
{
ShortcutError error = new ShortcutError();
error.Name = "AudioDeviceNotPresent";
error.Validity = ShortcutValidity.Error;
error.Message = $"The Audio Device {AudioDevice} is not present, so the shortcut '{Name}' cannot be used.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Error;
}
if (audioDevice.State == DeviceState.Unplugged)
{
ShortcutError error = new ShortcutError();
error.Name = "AudioDeviceUnplugged";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Audio Device {AudioDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the audio device to use this shortcut, or edit the shortcut to change the audio device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
}
}
}
}
else
{
ShortcutError error = new ShortcutError();
error.Name = "AudioChipsetNotSupported";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Audio chipset isn't supported by DisplayMagician. You need to edit the shortcut to not change the audio output settings.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
}
// Check the Capture Device is still valid (if one is specified)
if (ChangeCaptureDevice)
{
CoreAudioController audioController = ShortcutRepository.AudioController;
IEnumerable<CoreAudioDevice> captureDevices = audioController.GetCaptureDevices();
foreach (CoreAudioDevice captureDevice in captureDevices)
IEnumerable<CoreAudioDevice> captureDevices = null;
if (audioController != null)
{
if (captureDevice.FullName.Equals(CaptureDevice))
try
{
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.Disabled)
{
ShortcutError error = new ShortcutError();
error.Name = "CaptureDeviceDisabled";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Capture Device {CaptureDevice} is disabled, so the shortcut '{Name}' cannot be used. You need to enable the capture device to use this shortcut, or edit the shortcut to change the capture device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.NotPresent)
{
ShortcutError error = new ShortcutError();
error.Name = "CaptureDeviceNotPresent";
error.Validity = ShortcutValidity.Error;
error.Message = $"The Capture Device {CaptureDevice} is not present, so the shortcut '{Name}' cannot be used.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Error;
}
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.Unplugged)
{
ShortcutError error = new ShortcutError();
error.Name = "CaptureDeviceUnplugged";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Capture Device {CaptureDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the capture device to use this shortcut, or edit the shortcut to change the capture device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
captureDevices = audioController.GetCaptureDevices();
}
catch(Exception ex)
{
logger.Warn(ex, $"ShortcutRepository/RefreshValidity: Exception trying to get all capture devices!");
}
if (captureDevices != null)
{
foreach (CoreAudioDevice captureDevice in captureDevices)
{
if (captureDevice.FullName.Equals(CaptureDevice))
{
if (captureDevice.State == DeviceState.Disabled)
{
ShortcutError error = new ShortcutError();
error.Name = "CaptureDeviceDisabled";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Capture Device {CaptureDevice} is disabled, so the shortcut '{Name}' cannot be used. You need to enable the capture device to use this shortcut, or edit the shortcut to change the capture device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
if (captureDevice.State == DeviceState.NotPresent)
{
ShortcutError error = new ShortcutError();
error.Name = "CaptureDeviceNotPresent";
error.Validity = ShortcutValidity.Error;
error.Message = $"The Capture Device {CaptureDevice} is not present, so the shortcut '{Name}' cannot be used.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Error;
}
if (captureDevice.State == DeviceState.Unplugged)
{
ShortcutError error = new ShortcutError();
error.Name = "CaptureDeviceUnplugged";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Capture Device {CaptureDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the capture device to use this shortcut, or edit the shortcut to change the capture device.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
}
}
}
}
else
{
ShortcutError error = new ShortcutError();
error.Name = "AudioChipsetNotSupported";
error.Validity = ShortcutValidity.Warning;
error.Message = $"The Audio chipset isn't supported by DisplayMagician. You need to edit the shortcut to not change the microphone input settings.";
_shortcutErrors.Add(error);
if (worstError != ShortcutValidity.Error)
worstError = ShortcutValidity.Warning;
}
}

View File

@ -1,4 +1,5 @@
using AudioSwitcher.AudioApi.CoreAudio;
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi.CoreAudio;
using DisplayMagician.GameLibraries;
using DisplayMagician.InterProcess;
using DisplayMagicianShared;
@ -44,35 +45,24 @@ namespace DisplayMagician
try
{
NvAPIWrapper.NVIDIA.Initialize();
_audioController = new CoreAudioController();
// Create the Profile Storage Path if it doesn't exist so that it's avilable for all the program
if (!Directory.Exists(AppShortcutStoragePath))
{
Directory.CreateDirectory(AppShortcutStoragePath);
}
}
catch (UnauthorizedAccessException ex)
{
logger.Error(ex, $"ShortcutRepository/ShortcutRepository: DisplayMagician doesn't have permissions to create the Shortcut storage folder {AppShortcutStoragePath}.");
}
catch (ArgumentException ex)
{
logger.Error(ex, $"ShortcutRepository/ShortcutRepository: DisplayMagician can't create the Shortcut storage folder {AppShortcutStoragePath} due to an invalid argument.");
}
catch (PathTooLongException ex)
{
logger.Error(ex, $"ShortcutRepository/ShortcutRepository: DisplayMagician can't create the Shortcut storage folder {AppShortcutStoragePath} as the path is too long.");
}
catch (DirectoryNotFoundException ex)
{
logger.Error(ex, $"ShortcutRepository/ShortcutRepository: DisplayMagician can't create the Shortcut storage folder {AppShortcutStoragePath} as the parent folder isn't there.");
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutRepository/ShortcutRepository: Initialising NVIDIA NvAPIWrapper or CoreAudioController caused an exception.");
}
try
{
_audioController = new CoreAudioController();
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutRepository/ShortcutRepository: Exception while trying to initialise CoreAudioController. Audio Chipset on your computer is not supported. You will be unable to set audio settings.");
}
//_audioController.DefaultPlaybackDevice.SetAsDefault();
//_audioController.DefaultCaptureDevice.SetAsDefault();
// Load the Shortcuts from storage
LoadShortcuts();
@ -620,96 +610,148 @@ namespace DisplayMagician
}
// record the old audio device
// Get the list of Audio Devices currently connected and active
bool needToChangeAudioDevice = false;
CoreAudioDevice rollbackAudioDevice = _audioController.DefaultPlaybackDevice;
CoreAudioDevice rollbackAudioDevice = null;
double rollbackAudioVolume = 50;
if (rollbackAudioDevice != null)
{
rollbackAudioVolume = _audioController.DefaultPlaybackDevice.Volume;
if (!rollbackAudioDevice.FullName.Equals(shortcutToUse.AudioDevice))
{
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.AudioDevice} audio device.");
needToChangeAudioDevice = true;
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices.");
}
}
// Change Audio Device (if one specified)
if (shortcutToUse.ChangeAudioDevice)
{
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.AudioDevice} audio device.");
IEnumerable<CoreAudioDevice> audioDevices = _audioController.GetPlaybackDevices();
foreach (CoreAudioDevice audioDevice in audioDevices)
{
if (audioDevice.FullName.Equals(shortcutToUse.AudioDevice))
{
// use the Audio Device
audioDevice.SetAsDefault();
if (shortcutToUse.SetAudioVolume)
{
logger.Debug($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.AudioDevice} audio level to {shortcutToUse.AudioVolume}%.");
Task myTask = new Task(() =>
{
audioDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.AudioVolume));
});
myTask.Start();
myTask.Wait(2000);
}
}
}
}
// record the old microphone device
List<CoreAudioDevice> activeAudioDevices = new List<CoreAudioDevice>();
bool needToChangeCaptureDevice = false;
CoreAudioDevice rollbackCaptureDevice = _audioController.DefaultCaptureDevice;
CoreAudioDevice rollbackCaptureDevice = null;
double rollbackCaptureVolume = 50;
if (rollbackCaptureDevice != null)
{
rollbackCaptureVolume = _audioController.DefaultCaptureDevice.Volume;
if (!rollbackCaptureDevice.FullName.Equals(shortcutToUse.CaptureDevice))
{
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
needToChangeCaptureDevice = true;
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices.");
}
}
// Change capture Device (if one specified)
if (shortcutToUse.ChangeCaptureDevice)
{
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
List<CoreAudioDevice> activeCaptureDevices = new List<CoreAudioDevice>();
IEnumerable<CoreAudioDevice> captureDevices = _audioController.GetCaptureDevices();
foreach (CoreAudioDevice captureDevice in captureDevices)
{
if (captureDevice.FullName.Equals(shortcutToUse.CaptureDevice))
if (_audioController != null)
{
try {
activeAudioDevices = _audioController.GetPlaybackDevices(DeviceState.Active).ToList();
if (activeAudioDevices.Count > 0)
{
// use the Audio Device
captureDevice.SetAsDefault();
if (shortcutToUse.SetCaptureVolume)
// Change Audio Device (if one specified)
if (shortcutToUse.ChangeAudioDevice && !shortcutToUse.AudioDevice.Equals(""))
{
logger.Debug($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.CaptureDevice} audio level to {shortcutToUse.CaptureVolume}%.");
Task myTask = new Task(() =>
{
captureDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.CaptureVolume));
});
myTask.Start();
myTask.Wait(2000);
}
// record the old audio device
rollbackAudioDevice = _audioController.DefaultPlaybackDevice;
if (rollbackAudioDevice != null)
{
rollbackAudioVolume = _audioController.DefaultPlaybackDevice.Volume;
if (!rollbackAudioDevice.FullName.Equals(shortcutToUse.AudioDevice))
{
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.AudioDevice} audio device.");
needToChangeAudioDevice = true;
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices.");
}
}
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.AudioDevice} audio device.");
foreach (CoreAudioDevice audioDevice in activeAudioDevices)
{
if (audioDevice.FullName.Equals(shortcutToUse.AudioDevice))
{
// use the Audio Device
audioDevice.SetAsDefault();
if (shortcutToUse.SetAudioVolume)
{
logger.Debug($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.AudioDevice} audio level to {shortcutToUse.AudioVolume}%.");
Task myTask = new Task(() =>
{
audioDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.AudioVolume));
});
myTask.Start();
myTask.Wait(2000);
}
}
}
}
else
{
logger.Info($"ShortcutRepository/RunShortcut: Shortcut does not require changing Audio Device.");
}
}
else
{
logger.Warn($"ShortcutRepository/RunShortcut: No active Audio Devices to use so skipping audio device checks!");
}
}
catch(Exception ex)
{
logger.Warn(ex, $"ShortcutRepository/RunShortcut: Exception accessing or manipulating Audio Devices!");
}
try
{
// Get the list of Audio Devices currently connected
activeCaptureDevices = _audioController.GetCaptureDevices(DeviceState.Active).ToList();
if (activeCaptureDevices.Count > 0)
{
// Change capture Device (if one specified)
if (shortcutToUse.ChangeCaptureDevice && !shortcutToUse.CaptureDevice.Equals(""))
{
// record the old microphone device
rollbackCaptureDevice = _audioController.DefaultCaptureDevice;
if (rollbackCaptureDevice != null)
{
rollbackCaptureVolume = _audioController.DefaultCaptureDevice.Volume;
if (!rollbackCaptureDevice.FullName.Equals(shortcutToUse.CaptureDevice))
{
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
needToChangeCaptureDevice = true;
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices.");
}
}
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
foreach (CoreAudioDevice captureDevice in activeCaptureDevices)
{
if (captureDevice.FullName.Equals(shortcutToUse.CaptureDevice))
{
// use the Audio Device
captureDevice.SetAsDefault();
if (shortcutToUse.SetCaptureVolume)
{
logger.Debug($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.CaptureDevice} audio level to {shortcutToUse.CaptureVolume}%.");
Task myTask = new Task(() =>
{
captureDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.CaptureVolume));
});
myTask.Start();
myTask.Wait(2000);
}
}
}
}
else
{
logger.Info($"ShortcutRepository/RunShortcut: Shortcut does not require changing Capture Device.");
}
}
else
{
logger.Warn($"ShortcutRepository/RunShortcut: No active Capture Devices to use so skipping capture device checks!");
}
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutRepository/RunShortcut: Exception accessing or manipulating Capture Devices!");
}
}
else
{
logger.Warn($"ShortcutRepository/RunShortcut: CoreAudio Controller is null, so we can't set Audio or Capture Devices!");
}
// Set the IP Service status back to what it was
@ -1369,43 +1411,65 @@ namespace DisplayMagician
}
// Change Audio Device back (if one specified)
if (needToChangeAudioDevice)
if (activeAudioDevices.Count > 0)
{
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default audio back to {rollbackAudioDevice.Name} audio device");
// use the Audio Device
rollbackAudioDevice.SetAsDefault();
if (shortcutToUse.SetAudioVolume)
if (needToChangeAudioDevice)
{
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default audio volume back to {shortcutToUse.SetAudioVolume}% volume");
Task myTask = new Task(() =>
{
rollbackAudioDevice.SetVolumeAsync(Convert.ToDouble(rollbackAudioVolume));
});
myTask.Start();
myTask.Wait(2000);
}
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default audio back to {rollbackAudioDevice.Name} audio device");
// use the Audio Device
rollbackAudioDevice.SetAsDefault();
if (shortcutToUse.SetAudioVolume)
{
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default audio volume back to {shortcutToUse.SetAudioVolume}% volume");
Task myTask = new Task(() =>
{
rollbackAudioDevice.SetVolumeAsync(Convert.ToDouble(rollbackAudioVolume));
});
myTask.Start();
myTask.Wait(2000);
}
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: Shortcut did not require changing Audio Device, so no need to change it back.");
}
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: No Audio Devices active, so no need to change them back.");
}
// Change Capture Device back (if one specified)
if (needToChangeCaptureDevice)
if (activeCaptureDevices.Count > 0)
{
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default capture (microphone) device back to {rollbackCaptureDevice.Name} capture device");
// use the Audio Device
rollbackCaptureDevice.SetAsDefault();
if (shortcutToUse.SetCaptureVolume)
if (needToChangeCaptureDevice)
{
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default capture (microphone) volume back to {shortcutToUse.SetAudioVolume}% volume");
Task myTask = new Task(() =>
{
rollbackCaptureDevice.SetVolumeAsync(Convert.ToDouble(rollbackCaptureVolume));
});
myTask.Start();
myTask.Wait(2000);
}
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default capture (microphone) device back to {rollbackCaptureDevice.Name} capture device");
// use the Audio Device
rollbackCaptureDevice.SetAsDefault();
if (shortcutToUse.SetCaptureVolume)
{
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default capture (microphone) volume back to {shortcutToUse.SetAudioVolume}% volume");
Task myTask = new Task(() =>
{
rollbackCaptureDevice.SetVolumeAsync(Convert.ToDouble(rollbackCaptureVolume));
});
myTask.Start();
myTask.Wait(2000);
}
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: Shortcut did not require changing Capture Device, so no need to change it back.");
}
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: No Capture Devices active, so no need to change them back.");
}
// Change back to the original profile only if it is different
@ -1429,6 +1493,10 @@ namespace DisplayMagician
}
}
else
{
logger.Debug($"ShortcutRepository/RunShortcut: Shortcut did not require changing Display Profile, so no need to change it back.");
}
}

View File

@ -66,6 +66,7 @@ namespace DisplayMagician.UIForms
this.rb_no_change_audio = new System.Windows.Forms.RadioButton();
this.tabp_before = new System.Windows.Forms.TabPage();
this.pnl_start_program4 = new System.Windows.Forms.Panel();
this.cb_dont_start_if_running4 = new System.Windows.Forms.CheckBox();
this.cb_start_program4 = new System.Windows.Forms.CheckBox();
this.txt_start_program4 = new System.Windows.Forms.TextBox();
this.cb_start_program_close4 = new System.Windows.Forms.CheckBox();
@ -74,6 +75,7 @@ namespace DisplayMagician.UIForms
this.cb_start_program_pass_args4 = new System.Windows.Forms.CheckBox();
this.lbl_start_program4 = new System.Windows.Forms.Label();
this.pnl_start_program3 = new System.Windows.Forms.Panel();
this.cb_dont_start_if_running3 = new System.Windows.Forms.CheckBox();
this.cb_start_program3 = new System.Windows.Forms.CheckBox();
this.txt_start_program3 = new System.Windows.Forms.TextBox();
this.cb_start_program_close3 = new System.Windows.Forms.CheckBox();
@ -82,6 +84,7 @@ namespace DisplayMagician.UIForms
this.cb_start_program_pass_args3 = new System.Windows.Forms.CheckBox();
this.lbl_start_program3 = new System.Windows.Forms.Label();
this.pnl_start_program2 = new System.Windows.Forms.Panel();
this.cb_dont_start_if_running2 = new System.Windows.Forms.CheckBox();
this.cb_start_program2 = new System.Windows.Forms.CheckBox();
this.txt_start_program2 = new System.Windows.Forms.TextBox();
this.cb_start_program_close2 = new System.Windows.Forms.CheckBox();
@ -90,6 +93,7 @@ namespace DisplayMagician.UIForms
this.cb_start_program_pass_args2 = new System.Windows.Forms.CheckBox();
this.lbl_start_program2 = new System.Windows.Forms.Label();
this.pnl_start_program1 = new System.Windows.Forms.Panel();
this.cb_dont_start_if_running1 = new System.Windows.Forms.CheckBox();
this.cb_start_program1 = new System.Windows.Forms.CheckBox();
this.txt_start_program1 = new System.Windows.Forms.TextBox();
this.cb_start_program_close1 = new System.Windows.Forms.CheckBox();
@ -142,10 +146,9 @@ namespace DisplayMagician.UIForms
this.lbl_title = new System.Windows.Forms.Label();
this.lbl_shortcut_name = new System.Windows.Forms.Label();
this.cb_autosuggest = new System.Windows.Forms.CheckBox();
this.cb_dont_start_if_running1 = new System.Windows.Forms.CheckBox();
this.cb_dont_start_if_running2 = new System.Windows.Forms.CheckBox();
this.cb_dont_start_if_running3 = new System.Windows.Forms.CheckBox();
this.cb_dont_start_if_running4 = new System.Windows.Forms.CheckBox();
this.lbl_disabled_shortcut_audio_chipset = new System.Windows.Forms.Label();
this.lbl_no_active_audio_devices = new System.Windows.Forms.Label();
this.lbl_no_active_capture_devices = new System.Windows.Forms.Label();
this.tabc_shortcut.SuspendLayout();
this.tabp_display.SuspendLayout();
this.tabp_audio.SuspendLayout();
@ -321,6 +324,9 @@ namespace DisplayMagician.UIForms
// tabp_audio
//
this.tabp_audio.BackColor = System.Drawing.Color.Black;
this.tabp_audio.Controls.Add(this.lbl_no_active_capture_devices);
this.tabp_audio.Controls.Add(this.lbl_no_active_audio_devices);
this.tabp_audio.Controls.Add(this.lbl_disabled_shortcut_audio_chipset);
this.tabp_audio.Controls.Add(this.gb_capture_settings);
this.tabp_audio.Controls.Add(this.gb_audio_settings);
this.tabp_audio.Location = new System.Drawing.Point(4, 32);
@ -623,6 +629,19 @@ namespace DisplayMagician.UIForms
this.pnl_start_program4.Size = new System.Drawing.Size(959, 124);
this.pnl_start_program4.TabIndex = 19;
//
// cb_dont_start_if_running4
//
this.cb_dont_start_if_running4.AutoSize = true;
this.cb_dont_start_if_running4.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running4.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running4.Name = "cb_dont_start_if_running4";
this.cb_dont_start_if_running4.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running4.TabIndex = 20;
this.cb_dont_start_if_running4.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running4.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running4.Visible = false;
//
// cb_start_program4
//
this.cb_start_program4.Location = new System.Drawing.Point(21, 18);
@ -713,6 +732,19 @@ namespace DisplayMagician.UIForms
this.pnl_start_program3.Size = new System.Drawing.Size(959, 124);
this.pnl_start_program3.TabIndex = 18;
//
// cb_dont_start_if_running3
//
this.cb_dont_start_if_running3.AutoSize = true;
this.cb_dont_start_if_running3.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running3.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running3.Name = "cb_dont_start_if_running3";
this.cb_dont_start_if_running3.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running3.TabIndex = 20;
this.cb_dont_start_if_running3.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running3.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running3.Visible = false;
//
// cb_start_program3
//
this.cb_start_program3.Location = new System.Drawing.Point(21, 18);
@ -803,6 +835,19 @@ namespace DisplayMagician.UIForms
this.pnl_start_program2.Size = new System.Drawing.Size(959, 124);
this.pnl_start_program2.TabIndex = 18;
//
// cb_dont_start_if_running2
//
this.cb_dont_start_if_running2.AutoSize = true;
this.cb_dont_start_if_running2.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running2.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running2.Name = "cb_dont_start_if_running2";
this.cb_dont_start_if_running2.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running2.TabIndex = 19;
this.cb_dont_start_if_running2.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running2.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running2.Visible = false;
//
// cb_start_program2
//
this.cb_start_program2.Location = new System.Drawing.Point(21, 18);
@ -893,6 +938,19 @@ namespace DisplayMagician.UIForms
this.pnl_start_program1.Size = new System.Drawing.Size(959, 124);
this.pnl_start_program1.TabIndex = 0;
//
// cb_dont_start_if_running1
//
this.cb_dont_start_if_running1.AutoSize = true;
this.cb_dont_start_if_running1.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running1.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running1.Name = "cb_dont_start_if_running1";
this.cb_dont_start_if_running1.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running1.TabIndex = 18;
this.cb_dont_start_if_running1.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running1.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running1.Visible = false;
//
// cb_start_program1
//
this.cb_start_program1.Location = new System.Drawing.Point(21, 18);
@ -1525,57 +1583,55 @@ namespace DisplayMagician.UIForms
this.cb_autosuggest.UseVisualStyleBackColor = true;
this.cb_autosuggest.CheckedChanged += new System.EventHandler(this.cb_autosuggest_CheckedChanged);
//
// cb_dont_start_if_running1
// lbl_disabled_shortcut_audio_chipset
//
this.cb_dont_start_if_running1.AutoSize = true;
this.cb_dont_start_if_running1.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running1.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running1.Name = "cb_dont_start_if_running1";
this.cb_dont_start_if_running1.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running1.TabIndex = 18;
this.cb_dont_start_if_running1.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running1.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running1.Visible = false;
this.lbl_disabled_shortcut_audio_chipset.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.lbl_disabled_shortcut_audio_chipset.AutoSize = true;
this.lbl_disabled_shortcut_audio_chipset.BackColor = System.Drawing.Color.Brown;
this.lbl_disabled_shortcut_audio_chipset.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_disabled_shortcut_audio_chipset.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.lbl_disabled_shortcut_audio_chipset.ForeColor = System.Drawing.Color.White;
this.lbl_disabled_shortcut_audio_chipset.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lbl_disabled_shortcut_audio_chipset.Location = new System.Drawing.Point(326, 298);
this.lbl_disabled_shortcut_audio_chipset.Name = "lbl_disabled_shortcut_audio_chipset";
this.lbl_disabled_shortcut_audio_chipset.Size = new System.Drawing.Size(430, 22);
this.lbl_disabled_shortcut_audio_chipset.TabIndex = 34;
this.lbl_disabled_shortcut_audio_chipset.Text = "Unsupported Audio Chipset. Setting audio isn\'t supported :(";
this.lbl_disabled_shortcut_audio_chipset.Visible = false;
//
// cb_dont_start_if_running2
// lbl_no_active_audio_devices
//
this.cb_dont_start_if_running2.AutoSize = true;
this.cb_dont_start_if_running2.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running2.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running2.Name = "cb_dont_start_if_running2";
this.cb_dont_start_if_running2.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running2.TabIndex = 19;
this.cb_dont_start_if_running2.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running2.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running2.Visible = false;
this.lbl_no_active_audio_devices.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lbl_no_active_audio_devices.AutoSize = true;
this.lbl_no_active_audio_devices.BackColor = System.Drawing.Color.Brown;
this.lbl_no_active_audio_devices.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_no_active_audio_devices.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.lbl_no_active_audio_devices.ForeColor = System.Drawing.Color.White;
this.lbl_no_active_audio_devices.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lbl_no_active_audio_devices.Location = new System.Drawing.Point(131, 151);
this.lbl_no_active_audio_devices.Name = "lbl_no_active_audio_devices";
this.lbl_no_active_audio_devices.Size = new System.Drawing.Size(804, 22);
this.lbl_no_active_audio_devices.TabIndex = 35;
this.lbl_no_active_audio_devices.Text = "No active audio outputs found. Please connect or enable at least one audio output" +
" if you want to use this feature.";
this.lbl_no_active_audio_devices.Visible = false;
//
// cb_dont_start_if_running3
// lbl_no_active_capture_devices
//
this.cb_dont_start_if_running3.AutoSize = true;
this.cb_dont_start_if_running3.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running3.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running3.Name = "cb_dont_start_if_running3";
this.cb_dont_start_if_running3.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running3.TabIndex = 20;
this.cb_dont_start_if_running3.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running3.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running3.Visible = false;
//
// cb_dont_start_if_running4
//
this.cb_dont_start_if_running4.AutoSize = true;
this.cb_dont_start_if_running4.ForeColor = System.Drawing.Color.White;
this.cb_dont_start_if_running4.Location = new System.Drawing.Point(167, 82);
this.cb_dont_start_if_running4.Name = "cb_dont_start_if_running4";
this.cb_dont_start_if_running4.Size = new System.Drawing.Size(289, 24);
this.cb_dont_start_if_running4.TabIndex = 20;
this.cb_dont_start_if_running4.Text = "Don\'t start if program already running";
this.cb_dont_start_if_running4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.cb_dont_start_if_running4.UseVisualStyleBackColor = true;
this.cb_dont_start_if_running4.Visible = false;
this.lbl_no_active_capture_devices.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.lbl_no_active_capture_devices.AutoSize = true;
this.lbl_no_active_capture_devices.BackColor = System.Drawing.Color.Brown;
this.lbl_no_active_capture_devices.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_no_active_capture_devices.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.lbl_no_active_capture_devices.ForeColor = System.Drawing.Color.White;
this.lbl_no_active_capture_devices.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lbl_no_active_capture_devices.Location = new System.Drawing.Point(126, 433);
this.lbl_no_active_capture_devices.Name = "lbl_no_active_capture_devices";
this.lbl_no_active_capture_devices.Size = new System.Drawing.Size(831, 22);
this.lbl_no_active_capture_devices.TabIndex = 36;
this.lbl_no_active_capture_devices.Text = "No active microphone inputs found. Please connect or enable at least one micropho" +
"ne if you want to use this feature.";
this.lbl_no_active_capture_devices.Visible = false;
//
// ShortcutForm
//
@ -1606,6 +1662,7 @@ namespace DisplayMagician.UIForms
this.tabp_display.ResumeLayout(false);
this.tabp_display.PerformLayout();
this.tabp_audio.ResumeLayout(false);
this.tabp_audio.PerformLayout();
this.gb_capture_settings.ResumeLayout(false);
this.gb_capture_settings.PerformLayout();
this.gb_capture_volume.ResumeLayout(false);
@ -1759,5 +1816,8 @@ namespace DisplayMagician.UIForms
private System.Windows.Forms.CheckBox cb_dont_start_if_running3;
private System.Windows.Forms.CheckBox cb_dont_start_if_running2;
private System.Windows.Forms.CheckBox cb_dont_start_if_running1;
private System.Windows.Forms.Label lbl_disabled_shortcut_audio_chipset;
private System.Windows.Forms.Label lbl_no_active_audio_devices;
private System.Windows.Forms.Label lbl_no_active_capture_devices;
}
}

View File

@ -14,6 +14,7 @@ using System.Globalization;
using Manina.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using AudioSwitcher.AudioApi.CoreAudio;
using AudioSwitcher.AudioApi;
namespace DisplayMagician.UIForms
{
@ -44,11 +45,12 @@ namespace DisplayMagician.UIForms
private bool _autoName = true;
private int _gameId = 0;
private string _uuid = "";
private CoreAudioController audioController = new CoreAudioController();
private CoreAudioController audioController = null;
private List<CoreAudioDevice> audioDevices = null;
private CoreAudioDevice selectedAudioDevice = null;
private List<CoreAudioDevice> captureDevices = null;
private CoreAudioDevice selectedCaptureDevice = null;
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public ShortcutForm(ShortcutItem shortcutToEdit)
{
@ -69,6 +71,15 @@ namespace DisplayMagician.UIForms
lbl_profile_shown.Text = "No Display Profiles available";
lbl_profile_shown_subtitle.Text = "Please go back to the main window, click on 'Display Profiles', and save a new Display Profile. Then come back here.";
try
{
audioController = new CoreAudioController();
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutForm/ShortcutForm: Exception while trying to initialise CoreAudioController in ShortcutForm. Audio Chipset on your computer is not supported. You will be unable to set audio settings.");
}
}
public ShortcutItem Shortcut
@ -283,61 +294,6 @@ namespace DisplayMagician.UIForms
}
// Save the Audio features
if (rb_change_audio.Checked)
{
_changeAudioDevice = true;
_audioDevice = cb_audio_device.Text;
}
else
{
_changeAudioDevice = false;
_audioDevice = "";
}
if (rb_set_audio_volume.Checked)
{
_setAudioVolume = true;
_audioVolume = nud_audio_volume.Value;
}
else
{
_setAudioVolume = false;
_audioVolume = -1;
}
// Save the Capture features
if (rb_change_capture.Checked)
{
_changeCaptureDevice = true;
_captureDevice = cb_capture_device.Text;
}
else
{
_changeCaptureDevice = false;
_captureDevice = "";
}
if (rb_set_capture_volume.Checked)
{
_setCaptureVolume = true;
_captureVolume = nud_capture_volume.Value;
}
else
{
_setCaptureVolume = false;
_captureVolume = -1;
}
// Check the audio permanence requirements
if (rb_switch_audio_temp.Checked)
_audioPermanence = ShortcutPermanence.Temporary;
if (rb_switch_audio_permanent.Checked)
_audioPermanence = ShortcutPermanence.Permanent;
// Check the display permanence requirements
if (rb_switch_display_temp.Checked)
@ -346,12 +302,117 @@ namespace DisplayMagician.UIForms
if (rb_switch_display_permanent.Checked)
_displayPermanence = ShortcutPermanence.Permanent;
// Check the microphone permanence requirements
if (rb_switch_capture_temp.Checked)
_capturePermanence = ShortcutPermanence.Temporary;
if (rb_switch_capture_permanent.Checked)
_capturePermanence = ShortcutPermanence.Permanent;
// If we can get access to the audio chipset then
// we try to get the settings
if (audioController != null)
{
if (audioDevices != null && audioDevices.Count > 0)
{
// Save the Audio features
if (rb_change_audio.Checked)
{
_changeAudioDevice = true;
_audioDevice = cb_audio_device.Text;
}
else
{
_changeAudioDevice = false;
_audioDevice = "";
}
if (rb_set_audio_volume.Checked)
{
_setAudioVolume = true;
_audioVolume = nud_audio_volume.Value;
}
else
{
_setAudioVolume = false;
_audioVolume = -1;
}
// Check the audio permanence requirements
if (rb_switch_audio_temp.Checked)
_audioPermanence = ShortcutPermanence.Temporary;
if (rb_switch_audio_permanent.Checked)
_audioPermanence = ShortcutPermanence.Permanent;
}
else
{
// No active audio devices found, so we force the save to disable changing the audio device
logger.Warn($"ShortcutForm/btn_save_Click: No active audio devices found, so forcing the save to disable changing the audio device for this shortcut.");
_changeAudioDevice = false;
_audioDevice = "";
_setAudioVolume = false;
_audioVolume = -1;
_audioPermanence = ShortcutPermanence.Temporary;
}
if (captureDevices != null && captureDevices.Count > 0)
{
// Save the Capture features
if (rb_change_capture.Checked)
{
_changeCaptureDevice = true;
_captureDevice = cb_capture_device.Text;
}
else
{
_changeCaptureDevice = false;
_captureDevice = "";
}
if (rb_set_capture_volume.Checked)
{
_setCaptureVolume = true;
_captureVolume = nud_capture_volume.Value;
}
else
{
_setCaptureVolume = false;
_captureVolume = -1;
}
// Check the microphone permanence requirements
if (rb_switch_capture_temp.Checked)
_capturePermanence = ShortcutPermanence.Temporary;
if (rb_switch_capture_permanent.Checked)
_capturePermanence = ShortcutPermanence.Permanent;
}
else
{
// No active capture devices found, so we force the save to disable changing the capture device
logger.Warn($"ShortcutForm/btn_save_Click: No active capture devices found, so forcing the save to disable changing the capture device for this shortcut.");
_changeCaptureDevice = false;
_captureDevice = "";
_setCaptureVolume = false;
_captureVolume = -1;
_capturePermanence = ShortcutPermanence.Temporary;
}
}
// Otherwise we force set the audio settings to no change
// just to be sure
else
{
_changeAudioDevice = false;
_audioDevice = "";
_setAudioVolume = false;
_audioVolume = -1;
_changeCaptureDevice = false;
_captureDevice = "";
_setCaptureVolume = false;
_captureVolume = -1;
_audioPermanence = ShortcutPermanence.Temporary;
_capturePermanence = ShortcutPermanence.Temporary;
}
// Save the start program 1
StartProgram myStartProgram = new StartProgram
@ -631,164 +692,212 @@ namespace DisplayMagician.UIForms
// Populate all the Audio devices in the audio devices list.
// Set the Audio device to the shortcut audio device only if
// the Change Audio radiobutton is set
rb_change_audio.Checked = _shortcutToEdit.ChangeAudioDevice;
cb_audio_device.Items.Clear();
audioDevices = audioController.GetPlaybackDevices().ToList();
// If the shortcut is to change the audio device
if (_shortcutToEdit.ChangeAudioDevice)
if (audioController != null)
{
// Then we need to populate the list
bool foundAudioDevice = false;
foreach (CoreAudioDevice audioDevice in audioDevices)
rb_change_audio.Checked = _shortcutToEdit.ChangeAudioDevice;
cb_audio_device.Items.Clear();
try
{
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active)
audioDevices = audioController.GetPlaybackDevices(DeviceState.Active).ToList();
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutForm/ShortcutForm_Load: Exception while trying to get active playback devices.");
}
if (audioDevices != null && audioDevices.Count > 0)
{
// If the shortcut is to change the audio device
if (_shortcutToEdit.ChangeAudioDevice)
{
int index = cb_audio_device.Items.Add(audioDevice.FullName);
// Set the audio device to the default device by default
if (audioDevice.FullName.Equals(_shortcutToEdit.AudioDevice))
// Then we need to populate the list
bool foundAudioDevice = false;
foreach (CoreAudioDevice audioDevice in audioDevices)
{
foundAudioDevice = true;
selectedAudioDevice = audioDevice;
int index = cb_audio_device.Items.Add(audioDevice.FullName);
// Set the audio device to the default device by default
if (audioDevice.FullName.Equals(_shortcutToEdit.AudioDevice))
{
foundAudioDevice = true;
selectedAudioDevice = audioDevice;
cb_audio_device.SelectedIndex = index;
if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100)
{
nud_audio_volume.Value = _shortcutToEdit.AudioVolume;
rb_set_audio_volume.Checked = true;
}
else
{
nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume);
rb_set_audio_volume.Checked = false;
}
}
}
// We need to handle the edgecase where the selected audio device
// isn't currently plugged in. We don't want to break the shortcut
// as it could be plugged in when it comes time to actually run
// the shortcut, so we need to just add it to the list to not break
// the UI.
if (!foundAudioDevice)
{
int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice);
cb_audio_device.SelectedIndex = index;
selectedAudioDevice = null;
if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100)
{
nud_audio_volume.Value = _shortcutToEdit.AudioVolume;
rb_set_audio_volume.Checked = true;
nud_audio_volume.Value = _shortcutToEdit.AudioVolume;
}
else
{
nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume);
rb_set_audio_volume.Checked = false;
rb_keep_audio_volume.Checked = true;
nud_audio_volume.Value = 50;
}
}
}
}
// We need to handle the edgecase where the selected audio device
// isn't currently plugged in. We don't want to break the shortcut
// as it could be plugged in when it comes time to actually run
// the shortcut, so we need to just add it to the list to not break
// the UI.
if (!foundAudioDevice)
{
int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice);
cb_audio_device.SelectedIndex = index;
selectedAudioDevice = null;
if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100)
{
rb_set_audio_volume.Checked = true;
nud_audio_volume.Value = _shortcutToEdit.AudioVolume;
}
else
{
rb_keep_audio_volume.Checked = true;
nud_audio_volume.Value = 50;
}
}
}
else
{
// Then we need to populate the list
foreach (CoreAudioDevice audioDevice in audioDevices)
{
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active)
{
int index = cb_audio_device.Items.Add(audioDevice.FullName);
// Set the audio device to the default device by default
if (audioDevice.IsDefaultDevice)
// Then we need to populate the list
foreach (CoreAudioDevice audioDevice in audioDevices)
{
selectedAudioDevice = audioDevice;
cb_audio_device.SelectedIndex = index;
nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume);
int index = cb_audio_device.Items.Add(audioDevice.FullName);
// Set the audio device to the default device by default
if (audioDevice.IsDefaultDevice)
{
selectedAudioDevice = audioDevice;
cb_audio_device.SelectedIndex = index;
nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume);
}
}
rb_keep_audio_volume.Checked = true;
}
}
rb_keep_audio_volume.Checked = true;
}
// Populate all the Capture devices in the capture devices list.
// Set the Capture device to the shortcut capture device only if
// the Change Capture radiobutton is set
rb_change_capture.Checked = _shortcutToEdit.ChangeCaptureDevice;
cb_capture_device.Items.Clear();
captureDevices = audioController.GetCaptureDevices().ToList();
// If the shortcut is to change the capture device
if (_shortcutToEdit.ChangeCaptureDevice)
{
// Then we need to populate the list
bool foundCaptureDevice = false;
foreach (CoreAudioDevice captureDevice in captureDevices)
else
{
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.Active)
// There are no active audio devices found
// so we hide all audio changing controls
gb_audio_settings.Visible = false;
lbl_no_active_audio_devices.Visible = true;
logger.Warn($"ShortcutForm/ShortcutForm_Load: No active playback devices so hiding the audio output controls.");
}
// Populate all the Capture devices in the capture devices list.
// Set the Capture device to the shortcut capture device only if
// the Change Capture radiobutton is set
rb_change_capture.Checked = _shortcutToEdit.ChangeCaptureDevice;
cb_capture_device.Items.Clear();
try
{
captureDevices = audioController.GetCaptureDevices(DeviceState.Active).ToList();
}
catch (Exception ex)
{
logger.Warn(ex, $"ShortcutForm/ShortcutForm_Load: Exception while trying to get active capture devices.");
}
if (captureDevices != null && captureDevices.Count > 0)
{
// If the shortcut is to change the capture device
if (_shortcutToEdit.ChangeCaptureDevice)
{
int index = cb_capture_device.Items.Add(captureDevice.FullName);
// Set the capture device to the default device by default
if (captureDevice.FullName.Equals(_shortcutToEdit.CaptureDevice))
// Then we need to populate the list
bool foundCaptureDevice = false;
foreach (CoreAudioDevice captureDevice in captureDevices)
{
foundCaptureDevice = true;
selectedCaptureDevice = captureDevice;
int index = cb_capture_device.Items.Add(captureDevice.FullName);
// Set the capture device to the default device by default
if (captureDevice.FullName.Equals(_shortcutToEdit.CaptureDevice))
{
foundCaptureDevice = true;
selectedCaptureDevice = captureDevice;
cb_capture_device.SelectedIndex = index;
if (_shortcutToEdit.SetCaptureVolume && _shortcutToEdit.CaptureVolume >= 0 && _shortcutToEdit.CaptureVolume <= 100)
{
nud_capture_volume.Value = _shortcutToEdit.CaptureVolume;
rb_set_capture_volume.Checked = true;
}
else
{
nud_capture_volume.Value = Convert.ToDecimal(captureDevice.Volume);
rb_set_capture_volume.Checked = false;
}
}
}
// We need to handle the edgecase where the selected capture device
// isn't currently plugged in. We don't want to break the shortcut
// as it could be plugged in when it comes time to actually run
// the shortcut, so we need to just add it to the list to not break
// the UI.
if (!foundCaptureDevice)
{
int index = cb_capture_device.Items.Add(_shortcutToEdit.CaptureDevice);
cb_capture_device.SelectedIndex = index;
selectedCaptureDevice = null;
if (_shortcutToEdit.SetCaptureVolume && _shortcutToEdit.CaptureVolume >= 0 && _shortcutToEdit.CaptureVolume <= 100)
{
nud_capture_volume.Value = _shortcutToEdit.CaptureVolume;
rb_set_capture_volume.Checked = true;
nud_capture_volume.Value = _shortcutToEdit.CaptureVolume;
}
else
{
nud_capture_volume.Value = Convert.ToDecimal(captureDevice.Volume);
rb_set_capture_volume.Checked = false;
rb_keep_capture_volume.Checked = true;
nud_capture_volume.Value = 50;
}
}
}
}
// We need to handle the edgecase where the selected capture device
// isn't currently plugged in. We don't want to break the shortcut
// as it could be plugged in when it comes time to actually run
// the shortcut, so we need to just add it to the list to not break
// the UI.
if (!foundCaptureDevice)
{
int index = cb_capture_device.Items.Add(_shortcutToEdit.CaptureDevice);
cb_capture_device.SelectedIndex = index;
selectedCaptureDevice = null;
if (_shortcutToEdit.SetCaptureVolume && _shortcutToEdit.CaptureVolume >= 0 && _shortcutToEdit.CaptureVolume <= 100)
{
rb_set_capture_volume.Checked = true;
nud_capture_volume.Value = _shortcutToEdit.CaptureVolume;
}
else
{
// Then we need to populate the list
foreach (CoreAudioDevice captureDevice in captureDevices)
{
int index = cb_capture_device.Items.Add(captureDevice.FullName);
// Set the capture device to the default device by default
if (captureDevice.IsDefaultDevice)
{
selectedCaptureDevice = captureDevice;
cb_capture_device.SelectedIndex = index;
nud_capture_volume.Value = Convert.ToDecimal(captureDevice.Volume);
}
}
rb_keep_capture_volume.Checked = true;
nud_capture_volume.Value = 50;
}
}
else
{
// There are no active audio devices found
// so we hide all audio changing controls
gb_capture_settings.Visible = false;
lbl_no_active_capture_devices.Visible = true;
logger.Warn($"ShortcutForm/ShortcutForm_Load: No active capture devices so hiding the microphone input controls.");
}
}
else
{
// Then we need to populate the list
foreach (CoreAudioDevice captureDevice in captureDevices)
{
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.Active)
{
int index = cb_capture_device.Items.Add(captureDevice.FullName);
// Set the capture device to the default device by default
if (captureDevice.IsDefaultDevice)
{
selectedCaptureDevice = captureDevice;
cb_capture_device.SelectedIndex = index;
nud_capture_volume.Value = Convert.ToDecimal(captureDevice.Volume);
}
}
}
rb_keep_capture_volume.Checked = true;
}
// Audio Controller == null, so the audio device isn't supported by AudioSwitcher.CoreAudio!
// We just have to disable the switching functionality at present :(
// Hopefully I find another library that works with everything including RealTek Audio
gb_audio_settings.Visible = false;
gb_capture_settings.Visible = false;
lbl_disabled_shortcut_audio_chipset.Visible = true;
// We also force the audio settings to off, just in case
rb_change_audio.Checked = false;
rb_set_audio_volume.Checked = false;
rb_change_capture.Checked = false;
rb_set_capture_volume.Checked = false;
}
// Populate a full list of games
// Start with the Steam Games