2014-04-05 98 views
0

我有一个.au音频文件,我试图将其复制到另一个音频文件,并且我希望复制的音频文件具有一半的音量。我写了下面的代码,并生成以下音频文件:音频文件 - 给定字节帧操纵音量 - Java

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 mu-law),2个通道,每帧2个字节,采样率为48000.(它可以在Encoding 3上正常工作,但不能编码为1)。预先感谢任何帮助提供。

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

回答

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 

因为这样做有点换挡时似乎顺理成章!

+0

我想我现在需要它的工作方式。谢谢。 – user1567060