コード例 #1
0
        public async Task SpeakAsync(string text, int max, SpeechOptions options, CancellationToken cancelToken)
        {
            await Initialize();

            // Wait for any previous calls to finish up
            if (tcsUtterances?.Task != null)
            {
                await tcsUtterances.Task;
            }

            tcsUtterances = new TaskCompletionSource <bool>();

            if (cancelToken != default)
            {
                cancelToken.Register(() =>
                {
                    try
                    {
                        tts?.Stop();

                        tcsUtterances?.TrySetResult(true);
                    }
                    catch
                    {
                    }
                });
            }

            if (options?.Locale?.Language != null)
            {
                JavaLocale locale = null;
                if (!string.IsNullOrWhiteSpace(options?.Locale.Country))
                {
                    locale = new JavaLocale(options.Locale.Language, options.Locale.Country);
                }
                else
                {
                    locale = new JavaLocale(options.Locale.Language);
                }

                tts.SetLanguage(locale);
            }
            else
            {
                SetDefaultLanguage();
            }

            if (options?.Pitch.HasValue ?? false)
            {
                tts.SetPitch(options.Pitch.Value);
            }
            else
            {
                tts.SetPitch(TextToSpeech.PitchDefault);
            }

            tts.SetSpeechRate(1.0f);

            var parts = text.SplitSpeak(max);

            numExpectedUtterances = parts.Count;

            var guid = Guid.NewGuid().ToString();

            for (var i = 0; i < parts.Count && !cancelToken.IsCancellationRequested; i++)
            {
                // We require the utterance id to be set if we want the completed listener to fire
                var map = new Dictionary <string, string>
                {
                    { AndroidTextToSpeech.Engine.KeyParamUtteranceId, $"{guid}.{i}" }
                };

                if (options != null && options.Volume.HasValue)
                {
                    map.Add(AndroidTextToSpeech.Engine.KeyParamVolume, options.Volume.Value.ToString(CultureInfo.InvariantCulture));
                }

                // We use an obsolete overload here so it works on older API levels at runtime
                // Flush on first entry and add (to not flush our own previous) subsequent entries
#pragma warning disable CS0618
                tts.Speak(parts[i], i == 0 ? QueueMode.Flush : QueueMode.Add, map);
#pragma warning restore CS0618
            }

            await tcsUtterances.Task;
        }
コード例 #2
0
 public Task SpeakAsync(string text, SpeechOptions options, CancellationToken cancelToken) =>
 throw ExceptionUtils.NotSupportedOrImplementedException;
コード例 #3
0
    private void Start()
    {
        // Before we do anything, we need to set up Charisma. Put this in your initialisation code. You only need to do this one.
        Playthrough.Setup();

        // The Charisma logger logs events to and from Charisma.
        CharismaLogger.IsActive = showLog;

        // We create the config of our token, based on the settings we have defined in the inspector, here.
        var playthroughTokenParams = new CreatePlaythroughTokenParams(storyId: storyId, storyVersion: storyVersion, apiKey: apiKey);

        // We use these settings to create a play-through token.
        CharismaAPI.CreatePlaythroughToken(tokenParams: playthroughTokenParams, callback: token =>
        {
            // Once we receive the callback with our token, we can create a new conversation.
            CharismaAPI.CreateConversation(token: token, callback: conversationId =>
            {
                // We'll cache our conversation Id since we need this to send replies and other events to Charisma.
                this._conversationId = conversationId;

                // We can now create a new charisma object and pass it our token.
                this._charisma = new Playthrough(token: token);

                // We can now connect to Charisma. Once we receive the ready callback, we can start our play-through.
                _charisma.Connect(onReadyCallback: () =>
                {
                    Debug.Log("Ready!");
                    speechOptions = new SpeechOptions(SpeechOptions.AudioOutput.Buffer, SpeechOptions.Encoding.Ogg);

                    // In the start function, we pass the scene we want to start from, the conversationId we cached earlier, and the speech options from the inspector.
                    _charisma.Start(sceneIndex: startFromScene, conversationId: _conversationId, speechOptions: speechOptions);
                });

                // We can now subscribe to message events from charisma.
                _charisma.OnMessage += (id, message) =>
                {
                    Debug.Log(message);
                    // If the message is a panel-node, we should operate on this data without trying to generate audio or access the text & character data of the node since panel-nodes have neither.
                    if (message.MessageType == MessageType.panel)
                    {
                        CharismaLogger.Log("This is a panel node");

                        // We can't generate speech or access character & text data so we return after we have checked if this is the end of the story.
                        if (message.EndStory)
                        {
                            _charisma.Disconnect();
                        }

                        return;
                    }

                    if (useSpeech)
                    {
                        // Once we have received a message character message, we might want to play the audio. To do this we run the GetClip method and wait for the callback which contains our audio clip, then pass it to the audio player.
                        message.Message.Speech?.Audio.GetClip(options: speechOptions, onAudioGenerated: (clip =>
                        {
                            audioSource.clip = clip;
                            audioSource.Play();
                        }));
                    }

                    text.text = ($"{message.Message.Character?.Name}: {message.Message?.Text}");

                    // If this is the end of the story, we disconnect from Charisma.
                    if (message.EndStory)
                    {
                        _charisma.Disconnect();
                    }
                };
            });
        });

        // Bind the SendPlayerMessage function to the UI button.
        button.onClick.AddListener(call: SendPlayerMessage);
    }
