Skip to content

Instantly share code, notes, and snippets.

@SachinGanesh
Created December 10, 2018 04:29
Show Gist options
  • Save SachinGanesh/0b2f0cd02cc7050c6d5976c12ebbc4ad to your computer and use it in GitHub Desktop.
Save SachinGanesh/0b2f0cd02cc7050c6d5976c12ebbc4ad to your computer and use it in GitHub Desktop.
Example Program for SynSpeech & Unity3D (https://github.com/SynHub/syn-speech). Audio file is loaded from "StreamingAssets/Syn" directory
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Syn.Log;
using Syn.Speech.Api;
using Syn.Speech.Logging;
using UnityEngine;
using UnityEngine.UI;
public class SynSpeech : MonoBehaviour {
private static Configuration _configuration;
private static StreamSpeechRecognizer _speechRecognizer;
public Text lable;
// Start is called before the first frame update
void Start () {
Syn.Speech.Logging.Logger.LogReceived += LoggerLogReceived;
var modelsDirectory = Path.Combine (Application.streamingAssetsPath, "Syn", "Models");
ShowLog("Path: " + modelsDirectory);
_configuration = new Configuration {
AcousticModelPath = modelsDirectory,
DictionaryPath = Path.Combine (modelsDirectory, "cmudict-en-us.dict"),
LanguageModelPath = Path.Combine (modelsDirectory, "en-us.lm.dmp"),
// UseGrammar = true,
// GrammarPath = modelsDirectory,
// GrammarName = "numbers"
};
_speechRecognizer = new StreamSpeechRecognizer (_configuration);
AudioRecorder.Instance.OnMicRecordingComplete += OnMicRecordingComplete;
}
private void LoggerLogReceived(object sender, Syn.Speech.Logging.LogReceivedEventArgs e)
{
//ShowLog(e.Message);
}
public void StartRecognition () {
ShowLog("Speech Recognition Started");
FileStream stream = new FileStream (Path.Combine (Application.persistentDataPath, "Syn", "recording.wav"), FileMode.Open,FileAccess.ReadWrite);
_speechRecognizer.StartRecognition(stream);
var result = _speechRecognizer.GetResult ();
_speechRecognizer.StopRecognition ();
if (result != null) {
ShowLog(result.GetHypothesis ());
}
else {
ShowLog("result is null");
}
stream.Close();
stream.Dispose();
}
private void ShowLog(string message){
Debug.Log ("Speech Recognized: " + message);
lable.text = message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment