public Task <bool> Speak(string text, bool disableMusic = false)
        {
            _speakTask = new TaskCompletionSource <bool>();
            if (IsEnabled)
            {
                var speechUtterance = new AVSpeechUtterance(text)
                {
                    Rate            = AVSpeechUtterance.MaximumSpeechRate / 2,
                    Voice           = AVSpeechSynthesisVoice.FromLanguage("en-US"),
                    Volume          = 1.0f,
                    PitchMultiplier = 1.0f
                                      //PreUtteranceDelay = 0.1
                };

                _speechSynthesizer.DidFinishSpeechUtterance += (sender, e) =>
                {
                    _speakTask.TrySetResult(true);
                };

                if (_speechSynthesizer.Speaking)
                {
                    _speechSynthesizer.StopSpeaking(AVSpeechBoundary.Immediate);
                }

                _speechSynthesizer.SpeakUtterance(speechUtterance);
            }
            return(_speakTask.Task);
        }
Пример #2
0
        private void SpeakUtterance(bool queue, AVSpeechUtterance speechUtterance)
        {
            if (!queue && speechSynthesizer.Speaking)
            {
                speechSynthesizer.StopSpeaking(AVSpeechBoundary.Word);
            }

            speechSynthesizer.SpeakUtterance(speechUtterance);
        }
Пример #3
0
 public void StopAudio()
 {
     if (_speechSynthesizer != null && _speechSynthesizer.Speaking)
     {
         try
         {
             _speechSynthesizer.StopSpeaking(AVSpeechBoundary.Immediate);
         }
         catch (Exception) { }
     }
 }
Пример #4
0
 public void StopPlaying(FinishEvent audioFinishHandler = null)
 {
     if (speechSynthesizer != null && IsPlaying)
     {
         IsPlaying = false;
         speechSynthesizer.StopSpeaking(AVSpeechBoundary.Immediate);
     }
     else
     {
         audioFinishHandler?.Invoke();
     }
 }
 public async Task Speak(string text, CancellationToken cancelToken)
 {
     this.IsSpeaking = true;
     using (var speech = new AVSpeechSynthesizer()) {
         using (var utterance = this.CreateSpeech(text)) {
             cancelToken.Register(() => {
                 if (this.IsSpeaking)
                 {
                     speech.StopSpeaking(AVSpeechBoundary.Immediate);
                     this.IsSpeaking = false;
                 }
             });
             speech.SpeakUtterance(utterance);
         }
     }
     this.IsSpeaking = false;
 }
        public IObservable<Unit> SpeakAsync(string speechString, CancellationToken cancellationToken = default(CancellationToken))
        {
            speechString.AssertNotNull(nameof(speechString));

            var utterance = new AVSpeechUtterance(speechString)
            {
                Voice = voice,
                Rate = 0.55f
            };
            var synthesizer = new AVSpeechSynthesizer();
            var finishedUtterance = Observable
                .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x)
                .Select(_ => Unit.Default)
                .Publish();

            finishedUtterance
                .Subscribe(
                    _ =>
                    {
                        utterance.Dispose();
                        synthesizer.Dispose();
                    });

            if (cancellationToken.CanBeCanceled)
            {
                cancellationToken.Register(() => synthesizer.StopSpeaking(AVSpeechBoundary.Immediate));

                Observable
                    .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidCancelSpeechUtterance += x, x => synthesizer.DidCancelSpeechUtterance -= x)
                    .Select(_ => Unit.Default)
                    .Subscribe(
                        _ =>
                        {
                            utterance.Dispose();
                            synthesizer.Dispose();
                        });
            }

            synthesizer.SpeakUtterance(utterance);
            finishedUtterance.Connect();

            return finishedUtterance
                .FirstAsync()
                .RunAsync(cancellationToken);
        }
Пример #7
0
        public IObservable <Unit> SpeakAsync(string speechString, CancellationToken cancellationToken = default(CancellationToken))
        {
            speechString.AssertNotNull(nameof(speechString));

            var utterance = new AVSpeechUtterance(speechString)
            {
                Voice = voice,
                Rate  = 0.55f
            };
            var synthesizer       = new AVSpeechSynthesizer();
            var finishedUtterance = Observable
                                    .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x)
                                    .Select(_ => Unit.Default)
                                    .Publish();

            finishedUtterance
            .Subscribe(
                _ =>
            {
                utterance.Dispose();
                synthesizer.Dispose();
            });

            if (cancellationToken.CanBeCanceled)
            {
                cancellationToken.Register(() => synthesizer.StopSpeaking(AVSpeechBoundary.Immediate));

                Observable
                .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidCancelSpeechUtterance += x, x => synthesizer.DidCancelSpeechUtterance -= x)
                .Select(_ => Unit.Default)
                .Subscribe(
                    _ =>
                {
                    utterance.Dispose();
                    synthesizer.Dispose();
                });
            }

            synthesizer.SpeakUtterance(utterance);
            finishedUtterance.Connect();

            return(finishedUtterance
                   .FirstAsync()
                   .RunAsync(cancellationToken));
        }
