现在,我正在进行一个音频信号处理,我想要实时显示我用手机录制的音频声波。
这是我的音频格式为“ENCODING_PCM_16BIT”的问题。那么如何将16位短数据更改为双重格式呢?
这是我的代码,但它的工作不正确。任何人都可以帮我解决这个问题吗?
try {
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, Sample_rate, Channel, Encording,
Buffersize);
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(MainActivity.file)));
short[] buffer = new short[Buffersize / 2]; //870 double/ 2 = 435 double
System.out.println("The buffer size is " + Buffersize);
timer1();
audioRecord.startRecording(); // Start record
while (MainActivity.isrecord) {
int bufferReadResult = audioRecord.read(buffer, 0, buffer.length);
System.out.println("The buffer size is " + bufferReadResult);
for (int i = 0; i < bufferReadResult/2; i++) {
dos.writeShort(buffer[i]);
**tempraw[i] = (double)buffer[i];**
}
phase = DataProcess(tempraw);
}
audioRecord.stop(); // record stop
audioRecord.release();
audioRecord = null;
dos.close(); // Output stream close
dos = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我想把这些数据放在“short [] buffer”到“double [] tempraw”
谢谢!
在我从互联网上查看一些代码之后,我做了这个。我觉得它很有用,只是很慢,2毫秒一双
private static double shorttodouble(short[] a, int index) {
// TODO Auto-generated method stub
long l;
l = a[index + 0];
l &= 0xffff;
l |= ((long) a[index + 1] << 16);
l &= 0xffffffffl;
l |= ((long) a[index + 2] << 32);
l &= 0xffffffffffffl;
l |= ((long) a[index + 3] << 48);
l &= 0xffffffffffffffffl;
l |= ((long) a[index + 4] << 64);
return (double)l;
}
答案 0 :(得分:1)
就像int数组到double数组一样,你需要一个循环来完成这项工作:
short[] shorts = {1,2,3,4,5};
double[] doubles = new double[shorts.length];
for (int i = 0; i < shorts.length; i ++) {
doubles[i] = shorts[i];
System.out.println(doubles[i]);
}
低效但有效。
答案 1 :(得分:1)
试试这个:---
short[] buffer = new short[size];
double[] transformed = new double[buffer.length];
for (int j=0;j<buffer.length;j++) {
transformed[j] = (double)buffer[j];
}