Fixed audio volume setting

Modified some logic so that the audio device
volume and capture device volume will still
be applied even if the current device is selected.
Previous logic incorrectly would only change the
volume if the audio device was also changed.
Volume is always evaluated even if the audio
device doesn't have to change (unless don't
change audio device or don't change capture
device are selected).
This commit is contained in:
Terry MacDonald 2021-04-10 16:28:31 +12:00
parent 96749e8ba8
commit b83478a031

View File

@ -640,12 +640,11 @@ namespace DisplayMagician
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.AudioDevice} audio device."); logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.AudioDevice} audio device.");
needToChangeAudioDevice = true; needToChangeAudioDevice = true;
} }
else
{
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices.");
}
} }
if (needToChangeAudioDevice)
{
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.AudioDevice} audio device."); logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.AudioDevice} audio device.");
foreach (CoreAudioDevice audioDevice in activeAudioDevices) foreach (CoreAudioDevice audioDevice in activeAudioDevices)
@ -654,19 +653,27 @@ namespace DisplayMagician
{ {
// use the Audio Device // use the Audio Device
audioDevice.SetAsDefault(); audioDevice.SetAsDefault();
}
}
}
else
{
logger.Info($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices.");
}
if (shortcutToUse.SetAudioVolume) if (shortcutToUse.SetAudioVolume)
{ {
logger.Debug($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.AudioDevice} audio level to {shortcutToUse.AudioVolume}%."); logger.Info($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.AudioDevice} volume level to {shortcutToUse.AudioVolume}%.");
Task myTask = new Task(() => Task myTask = new Task(() =>
{ {
audioDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.AudioVolume)); _audioController.DefaultPlaybackDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.AudioVolume));
}); });
myTask.Start(); myTask.Start();
myTask.Wait(2000); myTask.Wait(2000);
} }
else
} {
logger.Info($"ShortcutRepository/RunShortcut: We don't need to set the {shortcutToUse.AudioDevice} volume level.");
} }
} }
else else
@ -705,12 +712,10 @@ namespace DisplayMagician
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.CaptureDevice} capture (microphone) device."); logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
needToChangeCaptureDevice = true; needToChangeCaptureDevice = true;
} }
else
{
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices.");
}
} }
if (needToChangeCaptureDevice)
{
logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.CaptureDevice} capture (microphone) device."); logger.Info($"ShortcutRepository/RunShortcut: Changing to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
foreach (CoreAudioDevice captureDevice in activeCaptureDevices) foreach (CoreAudioDevice captureDevice in activeCaptureDevices)
@ -719,24 +724,33 @@ namespace DisplayMagician
{ {
// use the Audio Device // use the Audio Device
captureDevice.SetAsDefault(); 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 else
{ {
logger.Info($"ShortcutRepository/RunShortcut: Shortcut does not require changing Capture Device."); logger.Info($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices.");
}
if (shortcutToUse.SetCaptureVolume)
{
logger.Info($"ShortcutRepository/RunShortcut: Setting {shortcutToUse.CaptureDevice} capture (microphone) level to {shortcutToUse.CaptureVolume}%.");
Task myTask = new Task(() =>
{
_audioController.DefaultCaptureDevice.SetVolumeAsync(Convert.ToDouble(shortcutToUse.CaptureVolume));
});
myTask.Start();
myTask.Wait(2000);
}
else
{
logger.Info($"ShortcutRepository/RunShortcut: We don't need to set the {shortcutToUse.CaptureDevice} capture (microphone) volume level.");
}
}
else
{
logger.Info($"ShortcutRepository/RunShortcut: Shortcut does not require changing capture (microphone) device.");
} }
} }
else else
@ -819,7 +833,7 @@ namespace DisplayMagician
} }
else else
{ {
logger.Debug($"ShortcutRepository/RunShortcut: No programs to start before the main game or executable"); logger.Info($"ShortcutRepository/RunShortcut: No programs to start before the main game or executable");
} }
// Add a status notification icon in the status area // Add a status notification icon in the status area