private void SetupTranscriptionAndTranslationService()
        {
            try
            {
                var lCognitiveKey    = _settings.AzureCognitiveKey;
                var lCognitiveRegion = _settings.AzureCognitiveRegion;

                _eventPublisher.Publish("MySTT Setup", $"Got region: {lCognitiveRegion}, key starting from: {lCognitiveKey??lCognitiveKey.Substring(0, lCognitiveKey.Length /2)}");

                this.mTransSpeechConfig = SpeechTranslationConfig.FromSubscription(lCognitiveKey, lCognitiveRegion);
                var fromLanguage = "en-US";
                var toLanguages  = new List <string> {
                    "el-GR"
                };
                //var toLanguages = new List<string> { "ru-RU" };
                this.mTransSpeechConfig.SpeechRecognitionLanguage = fromLanguage;
                toLanguages.ForEach(this.mTransSpeechConfig.AddTargetLanguage);
                this.mInputStream = AudioInputStream.CreatePushStream(AudioStreamFormat.GetWaveFormatPCM(SAMPLESPERSECOND, BITSPERSAMPLE, NUMBEROFCHANNELS));

                this.mAudioConfig           = AudioConfig.FromStreamInput(this.mInputStream);
                this.mTranslationRecognizer = new TranslationRecognizer(this.mTransSpeechConfig, this.mAudioConfig);

                this.mTranslationRecognizer.Recognizing       += this.MSpeechRecognizer_Recognizing;
                this.mTranslationRecognizer.Recognized        += this.MSpeechRecognizer_Recognized;
                this.mTranslationRecognizer.SpeechEndDetected += this.MSpeechRecognizer_SpeechEndDetected;

                this.StartRecognisionIfNeeded();
            }
            catch (Exception ex)
            {
                _eventPublisher.Publish("MySTT Setup - Failed", $"Failed to initialize: {ex.Message}");
            }
        }
예제 #2
0
    private async void StopRecognition()
    {
        if (!isRecognitionStarted)
        {
            return;
        }
        Debug.Log("stop recognition");

        translatingMessage = "";
        translatedMessage  = "";

        await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);

        recognizer.Canceled            -= CanceledHandler;
        recognizer.SessionStarted      -= SessionStartedHandler;
        recognizer.SessionStopped      -= SessionStoppedHandler;
        recognizer.SpeechStartDetected -= SpeechStartDetectedHandler;
        recognizer.SpeechEndDetected   -= SpeechEndDetectedHandler;
        recognizer.Recognizing         -= RecognizingHandler;
        recognizer.Recognized          -= RecognizedHandler;
        recognizer.Dispose();
        recognizer = null;

        isRecognitionStarted      = false;
        isRecognitionStateChanged = true;
    }
예제 #3
0
        public static async Task <String> TranslationContinuousRecognitionAsync()
        {
            var    config       = SpeechTranslationConfig.FromSubscription("a8c500c9ef9f421e977486a17f0adcca", "westeurope");
            string fromLanguage = "pl-PL";

            config.SpeechRecognitionLanguage = fromLanguage;
            config.AddTargetLanguage("pl");
            string result = "";
            // Sets voice name of synthesis output.
            const string PolishVoice = "pl-PL";

            config.VoiceName = PolishVoice;
            // Creates a translation recognizer using microphone as audio input.
            using (var recognizer = new TranslationRecognizer(config))
            {
                // Subscribes to events.


                recognizer.Recognized += async(s, e) =>
                {
                    if (e.Result.Reason == ResultReason.TranslatedSpeech)
                    {
                        await recognizer.StopContinuousRecognitionAsync();

                        result = e.Result.Text;
                    }
                };
                return(result);
            }
        }
        public async Task StartSpeechRecognitionAsync()
        {
            SpeechTranslationConfig config = GetRecognizerConfig();

            if (config == null)
            {
                return;
            }

            ResetState();
            DisposeRecognizer();

            DeviceInformation microphoneInput = await Util.GetDeviceInformation(DeviceClass.AudioCapture, SettingsHelper.Instance.MicrophoneName);

            using (AudioConfig audioConfig = AudioConfig.FromMicrophoneInput(microphoneInput.Id))
            {
                translationRecognizer                 = audioConfig != null ? new TranslationRecognizer(config, audioConfig) : new TranslationRecognizer(config);
                translationRecognizer.Recognizing    += OnTranslateRecognizing;
                translationRecognizer.Recognized     += OnTranslateRecognized;
                translationRecognizer.Canceled       += OnTranslateCanceled;
                translationRecognizer.SessionStarted += (s, e) =>
                {
                    recognizeCancellationTokenSource = new CancellationTokenSource();
                };

                await translationRecognizer.StartContinuousRecognitionAsync();
            }
        }
