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>
<h4>degrade</h4><pre id="degrade"></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 p = {};

p.amp = new Float32Array(13);
p.degrade = 1;

var Neume = neume(new AudioContext());

var Synth = new Neume(function($, i) {
    var out;
    
    out = $("sin", { freq: mtof(pt2map[i]) });
    out = $(p.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);
    out = $(degrator(), out);
    
    return out;
});

function degrator() {
    var context = Neume.context;
    
    var scp = context.createScriptProcessor(8192, 1, 1);
    var x = 1;
    var v = 0;
    
    scp.onaudioprocess = function(e) {
        var inp = e.inputBuffer.getChannelData(0);
        var out = e.outputBuffer.getChannelData(0);
        
        if (p.degrade === 1) {
            out.set(inp);
        } else {
            for (var i = 0; i < 8192; ++i) {
                if (x >= 1) {
                    x -= 1;
                    v = inp[i];
                }
                x += p.degrade;
                out[i] = v;
            }
        }
    };
    
    return scp;
}

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().fadeIn("+0", 0.5);
        timer = new Neume.Interval(2, function(e) {
            var i = e.count % score.length;
            
            if (e.count % 5 === 0) {
                receiveOSC();
            }
            
       ...