I'm working on a thesis about chord detection. After struggling hard, I found the root of my problem is at getting the amplitude of the wav file using my C# -based program.
I used this codes to retrieve the amplitude:
public WavProcessing(String fileNameWithPath)
{
_fileNameWithPath = fileNameWithPath;
}
public void Initialization()
{
AssignWaveHeaderData();
streamBuffer = File.ReadAllBytes(_fileNameWithPath);
fftBufferSize = 16384;
fftBufferSize2 = fftBufferSize / 2;
}
private void getAmplitude()
{
waveData = new short[numOfSamples];
var xsample = new short[numOfSamples];
var x = 0;
var str = "";
for (var i = 44 ; i <= waveData.Length; i = i + 2) //44 because the wave data starts at 44th byte.
{
waveData[x] = BitConverter.ToInt16(streamBuffer,i);
x++;
}
}
the code worked fine at first, I compared it with the delphi-based program which is also a chord detection program. But thing I didn't notice is, my code actually only retrieved the half array of amplitudes.
For example: I load a Chord C wav file into my C# program, the I got the array of amplitudes like this
[0] -279
[1] -262
[2] -231
[3] -216
[4] -199
[5] -185
[6] -178
[7] -186
[8] -217
[9] -237
[10] -267
[11] -298
[12] -319
[13] -348
[14] -374
[15] -373
[16] -376
[17] -366
[18] -357
[19] -340
[20] -319
[21] -312
[22] -300
[23] -301
[24] -308
[25] -321
[26] -339
...
[361042] 1950
[361043] 0
[361044] 0
...
[722128] 0
you can see that starting the 361043-rd array, it only returns zero... it is approximately only a half of the full stream size (which 722128)
Meanwhile in the someone's delphi program (I used this as my reference), whose code like this:
procedure TForm1.openfilewave(NamaFile : string);
var
Stream : TFileStream;
i, start, endwave : Integer;
begin
Application.ProcessMessages;
Stream := TFileStream.Create(FileName, fmOpenRead);
FillChar(wavehdr, SizeOf(wavehdr), 0);
Stream.Read(wavehdr, SizeOf(wavehdr));
SetLength(wavedata[0].Data, Round(wavehdr.chunkSize/wavehdr.BytesPerSample));
for i := 0 to High(wavedata[0].Data) do
begin
Stream.Read(wavedata[0].Data[i], 2);
end;
end;
returns the full array of amplitudes (same wav file) like:
0 -- -279
1 -- -262
2 -- -231
3 -- -216
4 -- -199
5 -- -185
6 -- -178
7 -- -186
8 -- -217
9 -- -237
10 -- -267
11 -- -298
12 -- -319
13 -- -348
14 -- -374
15 -- -373
16 -- -376
17 -- -366
18 -- -357
19 -- -340
20 -- -319
...
361042 -- 1950
361043 -- 1819 << not returning zero value
361044 -- 1655
361045 -- 1476
...
722100 -- 165
722101 -- 142
722102 -- 117
722103 -- 91
722104 -- 68
722105 -- 37
722106 -- 11
722107 -- -6
722108 -- -27
722109 -- -36
722110 -- 0
722111 -- 0
...
722128 -- 0
in that delphi program, full array of amplitudes are returned, resulting correct values for next calculations.
This is the problem:
for (var i = 44; i <= waveData.Length; i = i + 2)
{
waveData[x] = BitConverter.ToInt16(streamBuffer,i);
x++;
}
waveData
is half the size of streamBuffer
- you should be using:
for (var i = 44; i < streamBuffer.Length; i = i + 2)
Or perhaps simplify things a bit like this, using only one variable:
for (int x = 0; x < waveData.Length; x++)
{
waveData[x] = BitConverter.ToInt16(streamBuffer, x * 2 + 44);
}