Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions hydra/app/extensions/clock.js

This file was deleted.

79 changes: 79 additions & 0 deletions hydra/app/extensions/extra/clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const {
range,
bpmToMs,
wrapAt,
foldAt,
wrapBetween,
foldBetween,
wrapSeq,
foldSeq
} = require('./utils')

// class Clock {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voy a quitar esto

// 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))
// })
4 changes: 4 additions & 0 deletions hydra/app/extensions/extra/utils-tests.js
Original file line number Diff line number Diff line change
@@ -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)))
59 changes: 59 additions & 0 deletions hydra/app/extensions/extra/utils.js
Original file line number Diff line number Diff line change
@@ -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<to; i++) {
res.push(i)
}
return res
}

// inspired by sclang's warpAt
function wrapAt(modulo, index) {
return index%(modulo+1)
}

function wrapBetween(bottom, top, index) {
return wrapAt(top - bottom, index) + bottom
}

// inspired by sclang's foldAt
function foldAt(modulo, index) {
let res;
res = index % modulo
let isUp = parseInt(index / modulo) % 2 === 0
if (isUp) {
return res
} else {
return modulo - res
}
}

function foldBetween(bottom, top, index) {
return foldAt(top - bottom, index) + bottom
}

function wrapSeq(numberArray, index) {
const length = numberArray.length
return numberArray[index%(length)]
}

function foldSeq(numberArray, index) {
const length = numberArray.length
const index_ = foldAt(length - 1, index)
return numberArray[index_]
}

module.exports = {
bpmToMs,
range,
wrapAt,
foldAt,
wrapBetween,
foldBetween,
wrapSeq,
foldSeq
}
4 changes: 3 additions & 1 deletion hydra/app/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const loop = require('raf-loop')
// Core
const HydraSynth = require('hydra-synth')
const Editor = require('./core/hydra-editor')
const makeClock = require('./extensions/extra/clock')

// Extensions
window.Clock = require('./extensions/clock.js')
Expand All @@ -12,13 +13,14 @@ function init () {
// init hydra
hydra = new HydraSynth({ canvas: initCanvas(), autoLoop: false })
editor = new Editor({ loadFromStorage: true})

window.clock = makeClock()
// special functions for running hydra in electron
utils.initElectron(hydra)

// loop function run on each frame
var engine = loop(function(dt) {
hydra.tick(dt)
window.clock.update(dt)
if(window.update) {
try { window.update(dt) } catch (e) {editor.log(e.message, "log-error")}
}
Expand Down