title: Android active noise cancellation
body: <p>I'm working a somewhat ambitious project to get active noise-reduction achieved on Android with earbuds or headphones on.</p>
  
  <p>My objective is to record ambient noise with the android phone mic, invert the phase (a simple *-1 on the short-value pulled from the Audio Record?), and playback that inverted waveform through the headphones.  If the latency and amplitude are close to correct, it should nullify a good amount of mechanical structured noise in the environment.</p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>@Override
      public void run()
      {
          Log.i("Audio", "Running Audio Thread");
          AudioRecord recorder = null;
          AudioTrack track = null;
          short[][] buffers  = new short[256][160];
          int ix = 0;
  
      /*
       * Initialize buffer to hold continuously recorded audio data, start recording, and start
       * playback.
       */
          try
          {
              int N = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
              recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10);
              //NoiseSuppressor ns = NoiseSuppressor.create(recorder.getAudioSessionId());
              //ns.setEnabled(true);
  
              track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
                      AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);
              recorder.startRecording();
              track.play();
          /*
           * Loops until something outside of this thread stops it.
           * Reads the data from the recorder and writes it to the audio track for playback.
           */
              while(!stopped)
              {                    
                  short[] buffer = buffers[ix++ % buffers.length];
                  N = recorder.read(buffer,0,buffer.length);
  
                  for(int iii = 0;iii&lt;buffer.length;iii++){
                      //Log.i("Data","Value: "+buffer[iii]);
                      buffer[iii] = buffer[iii] *= -1;                        
                  }                    
                  track.write(buffer, 0, buffer.length);
              }
          }
          catch(Throwable x)
          {
              Log.w("Audio", "Error reading voice audio", x);
          }
      /*
       * Frees the thread's resources after the loop completes so that it can be run again
       */
          finally
          {
              recorder.stop();
              recorder.release();
              track.stop();
              track.release();
          }
      }
  </code></pre>
  
  <p>I was momentarily excited to find the Android API actually already has a NoiseSuppression algorithm (you'll see it commented out above).  I tested with it and found NoiseSuppressor wasn't doing much to null out constant tones which leads me to believe it's actually just performing a band-pass filter at non-vocal frequencies.</p>
  
  <p>So, my questions:</p>
  
  <p>1)  The above code takes about 250-500ms from mic record through playback in headphones.  This latency sucks and it would be great to reduce it.  Any suggestions there would be appreciated.</p>
  
  <p>2)  Regardless of how tight the latency is, my understanding is that the playback waveform WILL have phase offset from the actual ambient noise waveform.  This suggests I need to execute some kind of waveform matching to calculate this offset and compensate.  Thoughts on  how that gets calculated?</p>
  
  <p>3)  When it comes to compensating for latency, what would that look like?  I've got an array of shorts coming in every cycle, so what would a 30ms or 250ms latency look like?</p>
  
  <p>I'm aware of fundamental problems with this approach being that the location of the phone being not next to the head is likely to introduce some error, but I'm hopeful with some either dynamic or fixed latency correction it maybe be possible to overcome it.</p>
  
  <p>Thanks for any suggestions.</p>