2014-04-05 47 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