private async void Recorder_Stopped(bool requested) { SetStatus("stopped", Colors.Red); if (!requested) { LogTo.Error("Recorder unexpectedly stopped, restarting in 1 second"); await Task.Delay(1000); await Recorder.StartAsync(); } }
/// <summary> /// /// </summary> /// <param name="recognizer"></param> /// <param name="recorder"></param> /// <param name="process"></param> /// <param name="cancellationToken"></param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public static Task <IProcess> StartStreamingRecognitionAsync( this IRecognizer recognizer, IRecorder recorder, Process?process = null, CancellationToken cancellationToken = default) { recognizer = recognizer ?? throw new ArgumentNullException(nameof(recognizer)); recorder = recorder ?? throw new ArgumentNullException(nameof(recorder)); if (!recognizer.SupportedStreamingSettings.Any()) { throw new ArgumentException("Recognizer does not support streaming recognition."); } process ??= new Process(); process.Initialize(async() => { var settings = recognizer.SupportedStreamingSettings.First(); using var recording = await recorder.StartAsync(settings, cancellationToken) .ConfigureAwait(false); using var recognition = await recognizer.StartStreamingRecognitionAsync(settings, cancellationToken) .ConfigureAwait(false); await recognition.BindRecordingAsync(recording, process.Exceptions, cancellationToken).ConfigureAwait(false); await process.WaitAsync(cancellationToken).ConfigureAwait(false); await recording.StopAsync(cancellationToken).ConfigureAwait(false); await recognition.StopAsync(cancellationToken).ConfigureAwait(false); }, cancellationToken); return(Task.FromResult <IProcess>(process)); }
/// <summary> /// /// </summary> /// <param name="recorder"></param> /// <param name="timeout"></param> /// <param name="settings"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <IRecording> StartWithTimeoutAsync( this IRecorder recorder, TimeSpan timeout, AudioSettings?settings = null, CancellationToken cancellationToken = default) { recorder = recorder ?? throw new ArgumentNullException(nameof(recorder)); using var recording = await recorder.StartAsync(settings, cancellationToken).ConfigureAwait(false); await Task.Delay(timeout, cancellationToken).ConfigureAwait(false); await recording.StopAsync(cancellationToken).ConfigureAwait(false); return(recording); }
public static async Task ConvertTest_RealTime(IRecorder recorder, IRecognizer recognizer) { using var recognition = await recorder.StartAsync(recognizer.SupportedSettings.First()); await Task.Delay(TimeSpan.FromSeconds(5)); await recognition.StopAsync(); var bytes = recognition.Data; Assert.IsNotNull(bytes, $"{nameof(bytes)} == null"); var result = await recognizer.ConvertAsync(bytes); Console.WriteLine(result); }
private async void RecognizSound(object obj) { if (!isRecording) { ImageVisibility = Visibility.Visible; recorder.OnRecordingAbort += OnRecordingAbort; await recorder.StartAsync(); isRecording = true; } else { ImageVisibility = Visibility.Hidden; recorder.Stop(); RecognizeFile(recorder.FilePath); isRecording = false; } }
private async void Window_Initialized(object sender, EventArgs e) { Options = RecorderOptions.Load(ConfigFile, out var exists); if (!exists) { bool amd = await Utils.IsAcceleratorAvailable(RecorderOptions.HardwareAccel.AMD); if (amd) { Options.HardwareAcceleration = RecorderOptions.HardwareAccel.AMD; } else { bool nvidia = await Utils.IsAcceleratorAvailable(RecorderOptions.HardwareAccel.NVIDIA); if (nvidia) { Options.HardwareAcceleration = RecorderOptions.HardwareAccel.NVIDIA; } } Options.Save(ConfigFile); } AudioDevices = (await Utils.GetAudioDevices()); foreach (var device in AudioDevices) { device.Enabled = Options.AudioDevices.Contains(device.AltName); } SaveHotkey.Hotkey = Options.SaveReplayHotkey; SaveOptions(); Recorder = new FFmpegRecorder(Options); Recorder.Stopped += this.Recorder_Stopped; Recorder.Started += this.Recorder_Started; await Recorder.StartAsync(); }
private async void SnugglerRecognize() { recorderSnuggler = new LoopBackRecorder(); recorderSnuggler.Silent_Sec = 43; recorderSnuggler.OnRecordingAbort += OnRecordingSnugglerAbort; while (true) { if (!isRecordingSunn) { await recorderSnuggler.StartAsync(); isRecordingSunn = !isRecordingSunn; } if (!Snuggler) { isRecordingSunn = false; break; } } recorderSnuggler.Stop(); }
public static async Task ConvertTest_RealTime(IRecorder recorder, IConverter converter) { await recorder.InitializeAsync(); await recorder.StartAsync(); await Task.Delay(TimeSpan.FromMilliseconds(5000)); await recorder.StopAsync(); var bytes = recorder.WavData; Assert.IsNotNull(bytes, "bytes != null"); if (bytes == null) { return; } var result = await converter.ConvertAsync(bytes.ToArray()); Console.WriteLine(result); }
public static async Task StartStreamingRecognitionTest_RealTime(IRecorder recorder, IConverter converter, bool writeWavHeader = false) { await recorder.InitializeAsync(); await recorder.StartAsync(); using var recognition = await converter.StartStreamingRecognitionAsync(); recognition.AfterPartialResults += (_, args) => Console.WriteLine($"{DateTime.Now:h:mm:ss.fff} AfterPartialResults: {args.Text}"); recognition.AfterFinalResults += (_, args) => Console.WriteLine($"{DateTime.Now:h:mm:ss.fff} AfterFinalResults: {args.Text}"); if (writeWavHeader) { var wavHeader = recorder.WavHeader?.ToArray() ?? throw new InvalidOperationException("Recorder Wav Header is null"); await recognition.WriteAsync(wavHeader); } if (recorder.RawData != null) { await recognition.WriteAsync(recorder.RawData.ToArray()); } // ReSharper disable once AccessToDisposedClosure recorder.RawDataReceived += async(_, args) => { if (args.RawData == null) { return; } await recognition.WriteAsync(args.RawData.ToArray()); }; await Task.Delay(TimeSpan.FromMilliseconds(5000)); await recorder.StopAsync(); await recognition.StopAsync(); }
/// <summary> /// Dispose is required!. /// </summary> /// <param name="recognizer"></param> /// <param name="recorder"></param> /// <param name="exceptionsBag"></param> /// <param name="cancellationToken"></param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public static async Task <IStreamingRecognition> StartStreamingRecognitionAsync( this IRecognizer recognizer, IRecorder recorder, ExceptionsBag?exceptionsBag = null, CancellationToken cancellationToken = default) { recognizer = recognizer ?? throw new ArgumentNullException(nameof(recognizer)); recorder = recorder ?? throw new ArgumentNullException(nameof(recorder)); if (!recognizer.SupportedStreamingSettings.Any()) { throw new ArgumentException("Recognizer does not support streaming recognition."); } var isStopped = false; var settings = recognizer.SupportedStreamingSettings.First(); var recording = await recorder.StartAsync(settings, cancellationToken) .ConfigureAwait(false); recording.StopWhenSilence(); var recognition = await recognizer.StartStreamingRecognitionAsync(settings, cancellationToken) .ConfigureAwait(false); recognition.Stopping += async(_, _) => { if (isStopped) { return; } try { await recording.StopAsync(cancellationToken).ConfigureAwait(false); } catch (Exception exception) { exceptionsBag?.OnOccurred(exception); } finally { recording.Dispose(); } }; recording.Stopped += async(_, _) => { isStopped = true; try { await recognition.StopAsync(cancellationToken).ConfigureAwait(false); } catch (Exception exception) { exceptionsBag?.OnOccurred(exception); } finally { recording.Dispose(); } }; await recognition.BindRecordingAsync(recording, exceptionsBag, cancellationToken).ConfigureAwait(false); return(recognition); }