Why i am using an Extra Thread is simply to make it easier as i send the Mic data, and i write it, so if i just add the data to a Queue (Silence or real data) it will be in order on that thread anyway.
It also takes of some pressure on the DataAvailable thread, not sure if that matters though.
I prefer using a StopWatch as you said yourself for accuracy, but here is my attempt to your code, but with a StopWatch, it doesn't work as intended however.
It also takes of some pressure on the DataAvailable thread, not sure if that matters though.
I prefer using a StopWatch as you said yourself for accuracy, but here is my attempt to your code, but with a StopWatch, it doesn't work as intended however.
private void Sending(object sender, NAudio.Wave.WaveInEventArgs e)
{
try
{
var bytesPerMillisecond = 192;
if (!RecordTimer.IsRunning)
{
TestTimer = 0;
RecordTimer.Reset();
RecordTimer.Start();
if (RecordTimer.ElapsedMilliseconds < 9)
Thread.Sleep(1);
}
TestTimer += e.Buffer.Length / 192;
var timeStamp = RecordTimer.Elapsed;
this.BeginInvoke((Action)(() => { SilenceText.Text = String.Format("Silence: {0} ms", (timeStamp - expectedtime).TotalMilliseconds); }));
if (timeStamp.Milliseconds > (expectedtime.Milliseconds + 20))
{
double millisecondsToInsert = (timeStamp - expectedtime).TotalMilliseconds;
byte[] silence = new byte[bytesPerMillisecond * (int)millisecondsToInsert];
SendQueue.Add(silence);
}
if (connect && MuteMic.Checked == false && skip == false)
{
SendQueue.Add((byte[])e.Buffer.Clone());
}
if (Record && FileCreated == 0)
{
waveWriterYour = new WaveFileWriter(path + Environment.UserName + " - " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff") + ".wav", new WaveFormat(48000, 16, 2));
FileCreated = 1;
}
expectedtime = timeStamp.Add(TimeSpan.FromMilliseconds(e.BytesRecorded / bytesPerMillisecond));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ":Sending", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}