private void WaveIn_DataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
int index = 0;
int numSamples = waveInEventArgs.BytesRecorded / m_sampleSize / m_channels;
List<IMeasurement> measurements = new List<IMeasurement>();
long sampleTime;
LittleBinaryValue sampleValue;
// Get the timestamp for the first recorded sample
if (Interlocked.Read(ref m_lastSampleTime) == 0L)
Interlocked.Exchange(ref m_lastSampleTime, DateTime.UtcNow.Ticks - (m_ticksPerSample * numSamples));
// Get the timestamp for the first sample in this block of data
sampleTime = Interlocked.Read(ref m_lastSampleTime) + m_ticksPerSample;
// Parse each recorded sample in this block of data
for (int sampleIndex = 0; sampleIndex < numSamples; sampleIndex++)
{
// Parse one value per channel per sample
for (int channelIndex = 0; channelIndex < m_channels; channelIndex++)
{
if (channelIndex < OutputMeasurements.Length)
{
// Create a measurement for this value
sampleValue = new LittleBinaryValue(m_sampleTypeCode, waveInEventArgs.Buffer, index + (channelIndex * m_sampleSize), m_sampleSize);
measurements.Add(Measurement.Clone(OutputMeasurements[channelIndex], ConvertToPCM16(sampleValue.ConvertToType(TypeCode.Double).ToDouble()), sampleTime));
}
}
// Update the last sample time
Interlocked.Exchange(ref m_lastSampleTime, sampleTime);
// Get the timestamp for the next sample in this block of data
sampleTime += m_ticksPerSample;
// Update the index to the next sample in this block of data
index += m_sampleSize * m_channels;
}
// Publish streaming microphone data
OnNewMeasurements(measurements);
m_samplesProcessed += numSamples;
}