我写了这个类,来获取音频数据。我想使用音频输入来采样实时射频信号。我以 44kHz 的频率进行采样,并且我希望通过测量获得的总样本数知道耗时,知道采样频率。
我不知道为什么我发现 system.nanoTime 测量的耗时和获取的样本除以频率之间存在增量时间。为什么每次我开始/停止采集时这个大约 170 毫秒的增量都会发生变化?我是否会从采集的信号中丢失样本?
基本上,我所做的就是用 started
调用这个类。 bool 值设置为 true
,然后几秒钟后我将此 bool 值设置为 false
,然后类退出while循环,然后我测量耗时并提取增量。
这是我的测试代码:
public class RecordAudio extends AsyncTask<Void, Long, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
int bufferSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, bufferSize);
short[] buffer = new short[blockSize];
double[] toTransform = new double[blockSize];
audioRecord.startRecording();
// started = true; hopes this should true before calling
// following while loop
double aquiredSignalLen=0;
long elapsedTime = System.nanoTime();
while (started) {
int bufferReadResult = audioRecord.read(buffer, 0,blockSize);
double tmpElTime1=(double)bufferReadResult/(double)44000;
aquiredSignalLen=aquiredSignalLen+tmpElTime1;
}
//when i stop the acquisition, i calculate the elapsed time,
//and i compare the result with the elapsed time measured counting
//the total number of samples
elapsedTime = System.nanoTime() - elapsedTime;
double elapsedTimeDouble=(double)elapsedTime/1000000000;
double delta=elapsedTimeDouble-aquiredSignalLen;
audioRecord.stop();
} catch (Throwable t) {
t.printStackTrace();
Log.e("AudioRecord", "Recording Failed");
}
return null;
}
我问了这个问题,以解决这个问题:
我需要计算在麦克风输入上接收到的 2 个特定信号波形之间的精确耗时。
我希望至少有 1 毫秒的精度,如果可以实现更高的精度就更好了。
这段代码只是一个开始测试。可以计算我可以达到高精度的样本吗?我担心我会因为处理时间而丢失一些 sample ?
最佳答案
根据设备硬件的设置方式,您可能会使用两个异步时钟来测量时间。
音频编解码器很可能使用其自己的本地振荡器作为对音频进行采样的字时钟,并将以该速率提供样本。同时nanoTime()
来自 CPU 时钟。两者都不太可能是非常准确的时间引用。
关于Android 实时音频采集 - 丢失一些样本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19646057/