11

我正在尝试录制来自 MIC 的声音并绘制实时图表。我能够记录和绘制图表。问题是使用下面的代码记录的值不准确,例如......下面的图像是我在没有声音时得到的。我已经看到使用 fft 的示例,但我不确定这是否对我的情况有任何帮助,因为我正在尝试绘制时域图并且我认为没有将其转换为频域的目的(目前)。其他人正在使用平均功率,这可能会有所帮助,但我不确定。

谢谢你的帮助。

在此处输入图像描述

bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

    short data [] = new short[bufferSize];

    while (!Thread.interrupted()) {

        recorder.startRecording();

        recorder.read(data, 0, bufferSize);

        recorder.stop();

        for (short s : data)
        {
            try {
                Thread.sleep((long) 300.00);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            postUI (Math.abs(s));
        }
    }

    recorder.release();
4

2 回答 2

1

尝试使用更高的采样率。最大值为 48000,但标准为 44100。

此外,麦克风可能只是在记录背景噪音。

于 2012-05-23T10:41:43.627 回答
0

这是@Gourneau 的答案,但有代码

AudioRecord recorder; // our recorder, must be initialized first
short[] buffer; // buffer where we will put captured samples
DataOutputStream output; // output stream to target file
boolean isRecording; // indicates if sound is currently being captured
ProgressBar pb; // our progress bar recieved from layout
while (isRecording) {
    double sum = 0;
    int readSize = recorder.read(buffer, 0, buffer.length);
    for (int i = 0; i < readSize; i++) {
        output.writeShort(buffer [i]);
        sum += buffer [i] * buffer [i];
    }
    if (readSize > 0) {
        final double amplitude = sum / readSize;
        pb.setProgress((int) Math.sqrt(amplitude));
    }
}

http://web.archive.org/web/20140709183733/http://developer.samsung.com/android/technical-docs/Displaying-Sound-Volume-in-Real-Time-While-Recording

于 2021-03-10T12:53:01.233 回答