Skip to content

Commit 8b8ff19

Browse files
committed
improved error message for channel index out of range #208
1 parent 17c8ca2 commit 8b8ff19

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

NAudio/CoreAudioApi/AudioMeterInformationChannels.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace NAudio.CoreAudioApi
3030
/// </summary>
3131
public class AudioMeterInformationChannels
3232
{
33-
readonly IAudioMeterInformation audioMeterInformation;
33+
private readonly IAudioMeterInformation audioMeterInformation;
3434

3535
/// <summary>
3636
/// Metering Channel Count
@@ -54,8 +54,14 @@ public float this[int index]
5454
{
5555
get
5656
{
57+
var channels = Count;
58+
if (index >= channels)
59+
{
60+
throw new ArgumentOutOfRangeException(nameof(index),
61+
$"Peak index cannot be greater than number of channels ({channels})");
62+
}
5763
var peakValues = new float[Count];
58-
GCHandle Params = GCHandle.Alloc(peakValues, GCHandleType.Pinned);
64+
var Params = GCHandle.Alloc(peakValues, GCHandleType.Pinned);
5965
Marshal.ThrowExceptionForHR(audioMeterInformation.GetChannelsPeakValues(peakValues.Length, Params.AddrOfPinnedObject()));
6066
Params.Free();
6167
return peakValues[index];

0 commit comments

Comments
 (0)