[英]Java - converting byte array of audio into integer array
我需要将音频数据作为“16位整数数组”传递到第三方系统(来自我有限的文档)。
这是我到目前为止所尝试的(系统从生成的bytes.dat文件中读取它)。
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:\\all.wav"));
int numBytes = inputStream.available();
byte[] buffer = new byte[numBytes];
inputStream.read(buffer, 0, numBytes);
BufferedWriter fileOut = new BufferedWriter(new FileWriter(new File("c:\\temp\\bytes.dat")));
ByteBuffer bb = ByteBuffer.wrap(buffer);
while (bb.remaining() > 1) {
short current = bb.getShort();
fileOut.write(String.valueOf(current));
fileOut.newLine();
}
这似乎不起作用 - 第三方系统无法识别它,我也无法将文件作为原始音频导入Audacity。
有什么明显的我做错了,还是有更好的方法呢?
额外信息:波形文件是16位,44100Hz,单声道。
编辑2:我很少使用AudioInputStream,但你写出原始数据的方式似乎相当复杂。 文件只是一堆后续字节,因此您可以使用一个FileOutputStream.write()调用来编写音频字节数组。 系统可能使用大端格式,而WAV文件存储在小端(?)中。 然后你的音频可能会发挥,但非常默默。
编辑3
删除了代码示例。
您是否有理由将音频字节作为字符串写入带有换行符的文件中? 我认为系统需要二进制格式的音频数据,而不是字符串格式。
我刚刚设法解决了这个问题。
在创建ByteBuffer之后我不得不添加这一行。
bb.order(ByteOrder.LITTLE_ENDIAN);
AudioFileFormat audioFileFormat;
try {
File file = new File("path/to/wav/file");
audioFileFormat = AudioSystem.getAudioFileFormat(file);
int intervalMSec = 10; // 20 or 30
byte[] buffer = new byte[160]; // 320 or 480.
AudioInputStream audioInputStream = new AudioInputStream(new FileInputStream(file),
audioFileFormat.getFormat(), (long) audioFileFormat.getFrameLength());
int off = 0;
while (audioInputStream.available() > 0) {
audioInputStream.read(buffer, off, 160);
off += 160;
intervalMSec += 10;
ByteBuffer wrap = ByteBuffer.wrap(buffer);
int[] array = wrap.asIntBuffer().array();
}
audioInputStream.close();
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.