audio - Java: Mixing two WAV files without introducing noise -


i'm trying mix 2 wav files single wav file. files exact same duration , have same format (16 bit, 44.1 khz, signed, little endian, mono). both wavs put byte arrays using bytearrayoutputstream using audiosystem.getaudioinputsream ensure i'm getting pcm data , no headers.

with of few other threads, i've been able combine arrays, not without introducing significant amount of noise signal. doesn't sound clipping or distortion, have tried averaging sum of each pair of bytes safety makes quieter, noise , all.

any insight appreciated! i've tried 2 methods should same thing, , seem produce same sonic results.

the first, simpler method:

private byte[] mixbuffers(byte[] buffera, byte[] bufferb) {     byte[] array = new byte[buffera.length];     (int i=0; i<buffera.length; i++) {         array[i] = (byte) ((buffera[i] + bufferb[i]));     }     return array; } 

and second, more specific method:

private byte[] mixbuffers(byte[] buffera, byte[] bufferb) {     byte[] array = new byte[buffera.length];      (int i=0; i<buffera.length; i+=2) {         short buf1a = buffera[i+1];         short buf2a = buffera[i];         buf1a = (short) ((buf1a & 0xff) << 8);         buf2a = (short) (buf2a & 0xff);          short buf1b = bufferb[i+1];         short buf2b = bufferb[i];         buf1b = (short) ((buf1b & 0xff) << 8);         buf2b = (short) (buf2b & 0xff);          short buf1c = (short) (buf1a + buf1b);         short buf2c = (short) (buf2a + buf2b);          short res = (short) (buf1c | buf2c);          array[i] = (byte) res;         array[i+1] = (byte) (res >> 8);     }      return array; } 

if helps, here's how i'm getting files byte arrays:

{     bytearrayoutputstream baos = new bytearrayoutputstream();     audioinputstream ais;     ais = audiosystem.getaudioinputstream(audiofile);     int read;     byte[] buffer = new byte[1024];     while ((read = ais.read(buffer)) != -1) {         baos.write(buffer, 0, read);     }     baos.flush();     byte[] bytebuffera = baos.tobytearray();      baos = new bytearrayoutputstream();     ais = audiosystem.getaudioinputstream(audiofile2);     buffer = new byte[1024];     while ((read = ais.read(buffer)) != -1) {         baos.write(buffer, 0, read);     }     baos.flush();     byte[] bytebufferb = baos.tobytearray();      byte[] bytebufferc = mixbuffers(bytebuffera, bytebufferb); } 

these files contain 16-bit samples - each sample 2 bytes long. however, first method tries mix each byte independently. result adding number adding each digit (without carry): 165 + 248 -> 103.

your second method using bitwise or instead of addition. that's adding 2 numbers taking greatest value of each digit: 165 + 248 -> 268.

try using second method, replacing | +.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -