void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
//Console.WriteLine("WaveIn_DataAvailable");
//Console.WriteLine(e.BytesRecorded); //8288 bytes -> 2072 floats (24 too many)
// We have to concatenate consectutive buffers together to ensure that the time domain signal is continuous from the previous iteration.
// Here, we grab the length of the inbetween buffer.
if (init_inbetween)
{
inbetween = new float[e.BytesRecorded / 4 - buffersize];
for (int i = 0; i < e.BytesRecorded / 4 - buffersize; i++)
inbetween[i] = 0;
init_inbetween = false;
}
// Here, we grab the byte array provided by the callback, and convert it to a float array.
for (int index = 0; index < buffersize; index++)
{
int sample = (int)((e.Buffer[index * 4 + 3] << 24) |
(e.Buffer[index * 4 + 2] << 16) |
(e.Buffer[index * 4 + 1] << 8) |
(e.Buffer[index * 4 + 0]));
float sample32 = sample / 2147483648f;
if (index >= (buffersize - inbetween.Length))
sampledata[index] = inbetween[index - buffersize + inbetween.Length];
else
sampledata[index] = sampledata[index + buffersize + inbetween.Length];
sampledata[index + buffersize] = sample32;
}
if (e.BytesRecorded / 4 - buffersize < 0)
return;
inbetween = new float[e.BytesRecorded / 4 - buffersize];
// We then fill the inbetween buffer (extra data larger than the original buffer size) with the remainder.
for (int i = buffersize; i < e.BytesRecorded / 4; i++)
{
int sample = (int)((e.Buffer[i * 4 + 3] << 24) |
(e.Buffer[i * 4 + 2] << 16) |
(e.Buffer[i * 4 + 1] << 8) |
(e.Buffer[i * 4 + 0]));
float sample32 = sample / 2147483648f;
inbetween[i - buffersize] = sample32;
}
// bufferFFT grabs the sampledata buffer and calculates the Fourier transform, filters the data using a high-pass filter and stores it in filteredindata
bufferFFT();
// Updating the Kalman Filter
filter.time_Update();
// If the speakers are outputting the key tones.
if ((waveOut != null))
{
KF = new List<KeyFrequency>();
//gestureDetected.Text = "";
for (int i = 0; i < frequencies.Count; i++)
{
if (history[i].Count > 0)
KF.Add(new KeyFrequency(frequencies.ElementAt(i), i + 1, frequencyStep/30, filteredindata, centerbins.ElementAt(i), history[i].Last()));
else
KF.Add(new KeyFrequency(frequencies.ElementAt(i), i + 1, frequencyStep/30, filteredindata, centerbins.ElementAt(i), 0));
velocity[i] = KF.ElementAt(i).state;
filter.measurement_Update(velocity[i]);
displacement[i] += (int)filter.x_priori[0];
if ((displacement[i] - prev_displacement[i]) < 0 && velocity[i] > 0)
{
ratio = Math.Abs((double)((instant_displacement[i] - prev_displacement[i]) / (double)towards_displacement[i]));
prev_displacement[i] = displacement[i];
}
if ((displacement[i] - prev_displacement[i]) > 0 && velocity[i] < 0)
{
towards_displacement[i] = instant_displacement[i] - prev_displacement[i];
prev_displacement[i] = displacement[i];
}
instant_displacement[i] = displacement[i];
history[i].Add(velocity[i]);
inverse_history[i].Add(KF[i].inverse_state);
}
// Run through gesture detection.
detectGestures();
}
}