简体   繁体   English

如何使用FileInputStream将音频文件读取到具有2个字节数据的数组中

[英]How can I use FileInputStream to read a audio file into an array with 2 bytes data

In another word, the file was write with PCM_16bit, but those data was store as 8-bit. 换句话说,该文件是使用PCM_16bit写入的,但是这些数据被存储为8位。 I want to ananlyze this file with dsp, but how can I read this file in 16 bit a time and form this 16bit as one integer from 0-65535. 我想用dsp分析此文件,但是如何一次以16位读取此文件并将16位形成为0-65535之间的一个整数。

Not entirely sure what you're asking, but if what you want is to read two-bytes at a time as a single unsigned value, you can use something like this: 不能完全确定您要问的是什么,但是如果要一次将两个字节读取为一个无符号值,则可以使用以下方法:

File f = new File("/path/to/file");
DataInputStream dis = new DataInputStream(new FileInputStream(f));

List<Integer> values = new ArrayList<>();

try {
    while (true){
        values.add(dis.readUnsignedShort());
    }
} catch(EOFException e){
    /* you've read everything at this point */
} finally {
    dis.close();
}

You can change values into a primitive array at that point, or just work with the list directly. 您可以在那时将values更改为原始数组,或者直接使用列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM