I've got this code
static void Main(string[] args)
{
try
{
Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
long count = reader.Length;
if (count <= int.MaxValue)
{
byte[] info = new byte[count];
reader.Read(info, 0, (int)count);
Console.WriteLine("Succesfull read");
}
else
Console.WriteLine("Could not read");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
that prints out the following exception message
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30
I've dowloaded NAudio code and I've been debugging it but I can't find the cause of the error, although, as you can see on the stack trace is in *NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50*
¿Am I doing something wrong? By the way, it only happens with a few mp3 files, others are read just fine. I'm attaching one of the files that can't be read
Thanx
Comments: Hello Mark, I've continue to debug the code, NAdio include, and the problem has nothing to do with trying to read the whole file at once. Te problem occurs when decompressing one specific frame. The frame read is of size 3072 (bytes), when decompressing with AcmInterop.acmStreamConvert, the resulting decompressed length is of 27648 bytes. But then the decomprresBuffer on Mp3FileReader, where the decompressed bytes are copied, is of length 9216 (in my case), thus the error when trying to copy 27648 bytes into an 9216 lengthed byte array on Mp3FrameDecompressor.DecompressFrame (line 50). First, initialization of decompressBuffer on Mp3FileReader is as follows this.decompressBuffer = new byte[1152 * bytesPerSample * 2]; (which in may case yields an array of lenght 9216) This initialization is acompanied by the following comment: // no MP3 frames have more than 1152 samples in them // some MP3s I seem to get double and this has gotten me thinking "¿Is it alright if I increase the size of decompressBuffer by some value or would that have some freak effect somewhere else on the code? ¿Could it be possible that this particular frame contains more bytes than usual when decompressed or is the some error of AcmInterop.acmStreamConvert, that is, the codecs?" Some comments on this would be great. Thanks a lot again, an love using NAudio. (By the way, all this was tested with the file i previously attached. The conflicted frame is the second frame read if readding after calling Seek(19888128, SeekOrigin.Begin)). I'm pasting some code: public static void NAudioTest() { try { using (Mp3FileReader reader = new Mp3FileReader(@"F:\Music\__PRUEBA\14-Hammer to fall.mp3")) { byte[] info = new byte[2048]; reader.Seek(19888128, SeekOrigin.Begin); reader.Read(info, 0, 2048); Console.WriteLine("Succesfull read"); } } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadLine(); }
static void Main(string[] args)
{
try
{
Mp3FileReader reader = new Mp3FileReader(@"C:\testAudio.mp3");
long count = reader.Length;
if (count <= int.MaxValue)
{
byte[] info = new byte[count];
reader.Read(info, 0, (int)count);
Console.WriteLine("Succesfull read");
}
else
Console.WriteLine("Could not read");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
that prints out the following exception message
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame(Mp3Frame frame, Byte[] dest, Int32 destOffset) in C:\NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50
at NAudio.Wave.Mp3FileReader.Read(Byte[] sampleBuffer, Int32 offset, Int32 numBytes) in C:\NAudio-Source\NAudio\Wave\WaveStreams\Mp3FileReader.cs:line 338
at Tester.Program.Main(String[] args) in C:\Tester\Program.cs:line 30
I've dowloaded NAudio code and I've been debugging it but I can't find the cause of the error, although, as you can see on the stack trace is in *NAudio-Source\NAudio\FileFormats\Mp3\Mp3FrameDecompressor.cs:line 50*
¿Am I doing something wrong? By the way, it only happens with a few mp3 files, others are read just fine. I'm attaching one of the files that can't be read
Thanx
Comments: Hello Mark, I've continue to debug the code, NAdio include, and the problem has nothing to do with trying to read the whole file at once. Te problem occurs when decompressing one specific frame. The frame read is of size 3072 (bytes), when decompressing with AcmInterop.acmStreamConvert, the resulting decompressed length is of 27648 bytes. But then the decomprresBuffer on Mp3FileReader, where the decompressed bytes are copied, is of length 9216 (in my case), thus the error when trying to copy 27648 bytes into an 9216 lengthed byte array on Mp3FrameDecompressor.DecompressFrame (line 50). First, initialization of decompressBuffer on Mp3FileReader is as follows this.decompressBuffer = new byte[1152 * bytesPerSample * 2]; (which in may case yields an array of lenght 9216) This initialization is acompanied by the following comment: // no MP3 frames have more than 1152 samples in them // some MP3s I seem to get double and this has gotten me thinking "¿Is it alright if I increase the size of decompressBuffer by some value or would that have some freak effect somewhere else on the code? ¿Could it be possible that this particular frame contains more bytes than usual when decompressed or is the some error of AcmInterop.acmStreamConvert, that is, the codecs?" Some comments on this would be great. Thanks a lot again, an love using NAudio. (By the way, all this was tested with the file i previously attached. The conflicted frame is the second frame read if readding after calling Seek(19888128, SeekOrigin.Begin)). I'm pasting some code: public static void NAudioTest() { try { using (Mp3FileReader reader = new Mp3FileReader(@"F:\Music\__PRUEBA\14-Hammer to fall.mp3")) { byte[] info = new byte[2048]; reader.Seek(19888128, SeekOrigin.Begin); reader.Read(info, 0, 2048); Console.WriteLine("Succesfull read"); } } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadLine(); }