Wednesday, 3 October 2018

Audio Streaming and getting the Amplitude to draw Waveform same time

I am trying to stream and getting amplitude to draw waveform. When I stream and comment the code to get amplitude its works fine. I guess the problem is that audioRecord.read() which is called by streaming as well as amplitude method separately. Once it is called in read() method and next in getAmplitude() method. I looking for a way in which both stream and amplitude work together. With existed code there is a skipping in streaming. Below is the code:

public void start() {
  if (isCreated()) {
    init();
    new Thread(new Runnable() {
      @Override
      public void run() {
        while (running && !Thread.interrupted()) {
          DataTaken dataTaken = read();
          if (dataTaken != null) {
            Log.e("amplitude ",getAmplitude()+"");
            amplitude = getAmplitude();
            getMicrophoneData.inputPCMData(dataTaken.getPcmBuffer(), dataTaken.getSize());
          } else {
            running = false;
          }
        }
      }
    }).start();
  } else {
    Log.e(TAG, "Microphone no created, MicrophoneManager not enabled");
  }
} 

  private DataTaken read() {
    int size;
    if (muted) {
      size = audioRecord.read(pcmBufferMuted, 0, pcmBufferMuted.length);
    } else {
      size = audioRecord.read(pcmBuffer, 0, pcmBuffer.length);
    }
    if (size <= 0) {
      return null;
    }
    return new DataTaken(pcmBuffer, size);
}

public double getAmplitude() {
  if(audioRecord != null) {
    short[] buffer = new short[getPcmBufferSize()];
    audioRecord.read(buffer, 0, getPcmBufferSize());
    int max = 0;
    for (short s : buffer) {
      if (Math.abs(s) > max) {
        max = Math.abs(s);
      }
    }
    return max;
  }else{
    return 0;
  }
}



from Audio Streaming and getting the Amplitude to draw Waveform same time

No comments:

Post a Comment