From 5dec4a65f1500a5237a048d72145348d2ab4c26c Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Sat, 10 Dec 2022 14:52:28 -0800 Subject: [PATCH 1/2] Write-AudioDevice: Allowing -SampleInterval/-AsFloat (Fixes #66). Support for mutlichannel (Fixes #67). Internal refactoring (Fixes #68) --- SOURCE/AudioDeviceCmdlets.cs | 258 ++++++++++++++++++++++------------- 1 file changed, 166 insertions(+), 92 deletions(-) diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index ae7acaf..3d0f806 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -1457,16 +1457,40 @@ public SwitchParameter Version } private bool version; + [Parameter(Position=1)] + public TimeSpan SampleInterval { + get { return sampleInterval; } + set { sampleInterval = value;} + } + + [Parameter(ParameterSetName = "RecordingStream")] + [Parameter(ParameterSetName = "RecordingCommunicationStream")] + [Parameter(ParameterSetName = "PlaybackStream")] + [Parameter(ParameterSetName = "PlaybackCommunicationStream")] + public SwitchParameter AsFloat { + get; set; + } + + [Parameter()] + public SwitchParameter MultiChannel { + get; set; + } + + private TimeSpan sampleInterval = TimeSpan.FromMilliseconds(100); + // Cmdlet execution protected override void ProcessRecord() { // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); + MMDevice device = null; + float MasterPeakValue; + float[] ChannelPeakVolume; + string FriendlyName = null; // If the PlaybackCommunicationMeter parameter was called if (playbackcommunicationmeter) - { - string FriendlyName = null; + { try { // Get the name of the default communication playback device @@ -1486,31 +1510,24 @@ protected override void ProcessRecord() // 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; + 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"); } - // Set progress bar title - pr.Activity = FriendlyName; - - // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); - - // Write current audiometer result as a progress bar - WriteProgress(pr); - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + WriteDeviceProgress(device); + + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1522,22 +1539,25 @@ 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; + 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"); } - // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); + + WriteDeviceOutput(device); + + + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1545,8 +1565,7 @@ protected override void ProcessRecord() // If the PlaybackMeter parameter was called if (playbackmeter) - { - string FriendlyName = null; + { try { // Get the name of the default playback device @@ -1566,31 +1585,22 @@ protected override void ProcessRecord() // 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; + 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"); } - // Set progress bar title - pr.Activity = FriendlyName; - - // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); - - // Write current audiometer result as a progress bar - WriteProgress(pr); - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + WriteDeviceProgress(device); + + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1602,11 +1612,9 @@ 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; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); } catch { @@ -1614,10 +1622,13 @@ protected override void ProcessRecord() throw new System.ArgumentException("No playback AudioDevice found with the default role"); } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); + WriteDeviceOutput(device); - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } + } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1625,8 +1636,7 @@ 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 @@ -1646,31 +1656,23 @@ protected override void ProcessRecord() // 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; + 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"); } - // Set progress bar title - pr.Activity = FriendlyName; - - // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); // Write current audiometer result as a progress bar - WriteProgress(pr); + WriteDeviceProgress(device); - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1682,11 +1684,9 @@ 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; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications); } catch { @@ -1694,10 +1694,12 @@ protected override void ProcessRecord() throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); + WriteDeviceOutput(device); - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1705,8 +1707,7 @@ protected override void ProcessRecord() // If the RecordingMeter parameter was called if (recordingmeter) - { - string FriendlyName = null; + { try { // Get the name of the default recording device @@ -1726,31 +1727,22 @@ protected override void ProcessRecord() // 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; + 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"); } - // Set progress bar title - pr.Activity = FriendlyName; - - // Set progress bar to current audiometer result - pr.PercentComplete = System.Convert.ToInt32(MasterPeakValue * 100); - // Write current audiometer result as a progress bar - WriteProgress(pr); + WriteDeviceProgress(device); - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1762,11 +1754,9 @@ 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; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); } catch { @@ -1774,10 +1764,13 @@ protected override void ProcessRecord() throw new System.ArgumentException("No recording AudioDevice found with the default role"); } // Write current audiometer result as a value - WriteObject(System.Convert.ToInt32(MasterPeakValue * 100)); + WriteDeviceOutput(device); + - // Wait 100 milliseconds - System.Threading.Thread.Sleep(100); + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } // Loop interrupted ex: CTRL+C while (!Stopping); @@ -1806,5 +1799,86 @@ MIT License return; } } + + protected void WriteDeviceProgress(MMDevice device) { + ProgressRecord pr; + AudioMeterInformation audioMeter = device.AudioMeterInformation; + if (MultiChannel) { + + for (int channelNumber = 0; channelNumber < audioMeter.PeakValues.Count;channelNumber++) { + int channelPeak = System.Convert.ToInt32( + audioMeter.PeakValues[channelNumber] * 100); + + if (audioMeter.PeakValues.Count == 2) { + string leftOrRight = String.Empty; + if (channelNumber == 0) { + leftOrRight = "L"; + } else { + leftOrRight = "R"; + } + pr = new ProgressRecord(channelNumber, + device.FriendlyName + String.Format(" ({0})", leftOrRight), + String.Format("{0:P}", audioMeter.PeakValues[channelNumber])); + pr.PercentComplete = channelPeak; + } else { + pr = new ProgressRecord(channelNumber, + device.FriendlyName, + String.Format("{0:P}", audioMeter.PeakValues[channelNumber])); + pr.PercentComplete = channelPeak; + } + WriteProgress(pr); + } + } else { + int masterPeak = System.Convert.ToInt32(device.AudioMeterInformation.MasterPeakValue * 100); + pr = new ProgressRecord(0, device.FriendlyName, String.Format("{0}", masterPeak)); + pr.PercentComplete = masterPeak; + WriteProgress(pr); + } + } + + protected void WriteDeviceOutput(MMDevice device) { + AudioMeterInformation audioMeter = device.AudioMeterInformation; + if (MultiChannel) { + PSObject output = new PSObject(); + output.Members.Add( + (PSMemberInfo)new PSNoteProperty("Device", device.FriendlyName) + ); + + for (int channelNumber = 0; channelNumber < audioMeter.PeakValues.Count;channelNumber++) { + string channelName = "Channel " + channelNumber; + float channelPeak = audioMeter.PeakValues[channelNumber]; + if (AsFloat) { + WriteObject(channelPeak); + continue; + } + if (audioMeter.PeakValues.Count == 2) { + if (channelNumber == 0) { + channelName = "Left"; + } + else { + channelName = "Right"; + } + } + output.Members.Add( + (PSMemberInfo)(new PSNoteProperty(channelName, channelPeak)) + ); + } + if (! AsFloat) { + WriteObject(output); + } + return; + } + + if (AsFloat) { + WriteObject(audioMeter.MasterPeakValue); + return; + } + + WriteObject( + Convert.ToInt32( + audioMeter.MasterPeakValue * 100 + ) + ); + } } } From 965240b86ca33b34c385cf6c8c97172add0a2b1b Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Sat, 10 Dec 2022 15:43:34 -0800 Subject: [PATCH 2/2] Write-AudioDevice: Clearing up parameter sets (Fixes #69) --- SOURCE/AudioDeviceCmdlets.cs | 399 +++++++++++++---------------------- 1 file changed, 149 insertions(+), 250 deletions(-) diff --git a/SOURCE/AudioDeviceCmdlets.cs b/SOURCE/AudioDeviceCmdlets.cs index 3d0f806..34fbfbb 100644 --- a/SOURCE/AudioDeviceCmdlets.cs +++ b/SOURCE/AudioDeviceCmdlets.cs @@ -1373,11 +1373,11 @@ MIT License } // Write Cmdlet - [Cmdlet(VerbsCommunications.Write, "AudioDevice")] + [Cmdlet(VerbsCommunications.Write, "AudioDevice", DefaultParameterSetName="Meter")] 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")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter PlaybackCommunicationMeter { get { return playbackcommunicationmeter; } @@ -1386,7 +1386,7 @@ public SwitchParameter PlaybackCommunicationMeter 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")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter PlaybackCommunicationStream { get { return playbackcommunicationstream; } @@ -1395,7 +1395,7 @@ public SwitchParameter PlaybackCommunicationStream 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")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter PlaybackMeter { get { return playbackmeter; } @@ -1404,7 +1404,7 @@ public SwitchParameter PlaybackMeter private bool playbackmeter; // Parameter called to output audiometer result of the default playback device as a stream of values - [Parameter(Mandatory = true, Position = 0, ParameterSetName = "PlaybackStream")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter PlaybackStream { get { return playbackstream; } @@ -1413,7 +1413,7 @@ 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")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter RecordingCommunicationMeter { get { return recordingcommunicationmeter; } @@ -1422,7 +1422,7 @@ public SwitchParameter RecordingCommunicationMeter 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")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter RecordingCommunicationStream { get { return recordingcommunicationstream; } @@ -1431,7 +1431,7 @@ public SwitchParameter RecordingCommunicationStream 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")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter RecordingMeter { get { return recordingmeter; } @@ -1440,7 +1440,7 @@ public SwitchParameter RecordingMeter private bool recordingmeter; // Parameter called to output audiometer result of the default recording device as a stream of values - [Parameter(Mandatory = true, Position = 0, ParameterSetName = "RecordingStream")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter RecordingStream { get { return recordingstream; } @@ -1457,108 +1457,99 @@ public SwitchParameter Version } private bool version; - [Parameter(Position=1)] + [Parameter(Position=1,ValueFromPipelineByPropertyName=true)] public TimeSpan SampleInterval { get { return sampleInterval; } set { sampleInterval = value;} } - [Parameter(ParameterSetName = "RecordingStream")] - [Parameter(ParameterSetName = "RecordingCommunicationStream")] - [Parameter(ParameterSetName = "PlaybackStream")] - [Parameter(ParameterSetName = "PlaybackCommunicationStream")] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter AsFloat { get; set; } - [Parameter()] + [Parameter(ValueFromPipelineByPropertyName=true)] public SwitchParameter MultiChannel { get; set; } + [Parameter(ValueFromPipelineByPropertyName=true)] + public SwitchParameter Meter { + get; set; + } + + [Parameter(ValueFromPipelineByPropertyName=true)] + public SwitchParameter Stream { + get; set; + } + private TimeSpan sampleInterval = TimeSpan.FromMilliseconds(100); // Cmdlet execution protected override void ProcessRecord() { +// If the Version parameter was called + if (version) + { + // Version text + string text = @" + AudioDeviceCmdlets v3.1.0.2 + + Copyright (c) 2016-2022 Francois Gendron + MIT License + + Thank you for considering a donation + Bitcoin (BTC) 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF + BitcoinCash (BCH) qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 + Ethereum (ETH) 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE +"; + + // Write version text + WriteObject(text); + + // Stop checking for other parameters + return; + } + // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); MMDevice device = null; - float MasterPeakValue; - float[] ChannelPeakVolume; - string FriendlyName = null; + + System.Collections.Generic.List deviceList = new System.Collections.Generic.List(); // If the PlaybackCommunicationMeter parameter was called if (playbackcommunicationmeter) - { + { try { // Get the name of the default communication playback device - FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications).FriendlyName; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); + deviceList.Add(device); + this.Meter = true; } 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, FriendlyName, "Peak Value"); - - // Set the progress bar to zero - pr.PercentComplete = 0; - - // Loop until interruption ex: CTRL+C - do - { - - try - { - // Get the name of 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"); - } - - WriteDeviceProgress(device); - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } - } - // Loop interrupted ex: CTRL+C - while (!Stopping); } // If the PlaybackCommunicationStream parameter was called if (playbackcommunicationstream) { // Loop until interruption ex: CTRL+C - do + try { - try - { - // Get current audio meter master peak value - 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"); - } - - WriteDeviceOutput(device); - - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } - + // Get current audio meter master peak value + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); + deviceList.Add(device); + this.Stream = true; } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } // Loop interrupted ex: CTRL+C while (!Stopping); } @@ -1566,241 +1557,145 @@ protected override void ProcessRecord() // If the PlaybackMeter parameter was called if (playbackmeter) { + // Loop until interruption ex: CTRL+C try { - // Get the name of the default playback device - FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).FriendlyName; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); + deviceList.Add(device); + this.Meter = true; } 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, FriendlyName, "Peak Value"); - - // Set the progress bar to zero - pr.PercentComplete = 0; - - // Loop until interruption ex: CTRL+C - do - { - try - { - 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"); - } - - WriteDeviceProgress(device); - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } - } - // Loop interrupted ex: CTRL+C - while (!Stopping); } // If the PlaybackStream parameter was called if (playbackstream) { - // Loop until interruption ex: CTRL+C - do + try { - try - { - 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"); - } - // Write current audiometer result as a value - WriteDeviceOutput(device); - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } - + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); + deviceList.Add(device); + this.Stream = true; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default role"); } - // Loop interrupted ex: CTRL+C - while (!Stopping); } // If the RecordingCommunicationMeter parameter was called if (recordingcommunicationmeter) - { + { try { - // Get the name of the default communication recording device - FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications).FriendlyName; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications); + deviceList.Add(device); + this.Meter = true; } 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, FriendlyName, "Peak Value"); - - // Set the progress bar to zero - pr.PercentComplete = 0; - - // Loop until interruption ex: CTRL+C - do - { - try - { - 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"); - } - - // Write current audiometer result as a progress bar - WriteDeviceProgress(device); - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } - } - // Loop interrupted ex: CTRL+C - while (!Stopping); + // Loop until interruption ex: CTRL+C } // If the RecordingCommunicationStream parameter was called if (recordingcommunicationstream) { - // Loop until interruption ex: CTRL+C - do + try { - try - { - 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"); - } - // Write current audiometer result as a value - WriteDeviceOutput(device); - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications); + deviceList.Add(device); + this.Stream = true; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default communication role"); } - // Loop interrupted ex: CTRL+C - while (!Stopping); } // If the RecordingMeter parameter was called if (recordingmeter) - { + { try { - // Get the name of the default recording device - FriendlyName = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).FriendlyName; + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); + deviceList.Add(device); + this.Meter = true; } 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, FriendlyName, "Peak Value"); - - // Set the progress bar to zero - pr.PercentComplete = 0; - - // Loop until interruption ex: CTRL+C - do - { - try - { - 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"); - } - - WriteDeviceProgress(device); - - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); - } - } - // Loop interrupted ex: CTRL+C - while (!Stopping); + } } // If the RecordingStream parameter was called if (recordingstream) { - // Loop until interruption ex: CTRL+C - do + try { - try - { - 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"); - } - // Write current audiometer result as a value - WriteDeviceOutput(device); - + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); + deviceList.Add(device); + this.Stream = true; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No recording AudioDevice found with the default role"); + } + } - // Wait for SampleInterval milliseconds - if (SampleInterval.TotalMilliseconds > 0) { - System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + + if (deviceList.Count == 0) { + try + { + // Get the name of the default communication playback device + device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); + deviceList.Add(device); + this.Meter = true; + } + catch + { + // Throw an exception about the device not being found + throw new System.ArgumentException("No playback AudioDevice found with the default communication role"); + } + + if (deviceList.Count == 0) { + return; + } + } + + do + { + int progressID = 0; + foreach (MMDevice deviceInfo in deviceList) { + if (this.Meter) { + WriteDeviceProgress(deviceInfo, progressID); + progressID += deviceInfo.AudioMeterInformation.PeakValues.Count; + } + if (this.Stream) { + WriteDeviceOutput(deviceInfo); } } - // Loop interrupted ex: CTRL+C - while (!Stopping); + + // Wait for SampleInterval milliseconds + if (SampleInterval.TotalMilliseconds > 0) { + System.Threading.Thread.Sleep((int)SampleInterval.TotalMilliseconds); + } } + // Loop interrupted ex: CTRL+C + while (!Stopping); - // If the Version parameter was called - if (version) - { - // Version text - string text = @" - AudioDeviceCmdlets v3.1.0.2 - - Copyright (c) 2016-2022 Francois Gendron - MIT License - - Thank you for considering a donation - Bitcoin (BTC) 3AffczXX4Jb2iN8QWQhHQAsj9AqGFXgYUF - BitcoinCash (BCH) qraf6a3fklta7xkvwkh49zqn6mgnm2eyz589rkfvl3 - Ethereum (ETH) 0xE4EA2A2356C04c8054Db452dCBd6f958F74722dE -"; - - // Write version text - WriteObject(text); - - // Stop checking for other parameters - return; - } } - protected void WriteDeviceProgress(MMDevice device) { + protected void WriteDeviceProgress(MMDevice device, int progressID = 0) { ProgressRecord pr; AudioMeterInformation audioMeter = device.AudioMeterInformation; if (MultiChannel) { @@ -1816,12 +1711,12 @@ protected void WriteDeviceProgress(MMDevice device) { } else { leftOrRight = "R"; } - pr = new ProgressRecord(channelNumber, + pr = new ProgressRecord(progressID + channelNumber, device.FriendlyName + String.Format(" ({0})", leftOrRight), String.Format("{0:P}", audioMeter.PeakValues[channelNumber])); pr.PercentComplete = channelPeak; } else { - pr = new ProgressRecord(channelNumber, + pr = new ProgressRecord(progressID + channelNumber, device.FriendlyName, String.Format("{0:P}", audioMeter.PeakValues[channelNumber])); pr.PercentComplete = channelPeak; @@ -1830,7 +1725,11 @@ protected void WriteDeviceProgress(MMDevice device) { } } else { int masterPeak = System.Convert.ToInt32(device.AudioMeterInformation.MasterPeakValue * 100); - pr = new ProgressRecord(0, device.FriendlyName, String.Format("{0}", masterPeak)); + pr = new ProgressRecord(0, + device.FriendlyName, + String.Format("{0:P}", + device.AudioMeterInformation.MasterPeakValue + )); pr.PercentComplete = masterPeak; WriteProgress(pr); }