예제 #1
0
        private void StartListening()
        {
            PowerLock.RequestScreenLock(10);
            SetLabelText(string.Empty);
            Task.Run(() => Parallel.Invoke(
                         () =>
            {
                if (Pref.GetSoundFeedback())
                {
                    _player.Start();
                }
            },
                         () =>
            {
                if (Pref.GetVibrateFeedback())
                {
                    Vibrator vibrator = Vibrator.Vibrators[0];
                    vibrator.Vibrate(200, 100);
                }
            }
                         ));

            AudioRecorder.StartRecording(Pref.GetHtmlResponse());
            (ProgressPopup ?? (ProgressPopup = new ProgressPopup())).Show();

            SetActionButtonIsEnabled(false);
            SetButtonImage("listen_disabled_allgreyedout.png");
        }
예제 #2
0
        private void Connection_StatusChanged(object sender, ConnectionStatusEventArgs e)
        {
            if (e.Reason == ConnectionStatus.Connected)
            {
                IsConnected = true;
                TextPopUp.Dismiss();
                _onConnectedCallback();
                if (responseHandler == null)
                {
                    responseHandler = new ResponseHandler();
                }
            }

            if (e.Reason == ConnectionStatus.ConnectionClosed || e.Reason == ConnectionStatus.ConnectionLost || e.Reason == ConnectionStatus.Unknown)
            {
                IsConnected = false;
                _connection.Dispose();
                TextPopUp.Text = "Connection lost. Trying to reconnect in 5s.";
                TextPopUp.Show();
                MainPage.SetActionButtonIsEnabled(false);
                MainPage.SetButtonImage("listen_disabled_allgreyedout.png");

                if (AudioRecorder.IsRecording)
                {
                    AudioRecorder.StopRecording();
                }

                //if (AudioPlayer.IsPlaying)
                //{
                //    AudioPlayer.Stop();
                //}
                _connection.DataReceived  -= Connection_DataReceived;
                _connection.StatusChanged -= Connection_StatusChanged;
                _connection.Close();
                _connection = null;
                _peer       = null;
                _agent      = null;

                _reconnectTimer.Start();
            }
        }
예제 #3
0
        private async void Connect()
        {
            try
            {
                agent = await Agent.GetAgent("/org/cybernetic87/gassist");

                agent.PeerStatusChanged += PeerStatusChanged;
                var peers = await agent.FindPeers();

                if (peers.Count() > 0)
                {
                    peer       = peers.First();
                    connection = peer.Connection;
                    connection.DataReceived  -= Connection_DataReceived;
                    connection.DataReceived  += Connection_DataReceived;
                    connection.StatusChanged -= Connection_StatusChanged;
                    connection.StatusChanged += Connection_StatusChanged;
                    await connection.Open();

                    if (reconnectTimer.Enabled == true)
                    {
                        reconnectTimer.Stop();
                    }

                    actionButton.IsEnable = true;
                }
                else
                {
                    ShowMessage("Any peer not found, trying to launch app");
                    StartAndConnect();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message, ex.ToString());
            }
            audioRecorder = new AudioRecorder(connection, agent, bufferSize);
            audioPlayer   = new AudioPlayer(OnStopCallback);
        }
예제 #4
0
        public async Task HandleResponse(byte[] dataBytes)
        {
            var ar = AssistResponse.Parser.ParseFrom(dataBytes);

            //if (ar.DialogStateOut.MicrophoneMode == DialogStateOut.Types.MicrophoneMode.CloseMicrophone)
            //{
            //    return;
            //}

            if (ar.EventType == AssistResponse.Types.EventType.EndOfUtterance)
            {
                AudioRecorder.StopRecording();

                if (!string.IsNullOrEmpty(ar.DialogStateOut?.SupplementalDisplayText))
                {
                    MainPage.SetLabelText(ar.DialogStateOut.SupplementalDisplayText);
                }

                if (string.IsNullOrEmpty(MainPage.ProgressPopup.GetText()))
                {
                    MainPage.SetButtonImage("listen_blue.png");
                }

                MainPage.ProgressPopup.Dismiss();
                await Task.Run(Player.Prepare).ConfigureAwait(false);

                MainPage.SetActionButtonIsEnabled(true);
            }

            if (ar.SpeechResults?.Any(i => (int)i.Stability == 1) == false)
            {
                if (MainPage.Pref.GetRawVoiceRecognitionText())
                {
                    foreach (string transcript in ar.SpeechResults.Select(x => x.Transcript))
                    {
                        MainPage.ProgressPopup.UpdateText(transcript);
                    }
                }

                if (!MainPage.Pref.GetRawVoiceRecognitionText() && ar.SpeechResults.Any(i => i.Stability > 0.01))
                {
                    foreach (string transcript in ar.SpeechResults.Where(x => x.Stability > 0.01).Select(x => x.Transcript))
                    {
                        MainPage.ProgressPopup.UpdateText(transcript);
                    }
                }
            }

            if (!string.IsNullOrEmpty(ar.DialogStateOut?.SupplementalDisplayText))
            {
                MainPage.SetLabelText(ar.DialogStateOut.SupplementalDisplayText);
                //return;
            }

            if (ar.ScreenOut != null)
            {
                MainPage.SetHtmlView(ar.ScreenOut.Data.ToStringUtf8());
            }

            if ((ar.DialogStateOut?.VolumePercentage ?? 0) != 0)
            {
                var newVolumeLevel = Convert.ToInt32(15 * ar.DialogStateOut.VolumePercentage / 100);
                AudioManager.VolumeController.Level[AudioVolumeType.Media] = newVolumeLevel;
                MainPage.SetButtonImage("listen_blue.png");
                MainPage.SetActionButtonIsEnabled(true);
                return;
            }

            if (ar.AudioOut?.AudioData.Length > 0)
            {
                await Player.WriteBuffer(ar.AudioOut.AudioData.ToByteArray()).ConfigureAwait(false);

                if (AudioPlayer.IsPrepared && !AudioPlayer.IsPlaying && Player.Buffered >= 1600)
                {
                    AudioPlayer.IsPlaying = true;
                    Player.Play();
                }
            }
        }