diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-09-11 14:30:39 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-09-11 14:30:39 -0400 |
| commit | 0c2bc660e9350307930dfc40e2da1d46306e45a3 (patch) | |
| tree | f311d94b52d5c9bf69f64e5a9f6b1d20c2c247e0 /env.js | |
| parent | fbf05115c17163d91e9f649a348e3e6800a22d5e (diff) | |
set tone by index based on grid.. idk
Diffstat (limited to 'env.js')
| -rw-r--r-- | env.js | 131 |
1 files changed, 108 insertions, 23 deletions
@@ -5,7 +5,7 @@ var environment = (function(){ var image_index = -1 var snap, dot_grid, polysynth var strided_wires = [], wires = [] - var scale = [], root, tet, interval + var scale = [], root, tet, interval, duration var dot_color = "#333" var palette = [ @@ -25,6 +25,7 @@ var environment = (function(){ } environment.build = function(){ environment.scale() + environment.duration() snap = new Snap (window.innerWidth, window.innerHeight - $("#controls").height()) dot_grid = new DotGrid () @@ -35,16 +36,51 @@ var environment = (function(){ oscillator: { type: $("#waveform").val() }, envelope:{ attack: 0.01, - decay: 0.1, - sustain: 0.3, - release: 0.2, + decay: 2.5, + sustain: 0.0, + release: 0.1, } }) +// polysynth = new Tone.PolySynth(8, Tone.FMSynth) +// polysynth.set({ +// harmonicity:2, +// modulationIndex:3, +// detune:0, +// oscillator:{ +// type:"sine", +// }, +// envelope:{ +// attack:0.01, +// decay:0.01, +// sustain:1, +// release:0.2, +// }, +// moduation:{ +// type:"sawtooth", +// }, +// modulationEnvelope:{ +// attack:0.1, +// decay:0, +// sustain:1, +// release:0.2, +// } +// }) +// polysynth.set({ +// oscillator: { type: $("#waveform").val() }, +// envelope:{ +// attack: 0.01, +// decay: 2.5, +// sustain: 0.0, +// release: 0.1, +// } +// }) + var comp = new Tone.Compressor(-30, 3).toMaster() - var reverb = new Tone.Freeverb () - reverb.wet.value = 0.05 - polysynth.connect(reverb) - reverb.connect(comp) +// var reverb = new Tone.Freeverb () +// reverb.wet.value = 0.01 +// polysynth.connect(reverb) +// reverb.connect(comp) + polysynth.connect(comp) // environment.stride() // environment.randomize() } @@ -60,9 +96,10 @@ var environment = (function(){ $("#tet").on("input", environment.scale) $("#root").on("input", environment.scale) $("#interval").on("input", environment.scale) + $("#duration").on("input", environment.duration) $("#use_scale").on("change", environment.use_scale) $(snap.node).on("mousedown", dot_grid.mousedown.bind(dot_grid)) - $(snap.node).on("mouseup", dot_grid.mouseup.bind(dot_grid)) + $(window).on("mouseup", dot_grid.mouseup.bind(dot_grid)) } environment.blur = function(){ last_p = null @@ -72,6 +109,10 @@ var environment = (function(){ environment.use_scale = function(){ use_scale = $("#use_scale").get(0).checked } + environment.duration = function(){ + duration = parseInt( $("#duration").val() ) || 1000 + duration /= 1000 + } environment.keydown = function(e){ if (e.altKey || e.ctrlKey || e.metaKey) { e.stopPropagation() @@ -107,7 +148,12 @@ var environment = (function(){ environment.mousemove = function(e){ var p = environment.positionFromEvent(e) if (dot_grid.active) { - dot_grid.active.update({ point: p, muted: false, duration: 0.2 }) + dot_grid.active.update({ + point: p, + muted: false, + moving: true, + duration: 0.2, + }) } else { wires.forEach(function(wire){ @@ -184,7 +230,7 @@ var environment = (function(){ }) } // quantize a frequency to the scale - environment.quantize = function(f, get_index){ + environment.quantize_frequency = function(f, get_index){ if (f == 0) return 0 var scale_f = f var pow = 0 @@ -206,6 +252,17 @@ var environment = (function(){ // console.log(scale_f) return scale_f } + environment.quantize_index = function(index){ + return mod(index, scale.length)|0 + } + environment.index_to_frequency = function(index){ + var f = scale[ mod(index, scale.length)|0 ] + var pow = Math.floor(norm(index, 0, scale.length)) - 2 + f *= Math.pow(interval, pow) + // console.log(index, scale.length, pow, f) + return f + } + function DotGrid (opt){ this.opt = defaults(opt, { @@ -279,8 +336,8 @@ var environment = (function(){ } DotGrid.prototype.mouseup = function(e){ if (this.active && this.active.length > 0) { - var q = this.quantize(environment.last_p) - this.active.setTail(q) + // var q = this.quantize(environment.last_p) + this.active.setTail(environment.last_p) this.active = null } } @@ -312,9 +369,18 @@ var environment = (function(){ } } Wire.prototype.update = function(opt){ - var q = dot_grid.quantize(opt.point) - var length = this.length = dist(this.head.x, this.head.y, q.x, q.y) - var theta = angle(this.head.x, this.head.y, q.x, q.y) * 180 / Math.PI + var p = opt.point + var length = this.length = dist(this.head.x, this.head.y, p.x, p.y) + var theta = angle(this.head.x, this.head.y, p.x, p.y) * 180 / Math.PI + var index = Math.floor( length / dot_grid.opt.dot_spacing ) - 1 + if (index < 0) return + console.log(index) + var length = this.length = (index * dot_grid.opt.dot_spacing) + var q = { + x: Math.sin(theta) * length + this.head.x, + y: Math.cos(theta) * length + this.head.y, + } + // var q = dot_grid.quantize(p) var path_str = "M" + 0 + "," + 0 + "t" + length.toFixed(2) + "," + 0 var tran_str = "translate(" + this.head.x + "," + this.head.y + ") " @@ -326,7 +392,7 @@ var environment = (function(){ stroke: color, }) if (! opt.muted) { - this.play(opt.duration) + this.play(opt) } return length } @@ -352,16 +418,35 @@ var environment = (function(){ } } Wire.prototype.index = function(){ - var f = this.length / 340.29 * root - return environment.quantize( f, true ) + // var f = this.length / 340.29 * root + // return environment.quantize( f, true ) + return environment.quantize_index( this.length / dot_grid.opt.dot_spacing ) } - Wire.prototype.play = function(duration){ - var f = this.length / 340.29 * root + Wire.prototype.frequency = function(){ + var f if (use_scale) { - f = environment.quantize(f) + f = environment.index_to_frequency( this.length / dot_grid.opt.dot_spacing ) + // f = environment.quantize(f) + } + else { + f = this.length / 340.29 * root + } + return f + } + Wire.prototype.play = function(opt){ + opt = opt || {} + var d = opt.duration || duration + // console.log(d, duration) + // var f = this.length / 340.29 * root + var f = this.frequency() + if (opt.moving && last_f == f) { + return } - polysynth.triggerAttackRelease(f, duration || randrange(1.0, 2.5)) + last_f = f + // console.log(f, duration) + polysynth.triggerAttackRelease(f, d || randrange(1.0, 2.5)) } + var last_f = 0 Wire.prototype.remove = function(){ this.path.remove() wires.splice(wires.indexOf(this), 1) |
