Ejemplo n.º 1
0
 /**
  * Default constructor
  *
  * <p>Instantiates a new recorder In case of errors, no exception is thrown, but the state is set
  * to ERROR
  */
 public WavAudioRecorder(int audioSource, int sampleRate, int channelConfig, int audioFormat) {
   try {
     if (audioFormat == AudioFormat.ENCODING_PCM_16BIT) {
       mBitsPersample = 16;
     } else {
       mBitsPersample = 8;
     }
     if (channelConfig == AudioFormat.CHANNEL_IN_MONO) {
       nChannels = 1;
     } else {
       nChannels = 2;
     }
     mAudioSource = audioSource;
     sRate = sampleRate;
     aFormat = audioFormat;
     mPeriodInFrames = sampleRate * TIMER_INTERVAL / 1000; // ?
     mBufferSize = mPeriodInFrames * 2 * nChannels * mBitsPersample / 8; // ?
     if (mBufferSize < AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat)) {
       // Check to make sure buffer size is not smaller than the
       // smallest allowed one
       mBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
       // Set frame period and timer interval accordingly
       mPeriodInFrames = mBufferSize / (2 * mBitsPersample * nChannels / 8);
       Log.w(
           WavAudioRecorder.class.getName(),
           "Increasing buffer size to " + Integer.toString(mBufferSize));
     }
     audioRecorder =
         new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, mBufferSize);
     if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
       throw new Exception("AudioRecord initialization failed");
     }
     audioRecorder.setRecordPositionUpdateListener(updateListener);
     audioRecorder.setPositionNotificationPeriod(mPeriodInFrames);
     filePath = null;
     state = State.INITIALIZING;
   } catch (Exception e) {
     if (e.getMessage() != null) {
       Log.e(WavAudioRecorder.class.getName(), e.getMessage());
     } else {
       Log.e(
           WavAudioRecorder.class.getName(), "Unknown error occured while initializing recording");
     }
     state = State.ERROR;
   }
 }
Ejemplo n.º 2
0
 /**
  * Resets the recorder to the INITIALIZING state, as if it was just created. In case the class was
  * in RECORDING state, the recording is stopped. In case of exceptions the class is set to the
  * ERROR state.
  */
 public void reset() {
   try {
     if (state != State.ERROR) {
       release();
       filePath = null; // Reset file path
       audioRecorder = new AudioRecord(mAudioSource, sRate, nChannels, aFormat, mBufferSize);
       if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
         throw new Exception("AudioRecord initialization failed");
       }
       audioRecorder.setRecordPositionUpdateListener(updateListener);
       audioRecorder.setPositionNotificationPeriod(mPeriodInFrames);
       state = State.INITIALIZING;
     }
   } catch (Exception e) {
     Log.e(WavAudioRecorder.class.getName(), e.getMessage());
     state = State.ERROR;
   }
 }
Ejemplo n.º 3
0
  /**
   * Default constructor
   *
   * <p>Instantiates a new recorder, in case of compressed recording the parameters can be left as
   * 0. In case of errors, no exception is thrown, but the state is set to ERROR
   */
  public ExtAudioRecorder(
      boolean uncompressed, int audioSource, int sampleRate, int channelConfig, int audioFormat) {
    try {
      rUncompressed = uncompressed;
      if (rUncompressed) { // RECORDING_UNCOMPRESSED
        if (audioFormat == AudioFormat.ENCODING_PCM_16BIT) {
          bSamples = 16;
        } else {
          bSamples = 8;
        }

        if (channelConfig == AudioFormat.CHANNEL_CONFIGURATION_MONO) {
          nChannels = 1;
        } else {
          nChannels = 2;
        }

        aSource = audioSource;
        sRate = sampleRate;
        aFormat = audioFormat;

        framePeriod = sampleRate * TIMER_INTERVAL / 1000;
        bufferSize = framePeriod * 2 * bSamples * nChannels / 8;
        if (bufferSize
            < AudioRecord.getMinBufferSize(
                sampleRate,
                channelConfig,
                audioFormat)) { // Check to make sure buffer size is not smaller than the smallest
                                // allowed one
          bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
          // Set frame period and timer interval accordingly
          framePeriod = bufferSize / (2 * bSamples * nChannels / 8);
          Log.w(
              ExtAudioRecorder.class.getName(),
              "Increasing buffer size to " + Integer.toString(bufferSize));
        }

        audioRecorder =
            new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSize);

        if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
          throw new Exception("AudioRecord initialization failed");
        audioRecorder.setRecordPositionUpdateListener(updateListener);
        audioRecorder.setPositionNotificationPeriod(framePeriod);
      } else { // RECORDING_COMPRESSED
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      }
      cAmplitude = 0;
      filePath = null;
      state = State.INITIALIZING;
    } catch (Exception e) {
      if (e.getMessage() != null) {
        Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
      } else {
        Log.e(
            ExtAudioRecorder.class.getName(), "Unknown error occured while initializing recording");
      }
      state = State.ERROR;
    }
  }
Ejemplo n.º 4
0
  public void testAudioRecordOP() throws Exception {
    if (!hasMicrophone()) {
      return;
    }
    final int SLEEP_TIME = 10;
    final int RECORD_TIME = 10000;
    assertEquals(AudioRecord.STATE_INITIALIZED, mAudioRecord.getState());

    int markerInFrames = mAudioRecord.getSampleRate() / 2;
    assertEquals(AudioRecord.SUCCESS, mAudioRecord.setNotificationMarkerPosition(markerInFrames));
    assertEquals(markerInFrames, mAudioRecord.getNotificationMarkerPosition());
    int periodInFrames = mAudioRecord.getSampleRate();
    assertEquals(AudioRecord.SUCCESS, mAudioRecord.setPositionNotificationPeriod(periodInFrames));
    assertEquals(periodInFrames, mAudioRecord.getPositionNotificationPeriod());
    OnRecordPositionUpdateListener listener =
        new OnRecordPositionUpdateListener() {

          public void onMarkerReached(AudioRecord recorder) {
            mIsOnMarkerReachedCalled = true;
          }

          public void onPeriodicNotification(AudioRecord recorder) {
            mIsOnPeriodicNotificationCalled = true;
          }
        };
    mAudioRecord.setRecordPositionUpdateListener(listener);

    // use byte array as buffer
    final int BUFFER_SIZE = 102400;
    byte[] byteData = new byte[BUFFER_SIZE];
    long time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(byteData, 0, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    reset();

    // use short array as buffer
    short[] shortData = new short[BUFFER_SIZE];
    time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(shortData, 0, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    reset();

    // use ByteBuffer as buffer
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
    time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(byteBuffer, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    reset();

    // use handler
    final Handler handler =
        new Handler(Looper.getMainLooper()) {
          @Override
          public void handleMessage(Message msg) {
            mIsHandleMessageCalled = true;
            super.handleMessage(msg);
          }
        };

    mAudioRecord.setRecordPositionUpdateListener(listener, handler);
    time = System.currentTimeMillis();
    mAudioRecord.startRecording();
    assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
    while (System.currentTimeMillis() - time < RECORD_TIME) {
      Thread.sleep(SLEEP_TIME);
      mAudioRecord.read(byteData, 0, BUFFER_SIZE);
    }
    mAudioRecord.stop();
    assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
    assertTrue(mIsOnMarkerReachedCalled);
    assertTrue(mIsOnPeriodicNotificationCalled);
    // The handler argument is only ever used for getting the associated Looper
    assertFalse(mIsHandleMessageCalled);

    mAudioRecord.release();
    assertEquals(AudioRecord.STATE_UNINITIALIZED, mAudioRecord.getState());
  }