Rev 265 | Rev 382 | 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 | |||
9 | import javax.microedition.media.*; |
||
10 | import javax.microedition.media.control.*; |
||
11 | import java.util.*; |
||
12 | import java.io.*; |
||
13 | |||
14 | |||
15 | public class MKStatusVoice |
||
16 | implements Runnable,PlayerListener |
||
17 | { |
||
18 | |||
19 | org.ligi.ufo.MKCommunicator mk=null; |
||
20 | DUBwiseCanvas canvas=null; |
||
21 | Player player; |
||
22 | |||
23 | public final int PLAYERSTATE_IDLE=0; |
||
24 | public final int PLAYERSTATE_PLAYING=1; |
||
25 | public final int PLAYERSTATE_FIN=2; |
||
26 | |||
229 | ligi | 27 | |
28 | public int volume=100; |
||
29 | public int delay=5; |
||
30 | |||
206 | ligi | 31 | int act_player_state=PLAYERSTATE_IDLE; |
32 | VolumeControl vc; |
||
33 | |||
34 | public MKStatusVoice(org.ligi.ufo.MKCommunicator _mk,DUBwiseCanvas _canvas) |
||
35 | { |
||
36 | canvas=_canvas; |
||
37 | mk=_mk; |
||
38 | new Thread( this ).start(); // fire up main Thread |
||
39 | } |
||
40 | |||
41 | public void playerUpdate(Player player, String event, Object data) |
||
42 | { |
||
43 | if(event == PlayerListener.END_OF_MEDIA) { |
||
44 | try { |
||
45 | defplayer(); |
||
46 | } |
||
47 | catch(MediaException me) { } |
||
48 | act_player_state=PLAYERSTATE_FIN; |
||
49 | player=null; |
||
50 | |||
51 | } |
||
52 | } |
||
53 | |||
54 | void defplayer() throws MediaException { |
||
55 | if (player != null) { |
||
56 | if(player.getState() == Player.STARTED) { |
||
57 | player.stop(); |
||
58 | } |
||
59 | if(player.getState() == Player.PREFETCHED) { |
||
60 | player.deallocate(); |
||
61 | } |
||
62 | if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED) { |
||
63 | player.close(); |
||
64 | } |
||
65 | } |
||
66 | player = null; |
||
67 | } |
||
68 | |||
69 | |||
70 | public void start_playing(String name) |
||
71 | { |
||
72 | try { |
||
73 | |||
74 | try { |
||
75 | player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3"); |
||
76 | } |
||
77 | catch (Exception e) { |
||
78 | player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav"); |
||
79 | } |
||
80 | player.addPlayerListener(this); |
||
81 | player.realize(); |
||
82 | act_player_state=PLAYERSTATE_PLAYING; |
||
83 | |||
84 | vc = (VolumeControl) player.getControl("VolumeControl"); |
||
85 | if(vc != null) { |
||
229 | ligi | 86 | vc.setLevel(volume); |
206 | ligi | 87 | } |
88 | |||
89 | player.prefetch(); |
||
90 | player.setLoopCount(1); |
||
91 | player.start(); |
||
92 | } |
||
93 | catch (Exception e) { |
||
94 | |||
95 | } |
||
96 | } |
||
97 | |||
98 | public void wait_for_end() |
||
99 | { |
||
100 | while (act_player_state!=PLAYERSTATE_FIN) |
||
101 | { |
||
102 | try { Thread.sleep(5); } |
||
103 | catch (Exception e) { } |
||
104 | } |
||
105 | |||
106 | |||
107 | } |
||
108 | |||
109 | int info_from_debug_set=-1; |
||
265 | ligi | 110 | int volt_timeout=0; |
111 | |||
112 | public int last_alt=-1; |
||
113 | |||
206 | ligi | 114 | public void run() |
115 | { |
||
116 | while(true) |
||
117 | { |
||
265 | ligi | 118 | if (mk.connected&&(canvas.settings.do_sound)&&(mk.UBatt()!=-1)&&(!mk.force_disconnect)) |
206 | ligi | 119 | { |
265 | ligi | 120 | |
206 | ligi | 121 | if (info_from_debug_set!=mk.stats.debug_data_count) |
265 | ligi | 122 | { // only when newdata |
206 | ligi | 123 | |
265 | ligi | 124 | if (canvas.settings.do_volts_sound&&(volt_timeout--<0)) |
206 | ligi | 125 | { |
265 | ligi | 126 | volt_timeout=delay*10; |
127 | int ubatt=mk.UBatt(); |
||
128 | info_from_debug_set=mk.stats.debug_data_count; |
||
129 | start_playing(""+(ubatt/10)); |
||
206 | ligi | 130 | wait_for_end(); |
265 | ligi | 131 | |
132 | if((ubatt%10)!=0) |
||
133 | { |
||
134 | start_playing("komma"); |
||
135 | wait_for_end(); |
||
136 | start_playing(""+(ubatt%10)); |
||
137 | wait_for_end(); |
||
138 | } |
||
139 | start_playing("volt"); |
||
206 | ligi | 140 | wait_for_end(); |
141 | } |
||
265 | ligi | 142 | |
143 | |||
144 | |||
145 | if (canvas.settings.do_altimeter_sound) |
||
146 | { |
||
147 | if (last_alt==-1) last_alt=mk.Alt(); |
||
269 | ligi | 148 | |
265 | ligi | 149 | |
269 | ligi | 150 | if (last_alt>mk.Alt()+canvas.settings.altsteps) |
265 | ligi | 151 | { |
152 | start_playing("down"); |
||
153 | wait_for_end(); |
||
269 | ligi | 154 | last_alt-=canvas.settings.altsteps; |
265 | ligi | 155 | } |
269 | ligi | 156 | if (last_alt<mk.Alt()-canvas.settings.altsteps) |
265 | ligi | 157 | |
158 | { |
||
159 | start_playing("up"); |
||
160 | wait_for_end(); |
||
269 | ligi | 161 | last_alt+=canvas.settings.altsteps; |
265 | ligi | 162 | } |
163 | } |
||
164 | else |
||
165 | last_alt=-1; |
||
206 | ligi | 166 | } |
167 | |||
168 | } |
||
169 | |||
265 | ligi | 170 | try { |
171 | Thread.sleep(100); |
||
172 | /* |
||
229 | ligi | 173 | if (delay<1) |
174 | Thread.sleep(1000); |
||
175 | else |
||
176 | Thread.sleep(delay*1000); |
||
265 | ligi | 177 | */ |
229 | ligi | 178 | } |
206 | ligi | 179 | catch (Exception e) { } |
180 | |||
181 | } |
||
182 | } |
||
183 | |||
184 | |||
185 | |||
186 | } |