예제 #5
0
    void Start()
    {
        Debug.Log("Start");

        isRecognitionStarted      = false;
        isRecognitionStateChanged = false;
        recognizer = null;

        ui          = GameObject.Find("Canvas");
        uiCanvas    = ui.GetComponent <Canvas>();
        uiScaler    = ui.GetComponent <CanvasScaler>();
        isUIVisible = true;
        HideUI();

        apiKeyInputField    = GameObject.Find("ApiKeyInputField").GetComponent <InputField>();
        apiRegionInputField = GameObject.Find("ApiRegionInputField").GetComponent <InputField>();

        modeList = GameObject.Find("ModeList").GetComponent <Dropdown>();
        modeList.onValueChanged.AddListener(ModeListValueChangeHandler);

        startStopButton = GameObject.Find("StartStopButton").GetComponent <Button>();
        startStopButton.onClick.AddListener(StartStopButtonClickHandler);
        startStopButtonText = GameObject.Find("StartStopButtonText").GetComponent <Text>();
        applyStartStopButtonLabel();

        subtitles          = GameObject.Find("Subtitles");
        subtitlesText      = subtitles.GetComponent <TextMeshPro>();
        subtitlesText.text = "";

        float gray = 0.8f;

        translatingMessageColor = new Color(gray, gray, gray, 0.5f);
        translatedMessageColor  = new Color(1.0f, 1.0f, 1.0f, 1.0f);
    }
    /// <summary>
    /// Stops the recognition on the speech recognizer or translator as applicable.
    /// Important: Unhook all events & clean-up resources.
    /// </summary>
    public async void StopRecognition()
    {
        if (recognizer != null)
        {
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);

            recognizer.Recognizing         -= RecognizingHandler;
            recognizer.Recognized          -= RecognizedHandler;
            recognizer.SpeechStartDetected -= SpeechStartDetectedHandler;
            recognizer.SpeechEndDetected   -= SpeechEndDetectedHandler;
            recognizer.Canceled            -= CanceledHandler;
            recognizer.SessionStarted      -= SessionStartedHandler;
            recognizer.SessionStopped      -= SessionStoppedHandler;
            recognizer.Dispose();
            recognizer       = null;
            recognizedString = "Speech Recognizer is now stopped.";
            Debug.Log("Speech Recognizer is now stopped.");
        }
        if (translator != null)
        {
            await translator.StopContinuousRecognitionAsync().ConfigureAwait(false);

            translator.Recognizing         -= RecognizingTranslationHandler;
            translator.Recognized          -= RecognizedTranslationHandler;
            translator.SpeechStartDetected -= SpeechStartDetectedHandler;
            translator.SpeechEndDetected   -= SpeechEndDetectedHandler;
            translator.Canceled            -= CanceledTranslationHandler;
            translator.SessionStarted      -= SessionStartedHandler;
            translator.SessionStopped      -= SessionStoppedHandler;
            translator.Dispose();
            translator       = null;
            recognizedString = "Speech Translator is now stopped.";
            Debug.Log("Speech Translator is now stopped.");
        }
    }
