private void BtnSaveStop_Click(object sender, EventArgs e) { lblWait.Visible = true; Application.DoEvents(); BtnSaveStop.Hide(); StopCapture(); objStopWatch.Reset(); lblDisplayTime.Visible = false; // Process.Start(_FileName); AudioToText audioToText = new AudioToText(); string message = "Processing is Done!"; txtTranscript.Text = audioToText.GetTextBy16BitPCMAudio(_FileName, cmbLanguageCode.SelectedValue.ToString(), ref message); lblWait.Visible = false; BtnStart.Show(); MessageBox.Show(message); }
private void StartCapture(string fileName) { //Capture Mode CaptureMode = (CaptureMode)1; DataFlow dataFlow = CaptureMode == CaptureMode.Capture ? DataFlow.Capture : DataFlow.Render; // //Getting the audio devices from var devices = MMDeviceEnumerator.EnumerateDevices(dataFlow, DeviceState.Active); if (!devices.Any()) { MessageBox.Show("No devices found."); return; } int selectedDeviceIndex = 0; SelectedDevice = devices[selectedDeviceIndex]; if (SelectedDevice == null) { return; } if (CaptureMode == CaptureMode.Capture) { _soundIn = new WasapiCapture(); } else { _soundIn = new WasapiLoopbackCapture(); } _soundIn.Device = SelectedDevice; //Sample rate of audio int sampleRate = 16000; //bits per rate int bitsPerSample = 16; //chanels int channels = 1; //initialize the soundIn instance _soundIn.Initialize(); //create a SoundSource around the the soundIn instance //this SoundSource will provide data, captured by the soundIn instance var soundInSource = new SoundInSource(_soundIn) { FillWithZeros = false }; //create a source, that converts the data provided by the //soundInSource to any other format //in this case the "Fluent"-extension methods are being used IWaveSource convertedSource = soundInSource .ChangeSampleRate(sampleRate) // sample rate .ToSampleSource() .ToWaveSource(bitsPerSample); //bits per sample //channels=1 then we need to create mono audio convertedSource = convertedSource.ToMono(); AudioToText audioToText = new AudioToText(); audioToText.SetFolderPermission(_folderPath); //create a new wavefile waveWriter = new WaveWriter(fileName, convertedSource.WaveFormat); //register an event handler for the DataAvailable event of //the soundInSource //Important: use the DataAvailable of the SoundInSource //If you use the DataAvailable event of the ISoundIn itself //the data recorded by that event might won't be available at the //soundInSource yet soundInSource.DataAvailable += (s, e) => { //read data from the converedSource //important: don't use the e.Data here //the e.Data contains the raw data provided by the //soundInSource which won't have your target format byte[] buffer = new byte[convertedSource.WaveFormat.BytesPerSecond / 2]; int read; //keep reading as long as we still get some data //if you're using such a loop, make sure that soundInSource.FillWithZeros is set to false while ((read = convertedSource.Read(buffer, 0, buffer.Length)) > 0) { //write the read data to a file // ReSharper disable once AccessToDisposedClosure waveWriter.Write(buffer, 0, read); } }; //we've set everything we need -> start capturing data objStopWatch.Start(); _soundIn.Start(); }