问题描述
我写了这个类,来获取音频数据。我想用的音频输入采样实时RF信号。我品尝@ 44kHz的,我希望通过测量总收购样本,知道采样频率要知道所用的时间。
I wrote this class, to acquire audio data. I want to use the audio input to sample realtime RF signals. I sample @ 44kHz, and I expect to know the elapsed time by measuring the total acquired samples, knowing the sample frequency.
我不知道为什么,我发现通过system.nanoTime测定经过时间之间的时间增量,并收购了分频采样。为什么此增量约为170ms每次我启动/停止采集时间变化?我是不是从采集的信号丢失样品?
I don't know why I found a delta time between elapsed time measured by system.nanoTime and acquired samples divided by frequency. Why this delta of about 170ms changing each time I start/stop acquisition? Am I losing samples from acquired signal?
基本上,我做什么,是调用这个类设置为的
,再经过几秒钟我这个布尔设置为启动
布尔值true 假
,然后从while循环类退出,那么我测量经过时间和提取三角洲。
Basically, what I do, is to call this class with the started
boolean set to true
, then after few seconds I set this boolean to false
, then the class exits from the while loop, then I measure the elapsed time and extract the delta.
这是我的测试code:
This is my testing code:
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特定的信号波形之间的precise经过的时间,收到了麦克风输入。
我想至少有1毫秒precision,更好,如果高precision是可以实现的..
这code只是一个开始测试。可统计样本,我可以实现高precision?我担心的是,我可能会失去一些样品,由于处理时间?
I asked this question, to solve this problem:I need to calculate the precise elapsed time between 2 particular signals waveform, received on the microphone input.I would like to have at least 1mS precision, better if higher precision is achievable..this code was just a starting test. may be counting the samples i can achieve high precision? my fear is that i can lose some samples due to processing time?
推荐答案
根据如何你的设备的硬件设置,您可以使用两个异步时钟来测量时间。
Depending on how the hardware of your device is set up, you may be measuring time using two asynchronous clocks.
音频codeC很可能是用自己的本地振荡器作为字时钟采样的音频,并在此率将提供样品。与此同时 nanoTime()
从CPU时钟。既不很可能是一个巨大的精确的定时基准。
The audio codec is quite possibly using its own local oscillator as the word-clock for sampling audio and will be delivering samples at this rate. Meanwhile nanoTime()
is derived from the CPU clock. Neither is likely to be a hugely accurate timing reference.
这篇关于Android的实时音频采集 - 失去了一些样品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!