예제 #7
0
        /// <summary>
        /// Initializes the factory object with subscription key and region
        /// Initializes the recognizer object with a TranslationRecognizer
        /// Subscribes the recognizer to recognition Event Handlers
        /// If recognition is running, starts a thread which stops the recognition
        /// </summary>
        private void CreateRecognizer()
        {
            string region       = "eastasia";
            string fromLanguage = "en-US";
            //var toLanguages = new List<string>() { "zh", "de" };
            var toLanguages = new List <string>()
            {
                "ja"
            };
            // var voiceChinese = "zh-CN-Yaoyao";
            var    voiceChinese = "ja-JP-Ayumi";
            string sub_key      = "7620810559eb4d1c96f770c5ee019bd3";

            //string sub_key = "91ad01e1da954931955dc87b6fb71c0c";

            //this.factory = SpeechFactory.FromSubscription(SubscriptionKey, region);
            this.factory = SpeechFactory.FromSubscription(sub_key, region);

            this.recognizer = this.factory.CreateTranslationRecognizer(fromLanguage, toLanguages, voiceChinese);

            this.recognizer.IntermediateResultReceived += this.OnPartialResponseReceivedHandler;
            this.recognizer.FinalResultReceived        += this.OnFinalResponse;
            this.recognizer.SynthesisResultReceived    += this.OnSynthesis;
            this.recognizer.RecognitionErrorRaised     += this.OnError;
        }
