如何正常化混合WAV文件 [英] How to normalize mixed WAV files
本文介绍了如何正常化混合WAV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将两种WAV文件。
的WAV文件是作为字节数组,我使用下面code混合使用两种。
字节[]字节1,字节[]字节2
// 44 wav文件的标题
的for(int i = 44; I< byte1.length;我++){
字节1 [i] =字节1 [I] +字节2 [I]
}
以上code主要工作。但是,当其结果是大于最大波(16位音频文件),它有噪声。我怎样才能恢复正常的混合声音?
解决方案
短[] audioData1 = NULL;
短[] audioData2 = NULL;
INT N = 0;
尝试 {
IN1的DataInputStream;
IN1 =新的DataInputStream(新的FileInputStream(v1.wav));
ByteArrayOutputStream BOS =新ByteArrayOutputStream();
尝试 {
而((N = in1.read())!= - 1){
bos.write(N);
}
}赶上(IOException异常E){
e.printStackTrace();
}
ByteBuffer的BB = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer某人= bb.asShortBuffer();
audioData1 =新的短[sb.capacity()];
的for(int i = 0; I< sb.capacity();我++){
audioData1 [I] = sb.get(ⅰ);
}
}赶上(IOException异常E){
e.printStackTrace();
}
尝试 {
IN1的DataInputStream;
IN1 =新的DataInputStream(新的FileInputStream(v2.wav));
ByteArrayOutputStream BOS =新ByteArrayOutputStream();
尝试 {
而((N = in1.read())!= - 1){
bos.write(N);
}
}赶上(IOException异常E){
e.printStackTrace();
}
ByteBuffer的BB = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer某人= bb.asShortBuffer();
audioData2 =新的短[sb.capacity()];
sb.get(audioData2);
的System.out.println();
}赶上(IOException异常E){
e.printStackTrace();
}
//找到最大:
浮动最大= 0;
的for(int i = 22; I< audioData1.length;我++){
如果(Math.abs(audioData1 [I] + audioData2 [I])>最大)
最大= Math.abs(audioData1 [I] + audioData2 [I]);
}
的System.out.println(+(Short.MAX_VALUE - 最大值));
诠释A,B,C;
//现在发现的结果,具有缩放:
的for(int i = 22; I< audioData1.length;我++){
一个= audioData1 [I];
B = audioData2 [I];
C = Math.round(Short.MAX_VALUE *(audioData1 [I] + audioData2 [I])
/最大);
如果(c取代; Short.MAX_VALUE)
C = Short.MAX_VALUE;
如果(℃下Short.MIN_VALUE)
C = Short.MIN_VALUE;
audioData1 [I] =(短期)C;
}
//转向空头回字节。
byte []的结束=新的字节[audioData1.length * 2];
ByteBuffer.wrap(完).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer()把(audioData1)。
尝试 {
的OutputStream OUT =新的FileOutputStream(mixer.wav);
的for(int i = 0; I< end.length;我++){
out.write(完[I]);
了out.flush();
}
out.close();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
这个作品,感谢所有的答案
I am trying to mix two WAV files.
The WAV files are available as byte arrays and I am using below code to mix the two.
byte[] byte1 , byte[] byte2
// 44 is header of wav file
for( int i = 44 ; i < byte1.length ; i++){
byte1[i] = byte1[i] + byte2[i];
}
The above code mostly works. But when the result is more than maximum wave (16 bit audio file), it has noise. How can I normalize mixed sound?
解决方案
short[] audioData1 = null;
short[] audioData2 = null;
int n = 0;
try {
DataInputStream in1;
in1 = new DataInputStream(new FileInputStream("v1.wav"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((n = in1.read()) != -1) {
bos.write(n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
audioData1 = new short[sb.capacity()];
for (int i = 0; i < sb.capacity(); i++) {
audioData1[i] = sb.get(i);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
DataInputStream in1;
in1 = new DataInputStream(new FileInputStream("v2.wav"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((n = in1.read()) != -1) {
bos.write(n);
}
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
audioData2= new short[sb.capacity()];
sb.get(audioData2);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
// find the max:
float max = 0;
for (int i = 22; i < audioData1.length; i++) {
if (Math.abs(audioData1[i] + audioData2[i]) > max)
max = Math.abs(audioData1[i] + audioData2[i]);
}
System.out.println("" + (Short.MAX_VALUE - max));
int a, b, c;
// now find the result, with scaling:
for (int i = 22; i < audioData1.length; i++) {
a = audioData1[i];
b = audioData2[i];
c = Math.round(Short.MAX_VALUE * (audioData1[i] + audioData2[i])
/ max);
if (c > Short.MAX_VALUE)
c = Short.MAX_VALUE;
if (c < Short.MIN_VALUE)
c = Short.MIN_VALUE;
audioData1[i] = (short) c;
}
// to turn shorts back to bytes.
byte[] end = new byte[audioData1.length * 2];
ByteBuffer.wrap(end).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(audioData1);
try {
OutputStream out = new FileOutputStream("mixer.wav");
for (int i = 0; i < end.length; i++) {
out.write(end[i]);
out.flush();
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this works, thanks all for answers
这篇关于如何正常化混合WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文