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