Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-osin/92772fc299d4b5e7c2870acfa757de76 to your computer and use it in GitHub Desktop.
Save dmitry-osin/92772fc299d4b5e7c2870acfa757de76 to your computer and use it in GitHub Desktop.
public partial class PhraseSpotterPageViewModel : ViewModels.ViewModelBase.ViewModelBase
{
/// <summary>
/// Method initialize async SpeechKit for PhraseSpotterCase
/// </summary>
private void InitSk()
{
SpeechKitInitializer.Configure(SKStartupSettings.APIKey);
SpeechKitInitializer.InitializeAsync();
IsStartEnabled = true;
}
/// <summary>
/// Init SpeechKit commands
/// </summary>
private void InitSkCommands()
{
StartCommand = new RelayCommand(() =>
{
_phraseSpotter.Start();
});
StopCommand = new RelayCommand(() =>
{
_phraseSpotter.StopSync();
if (_phraseSpotter != null)
{
_phraseSpotter.ErrorHandler += PhraseSpotterOnErrorHandler;
_phraseSpotter.SpottedHandler += PhraseSpotterOnSpottedHandler;
_phraseSpotter.StartedHandler += PhraseSpotterOnStartedHandler;
_phraseSpotter.StoppedHandler += PhraseSpotterOnStoppedHandler;
}
});
StopCommandAsync = new RelayCommand(async () =>
{
await Task.Run(() =>
{
_phraseSpotter.Stop();
});
_phraseSpotter.ErrorHandler += PhraseSpotterOnErrorHandler;
_phraseSpotter.SpottedHandler += PhraseSpotterOnSpottedHandler;
_phraseSpotter.StartedHandler += PhraseSpotterOnStartedHandler;
_phraseSpotter.StoppedHandler += PhraseSpotterOnStoppedHandler;
});
LoadCommand = new RelayCommand(() =>
{
_phraseSpotter.SetModel(_phraseSpotterModel);
_phraseSpotter.ErrorHandler += PhraseSpotterOnErrorHandler;
_phraseSpotter.SpottedHandler += PhraseSpotterOnSpottedHandler;
_phraseSpotter.StartedHandler += PhraseSpotterOnStartedHandler;
_phraseSpotter.StoppedHandler += PhraseSpotterOnStoppedHandler;
SpotterModelError = _phraseSpotterModel.Load();
InvokeOnUiThread(() =>
{
Status = "Model loaded";
});
});
UnloadCommand = new RelayCommand(() =>
{
SpotterModelError = _phraseSpotterModel.Unload();
InvokeOnUiThread(() =>
{
Status = "Model unloaded";
});
});
DestroyCommand = new RelayCommand(() =>
{
_phraseSpotterModel = null;
InvokeOnUiThread(() =>
{
Status = "Model destroyed";
});
});
CreateCommand = new RelayCommand(() =>
{
_phraseSpotter = new PhraseSpotter();
_phraseSpotterModel = new PhraseSpotterModel(SelectedModelPath);
InvokeOnUiThread(() =>
{
Status = "Model created";
});
});
}
#region SpeechKit Event Handlers
private void PhraseSpotterOnStoppedHandler()
{
InvokeOnUiThread(() =>
{
Status = "Phrase spotter Stopped";
});
}
private void PhraseSpotterOnStartedHandler()
{
InvokeOnUiThread(() =>
{
Status = "Phrase spotter started";
});
}
private void PhraseSpotterOnSpottedHandler(string phase, int phaseId)
{
InvokeOnUiThread(() =>
{
LastPhrase = phase;
var data2 = phaseId;
});
}
private void PhraseSpotterOnErrorHandler(Error error)
{
InvokeOnUiThread(() =>
{
LastError = error.Text;
});
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment