sketch

by mohayonao

HTML

<script src="http://mohayonao.github.io/neume.js/build/neume.min.js"></script>
<button id="start">start</button>
<h4>amp</h4><pre id="amp"></pre>

JavaScript

var score = [[4,9],0,0,0,0,0,[1],0,0,0,[5],0,0,[4,8,11],0,0,0,[1],0,0,[4,9],[0],0,0,[1],0,[4,6,8,11],0,0,0,0,[5,7,9,12]];

var pt2map = [
    71, 67, 0, 65, 64, 62, 60, 59, 57, 55, 0, 52, 50
];

var amp = new Float32Array(13);

var Neume = neume(new AudioContext());

var Synth = new Neume(function($, i) {
    var out;
    
    out = $("sin", { freq: mtof(pt2map[i]) });
    out = $(amp, { key: i, interval: 14, timeConstant: 13 }, out);
    out = $("asr", {
        a: 4.78, s: 0.4, r: 46.4
    }, out).release("+12").on("end", function(e) {
        this.stop(e.playbackTime);
    });
    
    return $("out", { bus: 1 }, out);
});

var Efx = new Neume(function($) {
    var out;
    
    out = $("in", 1);
    
    return out;
});

function mtof(m) {
  return 440 * Math.pow(2, (m - 69) / 12);
}

var synth = null;
var timer = null;

$("#start").on("click", function() {
    if (synth) {
        synth.fadeOut("+0", 0.5);
        synth = null;
        timer.stop();
        timer = null;
        $(this).css("color", "black");
    } else {
        synth = new Efx().start();
        timer = new Neume.Interval(2, function(e) {
            var i = e.count % score.length;
            
            if (e.count % 5 === 0) {
                receiveOSC();
            }
            
            score[i] && score[i].forEach(function(i) {
                i && new Synth(i).start();
            });
        }).start();
        $(this).css("color", "red");
    }
});

function receiveOSC() {
    randomAmp(amp);
    $("#amp").text(
        [].slice.call(amp).join("\n")
    );
}

function randomAmp(amp) {
    for (var i = 0; i < 13; i++) {
        amp[i] = Math.random();
    }
    return amp;
}