2

请耐心等待,因为我对音频工作非常陌生,而且我已经在谷歌上搜索了好几天的解决方案,但没有找到任何解决方案。

所以我用这个检索 .wav 文件的字节数组(来源:Wav 文件转换为 java 中的字节数组

ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(WAV_FILE));

int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
     out.write(buff, 0, read);
}
out.flush();
byte[] audioBytes = out.toByteArray();

然后我将字节数组转换为浮点数组并将其从 -1.0 标准化为 1.0。(来源:将 wav 音频格式字节数组转换为浮点数

ShortBuffer sbuf =
ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
short[] audioShorts = new short[sbuf.capacity()];
sbuf.get(audioShorts);

float[] audioFloats = new float[audioShorts.length];
for (int i = 0; i < audioShorts.length; i++) {
    audioFloats[i] = ((float)audioShorts[i])/0x8000;
}
return audioFloats;

后来我将其转换为使用 java.swing 输出波形的线条图

class Panel2 extends JPanel {
float[] audioFloats;

    Dimension d;
    public Panel2(Dimension d, float[] audioFloats) {
        // set a preferred size for the custom panel.
        this.d = d;
        setPreferredSize(d);
        this.audioFloats = audioFloats;
    }


    @Override
    public void paint(Graphics g) {
        //super.paintComponent(g);
        super.paint(g); 

        //shift by 45 because first 44 bytes used for header
        for (int i = 45; i<audioFloats.length; i++){

            Graphics2D g2 = (Graphics2D) g;
            float inc = (i-45)*((float)d.width)/((float)(audioFloats.length-45-1));
            Line2D lin = new Line2D.Float(inc, d.height/2, inc, (audioFloats[i]*d.height+d.height/2));
            g2.draw(lin);

        }


    }
}

波形仅适用于 16 位 wav 文件(我与 goldwave 交叉检查,我的波形和它们的波形对于 16 位看起来都相似)。

我如何为 8 位 .wav 文件执行此操作?

因为这是为了家庭作业,我唯一的限制是逐字节读取 wav 文件。

我也知道 wav 文件是 PCM 编码的,并保留前 44 个字节作为标题

4

2 回答 2

0

音频流通常与一个数据通道交错,然后是相反的数据通道。例如,前 16 位是左声道,接下来的 16 位是右声道。这些中的每一个都被认为是一帧数据。我会确保您的 8 位流只有一个通道,因为看起来这些方法只设置为读取一个通道。

同样在您的示例中,将您抓取单个通道的帧转换为一个短,然后通过将其除以 0x8000 十六进制或有符号短的最大值来找到一个小数。

short[] audioShorts = new short[sbuf.capacity()];
sbuf.get(audioShorts);
...
audioFloats[i] = ((float)audioShorts[i])/0x8000;

我的猜测是,您需要将 8 字节流作为“字节”类型而不是短类型读取,然后将其除以 128 或有符号 8 位值的最大值。这将涉及制作一种全新的方法来处理 8 位流而不是 16 位流。进行以下更改。

byte[] audioBytes = new byte[sbuf.capacity()];
sbuf.get(audioBytes);
...
audioFloats[i] = ((float)audioBytes[i])/0x80;
于 2012-10-10T20:31:01.197 回答
0

您需要修改这部分代码:

ShortBuffer sbuf =
  ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
short[] audioShorts = new short[sbuf.capacity()];
sbuf.get(audioShorts);

float[] audioFloats = new float[audioShorts.length];
for (int i = 0; i < audioShorts.length; i++) {
    audioFloats[i] = ((float)audioShorts[i])/0x8000;
}

你根本不需要ByteBuffer——你已经有了你的字节数组。所以只需将其转换为浮点数:

float[] audioFloats = new float[audioBytes.length];
for (int i = 0; i < audioBytes.length; i++) {
    audioFloats[i] = ((float)audioBytes[i])/0x80;
}
于 2012-10-10T20:22:40.733 回答