Initial import to Tizen
[profile/ivi/pocketsphinx.git] / swig / pocketsphinx.i
1 %module pocketsphinx
2 %include <arrays_java.i>
3 %{
4 #include <pocketsphinx.h>
5 #include <sphinxbase/err.h>
6
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;
12 typedef int bool;
13 #define false 0
14 #define true 1
15
16 /* Auxiliary objects used to return multiple values. */
17 typedef struct hyp_s {
18         char *hypstr;
19         char *uttid;
20         int best_score;
21 } Hypothesis;
22 %}
23
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);
28 };
29 %typemap(freearg) (short const *SDATA, size_t NSAMP) {
30         JCALL3(ReleaseShortArrayElements, jenv, $input, $1, 0);
31 };
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"
36
37 /* Auxiliary types for returning multiple values. */
38 typedef struct hyp_s {
39         char *hypstr;
40         char *uttid;
41         int best_score;
42 } Hypothesis;
43
44 /* These are opaque types but we have to "define" them for SWIG. */
45 typedef struct cmd_ln_s {
46 } Config;
47 typedef struct ps_seg_s {
48 } SegmentIterator;
49 typedef struct ps_lattice_s {
50 } Lattice;
51 typedef struct ps_decoder_s {
52 } Decoder;
53
54
55 %extend Hypothesis {
56         Hypothesis(char const *hypstr, char const *uttid, int best_score) {
57                 Hypothesis *h = ckd_calloc(1, sizeof(*h));
58                 if (hypstr)
59                         h->hypstr = ckd_salloc(hypstr);
60                 if (uttid)
61                         h->uttid = ckd_salloc(uttid);
62                 h->best_score = best_score;
63                 return h;
64                 
65         }
66         ~Hypothesis() {
67                 ckd_free($self->hypstr);
68                 ckd_free($self->uttid);
69                 ckd_free($self);
70         }
71 }
72
73 %extend Config {
74         Config() {
75                 Config *c = cmd_ln_init(NULL, ps_args(), FALSE, NULL);
76                 return c;
77         }
78         Config(char const *file) {
79                 Config *c = cmd_ln_parse_file_r(NULL, ps_args(), file, FALSE);
80                 return c;
81         }
82         ~Config() {
83                 cmd_ln_free_r($self);
84         }
85         void setBoolean(char const *key, bool val) {
86                 cmd_ln_set_boolean_r($self, key, val);
87         }
88         void setInt(char const *key, int val) {
89                 cmd_ln_set_int_r($self, key, val);
90         }
91         void setFloat(char const *key, double val) {
92                 cmd_ln_set_float_r($self, key, val);
93         }
94         void setString(char const *key, char const *val) {
95                 cmd_ln_set_str_r($self, key, val);
96         }
97         bool exists(char const *key) {
98                 return cmd_ln_exists_r($self, key);
99         }
100         bool getBoolean(char const *key) {
101                 return cmd_ln_boolean_r($self, key);
102         }
103         int getInt(char const *key) {
104                 return cmd_ln_int_r($self, key);
105         }
106         double getFloat(char const *key) {
107                 return cmd_ln_float_r($self, key);
108         }
109         char const *getString(char const *key) {
110                 return cmd_ln_str_r($self, key);
111         }
112 };
113
114 %extend SegmentIterator {
115         SegmentIterator() {
116                 return NULL;
117         }
118 }
119
120 %extend Lattice {
121         Lattice() {
122                 return NULL;
123         }
124 };
125
126 %extend Decoder {
127         Decoder() {
128                 Decoder *d = ps_init(cmd_ln_init(NULL, ps_args(), FALSE, NULL));
129                 return d;
130         }
131         Decoder(Config *c) {
132                 Decoder *d = ps_init(c);
133                 return d;
134         }
135         Config *getConfig() {
136                 return cmd_ln_retain(ps_get_config($self));
137         }
138         int startUtt() {
139                 return ps_start_utt($self, NULL);
140         }
141         int startUtt(char const *uttid) {
142                 return ps_start_utt($self, uttid);
143         }
144         char const *getUttid() {
145                 return ps_get_uttid($self);
146         }
147         int endUtt() {
148                 return ps_end_utt($self);
149         }
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);
152         }
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);
155         }
156         Hypothesis *getHyp() {
157                 char const *hyp, *uttid;
158                 int32 best_score;
159                 hyp = ps_get_hyp($self, &best_score, &uttid);
160                 return new_Hypothesis(hyp, uttid, best_score);
161         }
162         ~Decoder() {
163                 ps_free($self);
164         }
165 };
166
167 %inline {
168         /* Static method to set the logging file. */
169         void setLogfile(char const *path) {
170                 err_set_logfile(path);
171         }
172 };
173