java - How do I combine two AudioInputStream? -
the file format "pcm_signed 44100.0 hz, 16 bit, stereo, 4 bytes/frame, little-endian", , want add them while amplifying 1 of 2 files. plan read 2 wav put them 2 audioinputstream instances, store instances 2 byte[] array, manipulate in arrays, , return audioinputstream instance.
i have done lot of research have got no results. know class www.jsresources.org mixing 2 audioinputstream, doesn't allow me modify either of 2 streams before mixing while want decrease 1 of streams before mixing them. think should do?
to this, can convert streams pcm data, multiply channel volume wish change desired factor, add pcm data results together, convert bytes.
to access audiostreams on per-byte basis, check out first extended code fragment @ java tutorials section on using files , format converters. shows how array of sound byte data. there comment reads:
// here, useful audio data that's // in audiobytes array...
at point, iterate through bytes, converting pcm. set of commands based on following should work:
for (int = 0; < numbytes; += 2) { pcma[i/2] = audiobytesa[i] & 0xff ) | ( audiobytesa[i + 1] << 8 ); pcmb[i/2] = audiobytesb[i] & 0xff ) | ( audiobytesb[i + 1] << 8 ); }
in above, audiobytesa , audiobytesb 2 input streams (names based on code example), , pcma , pcmb either int arrays or short arrays, holding values fit within range of short. might best make pcm arrays floats since doing math result in fractions. using floats in example below adds 1 place worth of accuracy (better rounding when using int), , int perform faster. think using floats more done if audio data gets normalized use additional processing.
from there, best way change volume multiply every pcm value same amount. example, increase volume 25%,
pcma[i] = pcma[i] * 1.25f;
then, add pcma , pcmb, , convert bytes. might want put in min or max functions ensure volume & merging not exceed values can fit in format's 16 bits.
i use following convert bytes:
for (int = 0; < numbytes; i++) { outbuffer[i*2] = (byte) pcmcombined[i]; outbuffer[(i*2) + 1] = (byte)((int)pcmcombined[i] >> 8 ); }
above assumes pcmcombined[] float array. conversion code can bit simpler if short[] or int[] array.
i cut , pasted above dev work did programs posted @ my website, , edited scenario, if there typo or bug crept in, please let me know in comments , fix it.
Comments
Post a Comment