Skip to content

Instantly share code, notes, and snippets.

@neon-izm
Created April 3, 2014 16:11
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 neon-izm/9957478 to your computer and use it in GitHub Desktop.
Save neon-izm/9957478 to your computer and use it in GitHub Desktop.
VoiceThread (Senz3D+Unity Voice Recognition minimal sample script)
using UnityEngine;
using System.Collections;
using System.Threading;
/* スレッドを分けて音声認識をする*/
public class VoiceThread : MonoBehaviour {
private System.Object cs=new System.Object();
private System.Threading.Thread thread=null;
private PXCUPipeline pp=null;
private volatile bool stop=false;
private volatile string line;
void Start () {
pp=new PXCUPipeline();
pp.SetVoiceLanguage(PXCMVoiceRecognition.ProfileInfo.Language.LANGUAGE_JP_JAPANESE);
if (!pp.Init(PXCUPipeline.Mode.VOICE_RECOGNITION)) {
print("Failed to initialize PXCUPipeline for voice recognition");
return;
}
/* set mic volume parametor */
/* 音量パラメータのセット */
float[] volume=new float[1]{0.2f};
pp.SetDeviceProperty(PXCMCapture.Device.Property.PROPERTY_AUDIO_MIX_LEVEL,volume);
Debug.Log ("start voice recognition");
stop=false;
line=null;
thread=new Thread(ThreadRoutine);
thread.Start();
}
void OnDisable() {
if (thread!=null) {
stop=true;
Thread.Sleep(5);
thread.Join();
thread=null;
}
if (pp!=null) {
pp.Dispose();
pp=null;
}
}
void Update () {
lock (cs) {
if (line!=null) {
Debug.Log(line);
//TODO:
//you should implement here your own code
//ここに任意の処理を追加
line=null;
}
}
}
private void ThreadRoutine() {
while (stop==false) {
pp.AcquireFrame(false);
PXCMVoiceRecognition.Recognition rdata;
if (pp.QueryVoiceRecognized(out rdata)) {
lock (cs) {
line="voice (label="+rdata.label+", text="+rdata.dictation+")";
}
}
pp.ReleaseFrame();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment