1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* */
34 /* Author: Richard Caley <rjc@cstr.ed.ac.uk> */
35 /* ------------------------------------------------------------------- */
36 /* Interface between java and C++ for EST_Utterance. */
37 /* */
38 /*************************************************************************/
39
40
41 #include <stdio.h>
42 #include "jni_Utterance.h"
43 #include "ling_class/EST_Utterance.h"
44
45 static jobject utterance_class;
46 static jfieldID handle_field;
47
abs(short s)48 static inline short abs(short s) { return s>0?s:-s; }
49
50 JNIEXPORT jboolean JNICALL
Java_cstr_est_Utterance_initialise_1cpp(JNIEnv * env,jclass myclass)51 Java_cstr_est_Utterance_initialise_1cpp (JNIEnv *env, jclass myclass)
52 {
53 utterance_class = env->NewGlobalRef(myclass);
54 handle_field = env->GetFieldID(myclass, "cpp_handle", "J");
55
56 if (!handle_field)
57 {
58 printf("can't find handle!\n");
59 return 0;
60 }
61
62 return 1;
63 }
64
65 JNIEXPORT jboolean JNICALL
Java_cstr_est_Utterance_finalise_1cpp(JNIEnv * env,jclass myclass)66 Java_cstr_est_Utterance_finalise_1cpp (JNIEnv *env, jclass myclass)
67 {
68 (void)env;
69 (void)myclass;
70 return 1;
71 }
72
73 JNIEXPORT jboolean JNICALL
Java_cstr_est_Utterance_create_1cpp_1utterance(JNIEnv * env,jobject self)74 Java_cstr_est_Utterance_create_1cpp_1utterance(JNIEnv *env, jobject self)
75 {
76 EST_Utterance *utterance = new EST_Utterance;
77
78 // printf("create utterance %p\n", utterance);
79
80 env->SetLongField(self, handle_field, (jlong)utterance);
81
82 return 1;
83 }
84
85 JNIEXPORT jboolean JNICALL
Java_cstr_est_Utterance_destroy_1cpp_1utterance(JNIEnv * env,jobject self)86 Java_cstr_est_Utterance_destroy_1cpp_1utterance (JNIEnv *env, jobject self)
87 {
88 EST_Utterance *utterance = (EST_Utterance *) env->GetLongField(self, handle_field);
89
90 // printf("destroy utterance %p\n", utterance);
91
92 if (utterance)
93 delete utterance;
94 return 1;
95 }
96
97 JNIEXPORT jint JNICALL
Java_cstr_est_Utterance_cpp_1num_1relations(JNIEnv * env,jobject self)98 Java_cstr_est_Utterance_cpp_1num_1relations(JNIEnv *env, jobject self)
99 {
100 EST_Utterance *utterance = (EST_Utterance *)env->GetLongField(self, handle_field);
101
102 return utterance->num_relations();
103 }
104
105 JNIEXPORT jboolean JNICALL
Java_cstr_est_Utterance_cpp_1has_1relation(JNIEnv * env,jobject self,jstring jname)106 Java_cstr_est_Utterance_cpp_1has_1relation(JNIEnv *env,
107 jobject self,
108 jstring jname)
109 {
110 EST_Utterance *utterance = (EST_Utterance *)env->GetLongField(self, handle_field);
111
112 const char *name = env->GetStringUTFChars(jname, 0);
113
114 bool has = utterance->relation_present(name);
115
116 env->ReleaseStringUTFChars(jname, name);
117
118 return has;
119 }
120
Java_cstr_est_Utterance_cpp_1relation_1n(JNIEnv * env,jobject self,jint n)121 JNIEXPORT jlong JNICALL Java_cstr_est_Utterance_cpp_1relation_1n(JNIEnv *env,
122 jobject self,
123 jint n)
124 {
125 EST_Utterance *utterance = (EST_Utterance *)env->GetLongField(self, handle_field);
126
127 EST_Relation * rel=NULL;
128
129 EST_Features::Entries p;
130 for(p.begin(utterance->relations); p && n>0; ++p,n--)
131 ;
132
133 if (n==0)
134 rel = relation(p->v);
135
136 return (jlong)rel;
137 }
138
139
140 JNIEXPORT jlong JNICALL
Java_cstr_est_Utterance_cpp_1relation(JNIEnv * env,jobject self,jstring jname)141 Java_cstr_est_Utterance_cpp_1relation(JNIEnv *env,
142 jobject self,
143 jstring jname)
144 {
145 EST_Utterance *utterance = (EST_Utterance *)env->GetLongField(self, handle_field);
146
147 const char *name = env->GetStringUTFChars(jname, 0);
148
149 if (!utterance->relation_present(name))
150 return (jlong)0l;
151
152 EST_Relation * rel = utterance->relation(name);
153
154
155 env->ReleaseStringUTFChars(jname, name);
156 return (jlong)rel;
157 }
158
159
160 JNIEXPORT jlong JNICALL
Java_cstr_est_Utterance_cpp_1create_1relation(JNIEnv * env,jobject self,jstring jname)161 Java_cstr_est_Utterance_cpp_1create_1relation(JNIEnv *env,
162 jobject self,
163 jstring jname)
164 {
165 EST_Utterance *utterance = (EST_Utterance *)env->GetLongField(self, handle_field);
166
167 const char *name = env->GetStringUTFChars(jname, 0);
168
169 if (utterance->relation_present(name))
170 return (jlong)0l;
171
172 EST_Relation * rel = utterance->create_relation(name);
173
174
175 env->ReleaseStringUTFChars(jname, name);
176 return (jlong)rel;
177 }
178
179
180 JNIEXPORT jstring JNICALL
Java_cstr_est_Utterance_cpp_1load(JNIEnv * env,jobject self,jstring jfilename)181 Java_cstr_est_Utterance_cpp_1load (JNIEnv *env, jobject self, jstring jfilename)
182 {
183 EST_Utterance *utterance = (EST_Utterance *) env->GetLongField(self, handle_field);
184
185 const char *filename = env->GetStringUTFChars(jfilename, 0);
186
187 EST_String fn(filename);
188 EST_read_status stat = read_ok;
189
190 CATCH_ERRORS()
191 {
192 env->ReleaseStringUTFChars(jfilename, filename);
193 return env->NewStringUTF(EST_error_message);
194 }
195
196 stat = utterance->load(fn);
197
198 END_CATCH_ERRORS();
199
200 env->ReleaseStringUTFChars(jfilename, filename);
201
202 const char *res = "";
203
204 if (stat == read_format_error)
205 res = "utterance format error";
206 else if (stat != read_ok)
207 res = "utterance load error";
208
209 return env->NewStringUTF(res);
210 }
211
212 JNIEXPORT jstring JNICALL
Java_cstr_est_Utterance_cpp_1save(JNIEnv * env,jobject self,jstring jfilename,jstring jformat)213 Java_cstr_est_Utterance_cpp_1save (JNIEnv *env, jobject self, jstring jfilename, jstring jformat)
214 {
215 const EST_Utterance *utterance = (EST_Utterance *) env->GetLongField(self, handle_field);
216
217 const char *filename = env->GetStringUTFChars(jfilename, 0);
218 const char *format = env->GetStringUTFChars(jformat, 0);
219 const char *res = "";
220
221 EST_write_status stat = format[0]=='\0'
222 ? utterance->save(filename)
223 : utterance->save(filename,format);
224
225 env->ReleaseStringUTFChars(jfilename, filename);
226 env->ReleaseStringUTFChars(jformat, format);
227
228 if (stat == write_error)
229 res = "utterance save error";
230
231 return env->NewStringUTF(res);
232 }
233
234