我想录制来自麦克风的输入,附加混响效果,并将结果保存到文件.我的用例是一个应用程序,让您可以在录制后播放歌曲并选择不同的预设混响选项,然后保存您的演奏并将其存储在后台服务器上.我发送到服务器的文件需要应用混响效果.
到目前为止,我已经能够使用AudioRecord录制输入,我可以添加一个混响效果到AudioTrack来听到混响效果,但是我坚持认为如何保存音频混合效果嵌入.这是我到目前为止
private void startRecording() {
final int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
mRecorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize);
if(NoiseSuppressor.isAvailable()) {
NoiseSuppressor ns = NoiseSuppressor.create(mRecorder.getAudioSessionId());
ns.setEnabled(true);
}
if(AcousticEchoCanceler.isAvailable()) {
AcousticEchoCanceler aec = AcousticEchoCanceler.create(mRecorder.getAudioSessionId());
aec.setEnabled(true);
}
mPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AUDIO_FORMAT, bufferSize, AudioTrack.MODE_STREAM);
PresetReverb reverb = new PresetReverb(1, mPlayer.getAudioSessionId());
reverb.setPreset(PresetReverb.PRESET_LARGEHALL);
reverb.setEnabled(true);
mPlayer.setAuxEffectSendLevel(1.f);
mRecorder.startRecording();
mPlayer.play();
new RecordingThread(mRecorder, mPlayer, bufferSize) {
@Override
public void onReleased() {
mRecorder = null;
mPlayer = null;
}
}.start();
}
public class RecordingThread extends Thread {
private static final String TAG = RecordingThread.class.getSimpleName();
private final AudioRecord mRecorder;
private final AudioTrack mPlayer;
private final int mBufferSize;
public RecordingThread(AudioRecord recorder, AudioTrack player, int bufferSize) {
mRecorder = recorder;
mPlayer = player;
mBufferSize = bufferSize;
}
@Override
public void run() {
byte[] buffer = new byte[mBufferSize];
int read;
while ((read = mRecorder.read(buffer, 0, buffer.length)) > 0) {
mPlayer.write(buffer, 0, buffer.length);
}
mRecorder.release();
mPlayer.release();
onReleased();
Timber.tag(TAG);
Timber.i("Exited with code %s", read);
}
public void onReleased() {
}
}
所以理想的情况是,可以将PresetReverb附加到AudioRecord而不是AudioTrack,但是当我做到:
java.lang.RuntimeException: Cannot initialize effect engine for type: 47382d60-ddd8-11db-bf3a-0002a5d5c51b Error: -3
所以现在我想我需要将PCM数据从AudioRecord传送到一些外部服务/库,或者用任何混响算法来修改缓冲区.