From 8b8ff190d98d20126d515b821645d957d1335ca6 Mon Sep 17 00:00:00 2001 From: Mark Heath Date: Sat, 22 Jul 2017 11:51:40 +0100 Subject: [PATCH] improved error message for channel index out of range #208 --- NAudio/CoreAudioApi/AudioMeterInformationChannels.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/NAudio/CoreAudioApi/AudioMeterInformationChannels.cs b/NAudio/CoreAudioApi/AudioMeterInformationChannels.cs index 9dba54f2..668927c4 100644 --- a/NAudio/CoreAudioApi/AudioMeterInformationChannels.cs +++ b/NAudio/CoreAudioApi/AudioMeterInformationChannels.cs @@ -30,7 +30,7 @@ namespace NAudio.CoreAudioApi /// public class AudioMeterInformationChannels { - readonly IAudioMeterInformation audioMeterInformation; + private readonly IAudioMeterInformation audioMeterInformation; /// /// Metering Channel Count @@ -54,8 +54,14 @@ public float this[int index] { get { + var channels = Count; + if (index >= channels) + { + throw new ArgumentOutOfRangeException(nameof(index), + $"Peak index cannot be greater than number of channels ({channels})"); + } var peakValues = new float[Count]; - GCHandle Params = GCHandle.Alloc(peakValues, GCHandleType.Pinned); + var Params = GCHandle.Alloc(peakValues, GCHandleType.Pinned); Marshal.ThrowExceptionForHR(audioMeterInformation.GetChannelsPeakValues(peakValues.Length, Params.AddrOfPinnedObject())); Params.Free(); return peakValues[index];