1

我尝试通过音频插孔近乎实时地检测方波信号(麦克风)。出于这个原因,我在流模式下使用 AudioRecord 类。但我的问题是,我的手机(麦克风)总是工作不同。有时我使用 20'000 的阈值,有时我必须将阈值调整为 1'000 以检测(第一个脉冲的)边缘。信号的电压范围为 0 到 3V。我不确定我的麦克风是否工作不正常,或者 adc 是否使用不同的参考电压?!

我不知道如何解决这个问题。我真的希望你能帮助我。

这是我的源代码:

公共类 ReceiveCom 扩展 AsyncTask 实现层{

private AudioRecord audioRecord;
private int sampleRate = 44100;

private short[] audioData;
private int sizeInShorts;

private boolean isRunning=false;
private int receiveBuffer;

private int minBuffersize;
private boolean ready=false;

private int audioResult,dataCounter=0;
private short[] dataBits = new short[8];

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public ReceiveCom(){
    try{
        minBuffersize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, 
                                                     AudioFormat.ENCODING_PCM_16BIT);


        audioRecord = new AudioRecord(AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, 
                                        AudioFormat.ENCODING_PCM_16BIT, minBuffersize);


        if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED){   
            throw new Exception("AudioRecord init failed");
        }

        audioData = new short[2*minBuffersize];
        Log.e("ERROR", "ReceiveCom constructor: OK ");
    }
    catch(IllegalArgumentException e){
        Log.e("ERROR", "IllegalArgumentException: " + e);
    }
    catch(Exception e){
        Log.e("ERROR", "ConstructorException: " + e);           
    }
    finally{
        Log.e("ERROR", "ReceiveCom constructor: END ");
    }

    if(Build.VERSION.SDK_INT >= 17){
        if(AutomaticGainControl.isAvailable()){
            Log.e("INFO", "AGC is available");
            //AutomaticGainControl.create(audioRecord.getAudioSessionId());
        }
    }
}


protected Void doInBackground(Void... arg0) {       
    audioRecord.setPositionNotificationPeriod(minBuffersize);
    audioRecord.setRecordPositionUpdateListener( new OnRecordPositionUpdateListener(){

        @Override   
        public void onPeriodicNotification(AudioRecord myRecorder) {
            int timeCounter=0,bitCounter=0,i=0;
            boolean edgeTriggered=false, bitReady=false;
            int dB,edgeThreshold=10000,idleThreshold=edgeThreshold/5;

            for(i=2; i<audioResult; i=i+1){
                if(edgeTriggered==false &&  audioData[i-2]<=idleThreshold && audioData[i-1]>=edgeThreshold && audioData[i]>=edgeThreshold ){
                    Log.e("DEBUG","audioData["+i+"]: "+audioData[i]);
                    timeCounter++;
                    edgeTriggered=true;
                    bitReady=false;
                }   
            }
        }
        @Override
        public void onMarkerReached(AudioRecord recorder) { }
    });

    audioRecord.startRecording();
    while(isRunning){
        audioResult = audioRecord.read(audioData, 0, minBuffersize);    
    }

    audioRecord.stop();
    audioRecord.release();  
    return null;
}
4

1 回答 1

0

有时 MIC 上有一个 AGC 或其他预处理滤波器。您可以尝试使用 AudioSource.VOICE_RECOGNITION 而不是 AudioSource.MIC ...

于 2014-01-30T16:29:05.050 回答