public async void Stop() { recording = false; await writer.FlushAsync(); writer.Close(); writer.Dispose(); Upload(); }
/// <summary> /// Gets the job's media PCM data. /// </summary> /// <returns></returns> static async Task <Tuple <byte[], int> > GetMediaAsWav(Stream src) { // convert PCM stream into WAV using (var dst = new MemoryStream()) using (var pcm = new WaveFileReader(src)) using (var cnv = new WaveFormatConversionStream(new WaveFormat(8000, 16, pcm.WaveFormat.Channels), pcm)) using (var wav = new WaveFileWriter(dst, cnv.WaveFormat)) { // run the conversion into the memory stream await cnv.CopyToAsync(wav, 65536 - (65536 % cnv.BlockAlign)); await wav.FlushAsync(); wav.Close(); // execute the recognition return(Tuple.Create(dst.ToArray(), cnv.WaveFormat.Channels)); } }
private void RecordButton_Click(object sender, EventArgs e) { var dialog = new SaveFileDialog(); dialog.Filter = "Wave files | *.wav"; if (dialog.ShowDialog() != DialogResult.OK) { return; } outputFileName = dialog.FileName; RecordButton.Enabled = false; StopButton.Enabled = true; capture = new WasapiLoopbackCapture(); var writer = new WaveFileWriter(outputFileName, capture.WaveFormat); capture.DataAvailable += async(s, e) => { if (writer != null) { await writer.WriteAsync(e.Buffer, 0, e.BytesRecorded); await writer.FlushAsync(); } }; capture.RecordingStopped += (s, e) => { if (writer != null) { writer.Dispose(); writer = null; } RecordButton.Enabled = true; capture.Dispose(); }; capture.StartRecording(); }
private void StartButton_Click(object sender, EventArgs e) { StartButton.Enabled = false; StopButton.Enabled = true; stopwatch.Start(); //-------------------SystemSoundRecord--------------------------- var dialog = new SaveFileDialog(); dialog.Filter = "Wave files | *.wav"; if (dialog.ShowDialog() != DialogResult.OK) { return; } outputFileNameSounds = dialog.FileName; capture = new WasapiLoopbackCapture(); var writer = new WaveFileWriter(outputFileNameSounds, capture.WaveFormat); capture.DataAvailable += async(s, ee) => { if (writer != null) { await writer.WriteAsync(ee.Buffer, 0, ee.BytesRecorded); await writer.FlushAsync(); } }; capture.RecordingStopped += (s, ee) => { if (writer != null) { writer.Dispose(); writer = null; } StartButton.Enabled = true; capture.Dispose(); }; capture.StartRecording(); //------------------MicRecord---------------------------------- var micFileDialog = new SaveFileDialog(); micFileDialog.Filter = "Wave files | *.wav"; if (micFileDialog.ShowDialog() != DialogResult.OK) { return; } outputFileNameMic = micFileDialog.FileName; wave = new WaveIn(); wave.WaveFormat = new WaveFormat(8000, 1); //44100, 1 wave.DeviceNumber = InputDeviceCombo.SelectedIndex; wave.DataAvailable += Wave_DataAvailable; wave.RecordingStopped += Wave_RecordingStopped; writerMic = new WaveFileWriter(outputFileNameMic, wave.WaveFormat); wave.StartRecording(); }