AndroidVoiceMessageNode.java
Go to the documentation of this file.
00001 package org.ros.android.androidVoiceMessage;
00002 
00003 import static org.jboss.netty.buffer.ChannelBuffers.LITTLE_ENDIAN;
00004 import static org.jboss.netty.buffer.ChannelBuffers.buffer;
00005 
00006 import java.io.File;
00007 import java.io.FileOutputStream;
00008 import java.util.ArrayList;
00009 import java.util.List;
00010 
00011 import org.jboss.netty.buffer.ChannelBuffer;
00012 import org.ros.namespace.GraphName;
00013 import org.ros.node.ConnectedNode;
00014 import org.ros.node.NodeMain;
00015 import org.ros.node.topic.Publisher;
00016 
00017 import android.content.Context;
00018 import android.content.Intent;
00019 import android.hardware.SensorEventListener;
00020 import android.hardware.SensorManager;
00021 import android.hardware.Sensor;
00022 import android.hardware.SensorEvent;
00023 
00024 import android.os.Bundle;
00025 import android.os.Environment;
00026 import android.os.Handler;
00027 import android.os.Looper;
00028 import android.speech.RecognitionListener;
00029 import android.speech.RecognizerIntent;
00030 import android.speech.SpeechRecognizer;
00031 import android.util.Log;
00032 import android.widget.Button;
00033 import android.widget.TextView;
00034 import android.widget.ImageView;
00035 import jsk_gui_msgs.VoiceMessage;
00036 import android.view.View;
00037 import android.media.AudioFormat;
00038 import android.media.AudioManager;
00039 import android.media.AudioRecord;
00040 import audio_common_msgs.AudioData;
00041 import android.media.MediaRecorder;
00042 
00043 import org.ros.node.*;
00044 
00045 public class AndroidVoiceMessageNode implements NodeMain,
00046                 VoiceNodeListenerInterface, Runnable {
00047 
00048         // for text_message
00049         private Publisher<jsk_gui_msgs.VoiceMessage> voice_pub;
00050         private jsk_gui_msgs.VoiceMessage voice_msg;
00051         private int startFlag = 0;
00052         private int not_working_flag = 0;
00053         private int mode = 1;
00054         private SpeechRecognizer sr;
00055         private String package_name;
00056         private TextView textView;
00057         private ImageView imageView;
00058         private AndroidVoiceMessageView androidVoiceMessageView;
00059         private SpeechListener sl;
00060         // for raw_sound
00061         private Publisher<audio_common_msgs.AudioData> audio_pub;
00062         private audio_common_msgs.AudioData audio_msg;
00063         final static int SAMPLING_RATE = 11025; // 8000,11025,16000,22050,44100
00064         AudioRecord audioRec = null;
00065         private int bufSize;
00066         private int audioFlag = 0;
00067         private Thread managerLoop;
00068         private Handler handler;
00069         private Context context;
00070 
00071         @Override
00072         public GraphName getDefaultNodeName() {
00073                 return GraphName.of("jsk_gui_msgs/VoiceMessage");
00074         }
00075 
00076         public AndroidVoiceMessageNode(SensorManager manager, Context context,
00077                         String package_name) {
00078                 this.context = context;
00079                 sr = SpeechRecognizer.createSpeechRecognizer(context);
00080                 sl = new SpeechListener();
00081                 sl.setListener(this);
00082                 sr.setRecognitionListener(sl);
00083                 this.package_name = package_name;
00084 
00085                 handler = new Handler();
00086 
00087                 managerLoop = new Thread(this);
00088                 managerLoop.start();
00089         }
00090 
00091         public void setView(TextView textView, ImageView imageView) {
00092                 this.textView = textView;
00093                 this.imageView = imageView;
00094         }
00095 
00096         @Override
00097         public void onStart(final ConnectedNode connectedNode) {
00098 
00099                 // for raw_audio
00100                 bufSize = AudioRecord.getMinBufferSize(SAMPLING_RATE,
00101                                 AudioFormat.CHANNEL_CONFIGURATION_MONO,
00102                                 AudioFormat.ENCODING_PCM_16BIT) * 2;
00103 
00104                 try {
00105                         voice_pub = connectedNode.newPublisher("Tablet/voice",
00106                                         "jsk_gui_msgs/VoiceMessage");
00107                         audio_pub = connectedNode.newPublisher("audio",
00108                                         "audio_common_msgs/AudioData");
00109                         startFlag = 1;
00110 
00111                 } catch (Exception e) {
00112 
00113                 }
00114 
00115                 // AudioRecord���������
00116                 audioRec = new AudioRecord(MediaRecorder.AudioSource.MIC,
00117                                 SAMPLING_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
00118                                 AudioFormat.ENCODING_PCM_16BIT, bufSize);
00119 
00120         }
00121 
00122         public void stopRawSound() {
00123                 audioFlag = 0;
00124         }
00125 
00126         public void publishRawSound(AndroidVoiceMessageView androidVoiceMessageView) {
00127                 this.androidVoiceMessageView = androidVoiceMessageView;
00128                 audioFlag = 1;
00129                 audioRec.startRecording();
00130                 // Record
00131                 new Thread(new Runnable() {
00132                         @Override
00133                         public void run() {
00134 
00135                                 try {
00136 
00137                                         byte buf[] = new byte[bufSize];
00138 
00139                                         while (audioFlag == 1) {
00140 
00141                                                 int returnSize = audioRec.read(buf, 0, buf.length);
00142 
00143                                                 audio_msg = audio_pub.newMessage();
00144                                                 ChannelBuffer heapBuffer = buffer(LITTLE_ENDIAN,
00145                                                                 bufSize);
00146                                                 heapBuffer.writeBytes(buf);
00147                                                 audio_msg.setData(heapBuffer);
00148                                                 audio_pub.publish(audio_msg);
00149 
00150                                                 double volume = 0;
00151                                                 for (int i = 0; i < buf.length; i++) {
00152                                                         volume += Math.abs(buf[i]);
00153                                                 }
00154                                                 volume /= buf.length;
00155                                                 addValue((float) volume);
00156                                                 Log.v("buf-size", "" + volume);
00157 
00158                                         }
00159                                         Log.v("AudioRecord", "stop");
00160                                         audioRec.stop();
00161 
00162                                 } catch (Exception e) {
00163 
00164                                 }
00165                         }
00166                 }).start();
00167 
00168         }
00169 
00170         private void addValue(float volume) {
00171                 androidVoiceMessageView.addValues(volume);
00172         }
00173 
00174         @Override
00175         public void onShutdown(Node node) {
00176                 // sr.destroy();
00177                 // voice_pub.shutdown();
00178         }
00179 
00180         @Override
00181         public void onShutdownComplete(Node node) {
00182         }
00183 
00184         @Override
00185         public void onError(Node node, Throwable throwable) {
00186         }
00187 
00188         public void setMode(int mode) {
00189                 this.mode = mode;
00190                 if (mode == -1) {
00191                         setFlag(-1);
00192                 } else {
00193                         sr.stopListening();
00194                 }
00195         }
00196 
00197         public void setFlag(int type) {
00198 
00199                 if (startFlag == 1 && type == mode) {
00200                         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
00201                         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
00202                                         RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
00203                         intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
00204                                         package_name);
00205                         Log.v("voice", "button pushed!");
00206                         sr.startListening(intent);
00207                         imageView.setImageResource(R.drawable.mike2);
00208                 }
00209         }
00210 
00211         /* inner class */
00212         public class SpeechListener implements RecognitionListener {
00213 
00214                 private VoiceNodeListenerInterface listener;
00215 
00216                 public void setListener(VoiceNodeListenerInterface listener) {
00217                         this.listener = listener;
00218                 }
00219 
00220                 @Override
00221                 public void onBeginningOfSpeech() {
00222                         imageView.setImageResource(R.drawable.mike3);
00223                 }
00224 
00225                 @Override
00226                 public void onBufferReceived(byte[] buffer) {
00227                         Log.e("voice", "buffer received");
00228                         // TODO Auto-generated method stub
00229                 }
00230 
00231                 @Override
00232                 public void onEndOfSpeech() {
00233                         Log.e("voice", "speech end");
00234                         imageView.setImageResource(R.drawable.mike);
00235                 }
00236 
00237                 @Override
00238                 public void onError(int error) {
00239                         imageView.setImageResource(R.drawable.mike);
00240 
00241                         switch (error) {
00242                         case SpeechRecognizer.ERROR_AUDIO:
00243                                 Log.e("voice", "save error");
00244                                 break;
00245                         case SpeechRecognizer.ERROR_CLIENT:
00246                                 Log.e("voice", "device error");
00247                                 break;
00248                         case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
00249                                 Log.e("voice", "nothing of right");
00250                                 break;
00251                         case SpeechRecognizer.ERROR_NETWORK:
00252                                 Log.e("voice", "network error");
00253                                 break;
00254                         case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
00255                                 Log.e("voice", "network timeout");
00256                                 break;
00257                         case SpeechRecognizer.ERROR_NO_MATCH:
00258                                 Log.e("voice", "nothing recognition");
00259                                 not_working_flag = 0;
00260                                 break;
00261                         case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
00262                                 Log.e("voice", "not request");
00263                                 if (mode == 1) {
00264                                         restart();
00265                                 }
00266                                 break;
00267                         case SpeechRecognizer.ERROR_SERVER:
00268                                 Log.e("voice", "from server");
00269                                 break;
00270                         case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
00271                                 Log.e("voice", "no input");
00272                                 not_working_flag = 0;
00273                                 break;
00274                         default:
00275                         }
00276                         listener.end();
00277                 }
00278 
00279                 @Override
00280                 public void onEvent(int eventType, Bundle params) {
00281                         Log.e("voice", "event");
00282                         // TODO Auto-generated method stub
00283 
00284                 }
00285 
00286                 @Override
00287                 public void onPartialResults(Bundle partialResults) {
00288                         // TODO Auto-generated method stub
00289 
00290                 }
00291 
00292                 @Override
00293                 public void onReadyForSpeech(Bundle params) {
00294                         Log.v("voice", "ready!");
00295                         // TODO Auto-generated method stub
00296                 }
00297 
00298                 @Override
00299                 public void onResults(Bundle results) {
00300                         Log.v("voice", "result");
00301                         voice_msg = voice_pub.newMessage();
00302                         ArrayList<String> candidates = results
00303                                         .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
00304                         voice_msg.setTexts(candidates);
00305                         textView.setText("recognized text candidates:\n");
00306                         for (int i = 0; i < candidates.size(); i++) {
00307                                 textView.setText(textView.getText() + "" + (i + 1) + ") "
00308                                                 + candidates.get(i) + "\n");
00309                         }
00310                         Log.v("voice", "publish ready");
00311                         voice_msg.setTexts(candidates);
00312                         Log.v("test", "publish ready");
00313                         if (startFlag == 1) {
00314                                 voice_pub.publish(voice_msg);
00315                                 Log.v("voice", "publish ok");
00316                         }
00317                         not_working_flag = 0;
00318                         listener.end();
00319                 }
00320 
00321                 @Override
00322                 public void onRmsChanged(float rmsdB) {
00323                 }
00324 
00325         }
00326 
00327         @Override
00328         public void end() {
00329                 Log.e("voice", "end");
00330                 setFlag(-1);
00331         }
00332 
00333         public void restart() {
00334                 handler.post(new Runnable() {
00335 
00336                         @Override
00337                         public void run() {
00338 
00339                                         sr.stopListening();
00340                                         sr.destroy();
00341 
00342                                         sr = null;
00343                                         sl = null;
00344 
00345                                         try {
00346                                                 Thread.sleep(1000);
00347                                         } catch (InterruptedException e) {
00348                                                 e.printStackTrace();
00349                                         }
00350 
00351                                         sr = SpeechRecognizer.createSpeechRecognizer(context);
00352                                         sl = new SpeechListener();
00353                                         sl.setListener(AndroidVoiceMessageNode.this);
00354                                         try {
00355                                                 Thread.sleep(1000);
00356                                         } catch (InterruptedException e) {
00357                                                 e.printStackTrace();
00358                                         }
00359                                         sr.setRecognitionListener(sl);
00360                                         
00361 
00362                                         if (mode == -1) {
00363                                                 setFlag(-1);
00364                                                 
00365                                         }
00366                         }
00367 
00368                 });
00369                 not_working_flag = 0;
00370         }
00371 
00372         @Override
00373         public void run() {
00374                 while (true) {
00375                         if (startFlag == 1 && mode == -1) {
00376                                 not_working_flag++;
00377                                 if (not_working_flag > 2) {
00378                                         Log.v("voice", "restart");
00379                                         restart();
00380                                 } else
00381                                         Log.v("voice", "no problem");
00382                         } else {
00383                                 not_working_flag = 0;
00384                         }
00385                         try {
00386                                 Thread.sleep(5000);
00387                         } catch (InterruptedException e) {
00388                                 e.printStackTrace();
00389                         }
00390                 }
00391 
00392         }
00393 
00394 }


jsk_android_apps
Author(s): Kazuto Murase
autogenerated on Thu Jun 6 2019 18:03:46