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.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         // for text_message
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         // for raw_sound
00060         private Publisher<audio_common_msgs.AudioData> audio_pub;
00061         private audio_common_msgs.AudioData audio_msg;
00062         final static int SAMPLING_RATE = 11025; // 8000,11025,16000,22050,44100
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                  * List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
00091                  * 
00092                  * for (Sensor sensor : sensors) { int sensorType = sensor.getType();
00093                  * switch (sensorType) { case Sensor.TYPE_MAGNETIC_FIELD:
00094                  * mSensorManager.registerListener(this, sensor,
00095                  * SensorManager.SENSOR_DELAY_NORMAL); } }
00096                  */
00097 
00098                 // for raw_audio
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                 // AudioRecordの作成
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                 // Record
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                 // 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 onSensorChanged(SensorEvent event) {
00189 
00190                 // if you want eternal working
00191 
00192                 /*
00193                  * if(busyFlag == 0){ busyFlag--; receiveFlag = 0;
00194                  * Log.v("voice","sensor change"); Intent intent = new
00195                  * Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
00196                  * intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
00197                  * RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
00198                  * intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
00199                  * package_name); sr.startListening(intent); }
00200                  * 
00201                  * else if(busyFlag < 0){ busyFlag--; if(busyFlag<-10 && receiveFlag
00202                  * ==0) busyFlag = 0; } else if(busyFlag > 0){ if(busyFlag == 2)
00203                  * sr.stopListening(); Log.v("voice","stoped"); busyFlag--; }
00204                  */
00205 
00206                 /*
00207                  * switch(event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER:
00208                  * imu_msg.getLinearAcceleration().setX(event.values[0]);
00209                  * imu_msg.getLinearAcceleration().setY(event.values[1]);
00210                  * imu_msg.getLinearAcceleration().setZ(event.values[2]);
00211                  * imu_msg.getHeader().setFrameId("/imu"); imu_pub.publish(imu_msg);
00212                  * Log.v("ok","ok"); break; }
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         /* inner class */
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                         // TODO Auto-generated method stub
00246                         receiveFlag++;
00247                 }
00248 
00249                 @Override
00250                 public void onEndOfSpeech() {
00251                         imageView.setImageResource(R.drawable.mike);
00252                         busyFlag = 2;
00253 
00254                         // TODO Auto-generated method stub
00255                         /*
00256                          * try{ Thread.sleep(1000); sr.stopListening(); Thread.sleep(1000);
00257                          * 
00258                          * } catch(Exception e){ }
00259                          * 
00260                          * Intent intent = new
00261                          * Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
00262                          * intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
00263                          * RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
00264                          * intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
00265                          * package_name); Log.v("voice", "restart!");
00266                          * sr.startListening(intent);
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                                 // Android端末内のエラー(その他)
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                                 // RecognitionServiceへ要求出せず
00301                                 Log.e("voice", "not request");
00302                                 break;
00303                         case SpeechRecognizer.ERROR_SERVER:
00304                                 // Server側からエラー通知
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                          * try{ Thread.sleep(1000); sr.stopListening(); Thread.sleep(1000);
00316                          * 
00317                          * } catch(Exception e){ }
00318                          */
00319 
00320                         // TODO Auto-generated method stub
00321 
00322                 }
00323 
00324                 @Override
00325                 public void onEvent(int eventType, Bundle params) {
00326                         // TODO Auto-generated method stub
00327 
00328                 }
00329 
00330                 @Override
00331                 public void onPartialResults(Bundle partialResults) {
00332                         // TODO Auto-generated method stub
00333 
00334                 }
00335 
00336                 @Override
00337                 public void onReadyForSpeech(Bundle params) {
00338                         Log.v("voice", "ready!");
00339 
00340                         // TODO Auto-generated method stub
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                         // TODO Auto-generated method stub
00370 
00371                 }
00372 
00373         }
00374 
00375 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


android_voice_message
Author(s): Damon Kohler
autogenerated on Sun Mar 24 2013 07:29:33