diff src/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 4:46a9cdf018a1

allow async tts
author Carl Byington <carl@five-ten-sg.com>
date Mon, 23 Jun 2014 17:09:52 -0700
parents 2be5bca648ab
children d1f095e4f8f0
line wrap: on
line diff
--- a/src/com/five_ten_sg/connectbot/monitor/MonitorService.java	Mon Jun 23 17:05:16 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/monitor/MonitorService.java	Mon Jun 23 17:09:52 2014 -0700
@@ -171,6 +171,36 @@
             }
         }
 
+        public void speak(byte [] msg, boolean flush, boolean synchronous) {
+            if (speech) {
+                String smsg = bytesToString(msg);
+                if (synchronous) {
+                    HashMap<String, String> myHashParms = new HashMap();
+                    myHashParms.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.format("connection %d", connection));
+                	talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, myHashParms);
+                    try {
+                        String x = talkerQueue.take(); // wait for completion
+                    } catch (InterruptedException e) {
+                        Log.e(TAG, "exception in cm.speak()", e);
+                    }
+                }
+                else {
+            	    talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null);
+                }
+            }
+        }
+
+        public String bytesToString(byte[] b) {
+            char[] c = new char[b.length];
+            int bp = 0;
+            for(int i = 0; i < c.length; i++) {
+                byte b1 = 0;
+                byte b2 = b[bp++];
+                c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF));
+            }
+            return new String(c);
+        }
+
         public char[] bytesToChars(byte[] b, int len) {
             char[] c = new char[len >> 1];
             int bp = 0;
@@ -381,8 +411,9 @@
         }
     }
 
-    public static void teSpeak(int connection, String msg, boolean flush) {
-        if (speech) talker.speak(msg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null);
+    public static void teSpeak(int connection, String msg, boolean flush, boolean synchronous) {
+        CommunicationThread cm = clients.get(connection);
+        if (cm != null) cm.speak(msg, flush, synchronous);
     }
 
     public static void teDepress(int connection, int vk_key) {