import Tone from 'tone' import Sampler from './lib/sampler' import draw from './draw' import keys from './lib/keys' import color from './lib/color' import mouse from './lib/mouse' import { browser, requestAudioContext, randint, randrange, clamp, isMobile, } from './lib/util' const root = 440 const s = 50 const w = window.innerWidth const h = window.innerHeight const ws = w/s, hs = h/s let samplers = {} requestAudioContext( () => { samplers['smash'] = new Sampler('samples/smash/g{}.mp3', 12) // samplers['glass'] = new Sampler('samples/glass/0{}Particle.mp3', 90) }) let last_index = 0 keys.listen(index => { index = Math.abs(index+10) const freq = 100 * Math.abs(index + 10) const now = Tone.now() const count = randrange(2, 6) if (last_index !== index) { samplers['smash'].play(randrange(90, 150) + index, 0) last_index = index draw.clear() draw.triangle(window.innerWidth/2, window.innerHeight/2, 500) } else if (Math.random() < 0.09) { last_index = -1 } let when, i for (i = 0; i < count; i++) { // kalimba.play(freq * (i+1)/4, now + Math.random()/(i+1)) when = Math.random()/2000 + (i + Math.random()/10)/randrange(2,5) // samplers['glass'].play( // 100 + index*(Math.random() * 10), // now + when // ) setTimeout( () => { draw.triangle(randint(window.innerWidth), randint(window.innerHeight), randrange(10, 100)) }, when * 1000) } }) mouse.register({ down: (x, y) => { samplers['smash'].play(randrange(90, 150) + 50 * (x/window.innerWidth + y/window.innerHeight), 0) draw.clear() draw.triangle(x, y, isMobile ? Math.min(window.innerWidth, window.innerHeight) / 2 : 400) }, move: (x, y, dx, dy) => { let count = (Math.abs(dx) + Math.abs(dy)) / (isMobile ? 1 : 40) if (count < 1) return count = clamp(count, 1, 10) if (Math.abs(dx) + Math.abs(dy) > 100) { samplers['smash'].play(randrange(50, 300) + 100 * (x/window.innerWidth + y/window.innerHeight), 0) draw.clear() draw.triangle(x, y, isMobile ? Math.min(window.innerWidth, window.innerHeight) / 2 : 500) } let now = Tone.now() let when, i, player for (i = 0; i < count; i++) { when = Math.random()/2000 + (i+ Math.random()/10)/randrange(2,5) // player = samplers['glass'].play( // 100 * randrange(2,5) / randrange(2,5) * randrange(0.5,1.5), // now + when // ) player.volume.value = isMobile ? -(1+i/2) : -i*i } setTimeout( () => { draw.triangle(x, y, Math.abs(dx) + Math.abs(dy)) }, when * 1000) }, up: (x, y) => { }, })