Search code examples
javascriptwaveformsynthesizer

What is this (128 + 127 *) part of these formulas for riffwave.js?


I'm trying to do some javascript music synth in browser, and I came across riffwave.js. From this question here, the answer gives a hint on how one could use riffwave.js.

I've worked through it a bit to figure out some things like multiple tones, and I understand nearly all of it, but I don't know why there's the 128 + 127 * in there.

It also shows up here in this demo page.

Can anyone offer an explination or tell me how I can use that part to modify the program?

Thanks.


Solution

  • The examples that you referenced show an equation in the form:

    A=128 + 127 * Sin(...)
    

    Since the Sin function can vary from -1 to +1, the result of the above equation is 1-255. So, adding the constant 128 and multiplying by the coefficient 127, this is basically a sin function whose output varies between 1 and 255, which is convenient because it can be stored using 8 bits (or 1 byte).