音频到实数

时间:2013-04-15 09:56:28

标签: java audio

我可以将音频转换为字节值。

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/scream2.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();


    }
}

我想要识别其中包含尖叫声的音频。为此我需要将音频转换为实数,以便我可以对其应用fft。任何人都可以帮助我如何做到这一点

1 个答案:

答案 0 :(得分:1)

我想出了这段代码并对其进行了测试。我希望它有所帮助。我正在分配我之前创建的4个浮点数(作为字节)并转换为字节。然后我使用ByteBuffer的NIO FloatBuffer视图,因此NIO会自动返回4个字节作为浮点数而无需进一步处理。

    ByteBuffer bb = ByteBuffer.allocate(4*4);
    bb.put(new byte[]{64,-112,0,0,66,-10, 22,-68, 66,9, 73, -43, 63,-114, 56, -38});
    bb.rewind();
    FloatBuffer floatBuffer = bb.asFloatBuffer();


    for(int i = 0; i < 4;i++){
        System.out.println(floatBuffer.get());
    }