Subversion Repositories Projects

Rev

Rev 382 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 382 Rev 493
1
/******************************************
1
/******************************************
2
 *
2
 *
3
 * Voice output for MK
3
 * Voice output for MK
4
 *                      
4
 *                      
5
 * Author:        Marcus -LiGi- Bueschleb
5
 * Author:        Marcus -LiGi- Bueschleb
6
 *
6
 *
7
 *******************************************/
7
 *******************************************/
8
 
8
 
9
//public class MKStatusVoice {}
9
//public class MKStatusVoice {}
10
 
10
 
11
import javax.microedition.media.*;
11
import javax.microedition.media.*;
12
import javax.microedition.media.control.*;
12
import javax.microedition.media.control.*;
13
import java.util.*;
13
import java.util.*;
14
import java.io.*;
14
import java.io.*;
15
 
15
 
16
 
16
 
17
public class MKStatusVoice
17
public class MKStatusVoice
18
    implements Runnable,PlayerListener
18
    implements Runnable,PlayerListener
19
{
19
{
20
 
20
 
21
    DUBwiseCanvas canvas=null;
21
    DUBwiseCanvas canvas=null;
22
    Player player;
22
    Player player;
23
 
23
 
24
    Player up_player;
24
    Player up_player;
25
    Player down_player;
25
    Player down_player;
26
   
26
   
27
    public final int PLAYERSTATE_IDLE=0;
27
    public final int PLAYERSTATE_IDLE=0;
28
    public final int PLAYERSTATE_PLAYING=1;
28
    public final int PLAYERSTATE_PLAYING=1;
29
    public final int PLAYERSTATE_FIN=2;
29
    public final int PLAYERSTATE_FIN=2;
30
 
30
 
31
 
31
 
32
    public int volume=100;
32
    public int volume=100;
33
    public int delay=5;
33
    public int delay=5;
34
 
34
 
35
    int act_player_state=PLAYERSTATE_IDLE;
35
    int act_player_state=PLAYERSTATE_IDLE;
36
    VolumeControl vc;
36
    VolumeControl vc;
37
 
37
 
38
    public MKStatusVoice(DUBwiseCanvas _canvas)
38
    public MKStatusVoice(DUBwiseCanvas _canvas)
39
    {
39
    {
40
        canvas=_canvas;
40
        canvas=_canvas;
41
 
41
 
42
        new Thread( this ).start(); // fire up main Thread 
42
        new Thread( this ).start(); // fire up main Thread 
43
    }
43
    }
44
 
44
 
45
    public void playerUpdate(Player player,  String event, Object data)
45
    public void playerUpdate(Player player,  String event, Object data)
46
    {
46
    {
47
      if(event == PlayerListener.END_OF_MEDIA) {
47
      if(event == PlayerListener.END_OF_MEDIA) {
48
          try {
48
          try {
49
              defplayer();
49
              defplayer();
50
          }
50
          }
51
          catch(MediaException me) {      }
51
          catch(MediaException me) {      }
52
          act_player_state=PLAYERSTATE_FIN;
52
          act_player_state=PLAYERSTATE_FIN;
53
          player=null;
53
          player=null;
54
 
54
 
55
      }
55
      }
56
    }
56
    }
57
   
57
   
58
    void defplayer() throws MediaException {
58
    void defplayer() throws MediaException {
59
      if (player != null) {
59
      if (player != null) {
60
         if(player.getState() == Player.STARTED) {
60
         if(player.getState() == Player.STARTED) {
61
            player.stop();
61
            player.stop();
62
         }
62
         }
63
         if(player.getState() == Player.PREFETCHED) {
63
         if(player.getState() == Player.PREFETCHED) {
64
            player.deallocate();
64
            player.deallocate();
65
         }
65
         }
66
         if(player.getState() == Player.REALIZED ||   player.getState() == Player.UNREALIZED) {
66
         if(player.getState() == Player.REALIZED ||   player.getState() == Player.UNREALIZED) {
67
           player.close();
67
           player.close();
68
         }
68
         }
69
      }
69
      }
70
      player = null;
70
      player = null;
71
    }
71
    }
72
 
72
 
73
 
73
 
74
    public Player init_player(String name)
74
    public Player init_player(String name)
75
    {
75
    {
76
        Player _player=null;
76
        Player _player=null;
77
        try {
77
        try {
78
               
78
               
79
            try {
79
            try {
80
                _player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3");
80
                _player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3");
81
            }
81
            }
82
            catch (Exception e)  {
82
            catch (Exception e)  {
83
                _player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav");
83
                _player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav");
84
            }
84
            }
85
            _player.addPlayerListener(this);
85
            _player.addPlayerListener(this);
86
            _player.realize();
86
            _player.realize();
87
 
87
 
88
 
88
 
89
            vc = (VolumeControl) _player.getControl("VolumeControl");
89
            vc = (VolumeControl) _player.getControl("VolumeControl");
90
            if(vc != null) {
90
            if(vc != null) {
91
                vc.setLevel(volume);
91
                vc.setLevel(volume);
92
            }
92
            }
93
           
93
           
94
            _player.prefetch();
94
            _player.prefetch();
95
            _player.setLoopCount(1);
95
            _player.setLoopCount(1);
96
            _player.start();   
96
            _player.start();   
97
        }
97
        }
98
        catch (Exception e)  {
98
        catch (Exception e)  {
99
           
99
           
100
        }      
100
        }      
101
        return _player;
101
        return _player;
102
 
102
 
103
    }
103
    }
104
 
104
 
105
    public void play_up()
105
    public void play_up()
106
    {
106
    {
107
 
107
 
108
        try {
108
        try {
109
            act_player_state=PLAYERSTATE_PLAYING;
-
 
-
 
109
 
110
            if (up_player==null)
110
            if (up_player==null)
111
                up_player=init_player("up");
111
                up_player=init_player("up");
112
 
112
 
113
 
113
 
114
            up_player.start(); 
114
            up_player.start(); 
-
 
115
            act_player_state=PLAYERSTATE_PLAYING;
115
        }
116
        }
116
        catch (Exception e)  {
117
        catch (Exception e)  {
117
           
118
           
118
        }      
119
        }      
119
    }
120
    }
120
 
121
 
121
 
122
 
122
    public void play_down()
123
    public void play_down()
123
    {
124
    {
124
 
-
 
125
        try {
125
        try {
126
            act_player_state=PLAYERSTATE_PLAYING;
-
 
127
            if (down_player==null)
126
            if (down_player==null)
128
                down_player=init_player("down");
127
                down_player=init_player("down");
129
            down_player.start();       
128
            down_player.start();       
-
 
129
            act_player_state=PLAYERSTATE_PLAYING;
130
        }
130
        }
131
        catch (Exception e)  {
131
        catch (Exception e)  {
132
           
132
           
133
        }      
133
        }      
134
    }
134
    }
135
    public void start_playing(String name)
135
    public void start_playing(String name)
136
    {
136
    {
137
 
137
 
138
        try {
138
        try {
139
            act_player_state=PLAYERSTATE_PLAYING;
139
            act_player_state=PLAYERSTATE_PLAYING;
140
            player=init_player(name);
140
            player=init_player(name);
141
            player.start();    
141
            player.start();    
142
        }
142
        }
143
        catch (Exception e)  {
143
        catch (Exception e)  {
144
           
144
           
145
        }      
145
        }      
146
    }
146
    }
147
   
147
   
148
    public void wait_for_end()
148
    public void wait_for_end()
149
    {
149
    {
150
        while (act_player_state!=PLAYERSTATE_FIN)
150
        while (act_player_state!=PLAYERSTATE_FIN)
151
            {
151
            {
152
                try { Thread.sleep(5); }
152
                try { Thread.sleep(5); }
153
                catch (Exception e)  {   }
153
                catch (Exception e)  {   }
154
            }
154
            }
155
               
155
               
156
 
156
 
157
    }
157
    }
158
 
158
 
159
    int info_from_debug_set=-1;
159
    int info_from_debug_set=-1;
160
    int volt_timeout=0;
160
    int volt_timeout=0;
161
 
161
 
162
    public int BASE_SLEEP=10;
162
    public int BASE_SLEEP=10;
163
 
163
 
164
    public int last_alt=-1;
164
    public int last_alt=-1;
165
 
165
 
166
 
166
 
167
    public int loop_cnt;
167
    public int loop_cnt;
168
    public int volts_play_cnt;
168
    public int volts_play_cnt;
169
 
169
 
170
    public void run()
170
    public void run()
171
    {
171
    {
172
        while(true)
172
        while(true)
173
            {
173
            {
174
                loop_cnt++;
174
                loop_cnt++;
175
                if ((canvas.mk!=null)&&(canvas.mk.connected)&&(canvas.settings.do_sound)&&(canvas.mk.UBatt()!=-1)&&(!canvas.mk.force_disconnect))
175
                if ((canvas.mk!=null)&&(canvas.mk.connected)&&(canvas.settings.do_sound)&&(canvas.mk.UBatt()!=-1)&&(!canvas.mk.force_disconnect))
176
                    {
176
                    {
177
                        volt_timeout--;
177
                        volt_timeout--;
178
       
178
       
179
                        if (info_from_debug_set!=canvas.mk.stats.debug_data_count)
179
                        if (info_from_debug_set!=canvas.mk.stats.debug_data_count)
180
                            { // only when newdata
180
                            { // only when newdata
181
                               
181
                               
182
                                if (canvas.settings.do_volts_sound&&(volt_timeout<0))
182
                                if (canvas.settings.do_volts_sound&&(volt_timeout<0))
183
                                    {
183
                                    {
184
                                        volts_play_cnt++;
184
                                        volts_play_cnt++;
185
 
185
 
186
                                        volt_timeout=(delay*1000)/BASE_SLEEP;
186
                                        volt_timeout=(delay*1000)/BASE_SLEEP;
187
                                        int ubatt=canvas.mk.UBatt();
187
                                        int ubatt=canvas.mk.UBatt();
188
                                        info_from_debug_set=canvas.mk.stats.debug_data_count;
188
                                        info_from_debug_set=canvas.mk.stats.debug_data_count;
189
                                        start_playing(""+(ubatt/10));
189
                                        start_playing(""+(ubatt/10));
190
                                        wait_for_end();
190
                                        wait_for_end();
191
                                       
191
                                       
192
                                        if((ubatt%10)!=0)
192
                                        if((ubatt%10)!=0)
193
                                            {
193
                                            {
194
                                                start_playing("komma");
194
                                                start_playing("komma");
195
                                                wait_for_end();
195
                                                wait_for_end();
196
                                                start_playing(""+(ubatt%10));
196
                                                start_playing(""+(ubatt%10));
197
                                                wait_for_end();
197
                                                wait_for_end();
198
                                            }
198
                                            }
199
                                        start_playing("volt");
199
                                        start_playing("volt");
200
                                        wait_for_end();
200
                                        wait_for_end();
201
                                    }
201
                                    }
202
 
202
 
203
                               
203
                               
204
 
204
 
205
                                if (canvas.settings.do_altimeter_sound)
205
                                if (canvas.settings.do_altimeter_sound)
206
                                    {
206
                                    {
207
                                        if (last_alt==-1) last_alt=canvas.mk.Alt();
207
                                        if (last_alt==-1) last_alt=canvas.mk.Alt();
208
 
208
 
209
                               
209
                               
210
                                        if (last_alt>canvas.mk.Alt()+canvas.settings.altsteps)
210
                                        if (last_alt>canvas.mk.Alt()+canvas.settings.altsteps)
211
                                            {
211
                                            {
212
                                                //                                              start_playing("down");
212
                                                //                                              start_playing("down");
213
                                                play_down();
213
                                                play_down();
214
                                                wait_for_end();
214
                                                wait_for_end();
215
                                                last_alt-=canvas.settings.altsteps;
215
                                                last_alt-=canvas.settings.altsteps;
216
                                            }
216
                                            }
217
                                        if (last_alt<canvas.mk.Alt()-canvas.settings.altsteps)
217
                                        if (last_alt<canvas.mk.Alt()-canvas.settings.altsteps)
218
                                           
218
                                           
219
                                            {
219
                                            {
220
                                                //start_playing("up");
220
                                                //start_playing("up");
221
                                                play_up();
221
                                                play_up();
222
                                                wait_for_end();
222
                                                wait_for_end();
223
                                                last_alt+=canvas.settings.altsteps;
223
                                                last_alt+=canvas.settings.altsteps;
224
                                            }
224
                                            }
225
                                    }
225
                                    }
226
                                else
226
                                else
227
                                    last_alt=-1;
227
                                    last_alt=-1;
228
                            }
228
                            }
229
       
229
       
230
                    }
230
                    }
231
 
231
 
232
                try {
232
                try {
233
                    Thread.sleep(BASE_SLEEP);
233
                    Thread.sleep(BASE_SLEEP);
234
                   
234
                   
235
                    //if (delay<1)
235
                    //if (delay<1)
236
                    //  Thread.sleep(1000); 
236
                    //  Thread.sleep(1000); 
237
                    //else
237
                    //else
238
                    //  Thread.sleep(delay*1000); 
238
                    //  Thread.sleep(delay*1000); 
239
                   
239
                   
240
                }
240
                }
241
                catch (Exception e)  {   }
241
                catch (Exception e)  {   }
242
               
242
               
243
            }
243
            }
244
    }
244
    }
245
   
245
   
246
 
246
 
247
   
247
   
248
}
248
}
249
 
249
 
250
 
250