void StartRecording() { // Cancel the previous task if it's running. recognitionTask?.Cancel(); recognitionTask = null; var audioSession = AVAudioSession.SharedInstance(); NSError err; err = audioSession.SetCategory(AVAudioSessionCategory.Record); audioSession.SetMode(AVAudioSession.ModeMeasurement, out err); err = audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); // Configure request so that results are returned before audio recording is finished recognitionRequest = new SFSpeechAudioBufferRecognitionRequest { ShouldReportPartialResults = true }; var inputNode = audioEngine.InputNode; if (inputNode == null) { throw new InvalidProgramException("Audio engine has no input node"); } // A recognition task represents a speech recognition session. // We keep a reference to the task so that it can be cancelled. recognitionTask = speechRecognizer.GetRecognitionTask(recognitionRequest, (result, error) => { var isFinal = false; if (result != null) { textView.Text = result.BestTranscription.FormattedString; isFinal = result.Final; } if (error != null || isFinal) { audioEngine.Stop(); inputNode.RemoveTapOnBus(0); recognitionRequest = null; recognitionTask = null; recordButton.Enabled = true; recordButton.SetTitle("Start Recording", UIControlState.Normal); } }); var recordingFormat = inputNode.GetBusOutputFormat(0); inputNode.InstallTapOnBus(0, 1024, recordingFormat, (buffer, when) => { recognitionRequest?.Append(buffer); }); audioEngine.Prepare(); audioEngine.StartAndReturnError(out err); textView.Text = "(Throw Darts!)"; textView2.Text = ""; }
public void StopRecording() { try { AudioEngine?.Stop(); LiveSpeechRequest?.EndAudio(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public bool TogglePlay() { stateChangeQueue.DispatchSync(() => { if (isPlaying) { player.Stop(); engine.Stop(); isPlaying = false; SetSessionActive(false); } else { SetSessionActive(true); ScheduleLoop(); ScheduleLoop(); NSError error; if (!engine.StartAndReturnError(out error)) { Console.WriteLine(error.LocalizedDescription); } player.Play(); isPlaying = true; } }); return(isPlaying); }
public string Stop() { audioEngine.Stop(); recognitionTask.Finish(); liveRequest.EndAudio(); return(recognizedText); }
public void StopRecording() { if (!IsAuthorized()) { return; } AudioEngine.Stop(); LiveSpeechRequest.EndAudio(); // Play stop sound if (player.IsPlaying) { player.Stop(); } player.Load("Sounds/siri_stop.mp3"); player.Play(); }
private void EndRecognition(object sender, ElapsedEventArgs e) { node.RemoveTapOnBus(0); audioEngine.Stop(); liveSpeechRequest.EndAudio(); RecognitionTask.Cancel(); recording = false; Recorded?.Invoke(lastSpokenString); }
async void AskQuestion_TouchUpInside(object sender, EventArgs e) { if (audioEngine.Running) { audioEngine.Stop(); recognitionRequest.EndAudio(); askQuestion.SetTitle("Ask Question", UIControlState.Normal); var targetLocal = new CrossLocale { Language = AppDelegate.CurrentLanguage.AppleLanguageCode }; await CrossTextToSpeech.Current.Speak(translatedText.Text, targetLocal, volume : 1.0f); } else { askQuestion.SetTitle("Finished With Question", UIControlState.Normal); StartRecording(); } }
private void StopRecordingAndRecognition(AVAudioSession aVAudioSession = null) { if (_audioEngine.Running) { _audioEngine.Stop(); _audioEngine.InputNode.RemoveTapOnBus(0); _recognitionTask?.Cancel(); _recognitionRequest.EndAudio(); _recognitionRequest = null; _recognitionTask = null; } }
// 音声認識の停止処理 public void StopRecognizing() { try { audioEngine?.Stop(); recognitionTask?.Cancel(); recognitionRequest?.EndAudio(); IsRecognizing = false; } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void StopRecording() { try { _audioEngine.Stop(); _audioEngine.InputNode.RemoveTapOnBus(0); _speechRecognitionTask?.Cancel(); _speechRecognitionRequest.EndAudio(); _speechRecognitionRequest = null; _speechRecognitionTask = null; } catch (Exception ex) { } }
public void stop() { if (!running) { return; } running = false; AVAudioSession.SharedInstance().SetActive(false); if (audioPlayer != null) { try { audioPlayer.Stop(); audioPlayer.Reset(); } catch (Exception) { } audioPlayer.Dispose(); audioPlayer = null; } if (audioDecoder != null) { audioDecoder.stop(); audioDecoder.Dispose(); audioDecoder = null; } if (audioEngine != null) { try { audioEngine.Stop(); audioEngine.Reset(); } catch (Exception) { } audioEngine.Dispose(); audioEngine = null; } }
public async Task <string> RecognizeAsync() { var node = audioEngine.InputNode; var recordingFormat = node.GetBusOutputFormat(0); var liveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest(); node.InstallTapOnBus(0, 1024, recordingFormat, (AVAudioPcmBuffer buffer, AVAudioTime when) => { // Append buffer to recognition request liveSpeechRequest.Append(buffer); }); // Start recording audioEngine.Prepare(); NSError error; audioEngine.StartAndReturnError(out error); // Did recording start? if (error != null) { return(null); } var taskCompletionSource = new TaskCompletionSource <string>(); // Start recognition var recognitionTask = speechRecognizer.GetRecognitionTask(liveSpeechRequest, (SFSpeechRecognitionResult recognitionResult, NSError err) => { // Was there an error? if (err != null) { taskCompletionSource?.SetResult(null); } else { taskCompletionSource?.SetResult(recognitionResult.BestTranscription.FormattedString); } }); var result = await taskCompletionSource.Task; taskCompletionSource = null; audioEngine.Stop(); liveSpeechRequest.EndAudio(); node.RemoveTapOnBus(0); return(result); }
static void StopInstances() { AudioEngine?.InputNode?.RemoveTapOnBus(0); AudioEngine?.Stop(); AudioEngine?.Dispose(); AudioEngine = null; LiveSpeechRequest?.EndAudio(); LiveSpeechRequest?.Dispose(); LiveSpeechRequest = null; SpeechRecognizer?.Dispose(); SpeechRecognizer = null; //Timer?.Dispose(); //Timer = null; }
public void StopRecording() { IsRecording = false; if (m_speechTimer != null) { m_speechTimer.Stop(); m_speechTimer.Dispose(); } if (AudioEngine != null) { AudioEngine.Stop(); LiveSpeechRequest.EndAudio(); } if (RecognitionTask != null) { RecognitionTask.Cancel(); } }
private async void StopSpeechRecognizer(bool waitForSpeech) { try { var task = _task; _task = null; if (task != null) { if (waitForSpeech) { await Task.Delay(1000); } task.Cancel(); engine.Stop(); engine.InputNode.RemoveTapOnBus(0); } } catch (Exception e) { } }
private void StopRecordingAndRecognition() { try { if (!_audioEngine.Running) { return; } _audioEngine.Stop(); _audioEngine.InputNode.RemoveTapOnBus(0); _recognitionTask?.Cancel(); _recognitionRequest.EndAudio(); _recognitionRequest = null; _recognitionTask = null; } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void stop() { if (!running) { return; } running = false; AVAudioSession.SharedInstance().SetActive(false); if (audioRecorder != null) { try { audioRecorder.InputNode.RemoveTapOnBus(0); audioRecorder.Stop(); audioRecorder.Reset(); } catch (Exception) { } audioRecorder.Dispose(); audioRecorder = null; } if (audioEncoder != null) { audioEncoder.stop(); audioEncoder.Dispose(); audioEncoder = null; } lock (outputBuffers) { outputBuffers.Clear(); } }
void DeletePlayer() { Stop(); if (player != null && player.Playing) { player.Stop(); } if (engine != null && engine.Running) { engine.Stop(); } if (player != null && engine != null && pitch != null) { engine.Dispose(); player.Dispose(); pitch.Dispose(); engine = null; player = null; pitch = null; } }
public void StopSpeechRecognition() { audioEngine.Stop(); recognitionRequest.EndAudio(); }
public IAsyncOperation <SpeechRecognitionResult> RecognizeAsync() { _initialSilenceTimeout = new Timer(); _initialSilenceTimeout.Interval = Math.Max(Timeouts.InitialSilenceTimeout.TotalMilliseconds, 5000); _initialSilenceTimeout.Elapsed += OnTimeout; _endSilenceTimeout = new Timer(); _endSilenceTimeout.Interval = Math.Max(Timeouts.EndSilenceTimeout.TotalMilliseconds, 150); _endSilenceTimeout.Elapsed += OnTimeout; // Cancel the previous task if it's running. _recognitionTask?.Cancel(); _recognitionTask = null; var audioSession = AVAudioSession.SharedInstance(); NSError err; err = audioSession.SetCategory(AVAudioSessionCategory.Record); audioSession.SetMode(AVAudioSession.ModeMeasurement, out err); err = audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); // Configure request to get partial results _recognitionRequest = new SFSpeechAudioBufferRecognitionRequest { ShouldReportPartialResults = true, TaskHint = SFSpeechRecognitionTaskHint.Dictation }; var inputNode = _audioEngine.InputNode; if (inputNode == null) { throw new InvalidProgramException("Audio engine has no input node"); } var tcs = new TaskCompletionSource <SpeechRecognitionResult>(); // Keep a reference to the task so that it can be cancelled. _recognitionTask = _speechRecognizer.GetRecognitionTask(_recognitionRequest, (result, error) => { var isFinal = false; var bestMatch = default(SpeechRecognitionResult); if (result != null) { _initialSilenceTimeout.Stop(); _endSilenceTimeout.Stop(); _endSilenceTimeout.Start(); bestMatch = new SpeechRecognitionResult() { Text = result.BestTranscription.FormattedString, Alternates = result.Transcriptions? .Select(t => new SpeechRecognitionResult() { Text = t.FormattedString }) .ToList() }; isFinal = result.Final; OnHypothesisGenerated(bestMatch.Text); } if (error != null || isFinal) { _initialSilenceTimeout.Stop(); _endSilenceTimeout.Stop(); _audioEngine.Stop(); inputNode.RemoveTapOnBus(0); inputNode.Reset(); audioSession = AVAudioSession.SharedInstance(); err = audioSession.SetCategory(AVAudioSessionCategory.Playback); audioSession.SetMode(AVAudioSession.ModeDefault, out err); err = audioSession.SetActive(false, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); _recognitionTask = null; OnStateChanged(SpeechRecognizerState.Idle); if (bestMatch != null) { tcs.TrySetResult(bestMatch); } else { tcs.TrySetException(new Exception($"Error during speech recognition: {error.LocalizedDescription}")); } } }); var recordingFormat = new AVAudioFormat(sampleRate: 44100, channels: 1); inputNode.InstallTapOnBus(0, 1024, recordingFormat, (buffer, when) => { _recognitionRequest?.Append(buffer); }); _initialSilenceTimeout.Start(); _audioEngine.Prepare(); _audioEngine.StartAndReturnError(out err); OnStateChanged(SpeechRecognizerState.Capturing); return(tcs.Task.AsAsyncOperation()); }
public void StopRecording() { AudioEngine.Stop(); LiveSpeechRequest.EndAudio(); }
void StopListening() { AudioEngine.Stop(); LiveSpeechRequest.EndAudio(); }
public async Task <Tuple <string, Tuple <bool, CMPSpeechError> > > StartRecordingAsync() { if (IsRecording() == true) { return(new Tuple <string, Tuple <bool, CMPSpeechError> > (string.Empty, null)); } var authorizationResult = await CheckAuthorizationAsync(); if (authorizationResult == null) { return(new Tuple <string, Tuple <bool, CMPSpeechError> >(string.Empty, null)); } if (authorizationResult.Item1 == false) { return(new Tuple <string, Tuple <bool, CMPSpeechError> >(string.Empty, authorizationResult)); } CMPSpeechError genericError = null; var inputNode = _speechAudioEngine.InputNode; if (inputNode == null) { var audioEngineError = new NSError(new NSString(string.Empty), nint.Parse(SpeechToTextErrorEnum. eNoInputNode.ToString())); genericError = PrepareAudioEngineError(audioEngineError); ResetSpeechToText(); return(new Tuple <string, Tuple <bool, CMPSpeechError> >(string.Empty, new Tuple <bool, CMPSpeechError> (false, genericError))); } Tuple <string, Tuple <bool, CMPSpeechError> > recognitionResult = null; await Task.Run(() => { try { _speechRecognitionTask = _speechRecognizer.GetRecognitionTask(_speechRecognitionRequest, (SFSpeechRecognitionResult result, NSError speechError) => { if (speechError != null) { _speechAudioEngine.Stop(); genericError = new CMPSpeechError(speechError); recognitionResult = new Tuple <string, Tuple <bool, CMPSpeechError> >(string.Empty, new Tuple <bool, CMPSpeechError>(false, genericError)); ResetSpeechToText(); _speechSemaphore.Release(); return; } if (result.Final == true) { _speechAudioEngine.Stop(); inputNode.RemoveTapOnBus(0); recognitionResult = new Tuple <string, Tuple <bool, CMPSpeechError> >(result.BestTranscription. FormattedString, new Tuple <bool, CMPSpeechError>(true, null)); ResetSpeechToText(); _speechSemaphore.Release(); return; } }); var audioFormat = inputNode.GetBusOutputFormat(0); inputNode.InstallTapOnBus(0, 2048, audioFormat, (AVAudioPcmBuffer buffer, AVAudioTime when) => { var state = _speechRecognitionTask.State; _speechRecognitionRequest.Append(buffer); }); _speechAudioEngine.Prepare(); NSError audioEngineError = null; bool couldStart = _speechAudioEngine.StartAndReturnError(out audioEngineError); if (couldStart == false) { genericError = PrepareAudioEngineError(audioEngineError); recognitionResult = new Tuple <string, Tuple <bool, CMPSpeechError> >(string.Empty, new Tuple <bool, CMPSpeechError>(false, genericError)); ResetSpeechToText(); _speechSemaphore.Release(); return; } } catch (Exception exception) { Diagonostics.Debug.WriteLine(exception.Message); ResetSpeechToText(); _speechSemaphore.Release(); } }); await _speechSemaphore.WaitAsync(); return(recognitionResult); }
public void CancelInterpretation() { AudioEngine.Stop(); RecognitionTask.Cancel(); }
public void StopRecording() { audioEngine.Stop(); recognitionRequest.EndAudio(); }
public void StartRecording(string voice, bool longTimeout = false) { if (!SpeechEnabled) { return; } // Setup audio session LastResult = ""; AudioEngine = new AVAudioEngine(); NSLocale voiceLocale = NSLocale.FromLocaleIdentifier(voice); SpeechRecognizer = new SFSpeechRecognizer(voiceLocale); LiveSpeechRequest = new SFSpeechAudioBufferRecognitionRequest(); NSError error; var audioSession = AVAudioSession.SharedInstance(); audioSession.SetCategory(AVAudioSessionCategory.Record); audioSession.SetMode(AVAudioSession.ModeMeasurement, out error); if (error != null) { OnSpeechError?.Invoke("Audio session error: " + error.ToString()); return; } audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); LiveSpeechRequest.ShouldReportPartialResults = true; var node = AudioEngine.InputNode; if (node == null) { OnSpeechError?.Invoke("Couldn't initialize Speech Input"); return; } RecognitionTask = SpeechRecognizer.GetRecognitionTask(LiveSpeechRequest, (SFSpeechRecognitionResult result, NSError err) => { if (IsCancelled) { node.RemoveTapOnBus(0); return; } if (err != null) { OnSpeechError?.Invoke(err.ToString()); } else if (result != null) { LastResult = result.BestTranscription.FormattedString; Console.WriteLine("You said: \"{0}\". Final: {1}", LastResult, result.Final); m_lastSpeech = DateTime.Now; if (result.Final)// || !IsRecording) { { OnSpeechOK?.Invoke(LastResult); } } if ((result != null && result.Final) || err != null || !IsRecording) { IsRecording = false; //node.RemoveTapOnBus(0); AudioEngine.Stop(); m_speechTimer.Close(); } }); var recordingFormat = node.GetBusOutputFormat(0); node.InstallTapOnBus(0, 1024, recordingFormat, (AVAudioPcmBuffer buffer, AVAudioTime when) => { //Console.WriteLine("--> {0}: {1} {2}.{3}", buffer.FrameLength, when.HostTime); // Append buffer to recognition request LiveSpeechRequest.Append(buffer); }); // Start recording AudioEngine.Prepare(); AudioEngine.StartAndReturnError(out error); if (error != null) { OnSpeechError?.Invoke("Speech init error: " + error.ToString()); IsRecording = false; return; } IsRecording = true; IsCancelled = false; LastResult = ""; m_lastSpeech = DateTime.MaxValue; m_startSpeech = DateTime.Now; m_timeout = longTimeout ? m_phraseTimeout : m_wordTimeout; m_speechTimer = new System.Timers.Timer(250); m_speechTimer.AutoReset = true; m_speechTimer.Elapsed += (sender, e) => { CheckRecording(); }; m_speechTimer.Start(); }
void StartRecording() { // Cancel the previous task if it's running. recognitionTask?.Cancel(); recognitionTask = null; var audioSession = AVAudioSession.SharedInstance(); NSError err; err = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.DefaultToSpeaker); audioSession.SetMode(AVAudioSession.ModeMeasurement, out err); err = audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); // Configure request so that results are returned before audio recording is finished recognitionRequest = new SFSpeechAudioBufferRecognitionRequest { ShouldReportPartialResults = true, }; var inputNode = audioEngine.InputNode; if (inputNode == null) { throw new InvalidProgramException("Audio engine has no input node"); } // A recognition task represents a speech recognition session. // We keep a reference to the task so that it can be cancelled. recognitionTask = speechRecognizer.GetRecognitionTask(recognitionRequest, (result, error) => { var isFinal = false; if (result != null) { speechIdleTimer.Stop(); speechIdleTimer.Start(); textView.Text = result.BestTranscription.FormattedString; isFinal = result.Final; } if (error != null || isFinal) { if (result != null) { var intent = nlp.GetMatchingIntent(result.BestTranscription.FormattedString); string resultText; if (intent != null) { textView.Text += "\nAction is " + intent.Action + "."; resultText = "Action is " + intent.Action + ". "; if (intent.Parameters != null) { intent.Parameters.ForEach(p => { resultText += "Parameter " + p.Key + " with values" + string.Join(",", p.Value) + ". "; textView.Text += "\nParameter " + p.Key + " with values " + string.Join(",", p.Value) + ". "; }); } } else { resultText = "Sorry, I did not get that."; } var su = new AVSpeechUtterance(resultText) { Rate = AVSpeechUtterance.MaximumSpeechRate / 2, Voice = AVSpeechSynthesisVoice.FromLanguage("en-US"), PitchMultiplier = 1.0f, Volume = 1 }; ss.SpeakUtterance(su); } audioEngine.Stop(); inputNode.RemoveTapOnBus(0); recognitionRequest = null; recognitionTask = null; recordButton.Enabled = true; //recordButton.SetTitle ("Start Recording", UIControlState.Normal); recordButton.Hidden = false; recordStatus.Hidden = true; speechIdleTimer.Stop(); } }); var recordingFormat = inputNode.GetBusOutputFormat(0); inputNode.InstallTapOnBus(0, 1024, recordingFormat, (buffer, when) => { recognitionRequest?.Append(buffer); }); audioEngine.Prepare(); audioEngine.StartAndReturnError(out err); }