我有以下代码:
public class MainActivity extends Activity {
private int freq = 8000;
private AudioRecord audioRecord = null;
private Thread Rthread = null;
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
private AudioTrack audioTrack = null;
byte[] buffer = new byte[freq];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
final int bufferSize = AudioRecord.getMinBufferSize(freq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
MediaRecorder.AudioEncoder.AMR_NB, bufferSize);
audioTrack = new AudioTrack(AudioManager.ROUTE_HEADSET, freq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
MediaRecorder.AudioEncoder.AMR_NB, bufferSize,
AudioTrack.MODE_STREAM);
LoudnessEnhancer enhancer = new LoudnessEnhancer(audioTrack.getAudioSessionId());
enhancer.setTargetGain(100);
enhancer.setEnabled(true);
audioTrack.setPlaybackRate(freq);
buffer = new byte[bufferSize];
audioRecord.startRecording();
Log.i("info", "Audio Recording started");
audioTrack.play();
Log.i("info", "Audio Playing started");
Rthread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
audioRecord.read(buffer, 0, bufferSize);
audioTrack.write(buffer, 0, buffer.length);
} catch (Throwable t) {
Log.e("Error", "Read write failed");
t.printStackTrace();
}
}
}
});
Rthread.start();
}
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
}
此代码能够读取麦克风的输入并在扬声器/连接的耳机上播放。到目前为止,一切都很好。现在,我需要放大输入的音量。我已经尝试了here提出的解决方案,但最终我得到的音频确实失真。我也尝试了LoudnessEnhancer,但似乎没有任何效果。
好的,我成功让 LoudnessEnhancer 正常工作了。我知道其中有巨大的增益值,但我需要在这方面获得很多增益。这是为可能遇到类似问题的其他人提供的更新代码。
public class MainActivity extends Activity {
private int freq = 8000;
private AudioRecord audioRecord = null;
private Thread Rthread = null;
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
private AudioTrack audioTrack = null;
byte[] buffer = new byte[freq];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
final int bufferSize = AudioRecord.getMinBufferSize(freq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
MediaRecorder.AudioEncoder.AMR_NB, bufferSize);
audioTrack = new AudioTrack(AudioManager.ROUTE_HEADSET, freq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
MediaRecorder.AudioEncoder.AMR_NB, bufferSize,
AudioTrack.MODE_STREAM);
LoudnessEnhancer enhancer = new LoudnessEnhancer(audioTrack.getAudioSessionId());
NoiseSuppressor.create(audioTrack.getAudioSessionId());
AcousticEchoCanceler.create(audioTrack.getAudioSessionId());
enhancer.setTargetGain(10000);
enhancer.setEnabled(true);
audioTrack.setPlaybackRate(freq);
buffer = new byte[bufferSize];
audioRecord.startRecording();
Log.i("info", "Audio Recording started");
audioTrack.play();
Log.i("info", "Audio Playing started");
Rthread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
audioRecord.read(buffer, 0, bufferSize);
audioTrack.write(buffer, 0, buffer.length);
} catch (Throwable t) {
Log.e("Error", "Read write failed");
t.printStackTrace();
}
}
}
});
Rthread.start();
}
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
}
这是在 AudioTrack 对象中添加音频增益、噪声抑制和回声消除器的更简单方法
AudioTrack audio = new AudioTrack(yourStreamOutput,
SAMPLING_RATE_IN_HZ,
AudioFormat.CHANNEL_OUT_MONO,
AUDIO_FORMAT,
BUFFER_SIZE,
AudioTrack.MODE_STREAM);
setPreferredOutputDevice(audio);//this function set the output device like mobile speaker or earphone speaker or bluetooth speaker
final LoudnessEnhancer enhancer = new LoudnessEnhancer(audio.getAudioSessionId());
NoiseSuppressor.create(audio.getAudioSessionId());
AcousticEchoCanceler.create(audio.getAudioSessionId());
enhancer.setTargetGain(10000);//your audio gain value higher the value the louder it become
enhancer.setEnabled(true);
audio.play();