【问题标题】:Android - Realtime recording visualization through Bluetooth microphoneAndroid - 通过蓝牙麦克风实时记录可视化
【发布时间】:2021-02-24 09:07:57
【问题描述】:

我正在使用与蓝牙设备的 HFP 连接来获取音频。 我正在使用 AudioRecord 类以 44100 采样率使用 PCM 16 位编码进行录制。单声道。 我还使用simple library 来显示可视化

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

【问题讨论】:

    标签: android audio fft visualization bytebuffer


    【解决方案1】:

    这段代码帮了我大忙

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      相关资源
      最近更新 更多