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; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 package org.altusmetrum.AltosDroid;
22 import android.speech.tts.TextToSpeech;
23 import android.speech.tts.TextToSpeech.OnInitListener;
24 import android.location.Location;
26 import org.altusmetrum.altoslib_11.*;
28 public class AltosVoice {
30 private TextToSpeech tts = null;
31 private boolean tts_enabled = false;
33 static final int TELL_MODE_NONE = 0;
34 static final int TELL_MODE_PAD = 1;
35 static final int TELL_MODE_FLIGHT = 2;
36 static final int TELL_MODE_RECOVER = 3;
38 static final int TELL_FLIGHT_NONE = 0;
39 static final int TELL_FLIGHT_STATE = 1;
40 static final int TELL_FLIGHT_SPEED = 2;
41 static final int TELL_FLIGHT_HEIGHT = 3;
42 static final int TELL_FLIGHT_TRACK = 4;
44 private int last_tell_mode;
45 private int last_tell_serial = AltosLib.MISSING;
46 private int last_state;
47 private AltosGPS last_gps;
48 private double last_height = AltosLib.MISSING;
49 private Location last_receiver;
50 private long last_speak_time;
51 private int last_flight_tell = TELL_FLIGHT_NONE;
54 return System.currentTimeMillis();
57 private void reset_last() {
58 last_tell_mode = TELL_MODE_NONE;
59 last_speak_time = now() - 100 * 1000;
61 last_height = AltosLib.MISSING;
63 last_state = AltosLib.ao_flight_invalid;
64 last_flight_tell = TELL_FLIGHT_NONE;
67 public AltosVoice(AltosDroid a) {
68 tts = new TextToSpeech(a, new OnInitListener() {
69 public void onInit(int status) {
70 if (status == TextToSpeech.SUCCESS) tts_enabled = true;
76 public synchronized void set_enable(boolean enable) {
80 public synchronized void speak(String s) {
81 if (!tts_enabled) return;
82 last_speak_time = now();
83 tts.speak(s, TextToSpeech.QUEUE_ADD, null);
86 public synchronized long time_since_speak() {
87 return now() - last_speak_time;
90 public synchronized void speak(String format, Object ... arguments) {
91 speak(String.format(format, arguments));
94 public synchronized boolean is_speaking() {
95 return tts.isSpeaking();
105 private boolean last_apogee_good;
106 private boolean last_main_good;
107 private boolean last_gps_good;
109 private boolean tell_gonogo(String name,
113 if (current != previous || new_mode)
114 speak("%s %s.", name, current ? "ready" : "not ready");
118 private boolean tell_pad(TelemetryState telem_state, AltosState state,
119 AltosGreatCircle from_receiver, Location receiver) {
124 if (state.apogee_voltage != AltosLib.MISSING)
125 last_apogee_good = tell_gonogo("apogee",
126 state.apogee_voltage >= AltosLib.ao_igniter_good,
128 last_tell_mode != TELL_MODE_PAD);
130 if (state.main_voltage != AltosLib.MISSING)
131 last_main_good = tell_gonogo("main",
132 state.main_voltage >= AltosLib.ao_igniter_good,
134 last_tell_mode != TELL_MODE_PAD);
136 if (state.gps != null)
137 last_gps_good = tell_gonogo("G P S",
140 last_tell_mode != TELL_MODE_PAD);
145 private boolean descending(int state) {
146 return AltosLib.ao_flight_drogue <= state && state <= AltosLib.ao_flight_landed;
149 private boolean target_moved(AltosState state) {
150 if (last_gps != null && state != null && state.gps != null) {
151 AltosGreatCircle moved = new AltosGreatCircle(last_gps.lat, last_gps.lon, last_gps.alt,
152 state.gps.lat, state.gps.lon, state.gps.alt);
153 double height_change = 0;
154 double height = state.height();
156 if (height != AltosLib.MISSING && last_height != AltosLib.MISSING)
157 height_change = Math.abs(last_height - height);
159 if (moved.range < 10 && height_change < 10)
165 private boolean receiver_moved(Location receiver) {
166 if (last_receiver != null && receiver != null) {
167 AltosGreatCircle moved = new AltosGreatCircle(last_receiver.getLatitude(),
168 last_receiver.getLongitude(),
169 last_receiver.getAltitude(),
170 receiver.getLatitude(),
171 receiver.getLongitude(),
172 receiver.getAltitude());
173 if (moved.range < 10)
179 private boolean tell_flight(TelemetryState telem_state, AltosState state,
180 AltosGreatCircle from_receiver, Location receiver) {
182 boolean spoken = false;
187 if (last_tell_mode != TELL_MODE_FLIGHT)
188 last_flight_tell = TELL_FLIGHT_NONE;
190 if (state.state() != last_state && AltosLib.ao_flight_boost <= state.state() && state.state() <= AltosLib.ao_flight_landed) {
191 speak(state.state_name());
192 if (descending(state.state()) && !descending(last_state)) {
193 if (state.max_height() != AltosLib.MISSING) {
194 speak("max height: %s.",
195 AltosConvert.height.say_units(state.max_height()));
198 last_flight_tell = TELL_FLIGHT_STATE;
202 if (last_tell_mode == TELL_MODE_FLIGHT && last_flight_tell == TELL_FLIGHT_TRACK) {
203 if (time_since_speak() < 10 * 1000)
205 if (!target_moved(state) && !receiver_moved(receiver))
212 if (last_flight_tell == TELL_FLIGHT_NONE || last_flight_tell == TELL_FLIGHT_STATE || last_flight_tell == TELL_FLIGHT_TRACK) {
213 last_flight_tell = TELL_FLIGHT_SPEED;
215 if (state.state() <= AltosLib.ao_flight_coast) {
216 speed = state.speed();
218 speed = state.gps_speed();
219 if (speed == AltosLib.MISSING)
220 speed = state.speed();
223 if (speed != AltosLib.MISSING) {
224 speak("speed: %s.", AltosConvert.speed.say_units(speed));
229 if (last_flight_tell == TELL_FLIGHT_SPEED) {
230 last_flight_tell = TELL_FLIGHT_HEIGHT;
231 height = state.height();
233 if (height != AltosLib.MISSING) {
234 speak("height: %s.", AltosConvert.height.say_units(height));
239 if (last_flight_tell == TELL_FLIGHT_HEIGHT) {
240 last_flight_tell = TELL_FLIGHT_TRACK;
241 if (from_receiver != null) {
242 speak("bearing %s %d, elevation %d, range %s.",
243 from_receiver.bearing_words(
244 AltosGreatCircle.BEARING_VOICE),
245 (int) (from_receiver.bearing + 0.5),
246 (int) (from_receiver.elevation + 0.5),
247 AltosConvert.distance.say(from_receiver.range));
255 private boolean tell_recover(TelemetryState telem_state, AltosState state,
256 AltosGreatCircle from_receiver, Location receiver) {
258 if (from_receiver == null)
261 if (last_tell_mode == TELL_MODE_RECOVER) {
262 if (!target_moved(state) && !receiver_moved(receiver))
264 if (time_since_speak() <= 10 * 1000)
268 String direction = AltosDroid.direction(from_receiver, receiver);
269 if (direction == null)
270 direction = String.format("Bearing %d", (int) (from_receiver.bearing + 0.5));
272 speak("%s, range %s.", direction,
273 AltosConvert.distance.say_units(from_receiver.distance));
278 public void tell(TelemetryState telem_state, AltosState state,
279 AltosGreatCircle from_receiver, Location receiver,
282 boolean spoken = false;
284 if (!tts_enabled) return;
286 if (is_speaking()) return;
288 int tell_serial = last_tell_serial;
291 tell_serial = state.serial;
293 if (tell_serial != last_tell_serial)
296 int tell_mode = TELL_MODE_NONE;
298 if (tab.tab_name().equals(AltosDroid.tab_pad_name))
299 tell_mode = TELL_MODE_PAD;
300 else if (tab.tab_name().equals(AltosDroid.tab_flight_name))
301 tell_mode = TELL_MODE_FLIGHT;
303 tell_mode = TELL_MODE_RECOVER;
305 if (tell_mode == TELL_MODE_PAD)
306 spoken = tell_pad(telem_state, state, from_receiver, receiver);
307 else if (tell_mode == TELL_MODE_FLIGHT)
308 spoken = tell_flight(telem_state, state, from_receiver, receiver);
310 spoken = tell_recover(telem_state, state, from_receiver, receiver);
313 last_tell_mode = tell_mode;
314 last_tell_serial = tell_serial;
316 last_state = state.state();
317 last_height = state.height();
318 if (state.gps != null)
319 last_gps = state.gps;
321 if (receiver != null)
322 last_receiver = receiver;