diff --git a/hydra/app/extensions/clock.js b/hydra/app/extensions/clock.js deleted file mode 100644 index ecb3b749..00000000 --- a/hydra/app/extensions/clock.js +++ /dev/null @@ -1,50 +0,0 @@ -class Clock { - constructor({ - startAt = 0, - countBy = 1, - countWith = null, - interval = 1000, - onTick = () => {} - } = {}) { - this.currentVal = startAt - this.countBy = countBy - this.countWith = countWith - ? countWith - : last => last + countBy - this.interval = interval - this.onTick = onTick - this.test = () => this.currentVal - this.counter = setInterval( - () => { - this.currentVal = this.countWith(this.currentVal) - this.onTick(this.currentVal) - }, - interval) // maybe add option for setting interval with bpm, i.e. 1 is one beat, 0.5 half a beat, etc. - } - - val() { - return this.currentVal - } - - stop() { - clearInterval(this.counter) - } - - - setInterval(interval, countWith) { - this.stop() - return new Clock({ - startAt: this.currentVal, - countBy: this.countBy, - countWith: countWith || this.countWith, - interval: interval, - onTick: this.onTick - }) - } - - reset(val) { - this.currentVal = val || 0 - } -} - -module.exports = Clock diff --git a/hydra/app/extensions/extra/clock.js b/hydra/app/extensions/extra/clock.js new file mode 100644 index 00000000..94b4c2c8 --- /dev/null +++ b/hydra/app/extensions/extra/clock.js @@ -0,0 +1,79 @@ +const { + range, + bpmToMs, + wrapAt, + foldAt, + wrapBetween, + foldBetween, + wrapSeq, + foldSeq +} = require('./utils') + +// class Clock { +// constructor({ +// bpm, // if bpm is specified, then interval will not be used +// interval = 1000, // tick interval in milliseconds +// startAt = 0, +// countBy = 1, +// countWith = null, +// onTick = [() => {}] +// } = {}) { +// this.dt = 0 +// this.currentVal = startAt +// this.countBy = countBy +// this.countWith = countWith +// ? countWith +// : last => last + countBy +// this.interval = bpm ? bpmToMs(bpm) : interval +// this.onTick = onTick + +// // Clock methods as function properties: shaders may crash if passed in a method instead of a function, so this is why. +// this.val = () => this.currentVal +// this.wrapAt = modulo => wrapAt(modulo, this.val()) +// this.warpBetween = (bottom, top) => wrapBetween(bottom, top, this.val()) +// this.warpSeq = (numberArray) => wrapSeq(numberArray, this.val()) +// this.foldAt = modulo => foldAt(modulo, this.val()) +// this.foldBetween = (bottom, top) => foldBetween(bottom, top, this.val()) +// this.foldSeq = (numberArray) => foldSeq(numberArray, this.val()) +// } + +// update(dt) { +// this.dt = dt +// } +// } + +// module.exports = Clock + + +// const loop = require('raf-loop') + +// loop(dt) + +const opable = (fn, initialVal, ops = []) => ({ + +}) + + +function makeClock() { + let dt_ = 0 + const getVal = interval => dt_/interval + const clock = (interval) => ({ + val: () => getVal(interval), + wrapAt: modulo => wrapAt(modulo, getVal(interval)), + wrapBetween: (bottom, top) => wrapBetween(bottom, top, getVal(interval)), + wrapSeq: (numberArray) => wrapSeq(numberArray, getVal(interval)), + foldAt: modulo => foldAt(modulo, getVal(interval)), + foldBetween: (bottom, top) => foldBetween(bottom, top, getVal(interval)), + foldSeq: (numberArray) => foldSeq(numberArray, getVal(interval)), + }) + clock.update = dt => {dt_ = dt} + return clock +} + +module.exports = makeClock +// const osc = console.log +// const clock = makeClock() +// range(0, 20).forEach(dt => { +// clock.update(dt) +// osc(clock(2).wrapAt(5)) +// }) diff --git a/hydra/app/extensions/extra/utils-tests.js b/hydra/app/extensions/extra/utils-tests.js new file mode 100644 index 00000000..1c0535b6 --- /dev/null +++ b/hydra/app/extensions/extra/utils-tests.js @@ -0,0 +1,4 @@ +console.log(range(0, 20).map(x => wrapBetween(5, 10, x))) +console.log(range(0, 20).map(x => foldBetween(5, 10, x))) +console.log(range(0, 20).map(x => wrapSeq([4, 5 ,2, 3], x))) +console.log(range(0, 20).map(x => foldSeq([4, 5 ,2, 3], x))) diff --git a/hydra/app/extensions/extra/utils.js b/hydra/app/extensions/extra/utils.js new file mode 100644 index 00000000..6f24296d --- /dev/null +++ b/hydra/app/extensions/extra/utils.js @@ -0,0 +1,59 @@ +// beats per minute to milliseconds +function bpmToMs(bpm) { + return 60/bpm*1000 +} + +function range(from, to) { + const res = [] + for(let i = from; i