I am reading a raw audio file (CD track rip) in, doing byte swapping, and then writing back out to a wav file.
The routine I have processes the bytes properly, but only does about half the file. I am a VB.NET Developer, not really a C# developer, and this code does not convert to VB.NET properly (It gets an overflow error).
It's not really a "swap" so much as a calc/prune. (short)((buffer[i + 1] * 256) + buffer[i])
I believe it is writing only half of the samples, but I've no idea how to fix!
public static short[] SwapBytesArray(string fileName)
{
byte[] buffer = System.IO.File.ReadAllBytes(fileName);
long fileLength = buffer.LongLength;
if ((fileLength & 1) == 1)
{
throw new ArgumentException("File length must be an even number of bytes");
}
System.Collections.Generic.List<short> sList = new System.Collections.Generic.List<short>();
for (long i = 0; i < fileLength - 1; i += 2)
{
byte tmp = buffer[i];
sList.Add((short)((buffer[i + 1] * 256) + buffer[i]));
//buffer[i + 1] = tmp;
}
return sList.ToArray();
}