예제 #8
0
        public static async Task TranslateSpeechToText()
        {
            // Creates an instance of a speech translation config with specified subscription key and service region.
            // Replace with your own subscription key and service region (e.g., "westus").
            var config = SpeechTranslationConfig.FromSubscription("311b76d8841344b6a277dbd8401611dc", "westus");

            // Sets source and target languages.
            // Replace with the languages of your choice, from list found here: https://aka.ms/speech/sttt-languages
            string fromLanguage = "en-US";

            config.SpeechRecognitionLanguage = fromLanguage;
            config.AddTargetLanguage("de");
            config.AddTargetLanguage("fr");

            // Creates a translation recognizer using the default microphone audio input device.
            using (var recognizer = new TranslationRecognizer(config))
            {
                // Starts translation, and returns after a single utterance is recognized. The end of a
                // single utterance is determined by listening for silence at the end or until a maximum of 15
                // seconds of audio is processed. The task returns the recognized text as well as the translation.
                // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
                // shot recognition like command or query.
                // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
                Console.WriteLine("Say something...");
                var result = await recognizer.RecognizeOnceAsync();

                // Checks result.
                if (result.Reason == ResultReason.TranslatedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED '{fromLanguage}': {result.Text}");
                    foreach (var element in result.Translations)
                    {
                        Console.WriteLine($"TRANSLATED into '{element.Key}': {element.Value}");
                    }
                }
                else if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED '{fromLanguage}': {result.Text} (text could not be translated)");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }
        /// <summary>
        /// Initializes the factory object with subscription key and region
        /// Initializes the recognizer object with a TranslationRecognizer
        /// Subscribes the recognizer to recognition Event Handlers
        /// If recognition is running, starts a thread which stops the recognition
        /// </summary>
        private void CreateRecognizer()
        {
            this.factory    = SpeechFactory.FromSubscription(SubscriptionKey, Region);
            this.recognizer = this.factory.CreateTranslationRecognizer(FromLanguage, ToLanguages, voice);

            this.recognizer.IntermediateResultReceived += this.OnPartialResponseReceivedHandler;
            this.recognizer.FinalResultReceived        += this.OnFinalResponse;
            this.recognizer.SynthesisResultReceived    += this.OnSynthesis;
            this.recognizer.RecognitionErrorRaised     += this.OnError;
        }
 private void DisposeRecognizer()
 {
     if (translationRecognizer != null)
     {
         translationRecognizer.Recognizing -= OnTranslateRecognizing;
         translationRecognizer.Recognized  -= OnTranslateRecognized;
         translationRecognizer.Canceled    -= OnTranslateCanceled;
         translationRecognizer              = null;
     }
 }
 public void Dispose()
 {
     if (recognized != null)
     {
         recognized.Recognizing    -= OnSpeechRecognizing;
         recognized.SessionStarted -= OnSessionStarted;
         recognized.SessionStopped -= OnSessionStopped;
         recognized.Dispose();
         recognized = null;
     }
 }
예제 #12
0
 private async Task StartSpeechTranscriptionEngine(string recognitionLanguage, string targetLanguage)
 {
     _translationConfig.SpeechRecognitionLanguage = recognitionLanguage;
     _translationConfig.AddTargetLanguage(targetLanguage);
     _speechConfig.SpeechRecognitionLanguage = targetLanguage;
     _speechConfig.SpeechSynthesisLanguage   = targetLanguage;
     _synthesizer            = new SpeechSynthesizer(_speechConfig, _output);
     _recognizer             = new TranslationRecognizer(_translationConfig, _audioInput);
     _recognizer.Recognized += RecognizerRecognized;
     await _recognizer.StartContinuousRecognitionAsync();
 }
예제 #13
0
        public async Task <ActionResult> translateAsync()
        {
            string fromLanguage = "en-US";


            const string GermanVoice = "Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)";

            var config = SpeechTranslationConfig.FromSubscription("ae9492aae8044a4c888a45a45e957d83", "westus");

            config.SpeechRecognitionLanguage = fromLanguage;
            config.VoiceName = GermanVoice;


            config.AddTargetLanguage("de");


            using (var recognizer = new TranslationRecognizer(config))
            {
                recognizer.Recognizing += (s, e) =>
                {
                    foreach (var element in e.Result.Translations)
                    {
                        ViewBag.message = element.Value;
                    }
                };

                recognizer.Recognized += (s, e) =>
                {
                    if (e.Result.Reason == ResultReason.TranslatedSpeech)
                    {
                        foreach (var element in e.Result.Translations)
                        {
                            ViewBag.message = element.Value;
                        }
                    }
                    else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        foreach (var element in e.Result.Translations)
                        {
                            ViewBag.message = element.Value;
                        }
                    }
                    else if (e.Result.Reason == ResultReason.NoMatch)
                    {
                    }
                };


                await recognizer.RecognizeOnceAsync();

                return(View("Index"));
            }
        }
예제 #14
0
        protected async Task StopRecognition()
        {
            if (speechRecognizer != null)
            {
                await speechRecognizer.StopContinuousRecognitionAsync();
            }
            if (translationRecognizer != null)
            {
                await translationRecognizer.StopContinuousRecognitionAsync();
            }

            speechRecognizer      = null;
            translationRecognizer = null;
        }
        /// <summary>
        /// Initializes the config object with subscription key and region
        /// Initializes the recognizer object with a TranslationRecognizer
        /// Subscribes the recognizer to recognition Event Handlers
        /// If recognition is running, starts a thread which stops the recognition
        /// </summary>
        private void CreateRecognizer()
        {
            this.config = SpeechTranslationConfig.FromSubscription(SubscriptionKey, Region);
            this.config.SpeechRecognitionLanguage = FromLanguage;
            this.config.VoiceName = voice;
            ToLanguages.ForEach(l => this.config.AddTargetLanguage(l));

            this.recognizer = new TranslationRecognizer(this.config);

            this.recognizer.Recognizing  += this.OnRecognizingEventHandler;
            this.recognizer.Recognized   += this.OnRecognizedEventHandler;
            this.recognizer.Synthesizing += this.OnSynthesizingEventHandler;
            this.recognizer.Canceled     += this.OnCanceledEventHandler;
        }
예제 #16
0
    void Start()
    {
        if (outputText == null)
        {
            UnityEngine.Debug.LogError("outputText property is null! Assign a UI Text element to it.");
        }
        else if (recoButton == null)
        {
            _message = "recoButton property is null! Assign a UI Button to it.";
            UnityEngine.Debug.LogError(_message);
        }
        else
        {
            // Continue with normal initialization, Text and Button objects are present.
#if PLATFORM_ANDROID
            // Request to use the microphone, cf.
            // https://docs.unity3d.com/Manual/android-RequestingPermissions.html
            message = "Waiting for mic permission";
            if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
            {
                Permission.RequestUserPermission(Permission.Microphone);
            }
#elif PLATFORM_IOS
            if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
            {
                Application.RequestUserAuthorization(UserAuthorization.Microphone);
            }
#else
            _micPermissionGranted = true;
            _message = "Click button to recognize speech";
#endif
            _config = SpeechTranslationConfig.FromSubscription(SubscriptionKey, SubscriptionRegion);
            _config.SpeechRecognitionLanguage = "es-US";
            _config.AddTargetLanguage("en-US");
            _pushStream              = AudioInputStream.CreatePushStream();
            _audioInput              = AudioConfig.FromStreamInput(_pushStream);
            _recognizer              = new TranslationRecognizer(_config, _audioInput);
            _recognizer.Recognizing += RecognizingHandler;
            _recognizer.Recognized  += RecognizedHandler;
            _recognizer.Canceled    += CanceledHandler;

            foreach (var device in Microphone.devices)
            {
                Debug.Log("DeviceName: " + device);
            }
            _audioSource = GameObject.Find("AudioSource").GetComponent <AudioSource>();
        }
    }
예제 #17
0
 public void Dispose()
 {
     if (recognizer != null)
     {
         recognizer.Recognizing    -= OnSpeechRecognizing;
         recognizer.SessionStarted -= OnSessionStarted;
         recognizer.SessionStopped -= OnSessionStopped;
         recognizer.Dispose();
         recognizer = null;
     }
     if (audioConfig != null)
     {
         audioConfig.Dispose();
         audioConfig = null;
     }
 }
예제 #18
0
    private void CreateRecognizer()
    {
        //Microsoft.CognitiveServices.Speech.Internal.carbon_csharp a = new car();

        this.config = SpeechTranslationConfig.FromSubscription(SubscriptionKey, Region);
        this.config.SpeechRecognitionLanguage = FromLanguage;
        this.config.VoiceName = voice;
        ToLanguages.ForEach(l => this.config.AddTargetLanguage(l));

        this.recognizer = new TranslationRecognizer(this.config);

        this.recognizer.Recognizing  += this.OnRecognizingEventHandler;
        this.recognizer.Recognized   += this.OnRecognizedEventHandler;
        this.recognizer.Synthesizing += this.OnSynthesizingEventHandler;
        this.recognizer.Canceled     += this.OnCanceledEventHandler;
    }
예제 #19
0
        void ListenForSpeech()
        {
            try
            {
                translationReco = factory.CreateTranslationRecognizer("en-US", new List <string> {
                    "de"
                }, "de-DE-Hedda");

                translationReco.SynthesisResult += (s, e) =>
                {
                    Log.Info("SpeechSDKDemo", $"Synthesis Result {e.Value.SynthesisStatus}");
                    if (e.Value.SynthesisStatus == SynthesisStatus.Success)
                    {
                        PlayWay(e.Value.GetAudio());
                    }
                };

                translationReco.FinalResult += (s, e) =>
                {
                    Log.Info("SpeechSDKDemo", $"Final result {e.Value.Text}");
                    if (!string.IsNullOrWhiteSpace(e.Value.Text))
                    {
                        RunOnUiThread(() => FullOutput.Text += $"{ e.Value.Translations["de"]} ");
                    }
                };

                translationReco.IntermediateResult += (s, e) =>
                {
                    Log.Info("SpeechSDKDemo", $"Translation intermediate result {e.Value.Text}");
                    if (!string.IsNullOrWhiteSpace(e.Value.Text))
                    {
                        RunOnUiThread(() => IntermediateOutput.Text = $"{e.Value.Text} - { e.Value.Translations["de"]} ");
                    }
                };

                translationReco.RecognitionError += (s, e) =>
                {
                    Log.Info("SpeechSDKDemo", $"Error result {e.Value?.Name()}");
                };

                translationReco.StartContinuousRecognitionAsync();
            }
            catch (Exception ex)
            {
                Log.Error("SpeechSDKDemo", $"unexpected {ex.Message}");
            }
        }
        public async Task CreateTranslationRecognizer()
        {
            if (recognized != null)
            {
                await recognized.StopContinuousRecognitionAsync();

                await recognized.StopKeywordRecognitionAsync();
            }

            recognized                 = new TranslationRecognizer(speechConfiguration);
            recognized.Recognizing    += OnSpeechRecognizing;
            recognized.SessionStarted += OnSessionStarted;
            recognized.SessionStopped += OnSessionStopped;
            recognized.Recognized     += OnRecognized;
            recognized.Synthesizing   += OnSynthesizing;
            recognized.Canceled       += OnCanceled;
        }
예제 #21
0
        private void Init(string from, string to)
        {
            this.toLanguage = to;

            Profile       = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Low);
            Profile.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16);

            byte channels         = 1;
            byte bitsPerSample    = 16;
            uint samplesPerSecond = 16000; // or 8000
            var  audioFormat      = AudioStreamFormat.GetWaveFormatPCM(samplesPerSecond, bitsPerSample, channels);

            // Init Push Stream

            pushStream = AudioInputStream.CreatePushStream(audioFormat);

            if (from == to)
            {
                var config = SpeechConfig.FromSubscription(apiKey, region);
                config.SpeechRecognitionLanguage = from;

                speechRecognizer = new SpeechRecognizer(config, AudioConfig.FromStreamInput(pushStream));

                speechRecognizer.Recognizing += RecognisingSpeechHandler;
                speechRecognizer.Recognized  += RecognisingSpeechHandler;

                speechRecognizer.SessionStarted += (sender, args) => this.RecognisionStarted?.Invoke();
                speechRecognizer.SessionStopped += (sender, args) => this.RecognisionStopped?.Invoke();
            }
            else
            {
                var config = SpeechTranslationConfig.FromSubscription(apiKey, region);
                config.SpeechRecognitionLanguage = from;
                config.AddTargetLanguage(to);

                translationRecognizer = new TranslationRecognizer(config, AudioConfig.FromStreamInput(pushStream));

                translationRecognizer.SessionStarted += (sender, args) => this.RecognisionStarted?.Invoke();
                translationRecognizer.SessionStopped += (sender, args) => this.RecognisionStopped?.Invoke();

                translationRecognizer.Recognizing += RecognisingTranslationHandler;
                translationRecognizer.Recognized  += RecognisingTranslationHandler;
            }
        }
