Skip to content

Instantly share code, notes, and snippets.

@jsundram
Created June 30, 2013 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsundram/5893707 to your computer and use it in GitHub Desktop.
Save jsundram/5893707 to your computer and use it in GitHub Desktop.
timbre.js pendulums
{"description":"timbre.js pendulums","endpoint":"","display":"svg","public":true,"require":[{"name":"supercollider","url":"http://mohayonao.github.io/subcollider.js/builds/subcollider-min.js"},{"name":"timbre-mod","url":"https://gist.github.com/enjalot/5893275/raw/3a9f3430225510d2258381c9e6ab1506ff8bb86f/timbre-mod.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":true,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
//http://mohayonao.github.io/timbre.js/
//hacky modifications to make it work in tributary
TReset();
TFactory();
var svg = d3.select("svg");
var color = d3.scale.category20c();
var midx = tributary.sw/2;
var xwidth = 200;
var midy = tributary.sh/2;
var xr = 27;
var yr = 45;
var r = 24;
var startNote = 72;
var octave = 12;
var noteOffset = 3;
var notes = d3.range(12).map(function(d,i) {
return {
note: startNote + i + noteOffset
}
})
var msec = timbre.timevalue("bpm100 l8");
function playMidi(note) {
var synth = T("OscGen", {mul:0.4, env:T("perc", {r:msec, ar:true})});
synth.noteOn(note, 50);
synth.play()
}
//playMidi(60);
tributary.init = function(g) {
g.append("line").attr({
x1: midx - xwidth - r,
y1: 0,
x2: midx - xwidth - r,
y2: tributary.sh
}).style({
stroke: "#000"
})
g.append("line").attr({
x1: midx + xwidth + r,
y1: 0,
x2: midx + xwidth + r,
y2: tributary.sh
}).style({
stroke: "#000"
})
g.selectAll("circle")
.data(notes)
.enter()
.append("circle")
.attr({
cx: function(d,i,j) { return midx },
cy: function(d,i,j) { return 49 + i * yr },
r: r,
fill: function(d,i,j) { return color(i) }
}).on("mouseover", function(d,i) {
playMidi(d.note)
})
}
tributary.run = function(g,t) {
var theta = Math.PI / 9;
var freq = 3;
var lr = -0.999
var rr = 0.999
function wave(i) {
return Math.sin(freq * t + i * theta);
}
g.selectAll("circle")
.attr({
cx: function(d,i) {
d.wave = wave(i);
var x = midx + xwidth * wave(i);
return x
}
})
.each(function(d,i) {
if(wave(i) < lr && !d.lastL) {
playMidi(d.note - octave)
d3.select(this).attr("fill", "#fff")
d.lastL = true
}
if(wave(i) > lr && !d.lastR) {
d.lastL = false;
d3.select(this).attr("fill", color(i))
}
if(wave(i) > rr && !d.lastR) {
playMidi(d.note + octave)
d3.select(this).attr("fill", "#fff")
d.lastR = true
}
if(wave(i) < rr && !d.lastL) {
d3.select(this).attr("fill", color(i))
d.lastR = false;
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment