1

使用 AudioRecord 录制时,我在计算每帧的时间时遇到问题。因此,如果有人可以成为兄弟并解释将不胜感激的基础知识。

以下是什么意思?

  • sampleRate - 它是您作为样本提供的字节数吗?或成为要使用 AudioRecord 检查的样本的字节数
  • FFT - 简单来说是什么意思?
  • 你如何计算时间?你是通过buffersize / frameByteSize来计算的吗?

参考从 Jacquet Wong 复制的示例代码

public class RecorderThread extends Thread {

private AudioRecord audioRecord;
private boolean isRecording;
private int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private int sampleRate = 44100;
private int frameByteSize = 2048; // for 1024 fft size (16bit sample size)
byte[] buffer;

public RecorderThread(){
    int recBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfiguration, audioEncoding); // need to be larger than size of a frame
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfiguration, audioEncoding, recBufSize);
    buffer = new byte[frameByteSize];
}

public AudioRecord getAudioRecord(){
    return audioRecord;
}

public boolean isRecording(){
    return this.isAlive() && isRecording;
}

public void startRecording(){
    try{
        audioRecord.startRecording();
        isRecording = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void stopRecording(){
    try{
        audioRecord.stop();
        isRecording = false;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public byte[] getFrameBytes(){
    audioRecord.read(buffer, 0, frameByteSize);

    // analyze sound
    int totalAbsValue = 0;
    short sample = 0; 
    float averageAbsValue = 0.0f;

    for (int i = 0; i < frameByteSize; i += 2) {
        sample = (short)((buffer[i]) | buffer[i + 1] << 8);
        totalAbsValue += Math.abs(sample);
    }
    averageAbsValue = totalAbsValue / frameByteSize / 2;

    //System.out.println(averageAbsValue);

    // no input
    if (averageAbsValue < 30){
        return null;
    }

    return buffer;
}

public void run() {
    startRecording();
}

}

对不起,我不是一个容易学习录音的人。我似乎还不能让它点击。希望各位大神能解答一下这个问题。

4

0 回答 0