예제 #22
0
    public async void ButtonClick()
    {
        Debug.Log("Onclick fired");
        var translationConfig = SpeechTranslationConfig.FromSubscription(SpeechServiceSubscriptionKey, SpeechServiceRegion);

        translationConfig.SpeechRecognitionLanguage = "en-US";
        translationConfig.AddTargetLanguage("fr");

        using (var recognizer = new TranslationRecognizer(translationConfig))
        {
            Debug.Log("Creating recognizer");
            lock (threadLocker)
            {
                waitingforReco = true;
            }

            var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

            if (result.Reason == ResultReason.TranslatedSpeech)
            {
                recognizedString = result.Text;
                Debug.Log("Text: " + recognizedString);
                foreach (var element in result.Translations)
                {
                    translatedString = element.Value;
                }
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                recognizedString = "NOMATCH: Speech could not be recognized.";
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                recognizedString = $"CANCELED: Reason={cancellation.Reason} ErrorDetails={cancellation.ErrorDetails}";
            }

            lock (threadLocker)
            {
                waitingforReco = false;
            }
        }
    }
예제 #23
0
 public void HandleOnSelectRecognitionMode(RecognitionMode recognitionMode)
 {
     if (recognitionMode == RecognitionMode.Tralation_Recognizer)
     {
         recognizedString = "Say something...";
         translatedString = "";
         BeginTranslating();
     }
     else
     {
         if (translator != null)
         {
             translator.StopContinuousRecognitionAsync();
         }
         translator       = null;
         recognizedString = "";
         translatedString = "";
     }
 }
        /// <summary>
        /// Initializes the factory object with subscription key and region
        /// Initializes the recognizer object with a TranslationRecognizer
        /// Subscribes the recognizer to recognition Event Handlers
        /// If recognition is running, starts a thread which stops the recognition
        /// </summary>
        private void CreateRecognizer()
        {
            string region       = "westus";
            string fromLanguage = "en-US";
            var    toLanguages  = new List <string>()
            {
                "zh", "de"
            };
            var voiceChinese = "zh-CN-Yaoyao";

            this.factory = SpeechFactory.FromSubscription(SubscriptionKey, region);

            this.recognizer = this.factory.CreateTranslationRecognizer(fromLanguage, toLanguages, voiceChinese);

            this.recognizer.IntermediateResultReceived += this.OnPartialResponseReceivedHandler;
            this.recognizer.FinalResultReceived        += this.OnFinalResponse;
            this.recognizer.SynthesisResultReceived    += this.OnSynthesis;
            this.recognizer.RecognitionErrorRaised     += this.OnError;
        }
