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