我使用源数据线生成正弦波并发出声音。
采样率为 44100,块大小为 441。
如果我播放 0.1 秒,则生成 4410 短正弦波并发出如下声音。
ByteBuffer cBuf = ByteBuffer.allocate(Constants.BLOCK_SIZE);
for(int i=0; i<SamplesTotal; i++){
if(!cBuf.hasRemaining()){
cBuf.putShort((short) (Short.MAX_VALUE * Math.sin(2 * Math.PI * time)));
time += samplingInterval;
}else{
line.write(cBuf.array(), 0, cBuf.position());
cBuf.clear();
cBuf.putShort((short) (Short.MAX_VALUE * Math.sin(2 * Math.PI * time)));
time += samplingInterval;
}
}
line.write(cBuf.array(), 0, cBuf.position());
现在的问题是,当我用智能手机从这个声音中读取数据时,为了找到频率,使用 fftpack 读取 441 短类型数据并进行转换。比如下面的源码。
while (started) {
int bufferReadResult = audioRecord.read(buffer, 0, blockSize);
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
toTransform[i] = (double) buffer[i] / (Short.MAX_VALUE);
}
transformer.ft(toTransform);
publishProgress(toTransform);
}
为了找到特定的频率 19000 和 20000,我像这样过滤并打印了如下所示的频率。
int temp = 0;
int temp_idx=0;
double freq=0;
int testFreq_19 = 380;
int testFreq_20 = 400;
for (int i = 0; i < toTransform[0].length; i++) {
int x = i;
int downy = (int) (100 - (toTransform[0][i] * 10));
int upy = 100;
if(i>testFreq_19-1 && downy<99){
if(temp<downy){
temp=downy;
temp_idx=i;
}
}
canvas.drawLine(x/2, downy, x/2, upy, paint);
}
if(temp_idx>testFreq_19-10 && temp_idx<testFreq_19+10){
freq = testFreq_19*(frequency/882);
}else if(temp_idx>testFreq_20-10 && temp_idx<testFreq_20+10){
freq = testFreq_20*(frequency/882);
}else{
freq= -1;
}
Log.d("frequency log", "frequency: " + String.valueOf(freq));
问题是当我生成 0.1 秒时,会写入 10 个块(4410)。当我从智能手机阅读时,我以为我会收到 4410 块,这意味着使用上述源打印 19000 10 次。
然而,结果是可怕的。有时收到 20 或 30 次,即使我收到了 5 次。数据之间有空白(没有数据)我得到了如下结果示例:
frequency: -1
frequency: -1
frequency: 19000.0
frequency: 19000.0
frequency: 19000.0
frequency: -1
frequency: -1
frequency: -1
frequency: 19000.0
frequency: -1
frequency: -1
frequency: -1
frequency: -1
frequency: -1
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: -1
frequency: -1
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: -1
frequency: 20000
frequency: 20000
frequency: 20000
frequency: 20000
frequency: -1
frequency: -1
frequency: -1
frequency: -1
这段代码有什么问题?