有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

音频文件操作给定字节帧的音量Java

我有一本书。au音频文件,我正试图复制到另一个音频文件,我希望复制的音频文件有一半的音量。我编写了以下代码,并生成以下音频文件:

for (int i = 24; i < bytes.length; i++) {
    // bytes is a byte[] array containing every byte in the .au file
    if (i % 2 == 0) {
        short byteFrame = (short) (((bytes[i - 0]&0xFF) << 8) | ((bytes[i - 1]&0xFF)));
        byteFrame >>= 1;
        bytes[i - 0] = (byte) (byteFrame);
        bytes[i - 1] = (byte) (byteFrame >>> 8);
    }
}

我从代码中得到的数据如下: enter image description here

下面的代码与上面的代码相同,只有“字节[i-0]”和“字节[i-1]”交换了位置。当我这样做时,通道中的信息会被交换到另一个通道

for (int i = 24; i < bytes.length; i++) {
    // bytes is a byte[] array containing every byte in the .au file
    if (i % 2 == 0) {
        short byteFrame = (short) (((bytes[i - 0]&0xFF) << 8) | ((bytes[i - 1]&0xFF)));
        byteFrame *= 0.5;
        bytes[i - 1] = (byte) (byteFrame);
        bytes[i - 0] = (byte) (byteFrame >>> 8);
    }
}

我从代码中获得的数据如下(通道中的信息已交换): enter image description here

我需要把两个频道的音量都减少一半。下面是关于au文件格式的维基百科页面。有没有办法让它在减少音量时正常工作?该文件编码为1(8位G.711μ定律)、2个通道、每帧2字节,采样率为48000。(它可以在编码3上正常工作,但不能在编码1上正常工作。)提前感谢您提供的任何帮助

http://en.wikipedia.org/wiki/Au_file_format


共 (1) 个答案

  1. # 1 楼答案

    使用ByteBuffer。看起来您使用的是16位的小尾端数量,并且您希望将它们右移1

    因此:

    final ByteBuffer orig = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)
        .asReadOnlyBuffer();
    
    final ByteBuffer transformed = ByteBuffer.wrap(bytes.length)
        .order(ByteOrder.LITTLE_ENDIAN);
    
    while (orig.hasRemaining())
        transformed.putShort(orig.getShort() >>> 1);
    
    return transformed.array();
    

    注意>>>是必需的;否则你会带上标志

    也就是说,尝试在以下方面使用>> 1

    1001 0111
    

    将提供:

    1100 1011
    

    也就是说,符号位(最高有效位)被携带。这就是为什么Java中存在>>>,它不携带符号位,因此在上面使用>>> 1将给出:

    0100 1011
    

    在进行位移位时,这似乎是合乎逻辑的