Example #1
0
 // data from mic
 protected short[] effectIn(short[] in) {
   double energy = 0;
   for (int i = 0; i < in.length; i++) {
     in[i] = (short) (in[i] * _gain);
     energy = energy + (Math.abs(in[i]));
   }
   _inEnergy = energy / in.length;
   return in;
 }
Example #2
0
  protected synchronized boolean initMic(int sampleRate, int bytesPerFrame) {
    boolean isOK = true;
    if (_mic == null) {
      // Will this work? In the emulator only 8k works!
      int micBufferSizeBytes =
          AudioRecord.getMinBufferSize(
              sampleRate, AudioFormat.CHANNEL_IN_MONO, AndroidAudio.ENCODING_FORMAT);

      Log.debug(
          this.getClass().getSimpleName()
              + ".init(): minMicBufferSizeBytes (1)="
              + micBufferSizeBytes);

      if (micBufferSizeBytes < bytesPerFrame) {
        micBufferSizeBytes = bytesPerFrame;
      }

      int mdeep = 2 + (micBufferSizeBytes / bytesPerFrame);

      mdeep = Math.max(mdeep, 4);

      int recBuffSizeBytes = bytesPerFrame * mdeep;
      _micFramebuff = new short[bytesPerFrame / 2];

      Log.info(
          this.getClass().getSimpleName()
              + ".initMic(): sampleRate="
              + sampleRate
              + ", bytesPerFrame="
              + bytesPerFrame
              + ", recBuffsz="
              + recBuffSizeBytes
              + ", ");
      try {
        /*
         * make sure you add to AndroidManifest.xml:
         *
         * <uses-permission
         * android:name="android.permission.RECORD_AUDIO"/>
         */
        /*
         * TODO, only read now that
         * 44100Hz is currently the only rate that is guaranteed to work on all devices!
         * I set it to 8000K!
         * Need to check this!
         */
        // try and take advantage of an echo can if there is one.
        int micdevice = (android.os.Build.VERSION.SDK_INT >= 11) ? 7 : AudioSource.MIC;
        _mic =
            new AudioRecord(
                micdevice,
                sampleRate,
                AudioFormat.CHANNEL_IN_MONO,
                AndroidAudio.ENCODING_FORMAT,
                recBuffSizeBytes);
        if (_mic.getState() != AudioRecord.STATE_INITIALIZED) {
          isOK = false;
        }
      } catch (Throwable exc) {
        Log.error(exc.toString());
        isOK = false;
      }
    }
    return isOK;
  }