我尝试近乎实时地(Mic)通过音频插孔检测方波信号。出于这个原因,我在流模式下使用AudioRecord类。但我的问题是,我的手机(麦克风)总是有所不同。有时我使用20'000的阈值,有时我必须将我的阈值调整到1000来检测边缘(第一个脉冲的边缘)。信号的电压范围为0至3V。我不确定我的麦克风是否工作正常或者adc是否使用不同的参考电压?!
我不知道如何解决这个问题。 我真的希望你能帮助我。
这是我的源代码:
public class ReceiveCom extends AsyncTask实现了Layers {
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;
}
答案 0 :(得分:0)
有时在MIC上有AGC或其他预处理过滤器。您可以尝试使用AudioSource.VOICE_RECOGNITION而不是AudioSource.MIC ...