loov / jsfx

Javascript Sound Effect Generator
MIT License
576 stars 49 forks source link

How to create sound for one channel like left speaker or right speaker #13

Closed dsithija closed 8 years ago

dsithija commented 8 years ago

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.

egonelbre commented 8 years ago

This would require some changes to the code:

  1. https://github.com/loov/jsfx/blob/master/jsfx.js#L7 change numChannels to 2.
  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];
}
dsithija commented 8 years ago

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];
}
egonelbre commented 8 years ago

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.

dsithija commented 8 years ago

Ok. Thanks again.