2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
3 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
21 import android.speech.tts.TextToSpeech;
22 import android.speech.tts.TextToSpeech.OnInitListener;
23 import android.location.Location;
25 import org.altusmetrum.altoslib_11.*;
27 public class AltosVoice {
29 private TextToSpeech tts = null;
30 private boolean tts_enabled = false;
32 static final int TELL_MODE_NONE = 0;
33 static final int TELL_MODE_PAD = 1;
34 static final int TELL_MODE_FLIGHT = 2;
35 static final int TELL_MODE_RECOVER = 3;
37 static final int TELL_FLIGHT_NONE = 0;
38 static final int TELL_FLIGHT_STATE = 1;
39 static final int TELL_FLIGHT_SPEED = 2;
40 static final int TELL_FLIGHT_HEIGHT = 3;
41 static final int TELL_FLIGHT_TRACK = 4;
43 private int last_tell_mode;
44 private int last_tell_serial = AltosLib.MISSING;
45 private int last_state;
46 private AltosGPS last_gps;
47 private double last_height = AltosLib.MISSING;
48 private Location last_receiver;
49 private long last_speak_time;
50 private int last_flight_tell = TELL_FLIGHT_NONE;
53 return System.currentTimeMillis();
56 private void reset_last() {
57 last_tell_mode = TELL_MODE_NONE;
58 last_speak_time = now() - 100 * 1000;
60 last_height = AltosLib.MISSING;
62 last_state = AltosLib.ao_flight_invalid;
63 last_flight_tell = TELL_FLIGHT_NONE;
66 public AltosVoice(AltosDroid a) {
67 tts = new TextToSpeech(a, new OnInitListener() {
68 public void onInit(int status) {
69 if (status == TextToSpeech.SUCCESS) tts_enabled = true;
75 public synchronized void set_enable(boolean enable) {
79 public synchronized void speak(String s) {
80 if (!tts_enabled) return;
81 last_speak_time = now();
82 tts.speak(s, TextToSpeech.QUEUE_ADD, null);
85 public synchronized long time_since_speak() {
86 return now() - last_speak_time;
89 public synchronized void speak(String format, Object ... arguments) {
90 speak(String.format(format, arguments));
93 public synchronized boolean is_speaking() {
94 return tts.isSpeaking();
104 private boolean last_apogee_good;
105 private boolean last_main_good;
106 private boolean last_gps_good;
108 private boolean tell_gonogo(String name,
112 if (current != previous || new_mode)
113 speak("%s %s.", name, current ? "ready" : "not ready");
117 private boolean tell_pad(TelemetryState telem_state, AltosState state,
118 AltosGreatCircle from_receiver, Location receiver) {
123 if (state.apogee_voltage != AltosLib.MISSING)
124 last_apogee_good = tell_gonogo("apogee",
125 state.apogee_voltage >= AltosLib.ao_igniter_good,
127 last_tell_mode != TELL_MODE_PAD);
129 if (state.main_voltage != AltosLib.MISSING)
130 last_main_good = tell_gonogo("main",
131 state.main_voltage >= AltosLib.ao_igniter_good,
133 last_tell_mode != TELL_MODE_PAD);
135 if (state.gps != null)
136 last_gps_good = tell_gonogo("G P S",
139 last_tell_mode != TELL_MODE_PAD);
144 private boolean descending(int state) {
145 return AltosLib.ao_flight_drogue <= state && state <= AltosLib.ao_flight_landed;
148 private boolean target_moved(AltosState state) {
149 if (last_gps != null && state != null && state.gps != null) {
150 AltosGreatCircle moved = new AltosGreatCircle(last_gps.lat, last_gps.lon, last_gps.alt,
151 state.gps.lat, state.gps.lon, state.gps.alt);
152 double height_change = 0;
153 double height = state.height();
155 if (height != AltosLib.MISSING && last_height != AltosLib.MISSING)
156 height_change = Math.abs(last_height - height);
158 if (moved.range < 10 && height_change < 10)
164 private boolean receiver_moved(Location receiver) {
165 if (last_receiver != null && receiver != null) {
166 AltosGreatCircle moved = new AltosGreatCircle(last_receiver.getLatitude(),
167 last_receiver.getLongitude(),
168 last_receiver.getAltitude(),
169 receiver.getLatitude(),
170 receiver.getLongitude(),
171 receiver.getAltitude());
172 if (moved.range < 10)
178 private boolean tell_flight(TelemetryState telem_state, AltosState state,
179 AltosGreatCircle from_receiver, Location receiver) {
181 boolean spoken = false;
186 if (last_tell_mode != TELL_MODE_FLIGHT)
187 last_flight_tell = TELL_FLIGHT_NONE;
189 if (state.state() != last_state && AltosLib.ao_flight_boost <= state.state() && state.state() <= AltosLib.ao_flight_landed) {
190 speak(state.state_name());
191 if (descending(state.state()) && !descending(last_state)) {
192 if (state.max_height() != AltosLib.MISSING) {
193 speak("max height: %s.",
194 AltosConvert.height.say_units(state.max_height()));
197 last_flight_tell = TELL_FLIGHT_STATE;
201 if (last_tell_mode == TELL_MODE_FLIGHT && last_flight_tell == TELL_FLIGHT_TRACK) {
202 if (time_since_speak() < 10 * 1000)
204 if (!target_moved(state) && !receiver_moved(receiver))
211 if (last_flight_tell == TELL_FLIGHT_NONE || last_flight_tell == TELL_FLIGHT_STATE || last_flight_tell == TELL_FLIGHT_TRACK) {
212 last_flight_tell = TELL_FLIGHT_SPEED;
214 if (state.state() <= AltosLib.ao_flight_coast) {
215 speed = state.speed();
217 speed = state.gps_speed();
218 if (speed == AltosLib.MISSING)
219 speed = state.speed();
222 if (speed != AltosLib.MISSING) {
223 speak("speed: %s.", AltosConvert.speed.say_units(speed));
228 if (last_flight_tell == TELL_FLIGHT_SPEED) {
229 last_flight_tell = TELL_FLIGHT_HEIGHT;
230 height = state.height();
232 if (height != AltosLib.MISSING) {
233 speak("height: %s.", AltosConvert.height.say_units(height));
238 if (last_flight_tell == TELL_FLIGHT_HEIGHT) {
239 last_flight_tell = TELL_FLIGHT_TRACK;
240 if (from_receiver != null) {
241 speak("bearing %s %d, elevation %d, range %s.",
242 from_receiver.bearing_words(
243 AltosGreatCircle.BEARING_VOICE),
244 (int) (from_receiver.bearing + 0.5),
245 (int) (from_receiver.elevation + 0.5),
246 AltosConvert.distance.say(from_receiver.range));
254 private boolean tell_recover(TelemetryState telem_state, AltosState state,
255 AltosGreatCircle from_receiver, Location receiver) {
257 if (from_receiver == null)
260 if (last_tell_mode == TELL_MODE_RECOVER) {
261 if (!target_moved(state) && !receiver_moved(receiver))
263 if (time_since_speak() <= 10 * 1000)
267 String direction = AltosDroid.direction(from_receiver, receiver);
268 if (direction == null)
269 direction = String.format("Bearing %d", (int) (from_receiver.bearing + 0.5));
271 speak("%s, range %s.", direction,
272 AltosConvert.distance.say_units(from_receiver.distance));
277 public void tell(TelemetryState telem_state, AltosState state,
278 AltosGreatCircle from_receiver, Location receiver,
281 boolean spoken = false;
283 if (!tts_enabled) return;
285 if (is_speaking()) return;
287 int tell_serial = last_tell_serial;
290 tell_serial = state.serial;
292 if (tell_serial != last_tell_serial)
295 int tell_mode = TELL_MODE_NONE;
297 if (tab.tab_name().equals(AltosDroid.tab_pad_name))
298 tell_mode = TELL_MODE_PAD;
299 else if (tab.tab_name().equals(AltosDroid.tab_flight_name))
300 tell_mode = TELL_MODE_FLIGHT;
302 tell_mode = TELL_MODE_RECOVER;
304 if (tell_mode == TELL_MODE_PAD)
305 spoken = tell_pad(telem_state, state, from_receiver, receiver);
306 else if (tell_mode == TELL_MODE_FLIGHT)
307 spoken = tell_flight(telem_state, state, from_receiver, receiver);
309 spoken = tell_recover(telem_state, state, from_receiver, receiver);
312 last_tell_mode = tell_mode;
313 last_tell_serial = tell_serial;
315 last_state = state.state();
316 last_height = state.height();
317 if (state.gps != null)
318 last_gps = state.gps;
320 if (receiver != null)
321 last_receiver = receiver;