summaryrefslogtreecommitdiff
path: root/client/index.js
blob: a1e0828c34181585f9cf92ccff3d162c07ddc434 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import keys from './lib/keys'
import color from './lib/color'
import kalimba from './lib/kalimba'
import { browser, requestAudioContext } from './lib/util'

const root = 440
const s = 50
const w = window.innerWidth
const h = window.innerHeight
const ws = w/s, hs = h/s

const add_on = 0
const mul_on = 1.0
const add_off = 0.1
const mul_off = 0.9

let dragging = false
let lastFreq = 0
let notes = []

requestAudioContext( () => {
  for (var i = 0; i < ws; i++) {
	  notes[i] = []
    for (var j = 0; j < hs; j++) {
      notes[i][j] = add(i, j)
    }
  }
})

function add (x, y) {
  const i = x + 1
  const j = y + 1
  const div = document.createElement('div')
  const freq = root * i/j
  let add = 0
  let frac = Math.log2(i/j) % 1
  div.style.left = (x * s) + 'px'
  div.style.top = (y * s) + 'px'
  div.innerHTML = `<div>${i}<\/div><div>\/</div><div>${j}<\/div>`
  if (frac < 0) {
    frac += 1
  }
  if (i < j) {
    add = -Math.log(j/i) / 3.5
  }
  else {
    add = Math.log(i/j) / 6
  }
  if ( frac === 0) {
    div.style.fontWeight = '900'
    div.style.left = (x * s) + 'px'
    div.style.top = (y * s) + 'px'
  }
  div.style.backgroundColor = color(frac, add_off + add, mul_off)

  if (browser.isDesktop) {
    div.addEventListener('mousedown', function(){
      div.style.backgroundColor = color(frac, add + add_on, mul_on)
      kalimba.play( freq )
    })
    div.addEventListener('mouseenter', function(){
      div.style.backgroundColor = color(frac, add + add_on, mul_on)
      if (dragging) {
        kalimba.play( freq )
      }
    })
    div.addEventListener('mouseleave', function(){
      div.style.backgroundColor = color(frac, add + add_off, mul_off)
    })
	}
	else {
    div.addEventListener('touchstart', function(e){
			e.preventDefault()
      kalimba.play( freq )
			lastFreq = freq
    })
	}
  document.body.appendChild(div)
	return freq
}

if (browser.isDesktop) {
  document.addEventListener('mousedown', () => { dragging = true })
  document.addEventListener('mouseup', () => { dragging = false })
}
else {
  document.addEventListener('touchstart', (e) => { e.preventDefault(); dragging = true })
  document.addEventListener('touchmove', (e) => {
    e.preventDefault()
		const x = Math.floor( e.touches[0].pageX / s )
		const y = Math.floor( e.touches[0].pageY / s )
		if (! (x in notes) || ! (y in notes[x])) return
		const freq = notes[x][y]
		if (freq !== lastFreq) {
			kalimba.play( freq )
			lastFreq = freq
		}
  })
  document.addEventListener('touchend', () => { dragging = false })
}

keys.listen(function(index){
  // const freq = scales.current().index(index)
  // document.body.style.backgroundColor = color( index / scales.current().scale.length )
  // kalimba.play(freq)
})