コード例 #4
0
        private async Task SpeakAsync(string value)
        {
            SpeechOptions settings = await GetSpeechOptions();

            await TextToSpeech.SpeakAsync(value, settings);
        }
コード例 #5
0
        private async Task InitializeRecognizer(string languageCode1, string languageCode2, string voice)
        {
            try
            {
                // Creates an instance of a speech translation config with specified subscription key and service region.
                var config = SpeechTranslationConfig.FromSubscription((string)Application.Current.Properties["SubscriptionKey"], (string)Application.Current.Properties["Region"]);

                // Sets source and target languages.
                string fromLanguage = languageCode1;
                string toLanguage   = languageCode2;
                config.SpeechRecognitionLanguage = fromLanguage;
                config.AddTargetLanguage(toLanguage);

                // Sets the synthesis output voice name.
                // Replace with the languages of your choice, from list found here: https://aka.ms/speech/tts-languages
                config.VoiceName = voice;

                var stopRecognition = new TaskCompletionSource <int>();
                using (recognizer = new TranslationRecognizer(config))
                {
                    // Subscribes to events.
                    recognizer.Recognized += (s, e) =>
                    {
                        if (e.Result.Reason == ResultReason.TranslatedSpeech)
                        {
                            foreach (var element in e.Result.Translations.Where(x => !string.IsNullOrWhiteSpace(x.Value)))
                            {
                                if (!string.IsNullOrWhiteSpace(element.Value))
                                {
                                    TextResults.Enqueue(new KeyValuePair <string, string>(e.Result.Text, element.Value));
                                }
                                else
                                {
                                    var x = "WHY?";
                                }
                                var options = new SpeechOptions();
                            }
                        }
                    };

                    recognizer.Synthesizing += async(s, e) =>
                    {
                        var audio = e.Result.GetAudio();

                        if (audio.Length > 0)
                        {
                            try
                            {
                                AudioResults.Enqueue(audio);
                            }
                            catch (Exception ex)
                            {
                                Log("error Synthesizing " + ex.Message);
                                Crashes.TrackError(ex);
                            }
                        }
                    };


                    recognizer.Canceled += (s, e) =>
                    {
                        Log($"CANCELED: Reason={e.Reason}");

                        if (e.Reason == CancellationReason.Error)
                        {
                            Log($"CANCELED: ErrorCode={e.ErrorCode}");
                            Log($"CANCELED: ErrorDetails={e.ErrorDetails}");
                            Log($"CANCELED: Did you update the subscription info?");
                        }

                        stopRecognition.TrySetResult(0);
                    };

                    recognizer.SessionStarted += (s, e) =>
                    {
                        Log("Session started event.");
                    };

                    recognizer.SessionStopped += async(s, e) =>
                    {
                        Device.BeginInvokeOnMainThread(async() =>
                        {
                            Log("Session stopped event.");

                            Log("Return results now.");

                            var sourceText = string.Empty;
                            var targetText = string.Empty;
                            KeyValuePair <string, string> result;

                            while (TextResults.Count > 0)
                            {
                                result     = TextResults.Dequeue();
                                sourceText = sourceText + " " + result.Key;
                                targetText = targetText + " " + result.Value;
                            }

                            if (!string.IsNullOrWhiteSpace(targetText))
                            {
                                if (languageCode1 == (string)Application.Current.Properties["LanguageCode1"])
                                {
                                    UpdateUI(LayoutOptions.Start, sourceText, targetText, (string)Application.Current.Properties["LanguageCode2"], true);
                                }
                                else
                                {
                                    UpdateUI(LayoutOptions.End, sourceText, targetText, (string)Application.Current.Properties["LanguageCode1"], false);
                                }

                                service = DependencyService.Get <IAudioService>();

                                service.PlaySound(AudioResults);

                                stopRecognition.TrySetResult(0);
                            }
                            else
                            {
                                var x = "WHY?";
                            }
                        });
                    };

                    // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                    await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                    // Waits for completion.
                    // Use Task.WaitAny to keep the task rooted.
                    Task.WaitAny(new[] { stopRecognition.Task });

                    // Stops recognition.
                    await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Log("From Pressed Error " + ex.Message);
                Crashes.TrackError(ex);
            }
        }
コード例 #6
0
        async void OnSaveButtonClicked(object sender, EventArgs e)
        {
            if (wordText.Text == null || translation.Text == null || picker1.SelectedIndex == -1)
            {
                await DisplayAlert("Alert!", "Fill all the fields!", "Ok");

                return;
            }

            var picker = stackLayout.Children.FirstOrDefault(x => x.ClassId == "picker2") as Picker;

            if (_word == null)
            {
                _word = new Word()
                {
                    Id = Guid.NewGuid(),
                }
            }
            ;

            _word.Text          = wordText.Text;
            _word.Transcription = transcription.Text;
            _word.Translation   = translation.Text;
            _word.Language      = picker1.SelectedItem.ToString();

            if (isEditing)
            {
                App.Context.Words.Update(_word);
            }
            else
            {
                App.Context.Add(_word);
            }

            if (picker != null && picker.SelectedIndex != -1)
            {
                Collection collection = await App.Context.Collections.FirstOrDefaultAsync(x =>
                                                                                          x.Name == picker.ItemsSource[picker.SelectedIndex].ToString());

                if (collection != null)
                {
                    collection.Words.Add(_word);

                    App.Context.Collections.Update(collection);
                }
            }

            await App.Context.SaveChangesAsync();

            await Navigation.PopAsync();
        }

        async void OnCancelButtonClicked(object sender, EventArgs e) => await Navigation.PopAsync();

        async void OnDeleteClicked(object sender, EventArgs e)
        {
            var result = await DisplayAlert("Delete this item?", "This is permanent and cannot be undone.", "Delete", "Cancel");

            if (!result)
            {
                return;
            }

            App.Context.Words.Remove(_word);

            await App.Context.SaveChangesAsync();

            await Navigation.PopAsync();
        }

        async void OnSpeachClicked(object sender, EventArgs e)
        {
            var locales = await TextToSpeech.GetLocalesAsync();

            // Grab the first locale
            var locale = locales.FirstOrDefault(x => x.Name.ToLower().Contains(_word.Language.ToLower()));

            var settings = new SpeechOptions
            {
                Volume = 1
            };

            if (locale != null)
            {
                settings.Locale = locale;
            }

            await TextToSpeech.SpeakAsync(_word.Text, settings);
        }
    }
