Android - 通过蓝牙麦克风实时录制可视化
Android - Realtime recording visualization through Bluetooth microphone
我正在使用与蓝牙设备的 HFP 连接来获取音频。
我正在使用 AudioRecord class 以 44100 采样率使用 PCM 16 位编码进行录音。单声道
我还使用 simple library 来显示可视化
我的目标是显示正在录制的当前音频的某种可视化效果。
但是,我无法找到获取当前音频缓冲区的振幅/FFT 的方法。
我的录音/文件保存线程是这样的
private class RecordingRunnable implements Runnable {
@Override
public void run() {
final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"recording.pcm");
final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); //buffer size allocation
try (final FileOutputStream outStream = new FileOutputStream(file)) {
while (recordingInProgress.get()) {
int result = recorder.read(buffer, BUFFER_SIZE); // reading
if (result < 0) {
throw new RuntimeException("Reading of audio buffer failed: " +
getBufferReadFailureReason(result));
}
audioRecordView.update(); // What to write here so that i can get Amplitude/waveform of current audio.
outStream.write(buffer.array(), 0, BUFFER_SIZE); // writing buffer to file
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Writing of recorded audio failed", e);
}
}
正如您在上面的代码中看到的那样,方法 audioRecordView.update();
是空的,因为我无法弄清楚如何获取音频信号的幅度。任何小 hint/help/suggestion 表示赞赏。
这段代码对我有用
for (int i=0; i<result/2; i++) {
short curSample = getShort(buffer.array()[i*2], buffer.array()[i*2+1]);
if (curSample > cAmplitude) {
cAmplitude = curSample;
}
}
audioRecordView.update(cAmplitude);
我正在使用与蓝牙设备的 HFP 连接来获取音频。 我正在使用 AudioRecord class 以 44100 采样率使用 PCM 16 位编码进行录音。单声道 我还使用 simple library 来显示可视化
我的目标是显示正在录制的当前音频的某种可视化效果。 但是,我无法找到获取当前音频缓冲区的振幅/FFT 的方法。
我的录音/文件保存线程是这样的
private class RecordingRunnable implements Runnable {
@Override
public void run() {
final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"recording.pcm");
final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); //buffer size allocation
try (final FileOutputStream outStream = new FileOutputStream(file)) {
while (recordingInProgress.get()) {
int result = recorder.read(buffer, BUFFER_SIZE); // reading
if (result < 0) {
throw new RuntimeException("Reading of audio buffer failed: " +
getBufferReadFailureReason(result));
}
audioRecordView.update(); // What to write here so that i can get Amplitude/waveform of current audio.
outStream.write(buffer.array(), 0, BUFFER_SIZE); // writing buffer to file
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Writing of recorded audio failed", e);
}
}
正如您在上面的代码中看到的那样,方法 audioRecordView.update();
是空的,因为我无法弄清楚如何获取音频信号的幅度。任何小 hint/help/suggestion 表示赞赏。
这段代码对我有用
for (int i=0; i<result/2; i++) {
short curSample = getShort(buffer.array()[i*2], buffer.array()[i*2+1]);
if (curSample > cAmplitude) {
cAmplitude = curSample;
}
}
audioRecordView.update(cAmplitude);