Пример #8
0
        public static void Speak(string text, bool force = false)
        {
            if (!force && !Sound)
            {
                return;
            }
            if (g_synthesizer.Speaking)
            {
                g_synthesizer.StopSpeaking(AVSpeechBoundary.Immediate);
            }

            var speechUtterance = new AVSpeechUtterance(text)
            {
                Rate            = SpeechRate * AVSpeechUtterance.MaximumSpeechRate,
                Voice           = AVSpeechSynthesisVoice.FromLanguage(Voice),
                Volume          = Volume,
                PitchMultiplier = PitchMultiplier
            };

            g_synthesizer.SpeakUtterance(speechUtterance);
        }
Пример #9
0
        public override async Task Speak(string text, System.Threading.CancellationToken cancellationToken)
        {
            var utterance = new AVSpeechUtterance(text);
            var source    = await utterances.GetOrAddAsync(utterance, (u) => new TaskCompletionSource <string>());

            cancellationToken.Register(() => {
                if (synth.Speaking)
                {
                    synth.StopSpeaking(AVSpeechBoundary.Immediate);
                }
                source.TrySetCanceled();
            });

            synth.SpeakUtterance(utterance);
            try
            {
                await source.Task;
            }
            finally
            {
                await utterances.RemoveAsync(utterance);
            }
        }
 private void SpeakDirection(object sender, RouteTrackerNewVoiceGuidanceEventArgs e)
 {
     // Say the direction using voice synthesis.
     _speechSynthesizer.StopSpeaking(AVSpeechBoundary.Word);
     _speechSynthesizer.SpeakUtterance(new AVSpeechUtterance(e.VoiceGuidance.Text));
 }
Пример #11
0
        /// <summary>
        /// Speak back text
        /// </summary>
        /// <param name="text">Text to speak</param>
        /// <param name="queue">If you want to chain together speak command or cancel current</param>
        /// <param name="crossLocale">Locale of voice</param>
        /// <param name="pitch">Pitch of voice</param>
        /// <param name="speakRate">Speak Rate of voice (All) (0.0 - 2.0f)</param>
        /// <param name="volume">Volume of voice (iOS/WP) (0.0-1.0)</param>
        public void Speak(string text, bool queue = false, CrossLocale?crossLocale = null, float?pitch = null, float?speakRate = null, float?volume = null)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            if (speechSynthesizer == null)
            {
                Init();
            }

            var localCode = crossLocale.HasValue &&
                            !string.IsNullOrWhiteSpace(crossLocale.Value.Language) ?
                            crossLocale.Value.Language :
                            AVSpeechSynthesisVoice.CurrentLanguageCode;


            pitch = !pitch.HasValue ? 1.0f : pitch;

            if (!volume.HasValue)
            {
                volume = 1.0f;
            }
            else if (volume > 1.0f)
            {
                volume = 1.0f;
            }
            else if (volume < 0.0f)
            {
                volume = 0.0f;
            }

            var divid = 4.0f;

            if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))//use default .5f
            {
                divid = 2.0f;
            }
            else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))//use .125f
            {
                divid = 8.0f;
            }
            else
            {
                divid = 4.0f; //use .25f
            }
            if (!speakRate.HasValue)
            {
                speakRate = AVSpeechUtterance.MaximumSpeechRate / divid; //normal speech, default is fast
            }
            else if (speakRate.Value > AVSpeechUtterance.MaximumSpeechRate)
            {
                speakRate = AVSpeechUtterance.MaximumSpeechRate;
            }
            else if (speakRate.Value < AVSpeechUtterance.MinimumSpeechRate)
            {
                speakRate = AVSpeechUtterance.MinimumSpeechRate;
            }

            var voice = AVSpeechSynthesisVoice.FromLanguage(localCode);

            if (voice == null)
            {
                Console.WriteLine("Locale not found for voice: " + localCode + " is not valid using default.");
                voice = AVSpeechSynthesisVoice.FromLanguage(AVSpeechSynthesisVoice.CurrentLanguageCode);
            }

            if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            {
                var dummyUtterance = new AVSpeechUtterance(" ");
                dummyUtterance.Voice = voice;
                speechSynthesizer.SpeakUtterance(dummyUtterance);
            }

            var speechUtterance = new AVSpeechUtterance(text)
            {
                Rate            = speakRate.Value,
                Voice           = voice,
                Volume          = volume.Value,
                PitchMultiplier = pitch.Value
            };

            if (!queue && speechSynthesizer.Speaking)
            {
                speechSynthesizer.StopSpeaking(AVSpeechBoundary.Word);
            }

            speechSynthesizer.SpeakUtterance(speechUtterance);
        }
Пример #12
0
 public void StopSpeach()
 {
     _speechSynthesizer.StopSpeaking(AVSpeechBoundary.Immediate);
 }
Пример #13
0
 static void DoStop()
 {
     SpeechSynthesizer.StopSpeaking(AVSpeechBoundary.Word);
 }
Пример #14
0
 void TryCancel()
 {
     speechSynthesizer.StopSpeaking(AVSpeechBoundary.Word);
     currentSpeak?.TrySetCanceled();
 }