简体   繁体   English

音频记录缓冲区填充零

[英]AudioRecord buffer filled with zeros

Why is the audioData short[] buffer I create in the code below filled with 0 upon each 为什么我在下面的代码中创建的audioData short[]缓冲区每个填充为0

samplesIn += mRecordInstance.read(audioData, samplesIn, bufferSize - samplesIn);

?

The size of the buffer ( audioData.length ) is NOT ZERO which means that the buffer is NOT EMPTY, it's just filled with zeros. 缓冲区的大小( audioData.length )不是零,这意味着缓冲区不是空的,只是填充了零。

I've tried changing the FREQUENCY to 8000 -> no change. 我试图改变FREQUENCY ,以8000 - >没有变化。

I'm implementing the Echoprint library - please refer to this for more information on this should you require it. 我正在实现Echoprint库-如果需要,请参考此文件以获取更多信息。

Proof from LogCat that the audioData buffer is filled with zeros: 从LogCat证明audioData缓冲区中填充了零:

// ...
public final static int LISTENING_PASS_TIME = 20;
// cap to 30 seconds max, 10 seconds min.
public final static int MAX_LISTENING_PASS_TIME = 30;
public final static int MIN_LISTENING_PASS_TIME = 10;

private final int FREQUENCY = 11025;
private final int CHANNEL = AudioFormat.CHANNEL_IN_MONO;
private final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;    

private Thread thread;
private volatile boolean isRunning = false;
AudioRecord mRecordInstance = null;

private short audioData[];
private int bufferSize; 
private int secondsToRecord;
private volatile boolean continuous;

public void stop() {
    this.continuous = false;
    if (mRecordInstance != null)
        mRecordInstance.stop();     
}

public void run() {
    this.isRunning = true;
    try {
        // create the audio buffer & get the minimum buffer size
        int minBufferSize = AudioRecord.getMinBufferSize(FREQUENCY,
                CHANNEL, ENCODING);
        Log.d("Fingerprinter", "minBufferSize: " + minBufferSize);

        // and the actual buffer size for the audio to record frequency *
        // seconds to record.
        bufferSize = Math.max(minBufferSize, this.FREQUENCY
                * this.secondsToRecord);
        Log.d("Fingerprinter", "bufferSize: " + bufferSize);

        audioData = new short[bufferSize];

        // start recorder
        mRecordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC,
                FREQUENCY, CHANNEL, ENCODING, minBufferSize);

        // start recording
        willStartListening();

        mRecordInstance.startRecording();
        boolean firstRun = true;
        do {
            try {
                willStartListeningPass();

                long time = System.currentTimeMillis();
                // fill audio buffer with mic data.
                int samplesIn = 0;
                do {
                    samplesIn += mRecordInstance.read(audioData, samplesIn,
                            bufferSize - samplesIn);

                    if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED)
                        break;
                } while (samplesIn < bufferSize);
                Log.d("Fingerprinter",
                        "Audio recorded: "
                                + (System.currentTimeMillis() - time)
                                + " millis");

                // see if the process was stopped.
                if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED
                        || (!firstRun && !this.continuous))
                    break;

                // debugging: print audioData short[]
                Log.d("Fingerprinter", "Audio Data Content:");

                // String audioDataContent = "";
                for (int i = 100; i < 110; i++) {
                    // audioDataContent = i + ":" + audioData[i];
                    Log.d("Fingerprinter", i + ":" + audioData[i]);
                }

                Log.d("Fingerprinter", "samplesIn: " + samplesIn);

                firstRun = false;

                didFinishListeningPass();
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Fingerprinter", e.getLocalizedMessage());

                didFailWithException(e);
            }
        } while (this.continuous);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Fingerprinter", e.getLocalizedMessage());

        didFailWithException(e);
    }

    if (mRecordInstance != null) {
        mRecordInstance.stop();
        mRecordInstance.release();
        mRecordInstance = null;
    }
    this.isRunning = false;

    didFinishListening();
}

I found that I did in fact record some data - it was just the section I chose to display that only recorded zeros . 我发现实际上确实记录了一些数据-只是我选择显示的部分仅记录了zeros I prove this by summarizing all entries into the audioData array and dividing it by the number of entries into the array. 我通过汇总audioData数组中的所有条目并将其audioData数组中的条目数来证明这一点。 The result was -3.455619047619048 . 结果是-3.455619047619048

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM