How to create sound for one channel like left speaker or right speaker
dsithija opened this issue · 4 comments
Hi Friends,
Can you teach me how to make sound works for only one channel. I want to make it sound from left speaker or the right speaker.
Thank you.
This would require some changes to the code:
- https://github.com/loov/jsfx/blob/master/jsfx.js#L7 change numChannels to 2.
- Before this https://github.com/loov/jsfx/blob/master/jsfx.js#L970, stride all data values with 0.
With regards to 2. point: M = Mono
, R = Right
, L = Left
. The data array contains all the samples [M, M, M, M ... ], stereo needs to have them in [L, R, L, R, L ... ]..., this means you need to convert the original data vector to [M, 0, M, 0 ...] or [0, M, 0, M ...], depending what you want.
Something like this should do:
var mono = data;
data = new createFloatArray(mono.length);
for(var i = 0; i < mono.length; i++){
data[i*2] = mono[i];
}
Hi Egon,
Thank you. It works but sound was different a bit. It must be because data[i*2] exceeds the array length in half way through the for loop. So it assigns half of the full wave as a mono sound. So changed the for loop like this.
var mono = data;
data = new createFloatArray(mono.length);
for(var i = 0; i < mono.length; i+=2){
data[i] = mono[i];
}
Nope, now the sound is two times shorter.... it should be data = new createFloatArray(mono.length * 2);
i.e.
var mono = data;
data = new createFloatArray(mono.length * 2);
for(var i = 0; i < mono.length; i++){
data[i*2] = mono[i];
}
Was tired yesterday, missed the multiplication.
Ok. Thanks again.