Android -通过蓝牙麦克风实时录制可视化



我正在使用HFP连接到蓝牙设备以获取音频。我使用AudioRecord类以44100采样率使用PCM 16位编码进行记录。MONO ch。我还使用了一个简单的库来显示可视化

我的目标是显示正在录制的当前音频的某种可视化。但是,我无法找到一种方法来获得当前音频缓冲区的幅度/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();是空的,因为我无法弄清楚如何获得音频信号的幅度。任何小小的提示/帮助/建议都是非常感谢的。

这段代码帮了我的忙

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