Skip to content

Instantly share code, notes, and snippets.

@khoparzi
Created April 9, 2021 07:58
Show Gist options
  • Save khoparzi/9d9f662627f88f0e692012cd8bd4b981 to your computer and use it in GitHub Desktop.
Save khoparzi/9d9f662627f88f0e692012cd8bd4b981 to your computer and use it in GitHub Desktop.
Continuous modulation and functions from Tidal implemented in JS (for using in Hydra or P5)
sin = ({time}) => Math.sin(time) // sin
sq = ({time}) => ((Math.sin(time) < 0) ? 0 : 1) // square
saw = ({time}) => (time % 1) * 2 - 1 // saw
sinf = (freq) => ({time}) => Math.sin(time*freq) // sin at freq
sqf = freq => ({time}) => ((Math.sin(time*freq) < 0) ? 0 : 1) // square at freq
sawf = (freq) => ({time}) => ((time * freq) % 1) * 2 - 1 // saw at freq
rsinf = (min,max,freq) => ({time}) => Math.sin(time*freq) * max + min // ranged sin at freq
rsqf = (min,max,freq) => ({time}) => ((Math.sin(time*freq) < 0) ? 0 : 1) * max + min // ranged square at freq
rsawf = (min,max,freq) => ({time}) => (((time * freq) % 1) * 2 - 1) * max + min // ranged saw at freq
run = (end,step=1,direction=1) => {
const len = Math.floor(end / step) + 1
if (direction === 1)
return Array(len).fill().map((_, idx) => (idx * step))
else if (direction === 0)
return Array(len).fill().map((_, idx) => (idx * step)).reverse()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment