Hi,
I'm trying to process my byte array which I got from the sampled sourcedataline (Java Sound API). If I'm multiplying the byte array with a fraction number, I will get noise while playing the stream.
Before I'm playing the sound I separate the stereo wav file into his left and right channel. This works fine. But if I want to process the channels with a gain control, which depends on a delay factor, I get noise.
for(int i=0; i<bufferSize; i++) { array[i] = (byte) (array[i] * gain); }
Does anyone know how to fix the problem?
//EDIT:
I tried to convert the two bytes into a short (2bytes) with bit shifting e.g.:
short leftMask = 0xff00;
short rightMask = 0x00ff;
short sValue = (array[i] + array[i+1] <<8) * gain;
array[i] = (sValue & leftMask) >> 8;
array[i+1] = (sValue & rightMask);
but I got the same when I just multiply the single bytes with the gain value.
//EDIT
OR should I just add the two array values into a short like this?
short shortValue = array[i] + array[i+1];
shortValue *= gain;
array[i] = ???
But how do I convert this short into the 2 single bytes without losing the sound?
//EDIT some code from the separating method:
public static void channelManipulation(byte[] arrayComplete) {
int i=2;
char channel='L';
int j=0;
/**
* The stereo stream will be divided into his channels - the Left and the Right channel.
* Every 2 bytes the channel switches.
* While data is collected for the left channel the right channel will be set by 0. Vice versa.
*/
while(j<arrayComplete.length) {
//while we are in the left channel we are collecting 2 bytes into the arrayLeft
while(channel=='L') {
if(i==0) {
channel='R'; //switching to the right channel
i=2;
break;
}
arrayLeft[j] = (byte)(arrayComplete[j]);
arrayRight[j] = 0;
i--; j++;
}
//while we are in the right channel we are collecting 2 bytes into the arrayRight
while(channel=='R') {
if(i==0) {
channel='L'; //switching to the left channel
i=2;
break;
}
arrayRight[j] = (byte) (arrayComplete[j]);
arrayLeft[j] = 0;
i--; j++;
}
}
}