gpt4 book ai didi

android - 增加录制音频的音量输出

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:13 24 4
gpt4 key购买 nike

我正在尝试在 Android 中制作一个通话录音应用程序。我正在使用扬声器录制上行链路和下行链路音频。我面临的唯一问题是音量太低。我已经使用 AudioManager 将设备的音量增加到最大,但不能超出这个范围。

我第一次使用 MediaRecorder,但由于它的功能有限并且提供压缩音频,所以我尝试使用 AudioRecorder。我仍然没有想出如何增加音频。 Github 上的项目我也查过了,但是没用。过去两周我一直在 stackoverflow 上搜索,但根本找不到任何东西。

我很确定这是可能的,因为许多其他应用程序都在这样做。例如,自动调用记录器就是这样做的。

我知道我必须对音频缓冲区做些什么,但我不太确定需要做些什么。你能指导我吗?

更新:-
很抱歉,我忘了说我已经在使用 Gain。我的代码几乎类似于 RehearsalAssistant (事实上​​我是从那里推导出来的)。增益不会超过 10dB,并且不会过多地增加音频音量。我想要的是我应该能够在不将耳朵放在扬声器上的情况下收听音频,这是我的代码中缺少的。

我在 SoundDesign SE here 上问过一个关于音量/响度功能的类似问题.它提到增益和响度是相关的,但它没有设置实际的响度级别。我不确定事情是如何运作的,但我决心获得大音量输出。

最佳答案

你显然有 AudioRecord 东西在运行,所以我跳过了 sampleRateinputSource 的决定。要点是您需要在录音循环中适本地操作您录制的数据的每个样本以增加音量。像这样:

    int minRecBufBytes = AudioRecord.getMinBufferSize( sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT );
// ...
audioRecord = new AudioRecord( inputSource, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minRecBufBytes );

// Setup the recording buffer, size, and pointer (in this case quadruple buffering)
int recBufferByteSize = minRecBufBytes*2;
byte[] recBuffer = new byte[recBufferByteSize];
int frameByteSize = minRecBufBytes/2;
int sampleBytes = frameByteSize;
int recBufferBytePtr = 0;

audioRecord.startRecording();

// Do the following in the loop you prefer, e.g.
while ( continueRecording ) {
int reallySampledBytes = audioRecord.read( recBuffer, recBufferBytePtr, sampleBytes );

int i = 0;
while ( i < reallySampledBytes ) {
float sample = (float)( recBuffer[recBufferBytePtr+i ] & 0xFF
| recBuffer[recBufferBytePtr+i+1] << 8 );

// THIS is the point were the work is done:
// Increase level by about 6dB:
sample *= 2;
// Or increase level by 20dB:
// sample *= 10;
// Or if you prefer any dB value, then calculate the gain factor outside the loop
// float gainFactor = (float)Math.pow( 10., dB / 20. ); // dB to gain factor
// sample *= gainFactor;

// Avoid 16-bit-integer overflow when writing back the manipulated data:
if ( sample >= 32767f ) {
recBuffer[recBufferBytePtr+i ] = (byte)0xFF;
recBuffer[recBufferBytePtr+i+1] = 0x7F;
} else if ( sample <= -32768f ) {
recBuffer[recBufferBytePtr+i ] = 0x00;
recBuffer[recBufferBytePtr+i+1] = (byte)0x80;
} else {
int s = (int)( 0.5f + sample ); // Here, dithering would be more appropriate
recBuffer[recBufferBytePtr+i ] = (byte)(s & 0xFF);
recBuffer[recBufferBytePtr+i+1] = (byte)(s >> 8 & 0xFF);
}
i += 2;
}

// Do other stuff like saving the part of buffer to a file
// if ( reallySampledBytes > 0 ) { ... save recBuffer+recBufferBytePtr, length: reallySampledBytes

// Then move the recording pointer to the next position in the recording buffer
recBufferBytePtr += reallySampledBytes;

// Wrap around at the end of the recording buffer, e.g. like so:
if ( recBufferBytePtr >= recBufferByteSize ) {
recBufferBytePtr = 0;
sampleBytes = frameByteSize;
} else {
sampleBytes = recBufferByteSize - recBufferBytePtr;
if ( sampleBytes > frameByteSize )
sampleBytes = frameByteSize;
}
}

关于android - 增加录制音频的音量输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26088427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com