.wav转换为数组

时间:2013-04-11 07:44:21

标签: java arrays wav

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;


public class Audio_to_bytes {

    public static void main(String args[]) throws IOException
{
    File WAV_FILE = new File("/home/cybersecurity/Desktop/scream.wav");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    AudioInputStream in = null;
    try {
        in = AudioSystem.getAudioInputStream(WAV_FILE);
    } catch (UnsupportedAudioFileException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


    int read,i;
    byte[] buff = new byte[1024];
    while ((read = in.read(buff)) > 0)
    {
        out.write(buff, 0, read);
    }
    out.flush();
    byte[] audioBytes = out.toByteArray();
    for(i=0;i<audioBytes.length;i++)
        System.out.println(audioBytes[i]);
}
}

上面的代码是将.wav文件转换为字节数组。当我执行程序时,我得到许多值为-128或-127,我想知道这些值是精确值还是值变为如果超出字节范围(-128到128),则为边界值, 如果可以,我可以将音频文件转换为整数数组,因为它具有更宽的范围。

1 个答案:

答案 0 :(得分:2)

原始文件无关紧要,因为WAVE文件是使用块构建的。我建议先了解这些块RIFF File Format,以了解WAVE文件。