mrcoles / javascript-piano

An HTML5 JavaScript synth piano that generates all of its sounds using data URIs.
http://mrcoles.com/piano/
MIT License
134 stars 52 forks source link

enhancement: Well Tempered tuning #1

Open lacojunior opened 11 years ago

lacojunior commented 11 years ago
function noteToFreq(stepsFromMiddleC) {
        //Thomas Young 1799 Well Temperament frequency table taken from http://www.math.uwaterloo.ca/~mrubinst/tuning/tuning.html
        thomasYoungWellTemperament = { table : {
            0:1, 
            1:1.055730636,
            2:1.119771437,
            3:1.187696971,
            4:1.253888072,
            5:1.334745462,
            6:1.407640848,
            7:1.496510232,
            8:1.583595961,
            9:1.675749414,
            10:1.781545449,
            11:1.878842233},
            positionInTable : function(stepsFromMiddleC) {
                return stepsFromMiddleC < 0 ? 12 - (-stepsFromMiddleC % 12) : stepsFromMiddleC % 12;
            },
            octaveFromMiddleC : function(stepsFromMiddleC) {
                return Math.floor(stepsFromMiddleC / 12);
            }
        };
        var stepsFromA440 = stepsFromMiddleC + 4;
        var freq = 440 * Math.pow(2, thomasYoungWellTemperament.octaveFromMiddleC(stepsFromA440) - 1 
            + thomasYoungWellTemperament.table[thomasYoungWellTemperament.positionInTable(stepsFromA440)]);
        return freq;
    }