Last active
May 26, 2023 07:04
-
-
Save jarmitage/4202002c78d6ff93fd41e3b8ed694635 to your computer and use it in GitHub Desktop.
Drawing Tidal sounds in Atom-Hydra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see tidal example: https://github.com/ojack/hydra-examples | |
p1.remove() | |
p1 = new P5() | |
p1.pixelDensity(1) | |
msg.setPort(3333) | |
tidalHistory = 128 | |
tidal = [] | |
sounds = {'s':[], 'c':[]} | |
eventRect = {'w':10,'h':5} | |
cps = 0 | |
parseTidal = (args) => { | |
cps = args[1] | |
obj = {} | |
for(var i = 0; i < args.length; i+=2) | |
obj[args[i]] = args[i+1] | |
return obj | |
} | |
updateTidal = (args) => { | |
tidal.unshift(parseTidal(args)) | |
if (tidal.length > tidalHistory) | |
tidal.pop() | |
} | |
updateSounds = () => { | |
if (tidal[0] != null) { | |
let currentSounds = {'s':[], 'c':[]} | |
for (var i = 0; i < tidal.length; i++) { | |
if (!currentSounds.s.includes(tidal[i].s)) { | |
let s = tidal[i].s | |
let c = [ | |
(hashString (tidal[i].s) % 255), | |
(hashString (tidal[i].s+tidal[i].s) % 255), | |
(hashString (tidal[i].s+tidal[i].s+tidal[i].s) % 255) | |
] | |
currentSounds.s.push (s) | |
currentSounds.c.push (c) | |
} | |
eventRect.w = p1.windowWidth / sounds.s.length | |
} | |
for (var i = 0; i < sounds.s.length; i++) { | |
if (!currentSounds.s.includes (sounds.s[i])) { | |
sounds.s.splice (i, 1) | |
sounds.c.splice (i, 1) | |
} | |
} | |
for (var i = 0; i < currentSounds.s.length; i++) { | |
if (!sounds.s.includes (currentSounds.s[i])) { | |
sounds.s.push (currentSounds.s[i]) | |
sounds.c.push (currentSounds.c[i]) | |
} | |
} | |
} | |
} | |
hashString = (string) => { | |
var hash = 0, i, chr; | |
if (string.length === 0) return hash; | |
for (i = 0; i < string.length; i++) { | |
chr = string.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; // Convert to 32bit integer | |
} | |
return Math.abs(hash); | |
}; | |
p1.windowResized = () => { | |
p1.resizeCanvas (p1.windowWidth, p1.windowHeight); | |
} | |
renderTidal = () => { | |
if (tidal[0] != null) { | |
setTimeout(() => { | |
p1.clear() | |
p1.fill(255) | |
let yr = [tidal[0].cycle, tidal[tidal.length-1].cycle] | |
for (var i = 0; i < tidal.length; i++) { | |
let sIndex = sounds.s.indexOf (tidal[i].s) | |
let x = p1.map (sIndex, 0, sounds.s.length, 0, p1.windowWidth) | |
let y = p1.map (tidal[i].cycle, yr[0], yr[1], 0, p1.windowHeight) | |
p1.fill (sounds.c[sIndex]) | |
p1.rect (x, y, eventRect.w, eventRect.h) | |
} | |
}, tidal[0].delta * 1000) | |
} | |
} | |
msg.on('/play2', (args) => { | |
updateTidal(args) | |
updateSounds() | |
renderTidal() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment