1
我試圖通過音頻插孔近乎實時地檢測出方波信號(麥克風)。出於這個原因,我在流模式下使用AudioRecord類。但我的問題是,我的手機(麥克風)總是工作不同。有時我使用20,000的閾值,有時我必須將閾值調整爲1'000以檢測(第一個脈衝的)邊緣。信號的電壓範圍是0到3V。我不確定我的麥克風是否工作不正常,或者adc是否使用不同的參考電壓?Android - AudioRecord:通過音頻插孔(麥克風)檢測脈寬調製信號
我還沒有任何想法如何解決這個問題。 我真的希望你能幫助我。
這裏我的源代碼:
公共類ReceiveCom擴展的AsyncTask實現對MIC層{
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;
}
您不應該期望音頻通道通過信號的DC分量,但輸入上出現的任何恆定偏移電壓可能會短暫出現然後衰減。可能你需要爲你的解碼器建立幅度和偏移的跟蹤方案。 – 2014-01-30 16:48:16