我尝试以近实时(Mic)检测音频插孔上的方波信号。因此,我在流模式下使用类AudioRecord。但是我的问题是,我的手机(mic)总是工作不同。有时我使用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;
}

最佳答案

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

09-12 19:22