Rev 269 | Rev 493 | Go to most recent revision | 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 { |
||
109 | act_player_state=PLAYERSTATE_PLAYING; |
||
110 | if (up_player==null) |
||
111 | up_player=init_player("up"); |
||
112 | |||
113 | |||
114 | up_player.start(); |
||
115 | } |
||
116 | catch (Exception e) { |
||
117 | |||
118 | } |
||
119 | } |
||
120 | |||
121 | |||
122 | public void play_down() |
||
123 | { |
||
124 | |||
125 | try { |
||
126 | act_player_state=PLAYERSTATE_PLAYING; |
||
127 | if (down_player==null) |
||
128 | down_player=init_player("down"); |
||
129 | down_player.start(); |
||
130 | } |
||
131 | catch (Exception e) { |
||
132 | |||
133 | } |
||
134 | } |
||
135 | public void start_playing(String name) |
||
136 | { |
||
137 | |||
138 | try { |
||
139 | act_player_state=PLAYERSTATE_PLAYING; |
||
140 | player=init_player(name); |
||
141 | player.start(); |
||
142 | } |
||
143 | catch (Exception e) { |
||
144 | |||
145 | } |
||
146 | } |
||
206 | ligi | 147 | |
148 | public void wait_for_end() |
||
149 | { |
||
150 | while (act_player_state!=PLAYERSTATE_FIN) |
||
151 | { |
||
152 | try { Thread.sleep(5); } |
||
153 | catch (Exception e) { } |
||
154 | } |
||
155 | |||
156 | |||
157 | } |
||
158 | |||
159 | int info_from_debug_set=-1; |
||
265 | ligi | 160 | int volt_timeout=0; |
161 | |||
382 | ligi | 162 | public int BASE_SLEEP=10; |
163 | |||
265 | ligi | 164 | public int last_alt=-1; |
165 | |||
382 | ligi | 166 | |
167 | public int loop_cnt; |
||
168 | public int volts_play_cnt; |
||
169 | |||
206 | ligi | 170 | public void run() |
171 | { |
||
172 | while(true) |
||
173 | { |
||
382 | ligi | 174 | loop_cnt++; |
175 | if ((canvas.mk!=null)&&(canvas.mk.connected)&&(canvas.settings.do_sound)&&(canvas.mk.UBatt()!=-1)&&(!canvas.mk.force_disconnect)) |
||
206 | ligi | 176 | { |
382 | ligi | 177 | volt_timeout--; |
265 | ligi | 178 | |
382 | ligi | 179 | if (info_from_debug_set!=canvas.mk.stats.debug_data_count) |
265 | ligi | 180 | { // only when newdata |
206 | ligi | 181 | |
382 | ligi | 182 | if (canvas.settings.do_volts_sound&&(volt_timeout<0)) |
206 | ligi | 183 | { |
382 | ligi | 184 | volts_play_cnt++; |
185 | |||
186 | volt_timeout=(delay*1000)/BASE_SLEEP; |
||
187 | int ubatt=canvas.mk.UBatt(); |
||
188 | info_from_debug_set=canvas.mk.stats.debug_data_count; |
||
265 | ligi | 189 | start_playing(""+(ubatt/10)); |
206 | ligi | 190 | wait_for_end(); |
265 | ligi | 191 | |
192 | if((ubatt%10)!=0) |
||
193 | { |
||
194 | start_playing("komma"); |
||
195 | wait_for_end(); |
||
196 | start_playing(""+(ubatt%10)); |
||
197 | wait_for_end(); |
||
198 | } |
||
199 | start_playing("volt"); |
||
206 | ligi | 200 | wait_for_end(); |
201 | } |
||
265 | ligi | 202 | |
203 | |||
204 | |||
205 | if (canvas.settings.do_altimeter_sound) |
||
206 | { |
||
382 | ligi | 207 | if (last_alt==-1) last_alt=canvas.mk.Alt(); |
269 | ligi | 208 | |
265 | ligi | 209 | |
382 | ligi | 210 | if (last_alt>canvas.mk.Alt()+canvas.settings.altsteps) |
265 | ligi | 211 | { |
382 | ligi | 212 | // start_playing("down"); |
213 | play_down(); |
||
265 | ligi | 214 | wait_for_end(); |
269 | ligi | 215 | last_alt-=canvas.settings.altsteps; |
265 | ligi | 216 | } |
382 | ligi | 217 | if (last_alt<canvas.mk.Alt()-canvas.settings.altsteps) |
265 | ligi | 218 | |
219 | { |
||
382 | ligi | 220 | //start_playing("up"); |
221 | play_up(); |
||
265 | ligi | 222 | wait_for_end(); |
269 | ligi | 223 | last_alt+=canvas.settings.altsteps; |
265 | ligi | 224 | } |
225 | } |
||
226 | else |
||
227 | last_alt=-1; |
||
206 | ligi | 228 | } |
229 | |||
230 | } |
||
231 | |||
265 | ligi | 232 | try { |
382 | ligi | 233 | Thread.sleep(BASE_SLEEP); |
234 | |||
235 | //if (delay<1) |
||
236 | // Thread.sleep(1000); |
||
237 | //else |
||
238 | // Thread.sleep(delay*1000); |
||
239 | |||
229 | ligi | 240 | } |
206 | ligi | 241 | catch (Exception e) { } |
242 | |||
243 | } |
||
244 | } |
||
245 | |||
246 | |||
247 | |||
248 | } |
||
382 | ligi | 249 |