package edu.cmu.pocketsphinx.demo;
import java.util.concurrent.LinkedBlockingQueue;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import edu.cmu.pocketsphinx.Config;
import edu.cmu.pocketsphinx.Decoder;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.pocketsphinx;
/**
* Speech recognition task, which runs in a worker thread.
*
* This class implements speech recognition for this demo application. It takes
* the form of a long-running task which accepts requests to start and stop
* listening, and emits recognition results to a listener.
*
* @author David Huggins-Daines <dhuggins@cs.cmu.edu>
*/
public class RecognizerTask implements Runnable {
/**
* Audio recording task.
*
* This class implements a task which pulls blocks of audio from the system
* audio input and places them on a queue.
*
* @author David Huggins-Daines <dhuggins@cs.cmu.edu>
*/
class AudioTask implements Runnable {
/**
* Queue on which audio blocks are placed.
*/
LinkedBlockingQueue<short[]> q;
AudioRecord rec;
int block_size;
boolean done;
static final int DEFAULT_BLOCK_SIZE = 512;
AudioTask() {
this.init(new LinkedBlockingQueue<short[]>(), DEFAULT_BLOCK_SIZE);
}
AudioTask(LinkedBlockingQueue<short[]> q) {
this.init(q, DEFAULT_BLOCK_SIZE);
}
AudioTask(LinkedBlockingQueue<short[]> q, int block_size) {
this.init(q, block_size);
}
void init(LinkedBlockingQueue<short[]> q, int block_size) {
this.done = false;
this.q = q;
this.block_size = block_size;
this.rec = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 8000,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, 8192);
}
public int getBlockSize() {
return block_size;
}
public void setBlockSize(int block_size) {
this.block_size = block_size;
}
public LinkedBlockingQueue<short[]> getQueue() {
return q;
}
public void stop() {
this.done = true;
}
public void run() {
this.rec.startRecording();
while (!this.done) {
int nshorts = this.readBlock();
if (nshorts <= 0)
break;
}
this.rec.stop();
this.rec.release();
}
int readBlock() {
short[] buf = new short[this.block_size];
int nshorts = this.rec.read(buf, 0, buf.length);
if (nshorts > 0) {
Log.d(getClass().getName(), "Posting " + nshorts
+ " samples to queue");
this.q.add(buf);
}
return nshorts;
}
}
/**
* PocketSphinx native decoder object.
*/
Decoder ps;
/**
* Audio recording task.
*/
AudioTask audio;
/**
* Thread associated with recording task.
*/
Thread audio_thread;
/**
* Queue of audio buffers.
*/
LinkedBlockingQueue<short[]> audioq;
/**
* Listener for recognition results.
*/
RecognitionListener rl;
/**
* Whether to report partial results.
*/
boolean use_partials;
/**
* State of the main loop.
*/
enum State {
IDLE, LISTENING
};
/**
* Events for main loop.
*/
enum Event {
NONE, START, STOP, SHUTDOWN
};
/**
* Current event.
*/
Event mailbox;
public RecognitionListener getRecognitionListener() {
return rl;
}
public void setRecognitionListener(RecognitionListener rl) {
this.rl = rl;
}
public void setUsePartials(boolean use_partials) {
this.use_partials = use_partials;
}
public boolean getUsePartials() {
return this.use_partials;
}
public RecognizerTask() {
// pocketsphinx
// .setLogfile("/sdcard/pocketsphinx.log");
Config c = new Config();
/*
* In 2.2 and above we can use getExternalFilesDir() or whatever it's
* called
*/
c.setString("-hmm", "/sdcard/hmm/tdt_sc_8k");
c.setString("-dict", "/sdcard/lm/2910.dic");
c.setString("-lm", "/sdcard/lm/2910.lm");
c.setFloat("-samprate", 8000.0);
c.setInt("-maxhmmpf", 2000);
c.setInt("-maxwpf", 10);
c.setInt("-pl_window", 2);
c.setBoolean("-backtrace", true);
c.setBoolean("-bestpath", false);
this.ps = new Decoder(c);
this.audio = null;
this.audioq = new LinkedBlockingQueue<short[]>();
this.use_partials = false;
this.mailbox = Event.NONE;
}
public void run() {
/* Main loop for this thread. */
boolean done = false;
/* State of the main loop. */
State state = State.IDLE;
/* Previous partial hypothesis. */
String partial_hyp = null;
while (!done) {
/* Read the mail. */
Event todo = Event.NONE;
synchronized (this.mailbox) {
todo = this.mailbox;
/* If we're idle then wait for something to happen. */
if (state == State.IDLE && todo == Event.NONE) {
try {
Log.d(getClass().getName(), "waiting");
this.mailbox.wait();
todo = this.mailbox;
Log.d(getClass().getName(), "got" + todo);
} catch (InterruptedException e) {
/* Quit main loop. */
Log.e(getClass().getName(),
"Interrupted waiting for mailbox, shutting down");
todo = Event.SHUTDOWN;
}
}
/* Reset the mailbox before releasing, to avoid race condition. */
this.mailbox = Event.NONE;
}
/* Do whatever the mail says to do. */
switch (todo) {
case NONE:
if (state == State.IDLE)
Log.e(getClass().getName(),
"Received NONE in mailbox when IDLE, threading error?");
break;
case START:
if (state == State.IDLE) {
Log.d(getClass().getName(), "START");
this.audio = new AudioTask(this.audioq, 1024);
this.audio_thread = new Thread(this.audio);
this.ps.startUtt();
this.audio_thread.start();
state = State.LISTENING;
} else
Log.e(getClass().getName(),
"Received START in mailbox when LISTENING");
break;
case STOP:
if (state == State.IDLE)
Log.e(getClass().getName(),
"Received STOP in mailbox when IDLE");
else {
Log.d(getClass().getName(), "STOP");
assert this.audio != null;
this.audio.stop();
try {
this.audio_thread.join();
} catch (InterruptedException e) {
Log.e(getClass().getName(),
"Interrupted waiting for audio thread, shutting down");
done = true;
}
/* Drain the audio queue. */
short[] buf;
while ((buf = this.audioq.poll()) != null) {
Log.d(getClass().getName(), "Reading " + buf.length
+ " samples from queue");
this.ps.processRaw(buf, buf.length, false, false);
}
this.ps.endUtt();
this.audio = null;
this.audio_thread = null;
Hypothesis hyp = this.ps.getHyp();
if (this.rl != null) {
if (hyp == null) {
Log.d(getClass().getName(), "Recognition failure");
this.rl.onError(-1);
} else {
Bundle b = new Bundle();
Log.e(getClass().getName(),
"zs log Final hypothesis: "
+ hyp.getHypstr());
Log.e(getClass().getName(), ps.getUttid());
b.putString("hyp", hyp.getHypstr());
this.rl.onResults(b);
}
}
state = State.IDLE;
}
break;
case SHUTDOWN:
Log.d(getClass().getName(), "SHUTDOWN");
if (this.audio != null) {
this.audio.stop();
assert this.audio_thread != null;
try {
this.audio_thread.join();
} catch (InterruptedException e) {
/* We don't care! */
}
}
this.ps.endUtt();
this.audio = null;
this.audio_thread = null;
state = State.IDLE;
done = true;
break;
}
/*
* Do whatever's appropriate for the current state. Actually this
* just means processing audio if possible.
*/
if (state == State.LISTENING) {
assert this.audio != null;
try {
short[] buf = this.audioq.take();
Log.d(getClass().getName(), "Reading " + buf.length
+ " samples from queue");
this.ps.processRaw(buf, buf.length, false, false);
Hypothesis hyp = this.ps.getHyp();
if (hyp != null) {
String hypstr = hyp.getHypstr();
if (hypstr != partial_hyp) {
Log.d(getClass().getName(),
"Hypothesis: " + hyp.getHypstr()
没有合适的资源?快使用搜索试试~ 我知道了~
PocketSphinxDemo
共226个文件
o:71个
d:71个
class:28个
5星 · 超过95%的资源 需积分: 49 459 下载量 145 浏览量
2014-10-13
21:50:22
上传
评论 10
收藏 3.23MB ZIP 举报
温馨提示
亲测可用的PocketSphinxDemo,最后结合博客一起!
资源推荐
资源详情
资源评论
收起资源包目录
PocketSphinxDemo (226个子文件)
libpocketsphinx.a 1.1MB
libsphinxutil.a 494KB
libsphinxlm.a 445KB
libsphinxfe.a 160KB
libsphinxfeat.a 116KB
resources.ap_ 12KB
PocketSphinxDemo.apk 589KB
.bzrignore 4KB
pocketsphinx_wrap.c 46KB
pocketsphinx_wrap.c 46KB
jarlist.cache 119B
RecognizerTask.class 7KB
PocketSphinxDemo.class 4KB
PocketSphinxIntent.class 4KB
RecognizerTask$AudioTask.class 3KB
Config.class 3KB
Decoder.class 3KB
pocketsphinxJNI.class 2KB
Hypothesis.class 2KB
PocketSphinxDemo$2.class 2KB
RecognizerTask$Event.class 1KB
RecognizerTask$State.class 1KB
SegmentIterator.class 1023B
PocketSphinxIntent$1.class 1018B
PocketSphinxDemo$1.class 1004B
Lattice.class 975B
PocketSphinxIntent$2.class 892B
PocketSphinxDemo$3.class 878B
R.class 690B
R$drawable.class 674B
R$color.class 551B
R$string.class 488B
pocketsphinx.class 485B
R$id.class 480B
R$style.class 425B
R$layout.class 415B
R$attr.class 361B
BuildConfig.class 359B
RecognitionListener.class 242B
.classpath 492B
pocketsphinx.o.d 14KB
fe_sigproc.o.d 14KB
ps_lattice.o.d 13KB
acmod.o.d 13KB
s2_semi_mgau.o.d 13KB
ptm_mgau.o.d 13KB
fsg_search.o.d 12KB
mmio.o.d 12KB
ms_gauden.o.d 12KB
ms_senone.o.d 12KB
ngram_search.o.d 12KB
fsg_history.o.d 12KB
ms_mgau.o.d 12KB
ngram_search_fwdtree.o.d 12KB
ngram_search_fwdflat.o.d 12KB
phone_loop_search.o.d 12KB
pio.o.d 11KB
profile.o.d 11KB
cmd_ln.o.d 11KB
ngram_model_arpa.o.d 11KB
ngram_model_dmp.o.d 11KB
pocketsphinx_wrap.o.d 10KB
feat.o.d 10KB
ps_mllr.o.d 10KB
fsg_lextree.o.d 10KB
fe_interface.o.d 10KB
sbthread.o.d 10KB
dict2pid.o.d 9KB
info.o.d 9KB
fsg_model.o.d 9KB
tmat.o.d 9KB
ngram_model.o.d 9KB
dict.o.d 9KB
lm3g_model.o.d 9KB
hmm.o.d 9KB
err.o.d 9KB
lda.o.d 9KB
logmath.o.d 8KB
cmn.o.d 8KB
fe_warp.o.d 8KB
huff_code.o.d 8KB
fe_warp_piecewise_linear.o.d 8KB
fe_warp_inverse_linear.o.d 8KB
fe_warp_affine.o.d 8KB
cmn_prior.o.d 8KB
agc.o.d 8KB
vector.o.d 8KB
jsgf.o.d 8KB
ngram_model_set.o.d 8KB
jsgf_parser.o.d 8KB
jsgf_scanner.o.d 7KB
dtoa.o.d 7KB
bin_mdef.o.d 7KB
hash_table.o.d 7KB
mdef.o.d 7KB
matrix.o.d 7KB
bio.o.d 7KB
blkarray_list.o.d 6KB
heap.o.d 6KB
listelem_alloc.o.d 6KB
共 226 条
- 1
- 2
- 3
Google_acmer
- 粉丝: 67
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页