nieldeokar / WhatsappAudioRecorder

Simple audio recorder using Android's AudioRecorder with .aac File extention.
23 stars 7 forks source link

Amplitude #1

Open deathvivek opened 4 years ago

deathvivek commented 4 years ago

Hello Nieldeokar, Can you tell me how to calculate amplitude from your code ? I have added this code , I am getting Amplitude but not more than 55 in any case private boolean handleCodecInput(AudioRecord audioRecord, MediaCodec mediaCodec, ByteBuffer[] codecInputBuffers, boolean running) throws IOException { byte[] audioRecordData = new byte[bufferSize]; int length = audioRecord.read(audioRecordData, 0, audioRecordData.length); computedbAmp(audioRecordData); ///this is amplitude code if (length == AudioRecord.ERROR_BAD_VALUE || length == AudioRecord.ERROR_INVALID_OPERATION || length != bufferSize) {

        if (length != bufferSize) {
            if (onRecorderFailedListener != null) {
                Log.d(TAG, "length != BufferSize calling onRecordFailed");
                onRecorderFailedListener.onRecorderFailed();
            }
            return false;
        }
    }

//code //compute here public double computedbAmp(byte[] audioData) { //System.out.println("::::: audioData :::::"+audioData); double amplitude = 0; double sum=0; for (int i = 0; i < audioData.length/2; i++) { double y = (audioData[i2] | audioData[i2+1] << 8); sum += yy; } double rms = sum / audioData.length/2; amplitude = 20.0Math.log10(rms/32768.0);

    return amplitude;

}
nieldeokar commented 4 years ago

Well, I'm not much into Android these days. Still I think this solution from SO will help. Also amplitude will be in handleCodecOutput() method.


 private void handleCodecOutput(MediaCodec mediaCodec,
                                   ByteBuffer[] codecOutputBuffers,
                                   MediaCodec.BufferInfo bufferInfo,
                                   OutputStream outputStream)
            throws IOException {
        int codecOutputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);

        while (codecOutputBufferIndex != MediaCodec.INFO_TRY_AGAIN_LATER) {
            if (codecOutputBufferIndex >= 0) {
                ByteBuffer encoderOutputBuffer = codecOutputBuffers[codecOutputBufferIndex];

                encoderOutputBuffer.position(bufferInfo.offset);
                encoderOutputBuffer.limit(bufferInfo.offset + bufferInfo.size);

                if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != MediaCodec.BUFFER_FLAG_CODEC_CONFIG) {
                    byte[] header = createAdtsHeader(bufferInfo.size - bufferInfo.offset);

                    outputStream.write(header);

                    byte[] data = new byte[encoderOutputBuffer.remaining()];
                    encoderOutputBuffer.get(data);

                    float accumulator = 0;
                    for (int i = 0; i < data.length - 1; i++) {
                       accumulator += Math.abs(data[i]);
                    }
                    // this is amp use it however you want
                    float amp = accumulator/(128 * data.length);

                    outputStream.write(data);
                }

                encoderOutputBuffer.clear();

                mediaCodec.releaseOutputBuffer(codecOutputBufferIndex, false);
            } else if (codecOutputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                codecOutputBuffers = mediaCodec.getOutputBuffers();
            }

            codecOutputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
        }
    }