Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latency parameter on WasapiCapture not adhered to #321

Closed
tbdx opened this issue Jan 21, 2018 · 1 comment
Closed

Latency parameter on WasapiCapture not adhered to #321

tbdx opened this issue Jan 21, 2018 · 1 comment
Labels

Comments

@tbdx
Copy link

tbdx commented Jan 21, 2018

Hi,

Unless I am misunderstanding this, which is possible, I would have expected the DataAvailable event to be raised less frequently.

I am trying to process audio in near realtime although stability is important so I am happy with 100 to 200 milliseconds.

Recording in 8kHz, 16 bit, mono.

Example code

private WasapiCapture soundIn;
private SoundInSource soundInSource;
private IWaveSource finalSource;
private MMDevice AudioDeviceIn;

Debug.WriteLine(DateTime.Now.ToString("HHmmss.fff") + "\t" + "recorder format = " + AudioDeviceIn.DeviceFormat.ToString());

soundIn = new WasapiCapture(true, AudioClientShareMode.Shared, 120, new WaveFormat(8000, 16, 1));
soundIn.Device = AudioDeviceIn;
soundIn.Initialize();

soundInSource = new SoundInSource(soundIn);
soundInSource.FillWithZeros = false;

var simpleStream = new SimpleNotificationSource(soundInSource.ToSampleSource());
finalSource = simpleStream.ToWaveSource(16);

soundInSource.DataAvailable += GotAudioData;

soundIn.Start();


private void GotAudioData(object sender, DataAvailableEventArgs e)
{
	Debug.WriteLine(DateTime.Now.ToString("HHmmss.fff") + "\t" + "got bytes = " + e.ByteCount);
	
	byte[] buffer = new byte[finalSource.WaveFormat.BytesPerSecond];
	
	int read;

	int bytesPerSample;
	int howManySamples;
	
	while ((read = finalSource.Read(buffer, 0, buffer.Length)) > 0)
	{
		Debug.WriteLine(DateTime.Now.ToString("HHmmss.fff") + "\t" + "read = " + read + " from finalSource");
				
		bytesPerSample = finalSource.WaveFormat.BytesPerSample;
		howManySamples = read / bytesPerSample;

	}
}

Debugging shows the DataAvailable event seems to be triggered every 20ms, sometimes as low as 10ms. (2 bytes per sample)

092727.219	recorder format = ChannelsAvailable: 1|SampleRate: 8000|Bps: 16000|BlockAlign: 2|BitsPerSample: 16|Encoding: Extensible|SubFormat: 00000001-0000-0010-8000-00aa00389b71|ChannelMask: SpeakerFrontCenter
092727.298	got bytes = 320
092727.302	read = 320 from finalSource
092727.315	got bytes = 320
092727.316	read = 320 from finalSource
092727.336	got bytes = 320
092727.337	read = 320 from finalSource
092727.355	got bytes = 320
092727.356	read = 320 from finalSource
092727.375	got bytes = 320
092727.376	read = 320 from finalSource
092727.395	got bytes = 320
092727.396	read = 320 from finalSource
092727.416	got bytes = 320
092727.417	read = 320 from finalSource
092727.436	got bytes = 320
092727.436	read = 320 from finalSource
092727.456	got bytes = 320
092727.457	read = 320 from finalSource
092727.476	got bytes = 320
092727.476	read = 320 from finalSource
092727.496	got bytes = 320
092727.496	read = 320 from finalSource
092727.515	got bytes = 320
092727.516	read = 320 from finalSource
092727.535	got bytes = 320
092727.536	read = 320 from finalSource
092727.555	got bytes = 320
092727.557	read = 320 from finalSource
092727.575	got bytes = 320
092727.577	read = 320 from finalSource
092727.595	got bytes = 320
092727.597	read = 320 from finalSource
092727.605	got bytes = 160
092727.606	read = 160 from finalSource
092727.625	got bytes = 320
092727.626	read = 320 from finalSource
092727.636	got bytes = 160
092727.637	read = 160 from finalSource
@filoe filoe added the question label Jan 28, 2018
filoe added a commit that referenced this issue Jan 28, 2018
@filoe
Copy link
Owner

filoe commented Jan 28, 2018

You're right. It was an old relict. I've fixed it. Should be stable now. Decreased the default latency to 25ms. Should be quite stable now.

@filoe filoe closed this as completed Jan 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants