Home | History | Annotate | Download | only in speechrecorder
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.speechrecorder;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.os.Handler;
     22 import android.speech.srec.Recognizer;
     23 import android.speech.srec.WaveHeader;
     24 import android.speech.srec.MicrophoneInputStream;
     25 import android.util.Log;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.widget.Button;
     29 import android.widget.RadioButton;
     30 import android.widget.TextView;
     31 import java.io.BufferedWriter;
     32 import java.io.ByteArrayOutputStream;
     33 import java.io.File;
     34 import java.io.FileFilter;
     35 import java.io.FileOutputStream;
     36 import java.io.FileWriter;
     37 import java.io.IOException;
     38 import java.io.InputStream;
     39 import java.io.OutputStream;
     40 
     41 public class SpeechRecorderActivity extends Activity {
     42     private static final String TAG = "SpeechRecorderActivity";
     43 
     44     private static final int DURATION_SEC = 7;
     45 
     46     private Handler mHandler;
     47 
     48     private TextView mCommand;
     49     private TextView mStatus;
     50     private Button mRecord;
     51     private Button mRedo;
     52     private RadioButton m8KHz;
     53     private RadioButton m11KHz;
     54     private RadioButton mCall;
     55     private RadioButton mDialNanp;
     56     private RadioButton mDialPairs;
     57 
     58     private InputStream mMicrophone;
     59     private ByteArrayOutputStream mBaos;
     60 
     61     private File mUtterance;
     62     private int mSampleRate;
     63     private Thread mThread;
     64     private boolean mStoppedListening;
     65 
     66     @Override
     67     protected void onCreate(Bundle icicle) {
     68         super.onCreate(icicle);
     69 
     70         mHandler = new Handler();
     71 
     72         setContentView(R.layout.recorder);
     73         mCommand = (TextView) findViewById(R.id.commandText);
     74         mStatus = (TextView) findViewById(R.id.statusText);
     75         mRecord = (Button) findViewById(R.id.recordButton);
     76         mRedo = (Button) findViewById(R.id.redoButton);
     77         m8KHz = (RadioButton)findViewById(R.id.codec8KHzRadioButton);
     78         m11KHz = (RadioButton)findViewById(R.id.codec11KHzRadioButton);
     79         mCall = (RadioButton)findViewById(R.id.callRadioButton);
     80         mDialNanp = (RadioButton)findViewById(R.id.dialNanpRadioButton);
     81         mDialPairs = (RadioButton)findViewById(R.id.dialPairsRadioButton);
     82 
     83         mCommand.setText("Please click 'Record' to begin");
     84         mRecord.setOnClickListener(new OnClickListener() {
     85             public void onClick(View v) {
     86                 if (false) {
     87                     Log.d(TAG, "mRecord.OnClickListener.onClick");
     88                 }
     89 
     90                 setupRecording();
     91             }
     92         });
     93 
     94         mRedo.setEnabled(false);
     95         mRedo.setOnClickListener(new OnClickListener() {
     96             public void onClick(View v) {
     97                 if (false) {
     98                     Log.d(TAG, "mRedo.onClickListener.onClick");
     99                 }
    100 
    101                 mUtterance.delete();
    102 
    103                 setupRecording();
    104             }
    105         });
    106 
    107         m8KHz.setText("PCM/16bit/8KHz");
    108         m11KHz.setText("PCM/16bit/11KHz");
    109         m11KHz.setChecked(true);
    110         mCall.setChecked(true);
    111     }
    112 
    113     private void setupRecording() {
    114         Log.d(TAG, "setupRecording");
    115         // disable buttons
    116         mRedo.setEnabled(false);
    117         mRecord.setEnabled(false);
    118         m8KHz.setFocusable(false);
    119         m11KHz.setFocusable(false);
    120         mCall.setFocusable(false);
    121         mDialNanp.setFocusable(false);
    122         mDialPairs.setFocusable(false);
    123 
    124         // find the first utterance not covered
    125         String[] utterances = mCall.isChecked() ? mCallUtterances :
    126             mDialNanp.isChecked() ? mDialNanpUtterances :
    127             mDialPairs.isChecked() ? mDialPairsUtterances :
    128                 null;
    129         mUtterance = null;
    130         int index = -1;
    131         for (int i = 0; i < utterances.length; i++) {
    132             File u = new File(getDir("recordings", MODE_PRIVATE),
    133                     utterances[i].toLowerCase().replace(' ', '_') + ".wav");
    134             if (!u.exists()) {
    135                 mUtterance = u;
    136                 index = i;
    137                 break;
    138             }
    139         }
    140 
    141         // check if done
    142         if (mUtterance == null) {
    143             mCommand.setText("Finished: Thank You!");
    144             return;
    145         }
    146         Log.d(TAG, "going to record " + mUtterance.toString());
    147 
    148         // fix up UI
    149         mCommand.setText("Say: \"" + utterances[index] + "\"");
    150         final String status = "item " + (index + 1) + "/" + utterances.length;
    151 
    152         // start the microphone
    153         mSampleRate = m8KHz.isChecked()? 8000 :
    154                 m11KHz.isChecked() ? 11025 :
    155                 11025;
    156         mBaos = new ByteArrayOutputStream(mSampleRate * 2 * 20);
    157         try {
    158             mMicrophone = new MicrophoneInputStream(mSampleRate, mSampleRate * 15);
    159 
    160 //            mMicrophone = logInputStream(mUtterance.toString(), mMicrophone, mSampleRate);
    161         } catch (IOException e) {
    162 
    163         }
    164 
    165         // post a number of delayed events to update the UI and to stop recording
    166         // after a few seconds.
    167         for (int i = 0; i <= DURATION_SEC; i++) {
    168             final int remain = DURATION_SEC - i;
    169             mHandler.postDelayed(new Runnable() {
    170                 public void run() {
    171                     if (remain > 0) {
    172                         mStatus.setText(status + "  Recording... " + remain);
    173                     }
    174                     else {
    175                         mStatus.setText(status);
    176                         stopRecording();
    177                     }
    178                 }
    179             }, i * 1000);
    180         }
    181 
    182         // now start a thread to store the audio.
    183         mStoppedListening = false;
    184         mThread = new Thread() {
    185             public void run() {
    186                 Log.d(TAG, "run audio capture thread");
    187                 byte buffer[] = new byte[512];
    188                 while (!mStoppedListening) {
    189                     try {
    190                         int rtn = 0;
    191                         rtn = mMicrophone.read(buffer, 0, 512);
    192                         if (rtn > 0) mBaos.write(buffer, 0, rtn);
    193                     } catch (IOException e) {
    194                     }
    195                 }
    196             }
    197         };
    198         mThread.start();
    199 
    200         // to avoid the button click
    201         try {
    202             Thread.sleep(100);
    203         } catch (InterruptedException ie) {
    204         }
    205 
    206     }
    207 
    208     private void stopRecording() {
    209         Log.d(TAG, "stopRecording");
    210         mStoppedListening = true;
    211         try {
    212             mThread.join();
    213         } catch (InterruptedException e) {
    214 
    215         }
    216         try {
    217             OutputStream out = new FileOutputStream(mUtterance.toString());
    218             try {
    219                 byte[] pcm = mBaos.toByteArray();
    220                 Log.d(TAG, "byteArray length " + pcm.length);
    221                 WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,
    222                         (short)1, mSampleRate, (short)16, pcm.length);
    223                 hdr.write(out);
    224                 out.write(pcm);
    225             } finally {
    226                 out.close();
    227                 mMicrophone.close();
    228                 mBaos.close();
    229             }
    230         } catch (IOException e) {
    231 
    232 
    233         } finally {
    234         }
    235 
    236         // stop the recording
    237         mRecord.setEnabled(true);
    238 
    239         mRedo.setEnabled(true);
    240 
    241         mCommand.setText("Got it!");
    242     }
    243 
    244 
    245     private final static String[] mCallUtterances = new String[] {
    246         "Call Adam Varro",
    247         "Call Alex Lloyd",
    248         "Call Amod Karve",
    249         "Call Ana Maria Lopez",
    250         "Call Ben Sigelman",
    251         "Call Chris Vennard",
    252         "Call Dana Pogoda",
    253         "Call Daryl Pregibon",
    254         "Call Davi Robison",
    255         "Call David Barrett Kahn",
    256         "Call David Hyman",
    257         "Call Douglas Gordin",
    258         "Call Gregor Rothfuss",
    259         "Call James Sheridan",
    260         "Call Jason Charo",
    261         "Call Jeff Reynar",
    262         "Call Joel Ward",
    263         "Call John Milton",
    264         "Call Lajos Nagy",
    265         "Call Lori Sobel",
    266         "Call Martin Jansche",
    267         "Call Meghan McGarry",
    268         "Call Meghan Shakar",
    269         "Call Nilka Thomas",
    270         "Call Pedro Colijn",
    271         "Call Pramod Adiddam",
    272         "Call Rajeev Sivaram",
    273         "Call Rich Armstrong",
    274         "Call Robin Watson",
    275         "Call Sam Morales",
    276     };
    277 
    278     private final static String[] mDialPairsUtterances = new String[] {
    279         // all possible pairs
    280         "Dial 000 000 0000",
    281 
    282         "Dial 101 010 1010",
    283         "Dial 111 111 1111",
    284 
    285         "Dial 202 020 2020",
    286         "Dial 212 121 2121",
    287         "Dial 222 222 2222",
    288 
    289         "Dial 303 030 3030",
    290         "Dial 313 131 3131",
    291         "Dial 323 232 3232",
    292         "Dial 333 333 3333",
    293 
    294         "Dial 404 040 4040",
    295         "Dial 414 141 4141",
    296         "Dial 424 242 4242",
    297         "Dial 434 343 4343",
    298         "Dial 444 444 4444",
    299 
    300         "Dial 505 050 5050",
    301         "Dial 515 151 5151",
    302         "Dial 525 252 5252",
    303         "Dial 535 353 5353",
    304         "Dial 545 454 5454",
    305         "Dial 555 555 5555",
    306 
    307         "Dial 606 060 6060",
    308         "Dial 616 161 6161",
    309         "Dial 626 262 6262",
    310         "Dial 636 363 6363",
    311         "Dial 646 464 6464",
    312         "Dial 656 565 6565",
    313         "Dial 666 666 6666",
    314 
    315         "Dial 707 070 7070",
    316         "Dial 717 171 7171",
    317         "Dial 727 272 7272",
    318         "Dial 737 373 7373",
    319         "Dial 747 474 7474",
    320         "Dial 757 575 7575",
    321         "Dial 767 676 7676",
    322         "Dial 777 777 7777",
    323 
    324         "Dial 808 080 8080",
    325         "Dial 818 181 8181",
    326         "Dial 828 282 8282",
    327         "Dial 838 383 8383",
    328         "Dial 848 484 8484",
    329         "Dial 858 585 8585",
    330         "Dial 868 686 8686",
    331         "Dial 878 787 8787",
    332         "Dial 888 888 8888",
    333 
    334         "Dial 909 090 9090",
    335         "Dial 919 191 9191",
    336         "Dial 929 292 9292",
    337         "Dial 939 393 9393",
    338         "Dial 949 494 9494",
    339         "Dial 959 595 9595",
    340         "Dial 969 696 9696",
    341         "Dial 979 797 9797",
    342         "Dial 989 898 9898",
    343         "Dial 999 999 9999",
    344 
    345     };
    346 
    347 
    348     private final static String[] mDialNanpUtterances = new String[] {
    349         "Dial 211",
    350         "Dial 411",
    351         "Dial 511",
    352         "Dial 811",
    353         "Dial 911",
    354         // random numbers
    355         "Dial 653 5763",
    356         "Dial 263 9072",
    357         "Dial 202 9781",
    358         "Dial 379 8229",
    359         "Dial 874 9139",
    360         "Dial 236 0163",
    361         "Dial 656 7455",
    362         "Dial 474 5254",
    363         "Dial 348 8687",
    364         "Dial 629 8602",
    365 
    366         //"Dial 272 717 8405",
    367         //"Dial 949 516 0162",
    368         //"Dial 795 117 7190",
    369         //"Dial 493 656 3767",
    370         //"Dial 588 093 9218",
    371         "Dial 511 658 3690",
    372         "Dial 440 301 8489",
    373         "Dial 695 713 6744",
    374         "Dial 581 475 8712",
    375         "Dial 981 388 3579",
    376 
    377         "Dial 840 683 3346",
    378         "Dial 303 467 7988",
    379         "Dial 649 504 5290",
    380         "Dial 184 577 4229",
    381         "Dial 212 286 3982",
    382         "Dial 646 258 0115",
    383         "Dial 427 482 6852",
    384         "Dial 231 809 9260",
    385         "Dial 681 930 4301",
    386         "Dial 246 650 8339",
    387     };
    388 }
    389