This project has moved. For the latest updates, please go here.

Stop writing to file when no audio detected

Jul 7, 2013 at 1:16 PM

I've been looking into NAudio recently and think it could help me.
Im using C#.

I have managed to save to a wavfile from mic input using this:
private void waveInEvent_DataAvailable(object sender, WaveInEventArgs e) {
    waveFileWriter.Write(e.Buffer, 0, e.BytesRecorded);
This i need to convert to a flac file. For this i use a simple Process.Start(...) using a downloaded libFlac.exe on the created wav file. If there is support in NAudio to save a wav to flac format that would be great. I haven't found something yet but perhaps there are?

The issue now is that i want to stop recording programmatically. I've seen examples on the web when people has a stop recording button which does this for them, i want to stop recording when i detect that there is "no" incoming audio, perhaps check a threshold value?

Would be glad if someone could help!

//Daniel Svensson, from Sweden.
Jul 9, 2013 at 2:52 PM
you need to break the audio into blocks of (say) 20ms, and then if no sample in that time period has a value above your threshold, then you consider it to be silence and use that.

NAudio doesn't contain built-in save as FLAC, so using a command line tool is probably the easiest option. If it supports getting the samples from stdin, then you could create it on the fly.
Jul 9, 2013 at 3:38 PM

Thanks for answering me.

Do you mean the audio/byte-data in e.Buffer that i need to split up into blocks?
How do i know the corresponding bytesize to ~20ms? Could you provide some pseudo

How does the DataAvailbale work actually? Is it called once only after StopRecording() call
or is it called whenever there are input to write?

Jul 15, 2013 at 1:39 PM
use WaveFormat.AverageBytesPerSecond to convert between bytes and time

DataAvailable is called whenever a buffer full of audio is returned by the soundcard