我想将一个wav文件转换为一个double数组来修改它的值,这是我的代码,但它不能正常工作:
private void bytesToDouble(File file) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
out.flush();
byte[] bytes = out.toByteArray();
int times = Double.SIZE / Byte.SIZE;
doubleArray = new double[bytes.length / times];
for(int i=0;i<doubleArray.length;i++){
doubleArray[i] = ByteBuffer.wrap(bytes, i*times, times).getDouble();
}
}
这里我有一些示例输出:
bytes = {byte[148012]@4655}
0 = 82
1 = 73
2 = 70
3 = 70
4 = 36
5 = 66
6 = 2
7 = 0
8 = 87
9 = 65
10 = 86
11 = 69
12 = 102
13 = 109
14 = 116
15 = 32
16 = 16
17 = 0
18 = 0
19 = 0
20 = 1
21 = 0
22 = 2
23 = 0
24 = 68
25 = -84
26 = 0
27 = 0
28 = 16
29 = -79
30 = 2
31 = 0
32 = 4
33 = 0
34 = 16
35 = 0
36 = 100
37 = 97
38 = 116
39 = 97
40 = 0
41 = 66
42 = 2
43 = 0
44 = 0
45 = 0
46 = 0
47 = 0
48 = 0
49 = 0
50 = 0
51 = 0
52 = 0
53 = 0
54 = 0
55 = 0
56 = 0
57 = 0
58 = 0
59 = 0
60 = 0
61 = 0
62 = 0
63 = 0
64 = 0
其余字节也只是零。字节数组的大小约为150.000。输入是一个普通的音频wav文件。
这是我在修改双数组后将其转换回来的方式:
for (double aDoubleArray : doubleArray) {
byteArray = ByteBuffer.allocate(8).putDouble(aDoubleArray).array();
}
但要么它在第一次或第二次转换中都不起作用。 谢谢你的帮助!