summaryrefslogtreecommitdiff
path: root/client/index.js
blob: 0b5f16b1d939a92363b92a6f4ca8dcad2393a9c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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,
} 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, 400)
  },
  move: (x, y, dx, dy) => {
    let count = Math.abs(dx + dy) / 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, 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 = -i*i
    }
    setTimeout( () => {
      draw.triangle(x, y, Math.abs(dx) + Math.abs(dy))
    }, when * 1000)
  },
  up: (x, y) => {
  },
})