How to adjust microphone sensitivity while recording audio in android

后端 未结 4 1591
清歌不尽
清歌不尽 2020-12-03 04:11

I\'m working on a voice recording app. In it, I have a Seekbar to change the input voice gain. I couldn\'t find any way to adjust the input voice gain.

I am using th

4条回答
  •  生来不讨喜
    2020-12-03 04:41

    This is working implementation based on ByteBuffer for 16bit audio. It's important to clamp the increased value from both sides since short is signed. It's also important to set the native byte order to ByteBuffer since audioRecord.read() returns native endian bytes.

    You may also want to perform audioRecord.read() and following code in a loop, calling data.clear() after each iteration.

        double gain = 2.0;
    
        ByteBuffer data = ByteBuffer.allocateDirect(SAMPLES_PER_FRAME).order(ByteOrder.nativeOrder());
    
        int audioInputLengthBytes = audioRecord.read(data, SAMPLES_PER_FRAME);
        ShortBuffer shortBuffer = data.asShortBuffer();
        for (int i = 0; i < audioInputLengthBytes / 2; i++) { // /2 because we need the length in shorts
            short s = shortBuffer.get(i);
            int increased = (int) (s * gain);
            s = (short) Math.min(Math.max(increased, Short.MIN_VALUE), Short.MAX_VALUE);
            shortBuffer.put(i, s);
        }
    

提交回复
热议问题