0
votes

I am using an HFP connection to a Bluetooth Device to get the audio. I am using AudioRecord class for recording with PCM 16-bit encoding at 44100 sampling rate. MONO ch. I am also using a simple library to show the visualization

My target is to show some kind of visualization of the current audio which is being recorded. However, I am not able to find a way to get the Amplitude / FFT of the current audio buffer.

My recording /file save thread looks like this

 private class RecordingRunnable implements Runnable {

    @Override
    public void run() {
        final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"recording.pcm");
        final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); //buffer size allocation
        try (final FileOutputStream outStream = new FileOutputStream(file)) {
            while (recordingInProgress.get()) {
                int result = recorder.read(buffer, BUFFER_SIZE); // reading
                if (result < 0) {
                    throw new RuntimeException("Reading of audio buffer failed: " +
                            getBufferReadFailureReason(result));
                }
               audioRecordView.update(); // What to write here so that i can get Amplitude/waveform of current audio.
                outStream.write(buffer.array(), 0, BUFFER_SIZE); // writing buffer to file
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Writing of recorded audio failed", e);
        }

    }

As you can see in the code above the method audioRecordView.update(); is empty as I am not able to figure out how to get the amplitude of the audio signal. Any small hint/help/suggestion is appreciated.

1

1 Answers

0
votes

This piece of code did the trick for me

                    for (int i=0; i<result/2; i++) {
                    short curSample = getShort(buffer.array()[i*2], buffer.array()[i*2+1]);
                    if (curSample > cAmplitude) {
                        cAmplitude = curSample;
                    }
                }
             
                 audioRecordView.update(cAmplitude);