Does anyone know a way to get the mean amplitude of a .wav file using C# (even if it means calling an outside command line program and parsing the output)? Thanks!
+1
A:
Normally the root-mean-squared method is used to calculate the "mean" amplitude of sin(x)-like signals.
Esteban Araya
2009-06-30 15:03:54
I think the OP is asking more in relation to the practical aspect of the problem. Though indeed, he will want to calculate the RMS value.
Noldorin
2009-06-30 15:11:22
Thank you for this info as I wasn't aware of this and it did indeed help a lot.
BarrettJ
2009-07-02 14:21:48
+1
A:
Here is a snip that reads in a stereo wav and puts the data in two arrays. It's untested because I had to remove some code (converting to mono and calculate a moving average)
/// <summary>
/// Read in wav file and put into Left and right array
/// </summary>
/// <param name="fileName"></param>
private void ReadWavfiles(string fileName)
{
byte[] fa = File.ReadAllBytes(fileName);
int startByte = 0;
// look for data header
{
var x = 0;
while (x < fa.Length)
{
if (fa[x] == 'd' && fa[x + 1] == 'a' &&
fa[x + 2] == 't' && fa[x + 3] == 'a')
{
startByte = x + 8;
break;
}
x++;
}
}
// Split out channels from sample
var sLeft = new short[fa.Length / 4];
var sRight = new short[fa.Length / 4];
{
var x = 0;
var length = fa.Length;
for (int s = startByte; s < length; s = s + 4)
{
sLeft[x] = (short)(fa[s + 1] * 0x100 + fa[s]);
sRight[x] = (short)(fa[s + 3] * 0x100 + fa[s + 2]);
x++;
}
}
// do somthing with the wav data in sLeft and sRight
}
Nifle
2009-06-30 15:41:42
If the wav file is mono, would I be able to just comment out the sRight[x] = (short)(fa[s + 3] * 0x100 + fa[s + 2]); line and change s = s + 4 to s = s +2?I'm not very familiar with the wav format. Thanks!
BarrettJ
2009-06-30 18:28:35
Making the changes I listed above and declaring sLeft as "var sLeft = new short[fa.Length / 2];" does indeed work for mono files.
BarrettJ
2009-07-02 14:21:01
A:
Hi i managed to use the above code snip to my project, but is it possible to playback the sLeft array and sRight array?
thanks
Tristan Demanuele
2010-01-29 15:33:14