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.