From cbee85a7830e8edff3049d1d712184e6c843a858 Mon Sep 17 00:00:00 2001 From: frgnca Date: Sun, 27 May 2018 14:50:39 -0400 Subject: [PATCH 01/16] change version number --- SOURCE/Properties/AssemblyInfo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SOURCE/Properties/AssemblyInfo.cs b/SOURCE/Properties/AssemblyInfo.cs index 44baa7b..ab408d5 100644 --- a/SOURCE/Properties/AssemblyInfo.cs +++ b/SOURCE/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("3.0.0.0")] -[assembly: AssemblyFileVersion("3.0.0.0")] +[assembly: AssemblyVersion("3.0.0.2")] +[assembly: AssemblyFileVersion("3.0.0.2")] From 6d0347e19bfd7eafa5acf3aa58df4d044aaaed9d Mon Sep 17 00:00:00 2001 From: frgnca Date: Sun, 27 May 2018 14:51:13 -0400 Subject: [PATCH 02/16] add nuspec file --- SOURCE/AudioDeviceCmdlets.nuspec | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 SOURCE/AudioDeviceCmdlets.nuspec diff --git a/SOURCE/AudioDeviceCmdlets.nuspec b/SOURCE/AudioDeviceCmdlets.nuspec new file mode 100644 index 0000000..db137b0 --- /dev/null +++ b/SOURCE/AudioDeviceCmdlets.nuspec @@ -0,0 +1,15 @@ + + + + AudioDeviceCmdlets + AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows + 3.0.0.2 + Package v3.0 for NuGet publication + Francois Gendron + https://github.com/frgnca/AudioDeviceCmdlets + Copyright (c) 2016-2018 Francois Gendron + https://github.com/frgnca/AudioDeviceCmdlets/blob/master/LICENSE + false + audio default playback recording device session volume mute mixer toggle switch set + + \ No newline at end of file From 03a0fc90802d61fd75389da208e95cb444241fbc Mon Sep 17 00:00:00 2001 From: frgnca Date: Sun, 27 May 2018 14:52:29 -0400 Subject: [PATCH 03/16] remove email address from author --- SOURCE/AudioDeviceCmdlets.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SOURCE/AudioDeviceCmdlets.nuspec b/SOURCE/AudioDeviceCmdlets.nuspec index db137b0..82d46c1 100644 --- a/SOURCE/AudioDeviceCmdlets.nuspec +++ b/SOURCE/AudioDeviceCmdlets.nuspec @@ -5,7 +5,7 @@ AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows 3.0.0.2 Package v3.0 for NuGet publication - Francois Gendron + Francois Gendron https://github.com/frgnca/AudioDeviceCmdlets Copyright (c) 2016-2018 Francois Gendron https://github.com/frgnca/AudioDeviceCmdlets/blob/master/LICENSE From 97e732d0d1125d63381bbd5dcc8692450640dfab Mon Sep 17 00:00:00 2001 From: frgnca Date: Sun, 27 May 2018 14:53:57 -0400 Subject: [PATCH 04/16] remove email from copyright --- SOURCE/AudioDeviceCmdlets.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SOURCE/AudioDeviceCmdlets.nuspec b/SOURCE/AudioDeviceCmdlets.nuspec index 82d46c1..561e194 100644 --- a/SOURCE/AudioDeviceCmdlets.nuspec +++ b/SOURCE/AudioDeviceCmdlets.nuspec @@ -7,7 +7,7 @@ Package v3.0 for NuGet publication Francois Gendron https://github.com/frgnca/AudioDeviceCmdlets - Copyright (c) 2016-2018 Francois Gendron + Copyright (c) 2016-2018 Francois Gendron https://github.com/frgnca/AudioDeviceCmdlets/blob/master/LICENSE false audio default playback recording device session volume mute mixer toggle switch set From 7ce17e43d143128777f1d8fc6fefe2ae1a45f2db Mon Sep 17 00:00:00 2001 From: frgnca Date: Fri, 8 Jun 2018 18:47:10 -0400 Subject: [PATCH 05/16] change future download link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c95bc59..efed871 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Set volume and mute state of default audio device (playback/recording) ## Import Cmdlet to PowerShell -Download AudioDeviceCmdlets.dll +Download AudioDeviceCmdlets.dll ```powershell New-Item "$($profile | split-path)\Modules\AudioDeviceCmdlets" -Type directory -Force Copy-Item "C:\Path\to\AudioDeviceCmdlets.dll" "$($profile | split-path)\Modules\AudioDeviceCmdlets\AudioDeviceCmdlets.dll" From fe51c8d09d650f71238639e8097f0cc561bcd8bd Mon Sep 17 00:00:00 2001 From: frgnca Date: Fri, 8 Jun 2018 19:08:51 -0400 Subject: [PATCH 06/16] change version number and future release notes --- SOURCE/AudioDeviceCmdlets.nuspec | 4 ++-- SOURCE/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SOURCE/AudioDeviceCmdlets.nuspec b/SOURCE/AudioDeviceCmdlets.nuspec index 561e194..83b0206 100644 --- a/SOURCE/AudioDeviceCmdlets.nuspec +++ b/SOURCE/AudioDeviceCmdlets.nuspec @@ -3,8 +3,8 @@ AudioDeviceCmdlets AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows - 3.0.0.2 - Package v3.0 for NuGet publication + 3.1.0.0 + TBD Francois Gendron https://github.com/frgnca/AudioDeviceCmdlets Copyright (c) 2016-2018 Francois Gendron diff --git a/SOURCE/Properties/AssemblyInfo.cs b/SOURCE/Properties/AssemblyInfo.cs index ab408d5..d4bdc6a 100644 --- a/SOURCE/Properties/AssemblyInfo.cs +++ b/SOURCE/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("3.0.0.2")] -[assembly: AssemblyFileVersion("3.0.0.2")] +[assembly: AssemblyVersion("3.1.0.0")] +[assembly: AssemblyFileVersion("3.1.0.0")] From a017d2ecfa21d9a78b3a42922d7bcfa6eb325715 Mon Sep 17 00:00:00 2001 From: frgnca Date: Mon, 24 Jan 2022 13:07:17 -0500 Subject: [PATCH 07/16] Default Communication Device (#30) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * change version number * change version number * set-audiodevice by object, ID, or index no longer sets as communication device * add bare version of cmdlet AudioDeviceCommunication * spacing * change installation instructions (#45) * change copyright year * change copyright year * Update build from source instructions The System.Management.Automation package is only compatible with .NET Core. The legacy version is bundled with Microsoft.PowerShell.5.1.ReferenceAssemblies * Add PlaybackOnly parameter to Set-AudioDevice Used when setting default audio device to only set it as default playback and not also default communication device * Add CommunicationOnly parameter to Set-AudioDevice The opposite of PlaybackOnly, only set the default communication device, and not default playback device. * Rename PlaybackOnly to DeafultOnly * revert to version without additional cmdlet * Revert "set-audiodevice by object, ID, or index no longer sets as communication device" This reverts commit b16518d92670ee3f8b952f4d89f622e493b9a7a6. * change version number * set-audiodevice by object, ID, or index no longer sets as communication device * add bare version of cmdlet AudioDeviceCommunication * spacing * change copyright year * change copyright year * revert to version without additional cmdlet * Revert "set-audiodevice by object, ID, or index no longer sets as communication device" This reverts commit b16518d92670ee3f8b952f4d89f622e493b9a7a6. * update copyright year * update build instructions * fix new line in build instructions * change attribution * add DefaultCommunication attribute to AudioDevice class change List parameter to support new AudioDevice * change AudioDevice creation in remaining parameters to support DefaultCommunication fix typo * add support for parameters PlaybackCommunication and RecordingCommunication * remove duplicate step in build instructions * add support in get cmdlet for parameters PlaybackCommunicationMute and RecordingCommunicationMute fix typo * add support in get cmdlet for parameters PlaybackCommunicationVolume and RecordingCommunicationVolume * add support in get cmdlet for parameters PlaybackCommunicationMuteToggle and RecordingCommunicationMuteToggle * add suggestion by @ImportTaste * add new features descriptions * update Usage section * add parameters PlaybackCommunicationMute and RecordingCommunicationMute to Set cmdlet * fix comment typo * Update README.md * add parameters PlaybackCommunicationMeter, PlaybackCommunicationStream, RecordingCommunicationMeter, and RecordingCommunicationStream to Write cmdlet * remove file added by mistake * fix typo * add parameters PlaybackCommunicationVolume and RecordingCommunicationVolume to Set cmdlet Co-authored-by: Øyvind Heddeland Instefjord --- EXAMPLE/Toggle-AudioDevice.ps1 | 2 +- LICENSE | 2 +- README.md | 122 ++++-- SOURCE/AudioDeviceCmdlets.cs | 657 +++++++++++++++++++++++++++--- SOURCE/AudioDeviceCmdlets.nuspec | 15 - SOURCE/Properties/AssemblyInfo.cs | 2 +- 6 files changed, 692 insertions(+), 108 deletions(-) delete mode 100644 SOURCE/AudioDeviceCmdlets.nuspec diff --git a/EXAMPLE/Toggle-AudioDevice.ps1 b/EXAMPLE/Toggle-AudioDevice.ps1 index 0ac9f58..7aa2812 100644 --- a/EXAMPLE/Toggle-AudioDevice.ps1 +++ b/EXAMPLE/Toggle-AudioDevice.ps1 @@ -1,5 +1,5 @@ <# -Copyright (c) 2016-2018 Francois Gendron +Copyright (c) 2016-2022 Francois Gendron MIT License This file is a script that toggles between two playback audio devices using diff --git a/LICENSE b/LICENSE index 7006b4a..b2c69ec 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016-2018 Francois Gendron +Copyright (c) 2016-2022 Francois Gendron Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 2c13b96..e3ac86d 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,13 @@ AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on ## Features Get list of all audio devices Get default audio device (playback/recording) +Get default communication audio device (playback/recording) Get volume and mute state of default audio device (playback/recording) +Get volume and mute state of default communication audio device (playback/recording) Set default audio device (playback/recording) +Set default communication audio device (playback/recording) Set volume and mute state of default audio device (playback/recording) +Set volume and mute state of default communication audio device (playback/recording) ## Installation @@ -19,66 +23,104 @@ Install-Module -Name AudioDeviceCmdlets ## Usage ```PowerShell -Get-AudioDevice -List # Outputs a list of all devices as - -ID # Outputs the device with the ID corresponding to the given - -Index # Outputs the device with the Index corresponding to the given - -Playback # Outputs the default playback device as - -PlaybackMute # Outputs the default playback device's mute state as - -PlaybackVolume # Outputs the default playback device's volume level on 100 as - -Recording # Outputs the default recording device as - -RecordingMute # Outputs the default recording device's mute state as - -RecordingVolume # Outputs the default recording device's volume level on 100 as +Get-AudioDevice -ID # Get the device with the ID corresponding to the given +Get-AudioDevice -Index # Get the device with the Index corresponding to the given +Get-AudioDevice -List # Get a list of all enabled devices as +Get-AudioDevice -PlaybackCommunication # Get the default communication playback device as +Get-AudioDevice -PlaybackCommunicationMute # Get the default communication playback device's mute state as +Get-AudioDevice -PlaybackCommunicationVolume # Get the default communication playback device's volume level on 100 as +Get-AudioDevice -Playback # Get the default playback device as +Get-AudioDevice -PlaybackMute # Get the default playback device's mute state as +Get-AudioDevice -PlaybackVolume # Get the default playback device's volume level on 100 as +Get-AudioDevice -RecordingCommunication # Get the default communication recording device as +Get-AudioDevice -RecordingCommunicationMute # Get the default communication recording device's mute state as +Get-AudioDevice -RecordingCommunicationVolume # Get the default communication recording device's volume level on 100 as +Get-AudioDevice -Recording # Get the default recording device as +Get-AudioDevice -RecordingMute # Get the default recording device's mute state as +Get-AudioDevice -RecordingVolume # Get the default recording device's volume level on 100 as + ``` ```PowerShell -Set-AudioDevice # Sets the default playback/recording device to the given , can be piped - -ID # Sets the default playback/recording device to the device with the ID corresponding to the given - -Index # Sets the default playback/recording device to the device with the Index corresponding to the given - -PlaybackMute # Sets the default playback device's mute state to the given - -PlaybackMuteToggle # Toggles the default playback device's mute state - -PlaybackVolume # Sets the default playback device's volume level on 100 to the given - -RecordingMute # Sets the default recording device's mute state to the given - -RecordingMuteToggle # Toggles the default recording device's mute state - -RecordingVolume # Sets the default recording device's volume level on 100 to the given +Set-AudioDevice # Set the given playback/recording device as both the default device and the default communication device, for its type +Set-AudioDevice -CommunicationOnly # Set the given playback/recording device as the default communication device and not the default device, for its type +Set-AudioDevice -DefaultOnly # Set the given playback/recording device as the default device and not the default communication device, for its type +Set-AudioDevice -ID # Set the device with the ID corresponding to the given as both the default device and the default communication device, for its type +Set-AudioDevice -ID -CommunicationOnly # Set the device with the ID corresponding to the given as the default communication device and not the default device, for its type +Set-AudioDevice -ID -DefaultOnly # Set the device with the ID corresponding to the given as the default device and not the default communication device, for its type +Set-AudioDevice -Index # Set the device with the Index corresponding to the given as both the default device and the default communication device, for its type +Set-AudioDevice -Index -CommunicationOnly # Set the device with the Index corresponding to the given as the default communication device and not the default device, for its type +Set-AudioDevice -Index -DefaultOnly # Set the device with the Index corresponding to the given as the default device and not the default communication device, for its type +Set-AudioDevice -PlaybackCommunicationMuteToggle # Set the default communication playback device's mute state to the opposite of its current mute state +Set-AudioDevice -PlaybackCommunicationMute # Set the default communication playback device's mute state to the given +Set-AudioDevice -PlaybackCommunicationVolume # Set the default communication playback device's volume level on 100 to the given +Set-AudioDevice -PlaybackMuteToggle # Set the default playback device's mute state to the opposite of its current mute state +Set-AudioDevice -PlaybackMute # Set the default playback device's mute state to the given +Set-AudioDevice -PlaybackVolume # Set the default playback device's volume level on 100 to the given +Set-AudioDevice -RecordingCommunicationMuteToggle # Set the default communication recording device's mute state to the opposite of its current mute state +Set-AudioDevice -RecordingCommunicationMute # Set the default communication recording device's mute state to the given +Set-AudioDevice -RecordingCommunicationVolume # Set the default communication recording device's volume level on 100 to the given +Set-AudioDevice -RecordingMuteToggle # Set the default recording device's mute state to the opposite of its current mute state +Set-AudioDevice -RecordingMute # Set the default recording device's mute state to the given +Set-AudioDevice -RecordingVolume # Set the default recording device's volume level on 100 to the given ``` ```PowerShell -Write-AudioDevice -PlaybackMeter # Writes the default playback device's power output on 100 as a meter - -PlaybackSteam # Writes the default playback device's power output on 100 as a stream of - -RecordingMeter # Writes the default recording device's power output on 100 as a meter - -RecordingSteam # Writes the default recording device's power output on 100 as a stream of +Write-AudioDevice -PlaybackCommunicationMeter # Write the default playback device's power output on 100 as a meter +Write-AudioDevice -PlaybackCommunicationStream # Write the default playback device's power output on 100 as a stream of +Write-AudioDevice -PlaybackMeter # Write the default playback device's power output on 100 as a meter +Write-AudioDevice -PlaybackStream # Write the default playback device's power output on 100 as a stream of +Write-AudioDevice -RecordingCommunicationMeter # Write the default recording device's power output on 100 as a meter +Write-AudioDevice -RecordingCommunicationStream # Write the default recording device's power output on 100 as a stream of +Write-AudioDevice -RecordingMeter # Write the default recording device's power output on 100 as a meter +Write-AudioDevice -RecordingStream # Write the default recording device's power output on 100 as a stream of ``` ## Build Cmdlet from source -1. Using Visual Studio Community, create new project from SOURCE folder - File -> New -> Project From Existing Code... - +1. Install Visual Studio 2022 + + Workloads: .NET desktop development + +2. Create new project from SOURCE folder +File -> New -> Project From Existing Code... + Type of project: Visual C# Folder: SOURCE Name: AudioDeviceCmdlets Output type: Class Library -2. Install System.Management.Automation NuGet package - Project -> Manage NuGet Packages... - - Browse: System.Management.Automation - Install: v6.3+ - 3. Set project properties - Project -> AudioDeviceCmdlets Properties... +Project -> AudioDeviceCmdlets Properties Assembly name: AudioDeviceCmdlets - Target framework: .NET Framework 4.5+ + Target framework: .NET Framework 4.6.1+ + +4. Install System.Management.Automation NuGet legacy package +Project -> Manage NuGet Packages... + + Package source: nuget.org + Browse: Microsoft.PowerShell.5.1.ReferenceAssemblies + Install: v1.0.0+ -4. Set solution configuration - Build -> Configuration Manager... +5. Set solution configuration +Build -> Configuration Manager... Active solution configuration: Release -5. Build Cmdlet - Build -> Build AudioDeviceCmdlets +6. Build Cmdlet +Build -> Build Solution - AudioDeviceCmdlets\bin\Release\AudioDeviceCmdlets.dll + AudioDeviceCmdlets\SOURCE\bin\Release\AudioDeviceCmdlets.dll + +7. Import Cmdlet to PowerShell on Windows + ```PowerShell + $FilePath = "C:\Path\To\AudioDeviceCmdlets\SOURCE\bin\Release\AudioDeviceCmdlets.dll" + New-Item "$($profile | split-path)\Modules\AudioDeviceCmdlets" -Type directory -Force + Copy-Item $FilePath "$($profile | split-path)\Modules\AudioDeviceCmdlets\AudioDeviceCmdlets.dll" + Set-Location "$($profile | Split-Path)\Modules\AudioDeviceCmdlets" + Get-ChildItem | Unblock-File + Import-Module AudioDeviceCmdlets + ``` 6. Import Cmdlet to PowerShell ```PowerShell @@ -92,7 +134,7 @@ Write-AudioDevice -PlaybackMeter # Writes the default playback device's power o ## Attribution -Based on code posted to Code Project by Ray Molenkamp with comments and suggestions by MadMidi +Based on code originally posted to Code Project by Ray Molenkamp with comments and suggestions by MadMidi http://www.codeproject.com/Articles/18520/Vista-Core-Audio-API-Master-Volume-Control -Based on code posted to GitHub by Chris Hunt +Based on code originally posted to GitHub by Chris Hunt https://github.com/cdhunt/WindowsAudioDevice-Powershell-Cmdlet diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index 916de68..7e28f8f 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -1,5 +1,5 @@ /* - Copyright (c) 2016-2018 Francois Gendron + Copyright (c) 2016-2022 Francois Gendron MIT License AudioDeviceCmdlets.cs @@ -21,6 +21,8 @@ public class AudioDevice public int Index; // Default (for its Type) is either true or false public bool Default; + // DefaultCommunication (for its Type) is either true or false + public bool DefaultCommunication; // Type is either "Playback" or "Recording" public string Type; // Name of the MMDevice ex: "Speakers (Realtek High Definition Audio)" @@ -31,7 +33,7 @@ public class AudioDevice public MMDevice Device; // To be created, a new AudioDevice needs an Index, and the MMDevice it will communicate with - public AudioDevice(int Index, MMDevice BaseDevice, bool Default = false) + public AudioDevice(int Index, MMDevice BaseDevice, bool Default = false, bool DefaultCommunication = false) { // Set this object's Index to the received integer this.Index = Index; @@ -39,6 +41,9 @@ public AudioDevice(int Index, MMDevice BaseDevice, bool Default = false) // Set this object's Default to the received boolean this.Default = Default; + // Set this object's DefaultCommunication to the received boolean + this.DefaultCommunication = DefaultCommunication; + // If the received MMDevice is a playback device if (BaseDevice.DataFlow == EDataFlow.eRender) { @@ -95,6 +100,33 @@ public int? Index } private int? index; + // Parameter called to list the default communication playback device + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunication")] + public SwitchParameter PlaybackCommunication + { + get { return playbackcommunication; } + set { playbackcommunication = value; } + } + private bool playbackcommunication; + + // Parameter called to list the default communication playback device's mute state + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationMute")] + public SwitchParameter PlaybackCommunicationMute + { + get { return playbackcommunicationmute; } + set { playbackcommunicationmute = value; } + } + private bool playbackcommunicationmute; + + // Parameter called to list the default communication playback device's volume + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationVolume")] + public SwitchParameter PlaybackCommunicationVolume + { + get { return playbackcommunicationvolume; } + set { playbackcommunicationvolume = value; } + } + private bool playbackcommunicationvolume; + // Parameter called to list the default playback device [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Playback")] public SwitchParameter Playback @@ -122,6 +154,33 @@ public SwitchParameter PlaybackVolume } private bool playbackvolume; + // Parameter called to list the default communication recording device + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunication")] + public SwitchParameter RecordingCommunication + { + get { return recordingcommunication; } + set { recordingcommunication = value; } + } + private bool recordingcommunication; + + // Parameter called to list the default communication recording device's mute state + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationMute")] + public SwitchParameter RecordingCommunicationMute + { + get { return recordingcommunicationmute; } + set { recordingcommunicationmute = value; } + } + private bool recordingcommunicationmute; + + // Parameter called to list the default communication recording device's volume + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationVolume")] + public SwitchParameter RecordingCommunicationVolume + { + get { return recordingcommunicationvolume; } + set { recordingcommunicationvolume = value; } + } + private bool recordingcommunicationvolume; + // Parameter called to list the default recording device [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Recording")] public SwitchParameter Recording @@ -131,7 +190,7 @@ public SwitchParameter Recording } private bool recording; - // Parameter called to list the default recording device' mute state + // Parameter called to list the default recording device's mute state [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingMute")] public SwitchParameter RecordingMute { @@ -140,7 +199,7 @@ public SwitchParameter RecordingMute } private bool recordingmute; - // Parameter called to list the default recording device' volume + // Parameter called to list the default recording device's volume [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingVolume")] public SwitchParameter RecordingVolume { @@ -163,16 +222,34 @@ protected override void ProcessRecord() // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { - // If this MMDevice's ID is either, the same the default playback device's ID, or the same as the default recording device's ID + // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } } else { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself - WriteObject(new AudioDevice(i + 1, DeviceCollection[i])); + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); + } } } @@ -189,16 +266,34 @@ protected override void ProcessRecord() // If this MMDevice's ID is the same as the string received by the ID parameter if (string.Compare(DeviceCollection[i].ID, id, System.StringComparison.CurrentCultureIgnoreCase) == 0) { - // If this MMDevice's ID is either, the same the default playback device's ID, or the same as the default recording device's ID + // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } } else { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself - WriteObject(new AudioDevice(i + 1, DeviceCollection[i])); + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); + } } // Stop checking for other parameters @@ -216,23 +311,41 @@ protected override void ProcessRecord() // If the Index is valid if (index.Value >= 1 && index.Value <= DeviceCollection.Count) { - // If the ID of the MMDevice associated with the Index is the same as, either the ID of the default playback device, or the ID of the default recording device - if (DeviceCollection[index.Value - 1].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[index.Value - 1].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) + // Use valid Index as iterative + int i = index.Value - 1; + + // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) { - // Output the result of the creation of a new AudioDevice while assining it the an index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(index.Value, DeviceCollection[index.Value - 1], true)); - - // Stop checking for other parameters - return; + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } } else { - // Output the result of the creation of a new AudioDevice while assining it the an index, and the MMDevice itself, and a default value of false - WriteObject(new AudioDevice(index.Value, DeviceCollection[index.Value - 1], false)); - - // Stop checking for other parameters - return; + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); + } } + + // Stop checking for other parameters + return; } else { @@ -241,17 +354,75 @@ protected override void ProcessRecord() } } + // If the PlaybackCommunication switch parameter was called + if (playbackcommunication) + { + // For every MMDevice in DeviceCollection + for (int i = 0; i < DeviceCollection.Count; i++) + { + // If this MMDevice's ID is the same as the default communication playback device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID) + { + + // If the MMDevice's ID is the same as the default playback device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + } + } + + // Stop checking for other parameters + return; + } + + // If the PlaybackCommunicationMute switch parameter was called + if (playbackcommunicationmute) + { + // Output the mute state of the default communication playback device + WriteObject(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute); + + // Stop checking for other parameters + return; + } + + // If the PlaybackCommunicationVolume switch parameter was called + if (playbackcommunicationvolume) + { + // Output the current volume level of the default communication playback device + WriteObject(string.Format("{0}%", DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar * 100)); + + // Stop checking for other parameters + return; + } + // If the Playback switch parameter was called if (playback) { // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { - // If this MMDevice's ID is the same the default playback device's ID + // If this MMDevice's ID is the same as the default playback device's ID if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID) { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); + + // If the MMDevice's ID is the same as the default communication playback device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } } } @@ -279,17 +450,74 @@ protected override void ProcessRecord() return; } + // If the RecordingCommunication switch parameter was called + if (recordingcommunication) + { + // For every MMDevice in DeviceCollection + for (int i = 0; i < DeviceCollection.Count; i++) + { + // If this MMDevice's ID is the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + + // If the MMDevice's ID is the same as the default recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + } + } + + // Stop checking for other parameters + return; + } + + // If the RecordingCommunicationMute switch parameter was called + if (recordingcommunicationmute) + { + // Output the mute state of the default communication recording device + WriteObject(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute); + + // Stop checking for other parameters + return; + } + + // If the RecordingCommunicationVolume switch parameter was called + if (recordingcommunicationvolume) + { + // Output the current volume level of the default communication recording device + WriteObject(string.Format("{0}%", DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar * 100)); + + // Stop checking for other parameters + return; + } + // If the Recording switch parameter was called if (recording) { // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { - // If this MMDevice's ID is the same the default recording device's ID + // If this MMDevice's ID is the same as the default recording device's ID if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); + // If the MMDevice's ID is the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } } } @@ -352,6 +580,34 @@ public int? Index } private int? index; + // Parameter called to set the default communication playback device's mute state + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationMute")] + public bool? PlaybackCommunicationMute + { + get { return playbackcommunicationmute; } + set { playbackcommunicationmute = value; } + } + private bool? playbackcommunicationmute; + + // Parameter called to toggle the default communication playback device's mute state + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationMuteToggle")] + public SwitchParameter PlaybackCommunicationMuteToggle + { + get { return playbackcommunicationmutetoggle; } + set { playbackcommunicationmutetoggle = value; } + } + private SwitchParameter playbackcommunicationmutetoggle; + + // Parameter receiving the volume level to set to the default communication playback device + [ValidateRange(0, 100.0f)] + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationVolume")] + public float? PlaybackCommunicationVolume + { + get { return playbackcommunicationvolume; } + set { playbackcommunicationvolume = value; } + } + private float? playbackcommunicationvolume; + // Parameter called to set the default playback device's mute state [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackMute")] public bool? PlaybackMute @@ -370,7 +626,7 @@ public SwitchParameter PlaybackMuteToggle } private SwitchParameter playbackmutetoggle; - // Parameter receiving the volume level to set to the defaut playback device + // Parameter receiving the volume level to set to the default playback device [ValidateRange(0, 100.0f)] [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackVolume")] public float? PlaybackVolume @@ -380,6 +636,34 @@ public float? PlaybackVolume } private float? playbackvolume; + // Parameter called to set the default communication recording device's mute state + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationMute")] + public bool? RecordingCommunicationMute + { + get { return recordingcommunicationmute; } + set { recordingcommunicationmute = value; } + } + private bool? recordingcommunicationmute; + + // Parameter called to toggle the default communication recording device's mute state + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationMuteToggle")] + public SwitchParameter RecordingCommunicationMuteToggle + { + get { return recordingcommunicationmutetoggle; } + set { recordingcommunicationmutetoggle = value; } + } + private SwitchParameter recordingcommunicationmutetoggle; + + // Parameter receiving the volume level to set to the default communication recording device + [ValidateRange(0, 100.0f)] + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationVolume")] + public float? RecordingCommunicationVolume + { + get { return recordingcommunicationvolume; } + set { recordingcommunicationvolume = value; } + } + private float? recordingcommunicationvolume; + // Parameter called to set the default recording device's mute state [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingMute")] public bool? RecordingMute @@ -398,7 +682,7 @@ public SwitchParameter RecordingMuteToggle } private SwitchParameter recordingmutetoggle; - // Parameter receiving the volume level to set to the defaut recording device + // Parameter receiving the volume level to set to the default recording device [ValidateRange(0, 100.0f)] [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingVolume")] public float? RecordingVolume @@ -408,9 +692,34 @@ public float? RecordingVolume } private float? recordingvolume; + // Parameter called to only set device as default and not default communication + [Parameter(Mandatory = false, ParameterSetName = "InputObject")] + [Parameter(Mandatory = false, ParameterSetName = "ID")] + [Parameter(Mandatory = false, ParameterSetName = "Index")] + public SwitchParameter DefaultOnly + { + get { return defaultOnly; } + set { defaultOnly = value; } + } + private SwitchParameter defaultOnly; + + // Parameter called to only set device as default communication and not default + [Parameter(Mandatory = false, ParameterSetName = "InputObject")] + [Parameter(Mandatory = false, ParameterSetName = "ID")] + [Parameter(Mandatory = false, ParameterSetName = "Index")] + public SwitchParameter CommunicationOnly + { + get { return communicationOnly; } + set { communicationOnly = value; } + } + private SwitchParameter communicationOnly; + // Cmdlet execution protected override void ProcessRecord() { + if (defaultOnly.ToBool() && communicationOnly.ToBool()) + throw new System.ArgumentException("Impossible to do both DefaultOnly and CommunicatioOnly at the same time."); + // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); // Create a MMDeviceCollection of every devices that are enabled @@ -427,13 +736,42 @@ protected override void ProcessRecord() { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); - // Using PolicyConfigClient, set the given device as the default playback communication device - client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); - // Using PolicyConfigClient, set the given device as the default playback device - client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); + // Using PolicyConfigClient, set the given device as the default communication device (for its type) + if (!defaultOnly.ToBool()) + client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); + // Using PolicyConfigClient, set the given device as the default device (for its type) + if (!communicationOnly.ToBool()) + client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); - // Output the result of the creation of a new AudioDevice while assining it the an index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); + // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) + { + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } + } + else + { + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); + } + } // Stop checking for other parameters return; @@ -456,12 +794,41 @@ protected override void ProcessRecord() // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default communication device (for its type) - client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); + if (!defaultOnly.ToBool()) + client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default device (for its type) - client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); + if (!communicationOnly.ToBool()) + client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); - // Output the result of the creation of a new AudioDevice while assining it the index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); + // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) + { + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } + } + else + { + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); + } + } // Stop checking for other parameters return; @@ -478,15 +845,47 @@ protected override void ProcessRecord() // If the Index is valid if (index.Value >= 1 && index.Value <= DeviceCollection.Count) { + // Use valid Index as iterative + int i = index.Value - 1; + // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default communication device (for its type) - client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eCommunications); + if (!defaultOnly.ToBool()) + client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default device (for its type) - client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eMultimedia); + if (!communicationOnly.ToBool()) + client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eMultimedia); - // Output the result of the creation of a new AudioDevice while assining it the index, and the MMDevice itself, and a default value of true - WriteObject(new AudioDevice(index.Value, DeviceCollection[index.Value - 1], true)); + // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) + { + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); + } + } + else + { + // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID + if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); + } + else + { + // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); + } + } // Stop checking for other parameters return; @@ -498,6 +897,27 @@ protected override void ProcessRecord() } } + // If the PlaybackCommunicationMute parameter received a value + if (playbackcommunicationmute != null) + { + // Set the mute state of the default communication playback device to that of the boolean value received by the Cmdlet + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute = (bool)playbackcommunicationmute; + } + + // If the PlaybackCommunicationMuteToggle parameter was called + if (playbackcommunicationmutetoggle) + { + // Toggle the mute state of the default communication playback device + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute; + } + + // If the PlaybackCommunicationVolume parameter received a value + if(playbackcommunicationvolume != null) + { + // Set the volume level of the default communication playback device to that of the float value received by the PlaybackCommunicationVolume parameter + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackcommunicationvolume / 100.0f; + } + // If the PlaybackMute parameter received a value if (playbackmute != null) { @@ -505,7 +925,7 @@ protected override void ProcessRecord() DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)playbackmute; } - // If the PlaybackMuteToggle paramter was called + // If the PlaybackMuteToggle parameter was called if (playbackmutetoggle) { // Toggle the mute state of the default playback device @@ -519,6 +939,27 @@ protected override void ProcessRecord() DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackvolume / 100.0f; } + // If the RecordingCommunicationMute parameter received a value + if (recordingcommunicationmute != null) + { + // Set the mute state of the default communication recording device to that of the boolean value received by the Cmdlet + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute = (bool)recordingcommunicationmute; + } + + // If the RecordingCommunicationMuteToggle parameter was called + if (recordingcommunicationmutetoggle) + { + // Toggle the mute state of the default communication recording device + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute; + } + + // If the RecordingCommunicationVolume parameter received a value + if (recordingcommunicationvolume != null) + { + // Set the volume level of the default communication recording device to that of the float value received by the RecordingCommunicationVolume parameter + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar = (float)recordingcommunicationvolume / 100.0f; + } + // If the RecordingMute parameter received a value if (recordingmute != null) { @@ -526,7 +967,7 @@ protected override void ProcessRecord() DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)recordingmute; } - // If the RecordingMuteToggle paramter was called + // If the RecordingMuteToggle parameter was called if (recordingmutetoggle) { // Toggle the mute state of the default recording device @@ -546,6 +987,24 @@ protected override void ProcessRecord() [Cmdlet(VerbsCommunications.Write, "AudioDevice")] public class WriteAudioDevice : Cmdlet { + // Parameter called to output audiometer result of the default communication playback device as a progress bar + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationMeter")] + public SwitchParameter PlaybackCommunicationMeter + { + get { return playbackcommunicationmeter; } + set { playbackcommunicationmeter = value; } + } + private bool playbackcommunicationmeter; + + // Parameter called to output audiometer result of the default communication playback device as a stream of values + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackCommunicationStream")] + public SwitchParameter PlaybackCommunicationStream + { + get { return playbackcommunicationstream; } + set { playbackcommunicationstream = value; } + } + private bool playbackcommunicationstream; + // Parameter called to output audiometer result of the default playback device as a progress bar [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackMeter")] public SwitchParameter PlaybackMeter @@ -564,6 +1023,24 @@ public SwitchParameter PlaybackStream } private bool playbackstream; + // Parameter called to output audiometer result of the default communication recording device as a progress bar + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationMeter")] + public SwitchParameter RecordingCommunicationMeter + { + get { return recordingcommunicationmeter; } + set { recordingcommunicationmeter = value; } + } + private bool recordingcommunicationmeter; + + // Parameter called to output audiometer result of the default communication recording device as a stream of values + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingCommunicationStream")] + public SwitchParameter RecordingCommunicationStream + { + get { return recordingcommunicationstream; } + set { recordingcommunicationstream = value; } + } + private bool recordingcommunicationstream; + // Parameter called to output audiometer result of the default recording device as a progress bar [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingMeter")] public SwitchParameter RecordingMeter @@ -588,6 +1065,46 @@ protected override void ProcessRecord() // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); + // If the PlaybackCommunicationMeter parameter was called + if (playbackcommunicationmeter) + { + // Create a new progress bar to output current audiometer result of the default communication playback device + ProgressRecord pr = new ProgressRecord(0, DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).FriendlyName, "Peak Value"); + // Set the progress bar to zero + pr.PercentComplete = 0; + + // Loop until interruption ex: CTRL+C + do + { + // Set progress bar to current audiometer result + pr.PercentComplete = System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100); + + // Write current audiometer result as a progress bar + WriteProgress(pr); + + // Wait 100 milliseconds + System.Threading.Thread.Sleep(100); + } + // Loop interrupted ex: CTRL+C + while (!Stopping); + } + + // If the PlaybackCommunicationStream parameter was called + if (playbackcommunicationstream) + { + // Loop until interruption ex: CTRL+C + do + { + // Write current audiometer result as a value + WriteObject(System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100)); + + // Wait 100 milliseconds + System.Threading.Thread.Sleep(100); + } + // Loop interrupted ex: CTRL+C + while (!Stopping); + } + // If the PlaybackMeter parameter was called if (playbackmeter) { @@ -628,6 +1145,46 @@ protected override void ProcessRecord() while (!Stopping); } + // If the RecordingCommunicationMeter parameter was called + if (recordingcommunicationmeter) + { + // Create a new progress bar to output current audiometer result of the default communication recording device + ProgressRecord pr = new ProgressRecord(0, DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).FriendlyName, "Peak Value"); + // Set the progress bar to zero + pr.PercentComplete = 0; + + // Loop until interruption ex: CTRL+C + do + { + // Set progress bar to current audiometer result + pr.PercentComplete = System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100); + + // Write current audiometer result as a progress bar + WriteProgress(pr); + + // Wait 100 milliseconds + System.Threading.Thread.Sleep(100); + } + // Loop interrupted ex: CTRL+C + while (!Stopping); + } + + // If the RecordingCommunicationStream parameter was called + if (recordingcommunicationstream) + { + // Loop until interruption ex: CTRL+C + do + { + // Write current audiometer result as a value + WriteObject(System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100)); + + // Wait 100 milliseconds + System.Threading.Thread.Sleep(100); + } + // Loop interrupted ex: CTRL+C + while (!Stopping); + } + // If the RecordingMeter parameter was called if (recordingmeter) { diff --git a/SOURCE/AudioDeviceCmdlets.nuspec b/SOURCE/AudioDeviceCmdlets.nuspec deleted file mode 100644 index 83b0206..0000000 --- a/SOURCE/AudioDeviceCmdlets.nuspec +++ /dev/null @@ -1,15 +0,0 @@ - - - - AudioDeviceCmdlets - AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows - 3.1.0.0 - TBD - Francois Gendron - https://github.com/frgnca/AudioDeviceCmdlets - Copyright (c) 2016-2018 Francois Gendron - https://github.com/frgnca/AudioDeviceCmdlets/blob/master/LICENSE - false - audio default playback recording device session volume mute mixer toggle switch set - - \ No newline at end of file diff --git a/SOURCE/Properties/AssemblyInfo.cs b/SOURCE/Properties/AssemblyInfo.cs index d4bdc6a..d48ee15 100644 --- a/SOURCE/Properties/AssemblyInfo.cs +++ b/SOURCE/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("frgn.ca")] [assembly: AssemblyProduct("AudioDeviceCmdlets")] -[assembly: AssemblyCopyright("Copyright (c) 2016-2018 Francois Gendron ")] +[assembly: AssemblyCopyright("Copyright (c) 2016-2022 Francois Gendron ")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] From 02ca18507336c4b8a5adba877f07ad536cd74c1d Mon Sep 17 00:00:00 2001 From: frgnca Date: Mon, 24 Jan 2022 13:09:35 -0500 Subject: [PATCH 08/16] remove duplicate step in build instruction --- README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/README.md b/README.md index e3ac86d..de12564 100644 --- a/README.md +++ b/README.md @@ -122,15 +122,6 @@ Build -> Build Solution Import-Module AudioDeviceCmdlets ``` -6. Import Cmdlet to PowerShell - ```PowerShell - New-Item "$($profile | split-path)\Modules\AudioDeviceCmdlets" -Type directory -Force - Copy-Item "C:\Path\to\AudioDeviceCmdlets.dll" "$($profile | split-path)\Modules\AudioDeviceCmdlets\AudioDeviceCmdlets.dll" - Set-Location "$($profile | Split-Path)\Modules\AudioDeviceCmdlets" - Get-ChildItem | Unblock-File - Import-Module AudioDeviceCmdlets - ``` - ## Attribution From 06147386d4b292b1f2f86b55adb2f374787f55ca Mon Sep 17 00:00:00 2001 From: frgnca Date: Wed, 16 Feb 2022 10:40:53 -0500 Subject: [PATCH 09/16] Get devices from a try-catch (#55) * fix List parameter by getting default devices from a try-catch * fix ID parameter by getting default devices from a try-catch * fix Index parameter by getting default devices from a try-catch * move outside of parameter the try-catch getting the IDs of default devices * replace direct use of DevEnum in Get cmdlet by try-catch in new AudioDeviceCreationToolkit class * put in try-catch the remaining DevEnum.EnumerateAudioEndPoints() add detail to error message rename variable * remove direct use of DevEnum.GetDefaultAudioEndpoint in parameters inputObject, ID, and Index of cmdlet Set * remove direct use of DevEnum.GetDefaultAudioEndpoint in remaining parameters of cmdlet Set * remove direct use of DevEnum.GetDefaultAudioEndpoint in cmdlet Write * move comments inside try-catch * add description of AudioDeviceCreationToolkit fix typo in comments * move DeviceCollection creation inside parameters InputObject, ID, and Index of cmdlet Set --- SOURCE/AudioDeviceCmdlets.cs | 1071 ++++++++++++++++++++++++---------- 1 file changed, 778 insertions(+), 293 deletions(-) diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index 7e28f8f..c99c22b 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -68,6 +68,118 @@ public AudioDevice(int Index, MMDevice BaseDevice, bool Default = false, bool De } } + // Class to get information on a MMDevice towards the creation of a corresponding AudioDevice + public class AudioDeviceCreationToolkit + { + // The MMDeviceEnumerator + public MMDeviceEnumerator DevEnum; + + // To be created, a new AudioDeviceCreationToolkit needs a MMDeviceEnumerator it will use to compare the ID its methods receive + public AudioDeviceCreationToolkit(MMDeviceEnumerator DevEnum) + { + // Set this object's DeviceEnumerator to the received MMDeviceEnumerator + this.DevEnum = DevEnum; + } + + // Method to find out, in a collection of all enabled MMDevice, the Index of a MMDevice, given its ID + public int FindIndex(string ID) + { + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in method AudioDeviceCreationToolkit.FindIndex(string ID) - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + + // For each device in the collection + for (int i = 0; i < DeviceCollection.Count; i++) + { + // If the received ID is the same as this device's ID + if(DeviceCollection[i].ID == ID) + { + // Return this device's Index + return (i + 1); + } + } + + // Error + throw new System.Exception("Error in method AudioDeviceCreationToolkit.FindIndex(string ID) - No MMDevice with the given ID was found in the collection of all enabled MMDevice"); + } + + // Method to find out if a MMDevice is the default MMDevice of its type, given its ID + public bool IsDefault(string ID) + { + string PlaybackID = ""; + try + { + // Get the ID of the default playback device + PlaybackID = (DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)).ID; + } + catch { } + + // If the received ID is the same as the default playback device's ID + if(ID == PlaybackID) + { + return (true); + } + + string RecordingID = ""; + try + { + // Get the ID of the default recording device + RecordingID = (DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia)).ID; + } + catch { } + + // If the received ID is the same as the default recording device's ID + if (ID == RecordingID) + { + return (true); + } + + return (false); + } + + // Method to find out if a MMDevice is the default communication MMDevice of its type, given its ID + public bool IsDefaultCommunication(string ID) + { + string PlaybackCommunicationID = ""; + try + { + // Get the ID of the default communication playback device + PlaybackCommunicationID = (DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications)).ID; + } + catch { } + + // If the received ID is the same as the default communication playback device's ID + if (ID == PlaybackCommunicationID) + { + return (true); + } + + string RecordingCommunicationID = ""; + try + { + // Get the ID of the default communication recording device + RecordingCommunicationID = (DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications)).ID; + } + catch { } + + // If the received ID is the same as the default communication recording device's ID + if (ID == RecordingCommunicationID) + { + return (true); + } + + return (false); + } + } + // Get Cmdlet [Cmdlet(VerbsCommon.Get, "AudioDevice")] public class GetAudioDevice : Cmdlet @@ -213,44 +325,30 @@ protected override void ProcessRecord() { // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); - // Create a MMDeviceCollection of every devices that are enabled - MMDeviceCollection DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); // If the List switch parameter was called if (list) { + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in parameter List - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { - // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } - } - else - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); - } - } + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], Toolkit.IsDefault(DeviceCollection[i].ID), Toolkit.IsDefaultCommunication(DeviceCollection[i].ID))); } // Stop checking for other parameters @@ -260,41 +358,29 @@ protected override void ProcessRecord() // If the ID parameter received a value if (!string.IsNullOrEmpty(id)) { + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in parameter ID - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { // If this MMDevice's ID is the same as the string received by the ID parameter if (string.Compare(DeviceCollection[i].ID, id, System.StringComparison.CurrentCultureIgnoreCase) == 0) { - // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } - } - else - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); - } - } + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], Toolkit.IsDefault(DeviceCollection[i].ID), Toolkit.IsDefaultCommunication(DeviceCollection[i].ID))); // Stop checking for other parameters return; @@ -308,41 +394,29 @@ protected override void ProcessRecord() // If the Index parameter received a value if (index != null) { + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in parameter Index - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + // If the Index is valid if (index.Value >= 1 && index.Value <= DeviceCollection.Count) { // Use valid Index as iterative int i = index.Value - 1; - - // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } - } - else - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); - } - } + + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], Toolkit.IsDefault(DeviceCollection[i].ID), Toolkit.IsDefaultCommunication(DeviceCollection[i].ID))); // Stop checking for other parameters return; @@ -357,26 +431,23 @@ protected override void ProcessRecord() // If the PlaybackCommunication switch parameter was called if (playbackcommunication) { - // For every MMDevice in DeviceCollection - for (int i = 0; i < DeviceCollection.Count; i++) - { - // If this MMDevice's ID is the same as the default communication playback device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID) - { + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); - // If the MMDevice's ID is the same as the default playback device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - } + MMDevice Device = null; + try + { + // Get the default communication playback device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } + + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(Toolkit.FindIndex(Device.ID), Device, Toolkit.IsDefault(Device.ID), Toolkit.IsDefaultCommunication(Device.ID))); // Stop checking for other parameters return; @@ -385,8 +456,20 @@ protected override void ProcessRecord() // If the PlaybackCommunicationMute switch parameter was called if (playbackcommunicationmute) { + MMDevice Device = null; + try + { + // Get the default communication playback device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } + // Output the mute state of the default communication playback device - WriteObject(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute); + WriteObject(Device.AudioEndpointVolume.Mute); // Stop checking for other parameters return; @@ -395,8 +478,20 @@ protected override void ProcessRecord() // If the PlaybackCommunicationVolume switch parameter was called if (playbackcommunicationvolume) { + MMDevice Device = null; + try + { + // Get the default communication playback device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } + // Output the current volume level of the default communication playback device - WriteObject(string.Format("{0}%", DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar * 100)); + WriteObject(string.Format("{0}%", Device.AudioEndpointVolume.MasterVolumeLevelScalar * 100)); // Stop checking for other parameters return; @@ -405,27 +500,24 @@ protected override void ProcessRecord() // If the Playback switch parameter was called if (playback) { - // For every MMDevice in DeviceCollection - for (int i = 0; i < DeviceCollection.Count; i++) - { - // If this MMDevice's ID is the same as the default playback device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID) - { + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); - // If the MMDevice's ID is the same as the default communication playback device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } - } + MMDevice Device = null; + try + { + // Get the default playback device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); } - + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } + + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(Toolkit.FindIndex(Device.ID), Device, Toolkit.IsDefault(Device.ID), Toolkit.IsDefaultCommunication(Device.ID))); + // Stop checking for other parameters return; } @@ -433,8 +525,20 @@ protected override void ProcessRecord() // If the PlaybackMute switch parameter was called if (playbackmute) { + MMDevice Device = null; + try + { + // Get the default playback device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } + // Output the mute state of the default playback device - WriteObject(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute); + WriteObject(Device.AudioEndpointVolume.Mute); // Stop checking for other parameters return; @@ -443,8 +547,20 @@ protected override void ProcessRecord() // If the PlaybackVolume switch parameter was called if(playbackvolume) { + MMDevice Device = null; + try + { + // Get the default playback device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } + // Output the current volume level of the default playback device - WriteObject(string.Format("{0}%", DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar * 100)); + WriteObject(string.Format("{0}%", Device.AudioEndpointVolume.MasterVolumeLevelScalar * 100)); // Stop checking for other parameters return; @@ -453,27 +569,24 @@ protected override void ProcessRecord() // If the RecordingCommunication switch parameter was called if (recordingcommunication) { - // For every MMDevice in DeviceCollection - for (int i = 0; i < DeviceCollection.Count; i++) - { - // If this MMDevice's ID is the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); - // If the MMDevice's ID is the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - } + MMDevice Device = null; + try + { + // Get the default communication recording device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); } + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(Toolkit.FindIndex(Device.ID), Device, Toolkit.IsDefault(Device.ID), Toolkit.IsDefaultCommunication(Device.ID))); + // Stop checking for other parameters return; } @@ -481,8 +594,20 @@ protected override void ProcessRecord() // If the RecordingCommunicationMute switch parameter was called if (recordingcommunicationmute) { + MMDevice Device = null; + try + { + // Get the default communication recording device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } + // Output the mute state of the default communication recording device - WriteObject(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute); + WriteObject(Device.AudioEndpointVolume.Mute); // Stop checking for other parameters return; @@ -491,8 +616,20 @@ protected override void ProcessRecord() // If the RecordingCommunicationVolume switch parameter was called if (recordingcommunicationvolume) { + MMDevice Device = null; + try + { + // Get the default communication recording device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } + // Output the current volume level of the default communication recording device - WriteObject(string.Format("{0}%", DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar * 100)); + WriteObject(string.Format("{0}%", Device.AudioEndpointVolume.MasterVolumeLevelScalar * 100)); // Stop checking for other parameters return; @@ -501,26 +638,24 @@ protected override void ProcessRecord() // If the Recording switch parameter was called if (recording) { - // For every MMDevice in DeviceCollection - for (int i = 0; i < DeviceCollection.Count; i++) + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + MMDevice Device = null; + try { - // If this MMDevice's ID is the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // If the MMDevice's ID is the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } - } + // Get the default recording device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); } - + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } + + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(Toolkit.FindIndex(Device.ID), Device, Toolkit.IsDefault(Device.ID), Toolkit.IsDefaultCommunication(Device.ID))); + // Stop checking for other parameters return; } @@ -528,8 +663,20 @@ protected override void ProcessRecord() // If the RecordingMute switch parameter was called if (recordingmute) { + MMDevice Device = null; + try + { + // Get the default recording device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } + // Output the mute state of the default recording device - WriteObject(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute); + WriteObject(Device.AudioEndpointVolume.Mute); // Stop checking for other parameters return; @@ -538,8 +685,20 @@ protected override void ProcessRecord() // If the RecordingVolume switch parameter was called if (recordingvolume) { + MMDevice Device = null; + try + { + // Get the default recording device + Device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } + // Output the current volume level of the default recording device - WriteObject(string.Format("{0}%", DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar * 100)); + WriteObject(string.Format("{0}%", Device.AudioEndpointVolume.MasterVolumeLevelScalar * 100)); // Stop checking for other parameters return; @@ -722,57 +881,79 @@ protected override void ProcessRecord() // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); - // Create a MMDeviceCollection of every devices that are enabled - MMDeviceCollection DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); // If the InputObject parameter received a value if (inputObject != null) { + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in parameter InputObject - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { // If this MMDevice's ID is the same as the ID of the MMDevice received by the InputObject parameter if (DeviceCollection[i].ID == inputObject.ID) { + // To use during creation of corresponding AudioDevice, assuming it is impossible to do both DefaultOnly and CommunicatioOnly at the same time + bool DefaultState; + bool CommunicationState; + // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); - // Using PolicyConfigClient, set the given device as the default communication device (for its type) + + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + // Unless the DefaultOnly parameter was called if (!defaultOnly.ToBool()) + { + // The DefaultOnly parameter was not called + + // Using PolicyConfigClient, set the given device as the default communication device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); - // Using PolicyConfigClient, set the given device as the default device (for its type) + + // Set default communication state to use + CommunicationState = true; + } + else + { + // The DefaultOnly parameter was called + + // Set default communication state to use + CommunicationState = Toolkit.IsDefaultCommunication(DeviceCollection[i].ID); + } + + // Unless the CommunicationOnly parameter was called if (!communicationOnly.ToBool()) + { + // The CommunicationOnly parameter was not called + + // Using PolicyConfigClient, set the given device as the default device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); - // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } + // Set default state to use + DefaultState = true; } else { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); - } + // The CommunicationOnly parameter was called + + // Set default state to use + DefaultState = Toolkit.IsDefault(DeviceCollection[i].ID); } + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], DefaultState, CommunicationState)); + // Stop checking for other parameters return; } @@ -785,51 +966,75 @@ protected override void ProcessRecord() // If the ID parameter received a value if (!string.IsNullOrEmpty(id)) { + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in parameter ID - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { // If this MMDevice's ID is the same as the string received by the ID parameter if (string.Compare(DeviceCollection[i].ID, id, System.StringComparison.CurrentCultureIgnoreCase) == 0) { + // To use during creation of corresponding AudioDevice, assuming it is impossible to do both DefaultOnly and CommunicatioOnly at the same time + bool DefaultState; + bool CommunicationState; + // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); - // Using PolicyConfigClient, set the given device as the default communication device (for its type) + + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + // Unless the DefaultOnly parameter was called if (!defaultOnly.ToBool()) + { + // The DefaultOnly parameter was not called + + // Using PolicyConfigClient, set the given device as the default communication device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); - // Using PolicyConfigClient, set the given device as the default device (for its type) + + // Set default communication state to use + CommunicationState = true; + } + else + { + // The DefaultOnly parameter was called + + // Set default communication state to use + CommunicationState = Toolkit.IsDefaultCommunication(DeviceCollection[i].ID); + } + + // Unless the CommunicationOnly parameter was called if (!communicationOnly.ToBool()) + { + // The CommunicationOnly parameter was not called + + // Using PolicyConfigClient, set the given device as the default device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); - // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) - { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } + // Set default state to use + DefaultState = true; } else { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); - } + // The CommunicationOnly parameter was called + + // Set default state to use + DefaultState = Toolkit.IsDefault(DeviceCollection[i].ID); } + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], DefaultState, CommunicationState)); + // Stop checking for other parameters return; } @@ -842,51 +1047,75 @@ protected override void ProcessRecord() // If the Index parameter received a value if (index != null) { + MMDeviceCollection DeviceCollection = null; + try + { + // Enumerate all enabled devices in a collection + DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); + } + catch + { + // Error + throw new System.Exception("Error in parameter Index - Failed to create the collection of all enabled MMDevice using MMDeviceEnumerator"); + } + // If the Index is valid if (index.Value >= 1 && index.Value <= DeviceCollection.Count) { // Use valid Index as iterative int i = index.Value - 1; + // To use during creation of corresponding AudioDevice, assuming it is impossible to do both DefaultOnly and CommunicatioOnly at the same time + bool DefaultState; + bool CommunicationState; + // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); - // Using PolicyConfigClient, set the given device as the default communication device (for its type) + + // Create a AudioDeviceCreationToolkit + AudioDeviceCreationToolkit Toolkit = new AudioDeviceCreationToolkit(DevEnum); + + // Unless the DefaultOnly parameter was called if (!defaultOnly.ToBool()) - client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eCommunications); - // Using PolicyConfigClient, set the given device as the default device (for its type) - if (!communicationOnly.ToBool()) - client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eMultimedia); + { + // The DefaultOnly parameter was not called + + // Using PolicyConfigClient, set the given device as the default communication device (for its type) + client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); - // If this MMDevice's ID is either, the same as the default playback device's ID, or the same as the default recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).ID) + // Set default communication state to use + CommunicationState = true; + } + else { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of true, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true, false)); - } + // The DefaultOnly parameter was called + + // Set default communication state to use + CommunicationState = Toolkit.IsDefaultCommunication(DeviceCollection[i].ID); + } + + // Unless the CommunicationOnly parameter was called + if (!communicationOnly.ToBool()) + { + // The CommunicationOnly parameter was not called + + // Using PolicyConfigClient, set the given device as the default device (for its type) + client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); + + // Set default state to use + DefaultState = true; } else { - // If the MMDevice's ID is either, the same as the default communication playback device's ID, or the same as the default communication recording device's ID - if (DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).ID || DeviceCollection[i].ID == DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).ID) - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of true - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, true)); - } - else - { - // Output the result of the creation of a new AudioDevice while assining it an index, and the MMDevice itself, a default value of false, and a default communication value of false - WriteObject(new AudioDevice(i + 1, DeviceCollection[i], false, false)); - } + // The CommunicationOnly parameter was called + + // Set default state to use + DefaultState = Toolkit.IsDefault(DeviceCollection[i].ID); } + // Output the result of the creation of a new AudioDevice, while assining it its index, the MMDevice itself, its default state, and its default communication state + WriteObject(new AudioDevice(i + 1, DeviceCollection[i], DefaultState, CommunicationState)); + // Stop checking for other parameters return; } @@ -900,85 +1129,181 @@ protected override void ProcessRecord() // If the PlaybackCommunicationMute parameter received a value if (playbackcommunicationmute != null) { - // Set the mute state of the default communication playback device to that of the boolean value received by the Cmdlet - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute = (bool)playbackcommunicationmute; + try + { + // Set the mute state of the default communication playback device to that of the boolean value received by the Cmdlet + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute = (bool)playbackcommunicationmute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } } // If the PlaybackCommunicationMuteToggle parameter was called if (playbackcommunicationmutetoggle) { - // Toggle the mute state of the default communication playback device - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute; + try + { + // Toggle the mute state of the default communication playback device + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.Mute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } } // If the PlaybackCommunicationVolume parameter received a value if(playbackcommunicationvolume != null) { - // Set the volume level of the default communication playback device to that of the float value received by the PlaybackCommunicationVolume parameter - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackcommunicationvolume / 100.0f; + try + { + // Set the volume level of the default communication playback device to that of the float value received by the PlaybackCommunicationVolume parameter + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackcommunicationvolume / 100.0f; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } } // If the PlaybackMute parameter received a value if (playbackmute != null) { - // Set the mute state of the default playback device to that of the boolean value received by the Cmdlet - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)playbackmute; + try + { + // Set the mute state of the default playback device to that of the boolean value received by the Cmdlet + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)playbackmute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } } // If the PlaybackMuteToggle parameter was called if (playbackmutetoggle) { - // Toggle the mute state of the default playback device - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute; + try + { + // Toggle the mute state of the default playback device + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } } // If the PlaybackVolume parameter received a value if(playbackvolume != null) { - // Set the volume level of the default playback device to that of the float value received by the PlaybackVolume parameter - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackvolume / 100.0f; + try + { + // Set the volume level of the default playback device to that of the float value received by the PlaybackVolume parameter + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackvolume / 100.0f; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } } // If the RecordingCommunicationMute parameter received a value if (recordingcommunicationmute != null) { - // Set the mute state of the default communication recording device to that of the boolean value received by the Cmdlet - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute = (bool)recordingcommunicationmute; + try + { + // Set the mute state of the default communication recording device to that of the boolean value received by the Cmdlet + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute = (bool)recordingcommunicationmute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } } // If the RecordingCommunicationMuteToggle parameter was called if (recordingcommunicationmutetoggle) { - // Toggle the mute state of the default communication recording device - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute; + try + { + // Toggle the mute state of the default communication recording device + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.Mute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } } // If the RecordingCommunicationVolume parameter received a value if (recordingcommunicationvolume != null) { - // Set the volume level of the default communication recording device to that of the float value received by the RecordingCommunicationVolume parameter - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar = (float)recordingcommunicationvolume / 100.0f; + try + { + // Set the volume level of the default communication recording device to that of the float value received by the RecordingCommunicationVolume parameter + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioEndpointVolume.MasterVolumeLevelScalar = (float)recordingcommunicationvolume / 100.0f; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } } // If the RecordingMute parameter received a value if (recordingmute != null) { - // Set the mute state of the default recording device to that of the boolean value received by the Cmdlet - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)recordingmute; + try + { + // Set the mute state of the default recording device to that of the boolean value received by the Cmdlet + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)recordingmute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } } // If the RecordingMuteToggle parameter was called if (recordingmutetoggle) { - // Toggle the mute state of the default recording device - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute; + try + { + // Toggle the mute state of the default recording device + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } } // If the RecordingVolume parameter received a value if (recordingvolume != null) { - // Set the volume level of the default recording device to that of the float value received by the RecordingVolume parameter - DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)recordingvolume / 100.0f; + try + { + // Set the volume level of the default recording device to that of the float value received by the RecordingVolume parameter + DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)recordingvolume / 100.0f; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } } } } @@ -1068,16 +1393,45 @@ protected override void ProcessRecord() // If the PlaybackCommunicationMeter parameter was called if (playbackcommunicationmeter) { + string FriendlyName = null; + try + { + // Get the name of the default communication playback device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).FriendlyName; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } // Create a new progress bar to output current audiometer result of the default communication playback device - ProgressRecord pr = new ProgressRecord(0, DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).FriendlyName, "Peak Value"); + ProgressRecord pr = new ProgressRecord(0, FriendlyName, "Peak Value"); + // Set the progress bar to zero pr.PercentComplete = 0; // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get the name of the default communication playback device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).FriendlyName; + + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } + // Set progress bar title + pr.Activity = FriendlyName; + // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100); + pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); // Write current audiometer result as a progress bar WriteProgress(pr); @@ -1095,9 +1449,20 @@ protected override void ProcessRecord() // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100)); - + WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); + // Wait 100 milliseconds System.Threading.Thread.Sleep(100); } @@ -1108,16 +1473,45 @@ protected override void ProcessRecord() // If the PlaybackMeter parameter was called if (playbackmeter) { + string FriendlyName = null; + try + { + // Get the name of the default playback device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).FriendlyName; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } // Create a new progress bar to output current audiometer result of the default playback device - ProgressRecord pr = new ProgressRecord(0, DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).FriendlyName, "Peak Value"); + ProgressRecord pr = new ProgressRecord(0, FriendlyName, "Peak Value"); + // Set the progress bar to zero pr.PercentComplete = 0; // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get the name of the default playback device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).FriendlyName; + + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } + // Set progress bar title + pr.Activity = FriendlyName; + // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue * 100); + pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); // Write current audiometer result as a progress bar WriteProgress(pr); @@ -1135,8 +1529,19 @@ protected override void ProcessRecord() // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); + } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue * 100)); + WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); // Wait 100 milliseconds System.Threading.Thread.Sleep(100); @@ -1148,16 +1553,45 @@ protected override void ProcessRecord() // If the RecordingCommunicationMeter parameter was called if (recordingcommunicationmeter) { + string FriendlyName = null; + try + { + // Get the name of the default communication recording device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).FriendlyName; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } // Create a new progress bar to output current audiometer result of the default communication recording device - ProgressRecord pr = new ProgressRecord(0, DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).FriendlyName, "Peak Value"); + ProgressRecord pr = new ProgressRecord(0, FriendlyName, "Peak Value"); + // Set the progress bar to zero pr.PercentComplete = 0; // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get the name of the default communication recording device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).FriendlyName; + + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } + // Set progress bar title + pr.Activity = FriendlyName; + // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100); + pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); // Write current audiometer result as a progress bar WriteProgress(pr); @@ -1175,8 +1609,19 @@ protected override void ProcessRecord() // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); + } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).AudioMeterInformation.MasterPeakValue * 100)); + WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); // Wait 100 milliseconds System.Threading.Thread.Sleep(100); @@ -1188,16 +1633,45 @@ protected override void ProcessRecord() // If the RecordingMeter parameter was called if (recordingmeter) { + string FriendlyName = null; + try + { + // Get the name of the default recording device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).FriendlyName; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } // Create a new progress bar to output current audiometer result of the default recording device - ProgressRecord pr = new ProgressRecord(0, DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).FriendlyName, "Peak Value"); + ProgressRecord pr = new ProgressRecord(0, FriendlyName, "Peak Value"); + // Set the progress bar to zero pr.PercentComplete = 0; // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get the name of the default recording device + FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).FriendlyName; + + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } + // Set progress bar title + pr.Activity = FriendlyName; + // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue * 100); + pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); // Write current audiometer result as a progress bar WriteProgress(pr); @@ -1215,8 +1689,19 @@ protected override void ProcessRecord() // Loop until interruption ex: CTRL+C do { + float MasterPeakValue; + try + { + // Get current audio meter master peak value + MasterPeakValue = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioMeterInformation.MasterPeakValue * 100)); + WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); // Wait 100 milliseconds System.Threading.Thread.Sleep(100); From 3f29a21f7505df94614a6dd1e9ed69ebd023cd5c Mon Sep 17 00:00:00 2001 From: frgnca Date: Fri, 18 Feb 2022 18:17:43 -0500 Subject: [PATCH 10/16] add donation section --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index de12564..e2c2722 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,17 @@ Build -> Build Solution ``` +## Donation + +
+ Thank you for considering + +* BTC [3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF](bitcoin:3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF) +* BCH [qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3](bitcoincash:qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3) +* ETH [0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE](ethereum:0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE) +
+ + ## Attribution Based on code originally posted to Code Project by Ray Molenkamp with comments and suggestions by MadMidi From ba6600fe220c69460cebb727a1ca982d5d7c7877 Mon Sep 17 00:00:00 2001 From: frgnca Date: Fri, 18 Feb 2022 18:23:49 -0500 Subject: [PATCH 11/16] move build instructions inside collapsible section --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e2c2722..22388ae 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ Get-AudioDevice -RecordingCommunicationVolume # Get the default communication re Get-AudioDevice -Recording # Get the default recording device as Get-AudioDevice -RecordingMute # Get the default recording device's mute state as Get-AudioDevice -RecordingVolume # Get the default recording device's volume level on 100 as - ``` ```PowerShell Set-AudioDevice # Set the given playback/recording device as both the default device and the default communication device, for its type @@ -77,6 +76,10 @@ Write-AudioDevice -RecordingStream # Write the default recording device's power ## Build Cmdlet from source + +
+ Build instructions + 1. Install Visual Studio 2022 Workloads: .NET desktop development @@ -121,6 +124,7 @@ Build -> Build Solution Get-ChildItem | Unblock-File Import-Module AudioDeviceCmdlets ``` +
## Donation From 5f5ab69e2aef6644f4c74ff34de0e32949b8a590 Mon Sep 17 00:00:00 2001 From: frgnca Date: Tue, 22 Feb 2022 17:24:38 -0500 Subject: [PATCH 12/16] add Version Parameter --- SOURCE/AudioDeviceCmdlets.cs | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index c99c22b..9a089f0 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -320,6 +320,15 @@ public SwitchParameter RecordingVolume } private bool recordingvolume; + // Parameter called to display version and credit info + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Version")] + public SwitchParameter Version + { + get { return version; } + set { version = value; } + } + private bool version; + // Cmdlet execution protected override void ProcessRecord() { @@ -703,6 +712,29 @@ protected override void ProcessRecord() // Stop checking for other parameters return; } + + // If the Version parameter was called + if (version) + { + // Version text + string text = @" + AudioDeviceCmdlets v3.1 + + Copyright (c) 2016-2022 Francois Gendron + MIT License + + Thank you for considering + BTC 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BCH qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + ETH 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE +"; + + // Write version text + WriteObject(text); + + // Stop checking for other parameters + return; + } } } @@ -873,6 +905,15 @@ public SwitchParameter CommunicationOnly } private SwitchParameter communicationOnly; + // Parameter called to display version and credit info + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Version")] + public SwitchParameter Version + { + get { return version; } + set { version = value; } + } + private bool version; + // Cmdlet execution protected override void ProcessRecord() { @@ -1305,6 +1346,29 @@ protected override void ProcessRecord() throw new System.ArgumentException("No recording AudioDevice found with the default role"); } } + + // If the Version parameter was called + if (version) + { + // Version text + string text = @" + AudioDeviceCmdlets v3.1 + + Copyright (c) 2016-2022 Francois Gendron + MIT License + + Thank you for considering + BTC 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BCH qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + ETH 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE +"; + + // Write version text + WriteObject(text); + + // Stop checking for other parameters + return; + } } } @@ -1384,6 +1448,15 @@ public SwitchParameter RecordingStream } private bool recordingstream; + // Parameter called to display version and credit info + [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Version")] + public SwitchParameter Version + { + get { return version; } + set { version = value; } + } + private bool version; + // Cmdlet execution protected override void ProcessRecord() { @@ -1709,6 +1782,29 @@ protected override void ProcessRecord() // Loop interrupted ex: CTRL+C while (!Stopping); } + + // If the Version parameter was called + if (version) + { + // Version text + string text = @" + AudioDeviceCmdlets v3.1 + + Copyright (c) 2016-2022 Francois Gendron + MIT License + + Thank you for considering + BTC 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BCH qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + ETH 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE +"; + + // Write version text + WriteObject(text); + + // Stop checking for other parameters + return; + } } } } From 1414a075714f38b278ce40fb66cd46c1425c71fd Mon Sep 17 00:00:00 2001 From: frgnca Date: Fri, 4 Mar 2022 07:57:57 -0500 Subject: [PATCH 13/16] fix assembly info --- SOURCE/Properties/AssemblyInfo.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SOURCE/Properties/AssemblyInfo.cs b/SOURCE/Properties/AssemblyInfo.cs index d48ee15..e499d11 100644 --- a/SOURCE/Properties/AssemblyInfo.cs +++ b/SOURCE/Properties/AssemblyInfo.cs @@ -8,9 +8,9 @@ [assembly: AssemblyTitle("AudioDeviceCmdlets")] [assembly: AssemblyDescription("AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("frgn.ca")] +[assembly: AssemblyCompany("frgnca")] [assembly: AssemblyProduct("AudioDeviceCmdlets")] -[assembly: AssemblyCopyright("Copyright (c) 2016-2022 Francois Gendron ")] +[assembly: AssemblyCopyright("(c) 2016-2022 Francois Gendron ")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("3.1.0.0")] -[assembly: AssemblyFileVersion("3.1.0.0")] +[assembly: AssemblyVersion("3.1")] +[assembly: AssemblyFileVersion("3.1")] From 8b31346d69f3cfd553af5339f23651d7a76396f4 Mon Sep 17 00:00:00 2001 From: frgnca Date: Sun, 22 May 2022 08:55:48 -0400 Subject: [PATCH 14/16] change donation section --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 22388ae..0b45ee7 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,11 @@ Build -> Build Solution ## Donation
- Thank you for considering + Thank you for considering a donation -* BTC [3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF](bitcoin:3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF) -* BCH [qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3](bitcoincash:qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3) -* ETH [0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE](ethereum:0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE) + Bitcoin (BTC) 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BitcoinCash (BCH) qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + Ethereum (ETH) 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE
From b87cb8c4bddee73e0b2d340c93f319d0baf622eb Mon Sep 17 00:00:00 2001 From: frgnca Date: Sun, 22 May 2022 08:59:11 -0400 Subject: [PATCH 15/16] change version text --- SOURCE/AudioDeviceCmdlets.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index 9a089f0..5f8e52a 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -723,10 +723,10 @@ AudioDeviceCmdlets v3.1 Copyright (c) 2016-2022 Francois Gendron MIT License - Thank you for considering - BTC 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF - BCH qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 - ETH 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE + Thank you for considering a donation + Bitcoin (BTC) 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BitcoinCash (BCH) qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + Ethereum (ETH) 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE "; // Write version text @@ -1357,10 +1357,10 @@ AudioDeviceCmdlets v3.1 Copyright (c) 2016-2022 Francois Gendron MIT License - Thank you for considering - BTC 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF - BCH qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 - ETH 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE + Thank you for considering a donation + Bitcoin (BTC) 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BitcoinCash (BCH) qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + Ethereum (ETH) 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE "; // Write version text @@ -1793,10 +1793,10 @@ AudioDeviceCmdlets v3.1 Copyright (c) 2016-2022 Francois Gendron MIT License - Thank you for considering - BTC 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF - BCH qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 - ETH 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE + Thank you for considering a donation + Bitcoin (BTC) 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BitcoinCash (BCH) qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + Ethereum (ETH) 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE "; // Write version text From 75d0fad3e3e7ba31e19f3993dbed196494ec69ab Mon Sep 17 00:00:00 2001 From: frgnca Date: Fri, 27 May 2022 21:46:10 -0400 Subject: [PATCH 16/16] increment version --- SOURCE/AudioDeviceCmdlets.cs | 6 +++--- SOURCE/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index 5f8e52a..1f0e02e 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -718,7 +718,7 @@ protected override void ProcessRecord() { // Version text string text = @" - AudioDeviceCmdlets v3.1 + AudioDeviceCmdlets v3.1.0.1 Copyright (c) 2016-2022 Francois Gendron MIT License @@ -1352,7 +1352,7 @@ protected override void ProcessRecord() { // Version text string text = @" - AudioDeviceCmdlets v3.1 + AudioDeviceCmdlets v3.1.0.1 Copyright (c) 2016-2022 Francois Gendron MIT License @@ -1788,7 +1788,7 @@ protected override void ProcessRecord() { // Version text string text = @" - AudioDeviceCmdlets v3.1 + AudioDeviceCmdlets v3.1.0.1 Copyright (c) 2016-2022 Francois Gendron MIT License diff --git a/SOURCE/Properties/AssemblyInfo.cs b/SOURCE/Properties/AssemblyInfo.cs index e499d11..a66d234 100644 --- a/SOURCE/Properties/AssemblyInfo.cs +++ b/SOURCE/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("3.1")] -[assembly: AssemblyFileVersion("3.1")] +[assembly: AssemblyVersion("3.1.0.1")] +[assembly: AssemblyFileVersion("3.1.0.1")]