gpt4 book ai didi

android - 如何增加放大 AudioTrack?

转载 作者:行者123 更新时间:2023-12-03 00:08:18 44 4
gpt4 key购买 nike

我有以下代码:

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 正常工作。我知道这是一个巨大的 yield 值(value),但我需要在这方面获得很多 yield 。这是针对可能遇到类似问题的任何其他人的更新代码。

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;
}
}

关于android - 如何增加放大 AudioTrack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153158/

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