Rev 493 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
206 | ligi | 1 | /****************************************** |
2 | * |
||
3 | * Voice output for MK |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
7 | *******************************************/ |
||
8 | |||
382 | ligi | 9 | //public class MKStatusVoice {} |
10 | |||
206 | ligi | 11 | import javax.microedition.media.*; |
12 | import javax.microedition.media.control.*; |
||
13 | import java.util.*; |
||
14 | import java.io.*; |
||
15 | |||
16 | |||
17 | public class MKStatusVoice |
||
18 | implements Runnable,PlayerListener |
||
19 | { |
||
20 | |||
21 | DUBwiseCanvas canvas=null; |
||
22 | Player player; |
||
382 | ligi | 23 | |
24 | Player up_player; |
||
25 | Player down_player; |
||
206 | ligi | 26 | |
27 | public final int PLAYERSTATE_IDLE=0; |
||
28 | public final int PLAYERSTATE_PLAYING=1; |
||
29 | public final int PLAYERSTATE_FIN=2; |
||
30 | |||
229 | ligi | 31 | |
32 | public int volume=100; |
||
33 | public int delay=5; |
||
34 | |||
206 | ligi | 35 | int act_player_state=PLAYERSTATE_IDLE; |
36 | VolumeControl vc; |
||
37 | |||
382 | ligi | 38 | public MKStatusVoice(DUBwiseCanvas _canvas) |
206 | ligi | 39 | { |
40 | canvas=_canvas; |
||
382 | ligi | 41 | |
206 | ligi | 42 | new Thread( this ).start(); // fire up main Thread |
43 | } |
||
44 | |||
45 | public void playerUpdate(Player player, String event, Object data) |
||
46 | { |
||
47 | if(event == PlayerListener.END_OF_MEDIA) { |
||
48 | try { |
||
49 | defplayer(); |
||
50 | } |
||
51 | catch(MediaException me) { } |
||
52 | act_player_state=PLAYERSTATE_FIN; |
||
53 | player=null; |
||
54 | |||
55 | } |
||
56 | } |
||
57 | |||
58 | void defplayer() throws MediaException { |
||
59 | if (player != null) { |
||
60 | if(player.getState() == Player.STARTED) { |
||
61 | player.stop(); |
||
62 | } |
||
63 | if(player.getState() == Player.PREFETCHED) { |
||
64 | player.deallocate(); |
||
65 | } |
||
66 | if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED) { |
||
67 | player.close(); |
||
68 | } |
||
69 | } |
||
70 | player = null; |
||
71 | } |
||
72 | |||
73 | |||
382 | ligi | 74 | public Player init_player(String name) |
206 | ligi | 75 | { |
382 | ligi | 76 | Player _player=null; |
206 | ligi | 77 | try { |
78 | |||
79 | try { |
||
382 | ligi | 80 | _player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3"); |
206 | ligi | 81 | } |
82 | catch (Exception e) { |
||
382 | ligi | 83 | _player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav"); |
206 | ligi | 84 | } |
382 | ligi | 85 | _player.addPlayerListener(this); |
86 | _player.realize(); |
||
206 | ligi | 87 | |
88 | |||
382 | ligi | 89 | vc = (VolumeControl) _player.getControl("VolumeControl"); |
90 | if(vc != null) { |
||
91 | vc.setLevel(volume); |
||
92 | } |
||
93 | |||
94 | _player.prefetch(); |
||
95 | _player.setLoopCount(1); |
||
96 | _player.start(); |
||
206 | ligi | 97 | } |
98 | catch (Exception e) { |
||
99 | |||
100 | } |
||
382 | ligi | 101 | return _player; |
102 | |||
206 | ligi | 103 | } |
382 | ligi | 104 | |
105 | public void play_up() |
||
106 | { |
||
107 | |||
108 | try { |
||
493 | ligi | 109 | |
382 | ligi | 110 | if (up_player==null) |
111 | up_player=init_player("up"); |
||
112 | |||
113 | |||
114 | up_player.start(); |
||
493 | ligi | 115 | act_player_state=PLAYERSTATE_PLAYING; |
382 | ligi | 116 | } |
117 | catch (Exception e) { |
||
118 | |||
119 | } |
||
120 | } |
||
121 | |||
122 | |||
123 | public void play_down() |
||
124 | { |
||
125 | try { |
||
126 | if (down_player==null) |
||
127 | down_player=init_player("down"); |
||
128 | down_player.start(); |
||
493 | ligi | 129 | act_player_state=PLAYERSTATE_PLAYING; |
382 | ligi | 130 | } |
131 | catch (Exception e) { |
||
132 | |||
133 | } |
||
134 | } |
||
517 | ligi | 135 | |
136 | |||
137 | public boolean play(String what) |
||
382 | ligi | 138 | { |
139 | |||
517 | ligi | 140 | // start play |
382 | ligi | 141 | try { |
142 | act_player_state=PLAYERSTATE_PLAYING; |
||
517 | ligi | 143 | player=init_player(what); |
382 | ligi | 144 | player.start(); |
145 | } |
||
146 | catch (Exception e) { |
||
517 | ligi | 147 | act_player_state=PLAYERSTATE_FIN; |
382 | ligi | 148 | } |
517 | ligi | 149 | |
150 | // wait for end |
||
206 | ligi | 151 | while (act_player_state!=PLAYERSTATE_FIN) |
152 | { |
||
153 | try { Thread.sleep(5); } |
||
154 | catch (Exception e) { } |
||
155 | } |
||
156 | |||
517 | ligi | 157 | |
158 | return true; |
||
206 | ligi | 159 | } |
160 | |||
517 | ligi | 161 | // play number |
162 | public void play(int what) |
||
163 | { |
||
164 | |||
165 | if (what==0) |
||
166 | { |
||
167 | play("0"); |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | if (((what/1000)%10)!=0) |
||
172 | { |
||
173 | play((what/1000)%10); |
||
174 | play("thousand"); |
||
175 | what%=1000; |
||
176 | } |
||
177 | |||
178 | |||
179 | if (((what/100)%10)!=0) |
||
180 | { |
||
181 | play((what/100)%10); |
||
182 | play("hundred"); |
||
183 | what%=100; |
||
184 | |||
185 | } |
||
186 | |||
187 | |||
188 | |||
189 | |||
190 | |||
191 | if (what<20) |
||
192 | { |
||
193 | |||
194 | if (what<13) |
||
195 | { |
||
196 | if(what!=0) |
||
197 | play(""+what); |
||
198 | } |
||
199 | else |
||
200 | { |
||
201 | switch (what%10) |
||
202 | { |
||
203 | case 3: |
||
204 | play("thir"); |
||
205 | break; |
||
206 | case 5: |
||
207 | play("fiv"); |
||
208 | break; |
||
209 | default: |
||
210 | play (""+what%10); |
||
211 | } |
||
212 | play("teen"); |
||
213 | } |
||
214 | } |
||
215 | else |
||
216 | { |
||
217 | |||
218 | switch (what/10) |
||
219 | { |
||
220 | |||
221 | case 2: |
||
222 | play("twen"); |
||
223 | break; |
||
224 | case 3: |
||
225 | play("thir"); |
||
226 | break; |
||
227 | case 5: |
||
228 | play("fiv"); |
||
229 | break; |
||
230 | default: |
||
231 | play (""+what/10); |
||
232 | } |
||
233 | play("ty"); |
||
234 | if ((what%10)!=0) |
||
235 | play (what%10); |
||
236 | } |
||
237 | |||
238 | |||
239 | } |
||
240 | |||
241 | |||
242 | |||
243 | |||
206 | ligi | 244 | int info_from_debug_set=-1; |
265 | ligi | 245 | int volt_timeout=0; |
246 | |||
382 | ligi | 247 | public int BASE_SLEEP=10; |
248 | |||
265 | ligi | 249 | public int last_alt=-1; |
250 | |||
382 | ligi | 251 | |
252 | public int loop_cnt; |
||
253 | public int volts_play_cnt; |
||
254 | |||
517 | ligi | 255 | boolean conn_told=false; |
256 | boolean disconn_told=true; |
||
257 | boolean sender_warning_told=false; |
||
258 | |||
259 | |||
206 | ligi | 260 | public void run() |
261 | { |
||
517 | ligi | 262 | |
263 | |||
206 | ligi | 264 | while(true) |
265 | { |
||
382 | ligi | 266 | loop_cnt++; |
517 | ligi | 267 | if ((canvas.mk!=null)&&(canvas.settings.do_sound)) |
206 | ligi | 268 | { |
517 | ligi | 269 | |
270 | if ((canvas.mk.connected)&&(!canvas.mk.force_disconnect)) |
||
271 | { |
||
272 | if (!conn_told) |
||
273 | { |
||
274 | if (canvas.mk.version.known) |
||
206 | ligi | 275 | { |
517 | ligi | 276 | play("connected"); |
277 | play("to"); |
||
278 | play("mikrokopter"); |
||
279 | play("version"); |
||
280 | |||
281 | play(canvas.mk.version.major); |
||
282 | play("point"); |
||
283 | play(canvas.mk.version.minor); |
||
284 | play("established"); |
||
285 | conn_told=true; |
||
286 | disconn_told=false; |
||
287 | } |
||
288 | } |
||
289 | else |
||
290 | { |
||
382 | ligi | 291 | |
517 | ligi | 292 | |
293 | if ((canvas.mk.SenderOkay()<1)) |
||
294 | { |
||
295 | if (!sender_warning_told) |
||
265 | ligi | 296 | { |
517 | ligi | 297 | play("warning"); |
298 | play("rc-signal"); |
||
299 | play("lost"); |
||
300 | sender_warning_told=true; |
||
265 | ligi | 301 | } |
206 | ligi | 302 | } |
517 | ligi | 303 | else |
304 | sender_warning_told=false; |
||
265 | ligi | 305 | |
306 | |||
517 | ligi | 307 | if ((canvas.mk.AngleNick()>400)||(canvas.mk.AngleRoll()>400)||(canvas.mk.AngleNick()<-400)||(canvas.mk.AngleRoll()<-400)) |
265 | ligi | 308 | { |
517 | ligi | 309 | play("tilt"); |
310 | play("warning"); |
||
311 | } |
||
269 | ligi | 312 | |
517 | ligi | 313 | volt_timeout--; |
265 | ligi | 314 | |
517 | ligi | 315 | if (info_from_debug_set!=canvas.mk.stats.debug_data_count) |
316 | { // only when newdata |
||
317 | |||
318 | |||
319 | if (canvas.settings.do_volts_sound&&(volt_timeout<0)) |
||
265 | ligi | 320 | { |
517 | ligi | 321 | play("battery"); |
322 | play("at"); |
||
323 | volts_play_cnt++; |
||
324 | |||
325 | volt_timeout=(delay*1000)/BASE_SLEEP; |
||
326 | int ubatt=canvas.mk.UBatt(); |
||
327 | info_from_debug_set=canvas.mk.stats.debug_data_count; |
||
328 | play((ubatt/10)); |
||
329 | |||
330 | if((ubatt%10)!=0) |
||
331 | { |
||
332 | play("point"); |
||
333 | play((ubatt%10)); |
||
334 | |||
335 | } |
||
336 | play("volts"); |
||
337 | |||
338 | |||
339 | play("altitude"); |
||
340 | |||
341 | play( canvas.mk.Alt()/10); |
||
342 | play("point"); |
||
343 | play( canvas.mk.Alt()%10); |
||
344 | play("meter"); |
||
345 | |||
346 | |||
347 | if ((canvas.mk.stats.flying_time()!=0)) |
||
348 | { |
||
349 | play("flight"); |
||
350 | play("time"); |
||
351 | switch (canvas.mk.stats.flying_time()/60) |
||
352 | { |
||
353 | case 0: |
||
354 | case 1: |
||
355 | play("1"); |
||
356 | play("minute"); |
||
357 | break; |
||
358 | default: |
||
359 | play(canvas.mk.stats.flying_time()/60); |
||
360 | play("minutes"); |
||
361 | } |
||
362 | |||
363 | switch (canvas.mk.stats.flying_time()%60) |
||
364 | { |
||
365 | case 0: |
||
366 | // case 1: |
||
367 | // play("1"); |
||
368 | break; |
||
369 | default: |
||
370 | play(canvas.mk.stats.flying_time()%60); |
||
371 | play("minutes"); |
||
372 | } |
||
373 | |||
374 | |||
375 | |||
376 | } |
||
265 | ligi | 377 | } |
517 | ligi | 378 | |
379 | |||
380 | |||
381 | if (canvas.settings.do_altimeter_sound) |
||
265 | ligi | 382 | { |
517 | ligi | 383 | if (last_alt==-1) last_alt=canvas.mk.Alt(); |
384 | |||
385 | |||
386 | if (last_alt>canvas.mk.Alt()+canvas.settings.altsteps) |
||
387 | { |
||
388 | play("down"); |
||
389 | |||
390 | last_alt-=canvas.settings.altsteps; |
||
391 | } |
||
392 | if (last_alt<canvas.mk.Alt()-canvas.settings.altsteps) |
||
393 | |||
394 | { |
||
395 | play("up"); |
||
396 | |||
397 | last_alt+=canvas.settings.altsteps; |
||
398 | } |
||
265 | ligi | 399 | } |
517 | ligi | 400 | else |
401 | last_alt=-1; |
||
265 | ligi | 402 | } |
206 | ligi | 403 | } |
517 | ligi | 404 | } |
405 | else |
||
406 | { |
||
407 | if (!disconn_told) |
||
408 | play("disconnected"); |
||
409 | conn_told=false; |
||
410 | disconn_told=true; |
||
411 | |||
412 | } |
||
206 | ligi | 413 | } |
414 | |||
265 | ligi | 415 | try { |
382 | ligi | 416 | Thread.sleep(BASE_SLEEP); |
417 | |||
418 | //if (delay<1) |
||
419 | // Thread.sleep(1000); |
||
420 | //else |
||
421 | // Thread.sleep(delay*1000); |
||
422 | |||
229 | ligi | 423 | } |
206 | ligi | 424 | catch (Exception e) { } |
425 | |||
426 | } |
||
427 | } |
||
428 | |||
429 | |||
430 | |||
431 | } |
||
382 | ligi | 432 |