예제 #25
0
    private async void StartRecognition()
    {
        if (isRecognitionStarted)
        {
            return;
        }
        Debug.Log("start recognition");

        string fromLang;
        string toLang;

        if (modeList.value == 0)
        {
            fromLang = "ja-JP";
            toLang   = "en";
        }
        else
        {
            fromLang = "en-US";
            toLang   = "ja";
        }
        Debug.Log("mode : " + fromLang + " -> " + toLang);

        var config = SpeechTranslationConfig.FromSubscription(apiKeyInputField.text, apiRegionInputField.text);

        config.SpeechRecognitionLanguage = fromLang;
        config.AddTargetLanguage(toLang);

        recognizer                      = new TranslationRecognizer(config);
        recognizer.Canceled            += CanceledHandler;
        recognizer.SessionStarted      += SessionStartedHandler;
        recognizer.SessionStopped      += SessionStoppedHandler;
        recognizer.SpeechStartDetected += SpeechStartDetectedHandler;
        recognizer.SpeechEndDetected   += SpeechEndDetectedHandler;
        recognizer.Recognizing         += RecognizingHandler;
        recognizer.Recognized          += RecognizedHandler;

        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

        isRecognitionStarted      = true;
        isRecognitionStateChanged = true;
    }
        public async Task SpeechRecognitionFromFileAsync(StorageFile file)
        {
            SpeechTranslationConfig config = GetRecognizerConfig();

            if (config == null)
            {
                return;
            }

            ResetState();
            stopRecognitionTaskCompletionSource = new TaskCompletionSource <int>();
            using (var audioInput = AudioConfig.FromWavFileInput(file.Path))
            {
                using (var recognizer = new TranslationRecognizer(config, audioInput))
                {
                    recognizer.Recognizing    += OnTranslateRecognizing;
                    recognizer.Recognized     += OnTranslateRecognized;
                    recognizer.Canceled       += OnTranslateCanceled;
                    recognizer.SessionStarted += (s, e) =>
                    {
                        recognizeCancellationTokenSource = new CancellationTokenSource();
                    };
                    recognizer.SessionStopped += (s, e) =>
                    {
                        if (recognizeCancellationTokenSource != null && recognizeCancellationTokenSource.Token.CanBeCanceled)
                        {
                            recognizeCancellationTokenSource.Cancel();
                        }
                        stopRecognitionTaskCompletionSource.TrySetResult(0);
                    };

                    // Starts continuous recognition.
                    await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                    // Waits for completion.
                    await stopRecognitionTaskCompletionSource.Task.ConfigureAwait(false);

                    // Stops recognition.
                    await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                }
            }
        }
예제 #27
0
    void CreateTranslationRecognizer()
    {
        if (translator == null)
        {
            SpeechTranslationConfig config = SpeechTranslationConfig.FromSubscription(lunarcomController.SpeechServiceAPIKey, lunarcomController.SpeechServiceRegion);
            config.SpeechRecognitionLanguage = fromLanguage;
            config.AddTargetLanguage(toLanguage);

            translator = new TranslationRecognizer(config);

            if (translator != null)
            {
                translator.Recognizing    += HandleTranslatorRecognizing;
                translator.Recognized     += HandleTranslatorRecognized;
                translator.Canceled       += HandleTranslatorCanceled;
                translator.SessionStarted += HandleTranslatorSessionStarted;
                translator.SessionStopped += HandleTranslatorSessionStopped;
            }
        }
    }
예제 #28
0
        public async Task <List <LanguageModel> > TranslateToText(string _fromLanguage, List <string> _toLanguages)
        {
            // Creates an instance of a speech factory with specified
            // subscription key and service region. Replace with your own subscription key
            // and service region (e.g., "westus").
            var factory = SpeechFactory.FromSubscription(this.key, this.azureServer);
            List <LanguageModel> textAndTranslations = new List <LanguageModel>();

            using (TranslationRecognizer trecognizer = factory.CreateTranslationRecognizer(_fromLanguage, _toLanguages))
            {
                var result = await trecognizer.RecognizeAsync();

                if (result.RecognitionStatus != RecognitionStatus.Recognized)
                {
                    if (result.RecognitionStatus == RecognitionStatus.Canceled)
                    {
                        textAndTranslations.Add(new LanguageModel("Error", "There was an error, reason:" + result.RecognitionFailureReason));
                    }
                    else
                    {
                        textAndTranslations.Add(new LanguageModel("Error", "No speech could be recognized.\n"));
                    }
                }
                else
                {
                    if (result.TranslationStatus == TranslationStatus.Success)
                    {
                        textAndTranslations.Add(new LanguageModel(_fromLanguage, result.Text));
                        foreach (var element in result.Translations)
                        {
                            textAndTranslations.Add(new LanguageModel(element.Key, element.Value));
                            Console.WriteLine(element.Key);
                        }
                    }
                }
            }
            return(textAndTranslations);
        }
