2

我能够将我的音频转换为字节值。

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。任何人都可以帮助我如何做到这一点

4

1 回答 1

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());
    }
于 2013-04-15T13:19:38.297 回答