2 %include <arrays_java.i>
4 #include <pocketsphinx.h>
5 #include <sphinxbase/err.h>
7 /* Typedefs to make Java-esque class names. */
8 typedef struct cmd_ln_s Config;
9 typedef struct ps_seg_s SegmentIterator;
10 typedef struct ps_lattice_s Lattice;
11 typedef struct ps_decoder_s Decoder;
16 /* Auxiliary objects used to return multiple values. */
17 typedef struct hyp_s {
24 /* Special typemap for arrays of audio. */
25 %typemap(in) (short const *SDATA, size_t NSAMP) {
26 $1 = (short const *) JCALL2(GetShortArrayElements, jenv, $input, NULL);
27 $2 = JCALL1(GetArrayLength, jenv, $input);
29 %typemap(freearg) (short const *SDATA, size_t NSAMP) {
30 JCALL3(ReleaseShortArrayElements, jenv, $input, $1, 0);
32 %typemap(jni) (short const *SDATA, size_t NSAMP) "jshortArray"
33 %typemap(jtype) (short const *SDATA, size_t NSAMP) "short[]"
34 %typemap(jstype) (short const *SDATA, size_t NSAMP) "short[]"
35 %typemap(javain) (short const *SDATA, size_t NSAMP) "$javainput"
37 /* Auxiliary types for returning multiple values. */
38 typedef struct hyp_s {
44 /* These are opaque types but we have to "define" them for SWIG. */
45 typedef struct cmd_ln_s {
47 typedef struct ps_seg_s {
49 typedef struct ps_lattice_s {
51 typedef struct ps_decoder_s {
56 Hypothesis(char const *hypstr, char const *uttid, int best_score) {
57 Hypothesis *h = ckd_calloc(1, sizeof(*h));
59 h->hypstr = ckd_salloc(hypstr);
61 h->uttid = ckd_salloc(uttid);
62 h->best_score = best_score;
67 ckd_free($self->hypstr);
68 ckd_free($self->uttid);
75 Config *c = cmd_ln_init(NULL, ps_args(), FALSE, NULL);
78 Config(char const *file) {
79 Config *c = cmd_ln_parse_file_r(NULL, ps_args(), file, FALSE);
85 void setBoolean(char const *key, bool val) {
86 cmd_ln_set_boolean_r($self, key, val);
88 void setInt(char const *key, int val) {
89 cmd_ln_set_int_r($self, key, val);
91 void setFloat(char const *key, double val) {
92 cmd_ln_set_float_r($self, key, val);
94 void setString(char const *key, char const *val) {
95 cmd_ln_set_str_r($self, key, val);
97 bool exists(char const *key) {
98 return cmd_ln_exists_r($self, key);
100 bool getBoolean(char const *key) {
101 return cmd_ln_boolean_r($self, key);
103 int getInt(char const *key) {
104 return cmd_ln_int_r($self, key);
106 double getFloat(char const *key) {
107 return cmd_ln_float_r($self, key);
109 char const *getString(char const *key) {
110 return cmd_ln_str_r($self, key);
114 %extend SegmentIterator {
128 Decoder *d = ps_init(cmd_ln_init(NULL, ps_args(), FALSE, NULL));
132 Decoder *d = ps_init(c);
135 Config *getConfig() {
136 return cmd_ln_retain(ps_get_config($self));
139 return ps_start_utt($self, NULL);
141 int startUtt(char const *uttid) {
142 return ps_start_utt($self, uttid);
144 char const *getUttid() {
145 return ps_get_uttid($self);
148 return ps_end_utt($self);
150 int processRaw(short const *SDATA, size_t NSAMP, bool no_search, bool full_utt) {
151 return ps_process_raw($self, SDATA, NSAMP, no_search, full_utt);
153 int processRaw(short const shorts[], size_t nshorts, bool no_search, bool full_utt) {
154 return ps_process_raw($self, shorts, nshorts, no_search, full_utt);
156 Hypothesis *getHyp() {
157 char const *hyp, *uttid;
159 hyp = ps_get_hyp($self, &best_score, &uttid);
160 return new_Hypothesis(hyp, uttid, best_score);
168 /* Static method to set the logging file. */
169 void setLogfile(char const *path) {
170 err_set_logfile(path);