java音频文件分帧处理,音频文件-处理给定字节帧的音量-Java

I have a .au audio file that I am trying to copy to another audio file, and I want the copied audio file to have half the volume. I have written the following code and it produces the following audio file:

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);

}

}

The data I get from that code is this:

kt5Z9.png

The following code is the same as above, only 'bytes[i - 0]' and 'bytes[i - 1]' have switched places. When I do that, the information in the channels gets swapped to the other channel.

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);

}

}

The data I get from that code is this (Information in the channels has been swapped):

WJUUB.png

I need to reduce the volume in both channels by half. Below is the wikipedia page on the au file format. Any ideas on how to get it to work properly in reducing the volume? This file is encoding 1 (8-bit G.711 mu-law), 2 channels, 2 bytes per frame, and sample rate of 48000. (It works properly on Encoding 3 but not encoding 1.) Thanks in advance for any help offered.

解决方案

Use a ByteBuffer. It appears that you use 16 bit quantities in little endian order, and that you want to right shift them by 1.

Therefore:

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();

Note that the >>> is necessary; otherwise you carry the sign bit.

That is, trying to use >> 1 on:

1001 0111

will give:

1100 1011

ie, the sign bit (the most significant bit) is carried. This is why >>> exists in Java, which DOES NOT carry the sign bit, therefore using >>> 1 on the above will give:

0100 1011

As seems logical when doing bit shifting!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值