noo*_*oob 9 audio android audio-recording android-audiorecord
我正在尝试在Android中拨打电话录音应用.我正在使用扬声器记录上行和下行音频.我面临的唯一问题是音量太低.我已经将使用AudioManager的设备的音量增加到最大,并且它不能超越它.
我第一次使用MediaRecorder,但由于它功能有限并提供压缩音频,我尝试使用AudioRecorder.我还没弄明白如何增加音频.我也检查了Github上的项目,但它没用.我在stackoverflow上搜索了最近两周,但根本找不到任何东西.
我很确定这是可能的,因为许多其他应用程序正在这样做.例如自动呼叫记录器就是这样做的.
我知道我必须对音频缓冲区做些什么,但我不太清楚需要做些什么.你能指导我吗?
更新: -
对不起,我忘了提到我已经在使用Gain了.我的代码几乎与RehearsalAssistant相似(事实上我是从那里派生出来的).增益不会超过10dB,并且不会过多地增加音量.我想要的是我应该能够听到音频,而不是把我的耳朵放在我的代码中缺少的扬声器上.
我问过关于在SoundDesign SE音量/响度的运作类似的问题在这里.它提到增益和响度是相关的,但它没有设置实际的响度级别.我不确定事情是如何运作的,但我决心获得大声的音量输出.
Har*_*ger 15
你显然有AudioRecord运行的东西,所以我跳过了决定sampleRate和inputSource.重点是您需要在录制循环中适当地操作记录数据的每个样本以增加音量.像这样:
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;
}
}
Run Code Online (Sandbox Code Playgroud)