0

I am trying to join byte-arrays of wav-sound and it works except for backgroundnoise. Anyone knows any algoritm to add two byte-arrays of sound.

This is what I have tried so far

for(int i=0;i<bArr1.length;i++)
{
   bArrJoined[i]=bArr1[i] + bArr2[i];
}

also tried to divide by 2 not to be to high numbers

for(int i=0;i<bArr1.length;i++)
{
   bArrJoined[i]=(bArr1[i] + bArr2[i]) / 2;
}

Anyone knows how to make this work without the noise?

bajen micke
  • 309
  • 1
  • 7
  • 18
  • This may be of interest [http://stackoverflow.com/questions/1281353/use-java-ffmpeg-wrapper-or-simply-use-java-runtime-to-execute-ffmpeg] – Curious Nov 23 '16 at 23:35
  • Hi, thanks for the advice but that was not really what I was looking for. – bajen micke Nov 24 '16 at 16:28

1 Answers1

0

A number of things could cause artifacts here. Different audio sampling rates or data bit sizes could do it.

Assuming those are non-issues, you should be aware you can't add a byte with another byte without overflow (256 will become 0, etc.). So convert to int before adding. Clipping will occur if you exceed the max volume, so your divide by 2 operation is smart and should stop that issue. The divide operation should occur with the int versions. Only cast back to byte at the end.

However, if you aren't working with 8-bit audio, then a byte is not your atomic unit. For example, 16-bit audio uses 2 bytes and you would need to convert every two consecutive bytes to an int (with respect to proper endianness) before you perform any mathematical operations on the values. 32-bit audio data occupies 4 consecutive bytes for each single numeric value. Just having an array of bytes does not in itself tell you where the data boundaries are.

Special Sauce
  • 5,338
  • 2
  • 27
  • 31
  • Hi. Thanks for the answer. It is audioclips of same original sound, so samplerate etc is the same and also the bit sizes, I only tried 1 byte as in 8-bit. That may be the problem, I now have some ideas to work with. One small thing I have considered is if for instance a value is 3 and the next is 4= 7, 7 devided to int is just the integer so the wave is not the exact wave anymore. Very small changes but maybe that can effect the soundquality? – bajen micke Nov 24 '16 at 16:28
  • 8-bit audio is rare nowadays, so I'm sure that's your problem if you are unsure of your audio's bit size (it is contained in the .wav file header information or in the .mp3 header if you decoded an MP3 stream to WAVE data). The integer division truncation you refer to would only be an issue at 8-bit audio. Because it only has 255 discrete value steps, it's possible a discerning ear might pick up on this. But with 16-bit audio or higher, a human ear can't tell the difference between 1 discrete value step (out of 65,535 discrete steps total for 16-bit). – Special Sauce Nov 25 '16 at 02:02
  • Yes. I have used it wrong with the bits. I have made the integer as if it was 8-bit sound with adding the integers from every byte instead of making integer of 16-bit. Thanks – bajen micke Nov 25 '16 at 17:21