public Soundbox( IServiceProvider serviceProvider, IHubContext <SoundboxHub, ISoundboxClient> hubContext, ISoundboxConfigProvider config, IDatabaseProvider database, ISpeechRecognitionServiceProvider speechRecognitionServiceProvider, ILogger <Soundbox> logger, IOptions <SoundboxAppSettings> appSettings) { this.Logger = logger; logger.LogInformation("Soundbox is starting"); this.AppSettings = appSettings.Value ?? new SoundboxAppSettings(); this.ServiceProvider = serviceProvider; this.HubContext = hubContext; this.BaseDirectory = config.GetRootDirectory(); this.SoundsRootDirectory = this.BaseDirectory + "sounds/"; Directory.CreateDirectory(this.SoundsRootDirectory); this.Database = database; //initialize our file tree var taskSoundsRoot = LoadRoot(); taskSoundsRoot.Wait(); this.SoundsRoot = taskSoundsRoot.Result; BuildNodeCache(this.SoundsRoot); SpeechRecognition_Setup(speechRecognitionServiceProvider); }
/// <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); }