Android realtime audio acquisition - losing some samples?

痞子三分冷 提交于 2019-12-12 02:34:23

问题


I wrote this class, to acquire audio data. I want to use the audio input to sample realtime RF signals. I sample @ 44kHz, and I expect to know the elapsed time by measuring the total acquired samples, knowing the sample frequency.

I don't know why I found a delta time between elapsed time measured by system.nanoTime and acquired samples divided by frequency. Why this delta of about 170ms changing each time I start/stop acquisition? Am I losing samples from acquired signal?

Basically, what I do, is to call this class with the started boolean set to true, then after few seconds I set this boolean to false, then the class exits from the while loop, then I measure the elapsed time and extract the delta.

This is my testing code:

 public class RecordAudio extends AsyncTask<Void, Long, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            int bufferSize = AudioRecord.getMinBufferSize(frequency, 
                    channelConfiguration, audioEncoding); 

            AudioRecord audioRecord = new AudioRecord( 
                    MediaRecorder.AudioSource.MIC, frequency, 
                    channelConfiguration, audioEncoding, bufferSize); 

            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];

            audioRecord.startRecording();

            // started = true; hopes this should true before calling
            // following while loop
            double aquiredSignalLen=0;
            long elapsedTime = System.nanoTime();

            while (started) {
                int bufferReadResult = audioRecord.read(buffer, 0,blockSize);

                double tmpElTime1=(double)bufferReadResult/(double)44000;
                aquiredSignalLen=aquiredSignalLen+tmpElTime1;
            }

            //when i stop the acquisition, i calculate the elapsed time,
            //and i compare the result with the elapsed time measured counting
            //the total number of samples

            elapsedTime = System.nanoTime() - elapsedTime;
            double elapsedTimeDouble=(double)elapsedTime/1000000000;
            double delta=elapsedTimeDouble-aquiredSignalLen;
            audioRecord.stop();


        } catch (Throwable t) {
            t.printStackTrace();
            Log.e("AudioRecord", "Recording Failed");
        }
        return null;
    }

I asked this question, to solve this problem: I need to calculate the precise elapsed time between 2 particular signals waveform, received on the microphone input. I would like to have at least 1mS precision, better if higher precision is achievable.. this code was just a starting test. may be counting the samples i can achieve high precision? my fear is that i can lose some samples due to processing time?


回答1:


Depending on how the hardware of your device is set up, you may be measuring time using two asynchronous clocks.

The audio codec is quite possibly using its own local oscillator as the word-clock for sampling audio and will be delivering samples at this rate. Meanwhile nanoTime() is derived from the CPU clock. Neither is likely to be a hugely accurate timing reference.



来源:https://stackoverflow.com/questions/19646057/android-realtime-audio-acquisition-losing-some-samples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!