コード例 #1
0
        /// <summary>
        /// Sets the recognizer's config. For internal usage only.
        /// </summary>
        /// <param name="config"></param>
        /// <returns>
        /// False: the given config is not compatible.
        /// </returns>
        internal bool SetConfig(SpeechRecognitionConfig config, SpeechRecognitionAppSettings settings)
        {
            this.Settings    = settings;
            this.Config      = config;
            this.AudioConfig = GetAudioConfig();

            return(this.AudioConfig != null);
        }
コード例 #2
0
ファイル: Soundbox.cs プロジェクト: FWink/Soundbox
        /// <summary>
        /// Called on application start to create an instance of <see cref="ISpeechRecognitionService"/> if enabled in the current configuration.
        /// Then starts the speech recognition via <see cref="ISpeechRecognitionService.Start(SpeechRecognitionOptions)"/>
        /// </summary>
        /// <param name="provider"></param>
        protected void SpeechRecognition_Setup(ISpeechRecognitionServiceProvider provider)
        {
            if (provider == null)
            {
                //nothing to do, not installed
                return;
            }

            if (AppSettings.SpeechRecognition?.AudioDevice == null || AppSettings?.SpeechRecognition?.Languages == null || AppSettings.SpeechRecognition.Languages.Count == 0)
            {
                Logger.LogInformation("No Soundbox.SpeechRecognition config");
                return;
            }

            var config = new SpeechRecognitionConfig()
            {
                AudioSource     = AppSettings.SpeechRecognition.AudioDevice,
                VolumeThreshold = AppSettings.SpeechRecognition.VolumeThreshold
            };

            var recognizer = provider.GetSpeechRecognizer(config);

            if (recognizer == null)
            {
                //no speech recognizer is installed for the current config
                return;
            }

            recognizer.Recognized += (sender, e) =>
            {
                //try and match the spoken words
                var match = e.Match(NodesCache.Values.Where(node => node is Sound).Cast <Sound>(), speechRecognitionState);
                speechRecognitionState = match.State;
                if (match.Success)
                {
                    Play(new User(), new SoundPlaybackRequest()
                    {
                        Sounds = new List <SoundPlayback>()
                        {
                            new SoundPlayback()
                            {
                                Sound = match.Recognizable as Sound
                            }
                        }
                    });
                }
            };

            var options = SpeechRecognition_GetOptions();

            speechRecognizer = recognizer;
            recognizer.Start(options);
        }
コード例 #3
0
        public ISpeechRecognitionService GetSpeechRecognizer(SpeechRecognitionConfig config)
        {
            //check on azure settings and credentials
            var azureSettings = AppSettings?.Providers?.Azure;

            if (azureSettings == null || azureSettings.Credentials == null || azureSettings.Credentials.Count == 0)
            {
                //not set up
                return(null);
            }
            foreach (var credentials in azureSettings.Credentials)
            {
                if (credentials == null || string.IsNullOrWhiteSpace(credentials.Region) || string.IsNullOrWhiteSpace(credentials.SubscriptionKey))
                {
                    Logger.LogWarning("No or invalid Azure Speech-to-text credentials");
                    return(null);
                }
            }

            var service = ServiceProvider.GetService(typeof(AzureSpeechRecognitionService)) as AzureSpeechRecognitionService;

            if (service == null)
            {
                return(null);
            }

            try
            {
                if (service.SetConfig(config, AppSettings))
                {
                    return(service);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Could not initialize AzureSpeechRecognitionService");
                service.Dispose();
            }

            return(null);
        }
コード例 #4
0
 public SpeechRecognitionRequest(AudioClip audioClip, string languageCode, bool useEnhancedModel)
 {
     config = new SpeechRecognitionConfig(audioClip, languageCode, useEnhancedModel);
     audio  = new SpeechRecognitionAudio(AudioConverter.AudioClipToBase64(audioClip));
 }