コード例 #7
0
 /// <inheritdoc />
 public IObservable <Unit> Speak(string text, SpeechOptions settings, CancellationToken cancelToken = default) =>
 Observable.FromAsync(() => TextToSpeech.SpeakAsync(text, settings, cancelToken));
コード例 #8
0
        private void CheckWord(Button checkButton)
        {
            var settings = new SpeechOptions()
            {
                Volume = 1.0f, Pitch = 1.0f
            };

            TextToSpeech.SpeakAsync(currentWord, settings);

            if (checkButton.Text == currentWord)
            {
                using (var db = new RandomWordsContext(Path.Combine(dbFolder, fileName)))
                {
                    var tableToChange = db.Words.SingleOrDefault(b => b.EnglishWord == currentWord);    // add try +1
                    int numberOfTry   = tableToChange.Try;

                    if (numberOfTry == 5)
                    {
                        db.Remove(tableToChange);
                    }
                    else
                    {
                        tableToChange.Try++;
                    }
                    db.SaveChanges();
                }
            }
            else
            {
                using (var db = new RandomWordsContext(Path.Combine(dbFolder, fileName)))
                {
                    var tableToChange = db.Words.SingleOrDefault(b => b.EnglishWord == currentWord); // refresh try 0
                    tableToChange.Try = 0;
                    db.SaveChanges();
                }

                Thread.Sleep(500);
                try
                {
                    TextToSpeech.SpeakAsync(currentWord, settings);
                }
                catch {
                }
            }


            MainThread.BeginInvokeOnMainThread(() =>
            {
                if (btn1.Text == currentWord)
                {
                    btn1.BackgroundColor = Color.Green;
                }                                                                     //else { button2.BackColor = Color.Red; }
                if (btn2.Text == currentWord)
                {
                    btn2.BackgroundColor = Color.Green;
                }                                                                     //else { button3.BackColor = Color.Red; }
                if (btn3.Text == currentWord)
                {
                    btn3.BackgroundColor = Color.Green;
                }                                                                     //else { button4.BackColor = Color.Red; }
                if (btn4.Text == currentWord)
                {
                    btn4.BackgroundColor = Color.Green;
                }                                                                     //else { button5.BackColor = Color.Red; }
                btn1.IsEnabled = false;
                btn2.IsEnabled = false;
                btn3.IsEnabled = false;
                btn4.IsEnabled = false;
            });


            Thread.Sleep(3000);
            TextToSpeech.SpeakAsync(currentWord, settings);
            Thread.Sleep(2000);
            MainThread.BeginInvokeOnMainThread(() =>
            {
                btn1.BackgroundColor = Color.DarkGray;
                btn2.BackgroundColor = Color.DarkGray;
                btn3.BackgroundColor = Color.DarkGray;
                btn4.BackgroundColor = Color.DarkGray;
                SetWords();
            });
        }
コード例 #9
0
 private void SetSpechOptions(PropertiesPage sender, SpeechOptions newOptions)
 {
     currentOptions = newOptions;
 }