Skip to content

Commit

Permalink
Merge pull request #97 from strem-app/bug/96-voice-recognition-init-i…
Browse files Browse the repository at this point in the history
…ssue

This should hopefully provide more logs and stop it blowing up system
  • Loading branch information
grofit authored Apr 3, 2024
2 parents 8ab8fc9 + 6427061 commit 949cfe3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
21 changes: 19 additions & 2 deletions src/Strem.Flows/Plugins/FlowsPluginStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,35 @@ public async Task StartEventExecutor()
}

public void StartRegistries()
{
SetupTaskDescriptors();
SetupTriggerDescriptors();
}

public void SetupTaskDescriptors()
{
try
{
var taskDescriptors = Services.GetServices<TaskDescriptor>();
TaskRegistry?.AddMany(taskDescriptors);

}
catch (Exception e)
{
Logger.Error($"Unable to initialize task descriptors, error listed: {e.Message}");
throw;
}
}

public void SetupTriggerDescriptors()
{
try
{
var triggerDescriptors = Services.GetServices<TriggerDescriptor>();
TriggerRegistry?.AddMany(triggerDescriptors);
}
catch (Exception e)
{
Logger.Error(e.Message);
Logger.Error($"Unable to initialize trigger descriptors, error listed: {e.Message}");
throw;
}
}
Expand Down
48 changes: 33 additions & 15 deletions src/Strem.Platforms.Windows/Services/Audio/VoiceRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class VoiceRecognition : IVoiceRecognition
public SpeechRecognitionEngine SpeechRecognitionEngine { get; private set; }
public IObservable<SpeechRecognizedEventArgs> OnRecognizedTerm { get; private set; }
public bool IsActive { get; private set; }
public bool IsInitialized { get; private set; }

public List<string> KnownPhrases { get; } = new();
private bool _isAwaitingUpdate;
Expand All @@ -26,22 +27,33 @@ public class VoiceRecognition : IVoiceRecognition
public VoiceRecognition(ILogger<IVoiceRecognition> logger)
{
Logger = logger;

// TODO: Not sure how many cultures are supported by this
SpeechRecognitionEngine = new SpeechRecognitionEngine(CultureInfo.CurrentCulture);

SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

var noiseSink = new DictationGrammar("grammar:dictation#pronunciation") { Name = NoiseSink };
SpeechRecognitionEngine.LoadGrammar(noiseSink);

SpeechRecognitionEngine.RecognizerUpdateReached += OnUpdateGrammer;
try
{
// TODO: Not sure how many cultures are supported by this
SpeechRecognitionEngine = new SpeechRecognitionEngine(CultureInfo.CurrentCulture);
SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

var noiseSink = new DictationGrammar("grammar:dictation#pronunciation") { Name = NoiseSink };
SpeechRecognitionEngine.LoadGrammar(noiseSink);

SpeechRecognitionEngine.RecognizerUpdateReached += OnUpdateGrammer;

OnRecognizedTerm = Observable.FromEventPattern<SpeechRecognizedEventArgs>(
x => SpeechRecognitionEngine.SpeechRecognized += x,
x => SpeechRecognitionEngine.SpeechRecognized -= x)
.Select(x => x.EventArgs)
.Where(ConfirmNotNoiseSink);
OnRecognizedTerm = Observable.FromEventPattern<SpeechRecognizedEventArgs>(
x => SpeechRecognitionEngine.SpeechRecognized += x,
x => SpeechRecognitionEngine.SpeechRecognized -= x)
.Select(x => x.EventArgs)
.Where(ConfirmNotNoiseSink);

IsInitialized = true;
Logger.Information($"Speech Recognition has initialized for culture [{CultureInfo.CurrentCulture.Name}], with Recognizer [{SpeechRecognitionEngine.RecognizerInfo.Name} | {SpeechRecognitionEngine.RecognizerInfo.Description}]");
}
catch (Exception ex)
{
IsInitialized = false;
Logger.Error($"Unable to initialize Voice Recognition for Culture [{CultureInfo.CurrentCulture.Name}], native error was [{ex.Message}]");
Logger.Information("This may be due to you not having speech recognition runtimes installed, go to https://www.microsoft.com/en-us/download/details.aspx?id=27224");
}
}

private bool ConfirmNotNoiseSink(SpeechRecognizedEventArgs arg)
Expand Down Expand Up @@ -92,6 +104,12 @@ public bool MatchesTerm(string text, string term, bool exact)

public IObservable<TermRecognized> ListenForTerm(string term, bool exact)
{
if (!IsInitialized)
{
Logger.Warning($"Tried to listen for term [{term}], but the speech recognition is not initialized");
return Observable.Empty<TermRecognized>();
}

if (!KnownPhrases.Contains(term, StringComparer.OrdinalIgnoreCase))
{ UpdateTerms(term); }

Expand All @@ -101,5 +119,5 @@ public IObservable<TermRecognized> ListenForTerm(string term, bool exact)
}

public void Dispose()
{ SpeechRecognitionEngine.Dispose(); }
{ SpeechRecognitionEngine?.Dispose(); }
}

0 comments on commit 949cfe3

Please sign in to comment.