Advertisement
saurabhmesh17

AudioRecord/AudioTrack-UsingBuffer

Aug 27th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. /** Record and Playback using a single buffer, running in a thread
  2. Code written on : Aug 27th 2014
  3. **/
  4.  
  5.  
  6. public class BufferMain extends ActionBarActivity
  7. {
  8.     private String TAG = "AUDIO_RECORD_PLAYBACK_SAURABH_BUFFER";
  9.     private boolean isRunning = true;
  10.    
  11.     private Thread m_thread;
  12.  
  13.     private AudioRecord recorder = null;
  14.     private AudioTrack track = null;
  15.  
  16.     byte buffer[] = new byte[640];
  17.     int bufferSize;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_buffer_main);
  23.  
  24.         enableButton(R.id.StartButton,true);
  25.         enableButton(R.id.StopButton,false);
  26.  
  27.         /* Assign Button Click Handlers */
  28.         ((Button)findViewById(R.id.StartButton)).setOnClickListener(btnClick);
  29.         ((Button)findViewById(R.id.StopButton)).setOnClickListener(btnClick);
  30.  
  31.         /*if (savedInstanceState == null) {
  32.             getSupportFragmentManager().beginTransaction()
  33.                     .add(R.id.container, new PlaceholderFragment()).commit();
  34.         }*/
  35.         Log.d(TAG, "\n\n\n\n\n\n============================= Starting Application.. =====================================");
  36.     }
  37.  
  38.     private View.OnClickListener btnClick = new View.OnClickListener() {
  39.         @Override
  40.         public void onClick(View v) {
  41.             switch(v.getId()){
  42.             case R.id.StartButton:
  43.             {
  44.                 Log.d(TAG, "==========================================Start Recording==========================================");
  45.                 isRunning = true;
  46.                 Log.d(TAG, "========== isRunning = true =============");
  47.                 do_loopback(isRunning);
  48.                 enableButton(R.id.StartButton,false);
  49.                 enableButton(R.id.StopButton,true);
  50.                 break;
  51.             }
  52.             case R.id.StopButton:
  53.             {
  54.                 Log.d(TAG, "==========================================Stop Recording==========================================");
  55.                 isRunning = false;
  56.                 Log.d(TAG, "========== isRunning = false =============");
  57.                 do_loopback(isRunning);
  58.                 enableButton(R.id.StopButton,false);
  59.                 enableButton(R.id.StartButton,true);
  60.                 break;
  61.             }
  62.             }
  63.         }
  64.     };
  65. public void run_loop (boolean isRunning)
  66.     {
  67.         int readbytes, writebytes, state;
  68.         Log.d(TAG, "===== Entering run Loop ===== ");
  69.         bufferSize = 640;
  70.  
  71.         recorder = findAudioRecord(recorder);
  72.         if (recorder == null) {
  73.             Log.e(TAG, "=============================== findAudioRecord : Returned Error! =============================== ");
  74.             return;
  75.         }
  76.        
  77.         track = findAudioTrack(track);
  78.         if (track == null) {
  79.             Log.e(TAG, "=============================== findAudioTrack : Returned Error! =============================== ");
  80.             return;
  81.         }
  82.  
  83.         if ((AudioRecord.STATE_INITIALIZED == recorder.getState()) && (AudioTrack.STATE_INITIALIZED == track.getState()))
  84.         {
  85.             recorder.startRecording();
  86.             Log.d(TAG, "========= recorder Started... =========");
  87.             track.play();
  88.             Log.d(TAG, "========= Track Started... =========");
  89.         } else {
  90.             Log.d(TAG, "========= Initilazation failed for AudioRecord or AudioTrack =========");
  91.             return;
  92.         }
  93.  
  94.         while (isRunning) {
  95.             readbytes = 0;                                              /* Reset read bytes for next Iteration */
  96.             writebytes = 0;                                             /* Reset write bytes for next Iteration */
  97.  
  98.             readbytes = recorder.read(buffer, 0, bufferSize);
  99.             if(-1 == checkAudioRecordforError(readbytes))               /* Error Checking Code for AudioRecord */
  100.             {
  101.                 Log.d(TAG, "========= Read Error =========");
  102.                 return;
  103.             }
  104.  
  105.             writebytes = track.write(buffer, 0, bufferSize);
  106.             if (-1 == checkAudioTrackforError(writebytes))              /* Error Checking Code for AudioRecord */
  107.             {
  108.                 Log.d(TAG, "========= Write Error =========");
  109.                 return;
  110.             }
  111.         }
  112.         Log.i(TAG, "loopback exit");
  113.     return ;
  114. }
  115.  
  116.  
  117.  
  118. public AudioTrack findAudioTrack (AudioTrack track)
  119.     {
  120.         Log.d(TAG, "=============================== Initialising Playing API ===============================");
  121.        
  122.         bufferSize = AudioTrack.getMinBufferSize(8000,
  123.                 AudioFormat.CHANNEL_OUT_MONO,
  124.                 AudioFormat.ENCODING_PCM_16BIT);        /* Return 640 */
  125.  
  126.         /** Overriding bufferSize value of 640 with 320**/
  127.         Log.d(TAG, "========= BEFORE AudioTrack ==> bufferSize : "+bufferSize+"=========");
  128.         //bufferSize = 320;
  129.         //Log.d(TAG, "========= AudioTrack ==> bufferSize : "+bufferSize+"=========");
  130.  
  131.         if (bufferSize != AudioTrack.ERROR_BAD_VALUE)
  132.         {
  133.             track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
  134.                     AudioFormat.CHANNEL_OUT_MONO,
  135.                     AudioFormat.ENCODING_PCM_16BIT, bufferSize,
  136.                     AudioTrack.MODE_STREAM);
  137.  
  138.             int type = track.getStreamType();
  139.             Log.d(TAG, "========= AudioTrack ==> getStreamType : "+type+"====");
  140.  
  141.             if (track.getState() == AudioTrack.STATE_UNINITIALIZED) {
  142.                 Log.e(TAG, "=============================== AudioTrack UnInitilaised =============================== ");
  143.                 return null;
  144.             }
  145.         }
  146.         return track;
  147.     }
  148.  
  149.  
  150.     public AudioRecord findAudioRecord (AudioRecord recorder)
  151.     {
  152.         Log.d(TAG, "=============================== Initialising Record API ===============================");
  153.         bufferSize = AudioRecord.getMinBufferSize(8000,
  154.                 AudioFormat.CHANNEL_IN_MONO,
  155.                 AudioFormat.ENCODING_PCM_16BIT);
  156.  
  157.         /** Overriding bufferSize value of 640 with 320**/
  158.         //Log.d(TAG, "========= BEFORE AudioRecord ==> bufferSize : "+bufferSize+"=========");
  159.         //bufferSize = 320;
  160.         Log.d(TAG, "========= AudioRecord ==> bufferSize : "+bufferSize+"=========");
  161.  
  162.         if (bufferSize != AudioRecord.ERROR_BAD_VALUE)
  163.         {
  164.             recorder = new AudioRecord(AudioSource.DEFAULT, 8000,
  165.                     AudioFormat.CHANNEL_IN_MONO,
  166.                     AudioFormat.ENCODING_PCM_16BIT, bufferSize);
  167.  
  168.             if (recorder.getState() == AudioRecord.STATE_UNINITIALIZED) {
  169.                 Log.e(TAG, "=============================== AudioRecord UnInitilaised =============================== ");
  170.                 return null;
  171.             }
  172.         }
  173.         Log.d(TAG, "=============================== Initialising Record Completed ===============================");
  174.         return recorder;
  175.     }
  176.  
  177.     public int checkAudioRecordforError(int readbytes)
  178.     {
  179.         if (readbytes ==  recorder.ERROR_INVALID_OPERATION || readbytes ==  recorder.ERROR_BAD_VALUE)
  180.         {
  181.             if(readbytes == AudioRecord.ERROR_INVALID_OPERATION)
  182.                 Log.d(TAG, "========= read Error : ERROR_INVALID_OPERATION ===========");
  183.             else if (readbytes == AudioRecord.ERROR_BAD_VALUE)
  184.                 Log.d(TAG, "========= read Error : ERROR_BAD_VALUE ===========");
  185.             else if (readbytes == AudioRecord.ERROR)
  186.                 Log.d(TAG, "========= read Error : ERROR Unknown ===========");
  187.             return -1;
  188.         }
  189.         return readbytes;
  190.     }
  191.  
  192.     public int checkAudioTrackforError(int writebytes)
  193.     {
  194.         if (writebytes ==  track.ERROR_INVALID_OPERATION || writebytes ==  track.ERROR_BAD_VALUE)
  195.         {
  196.             if(writebytes == track.ERROR_INVALID_OPERATION)
  197.                 Log.d(TAG, "========= read Error : ERROR_INVALID_OPERATION ===========");
  198.             else if (writebytes == track.ERROR_BAD_VALUE)
  199.                 Log.d(TAG, "========= read Error : ERROR_BAD_VALUE ===========");
  200.             else if (writebytes == track.ERROR)
  201.                 Log.d(TAG, "========= read Error : ERROR Unknown ===========");
  202.             return -1;
  203.         }
  204.         return writebytes;
  205.     }
  206.  
  207.     private void do_loopback(final boolean flag) {
  208.         Log.d(TAG, "========== within do_loopback ============");
  209.         m_thread = new Thread(new Runnable() {
  210.             public void run() {
  211.                 run_loop(flag);
  212.             }
  213.         });
  214.         m_thread.start();
  215.     }
  216.  
  217.     /* Function to Enable/Disable Buttons */
  218.     private void enableButton(int id,boolean isEnable){
  219.         ((Button)findViewById(id)).setEnabled(isEnable);
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement