public void Dispose()
 {
     synthesizer.DidFinishSpeechUtterance -= OnFinishedSpeechUtterance;
     utterance.Dispose();
     synthesizer.Dispose();
     utterance   = null;
     synthesizer = null;
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        public IObservable <Unit> Speak(string speechString)
        {
            Ensure.ArgumentNotNull(speechString, nameof(speechString));

            return(Observable
                   .Create <Unit>(
                       observer =>
            {
                var disposables = new CompositeDisposable();
                var utterance = new AVSpeechUtterance(speechString)
                {
                    Voice = voice,
                    Rate = 0.55f
                }
                .AddTo(disposables);
                var synthesizer = new AVSpeechSynthesizer()
                                  .AddTo(disposables);
                var finishedUtterance = Observable
                                        .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x)
                                        .ToSignal()
                                        .Publish();

                finishedUtterance
                .SubscribeSafe(
                    _ =>
                {
                    utterance.Dispose();
                    synthesizer.Dispose();
                })
                .AddTo(disposables);

                finishedUtterance
                .FirstAsync()
                .Subscribe(observer)
                .AddTo(disposables);

                finishedUtterance
                .Connect()
                .AddTo(disposables);

                synthesizer.SpeakUtterance(utterance);

                return disposables;
            }));
        }
Ejemplo n.º 5
0
        public IObservable <Unit> Speak(string speechString)
        {
            Ensure.ArgumentNotNull(speechString, 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());
        }
Ejemplo n.º 6
0
        public IObservable<Unit> Speak(string speechString)
        {
            Ensure.ArgumentNotNull(speechString, 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();
        }