예제 #29
0
        static async Task Translate(string targetLanguage)
        {
            string translation = "";

            // Translate speech
            string      audioFile = "station.wav";
            SoundPlayer wavPlayer = new SoundPlayer(audioFile);

            // Play the audio synchronously, otherwise the translation will interrupt it
            wavPlayer.PlaySync();
            using AudioConfig audioConfig          = AudioConfig.FromWavFileInput(audioFile);
            using TranslationRecognizer translator = new TranslationRecognizer(translationConfig, audioConfig);

            // Add an inline event handler to handle the Synthesizing event and output the audio to the current output device
            translator.Synthesizing += (_, e) =>
            {
                var audio = e.Result.GetAudio();
                if (audio.Length > 0)
                {
                    // Output to a file using File.WriteAllBytes("YourAudioFile.wav", audio);
                    // Or place the data into a stream and play to the current output device
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(audio))
                    {
                        // Construct the sound player
                        SoundPlayer player = new SoundPlayer(ms);
                        player.Play();
                    }
                }
            };
            Console.WriteLine("Getting speech from file...");
            TranslationRecognitionResult result = await translator.RecognizeOnceAsync();

            Console.WriteLine($"Translating '{result.Text}'");
            translation            = result.Translations[targetLanguage];
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine(translation);
        }
    /// <summary>
    /// Creates a class-level Translation Recognizer for a specific language using Azure credentials
    /// and hooks-up lifecycle & recognition events. Translation can be enabled with one or more target
    /// languages translated simultaneously
    /// </summary>
    void CreateTranslationRecognizer()
    {
        Debug.Log("Creating Translation Recognizer.");
        recognizedString = "Initializing speech recognition with translation, please wait...";

        if (translator == null)
        {
            SpeechTranslationConfig config = SpeechTranslationConfig.FromSubscription(SpeechServiceAPIKey, SpeechServiceRegion);
            config.SpeechRecognitionLanguage = fromLanguage;
            if (Languages1.captionText.text.Length > 0)
            {
                config.AddTargetLanguage(ExtractLanguageCode(Languages1.captionText.text));
            }
            if (Languages2.captionText.text.Length > 0)
            {
                config.AddTargetLanguage(ExtractLanguageCode(Languages2.captionText.text));
            }
            if (Languages3.captionText.text.Length > 0)
            {
                config.AddTargetLanguage(ExtractLanguageCode(Languages3.captionText.text));
            }
            translator = new TranslationRecognizer(config);

            if (translator != null)
            {
                translator.Recognizing         += RecognizingTranslationHandler;
                translator.Recognized          += RecognizedTranslationHandler;
                translator.SpeechStartDetected += SpeechStartDetectedHandler;
                translator.SpeechEndDetected   += SpeechEndDetectedHandler;
                translator.Canceled            += CanceledTranslationHandler;
                translator.SessionStarted      += SessionStartedHandler;
                translator.SessionStopped      += SessionStoppedHandler;
            }
        }
        Debug.Log("CreateTranslationRecognizer exit");
    }