在我的项目中,我必须校准Android手机的麦克风来做一些录音并进行分析。
对于不同的手机,我获得了以dB为单位测量的不同记录参数值。
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class RecordingAudioThreshold extends AppCompatActivity {
// This class generates the spectrogram of a wav file
private MediaRecorder mediaRecorder = null;
private Timer timerThread;
private Button startRecording, stopRecording;
private TextView recordingThreshold, recordingThresholdDB;
int amplitude = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate_spectrogram);
// Initialize the timer (used to cancel the thread if it's not running).
timerThread = new Timer();
// Method to calibrate the microphone
startRecording = (Button) findViewById(R.id.button_startRecording);
stopRecording = (Button) findViewById(R.id.button_stopRecording);
recordingThreshold = (TextView) findViewById(R.id.textView_threshold);
recordingThresholdDB = (TextView) findViewById(R.id.textView_thresholdDB);
startRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile("/dev/null");
try {
mediaRecorder.prepare();
mediaRecorder.start();
System.out.println("Started Recording using Media Recorder");
} catch (IOException e) {
System.out.println("Exception while recording of type : " + e.toString());
}
// start the timer to print the recorded values
timerThread.schedule(new TimerTask() {
@Override
public void run() {
amplitude = mediaRecorder.getMaxAmplitude();
recordingThreshold.post(new Runnable() {
@Override
public void run() {
recordingThreshold.setText("The recorded value is : " + amplitude);
}
});
recordingThresholdDB.post(new Runnable() {
@Override
public void run() {
recordingThresholdDB.setText("The decibel value is : " + 20 * Math.log10(amplitude));
}
});
}
}, 0, 500);
}
});
stopRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timerThread.cancel();
if (mediaRecorder != null) {
mediaRecorder.release();
mediaRecorder = null;
}
recordingThreshold.setText("Calibration complete.");
recordingThresholdDB.setText("Calibration complete.");
}
});
}
}
这段代码效果很好,除了在不同品牌和型号的手机上运行时,我得到不同的录音值(变化通常很高+/- 10dB。
然后我从playstore安装了一个应用程序SoundMeter,即使在不同的Android设备上也会提供相同的录制参数(+/- 2.5 dB)。然后我读到了如何从here实现校准。
我仍然不知道如何校准Android的麦克风。非常感谢任何帮助。