gpt4 book ai didi

android - 如何将 short[] 转换为 double[]?

转载 作者:行者123 更新时间:2023-11-29 19:33:00 24 4
gpt4 key购买 nike

现在,我正在处理一个音频信号,我想实时显示我用手机录制的音频声波。

这是一个问题,我的音频格式是“ENCODING_PCM_16BIT”。那么如何将 16 位短数据转换为 double 格式呢?

这是我的代码,但它不能正常工作。谁能帮我解决这个问题?

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”

谢谢!

在我从网上看了一些代码后,我做了这个。我认为它有效,只是很慢,一个 double 需要 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;
}

最佳答案

试试这个:---

 short[] buffer = new short[size];
double[] transformed = new double[buffer.length];
for (int j=0;j<buffer.length;j++) {
transformed[j] = (double)buffer[j];
}

关于android - 如何将 short[] 转换为 double[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39629680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com