summaryrefslogtreecommitdiff
path: root/client/index.js
blob: e08aab01b9d420bec9a700f0ca094bf81abe6dee (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Tone from 'tone'

import Sampler from './lib/sampler'

import draw from './draw'
import keys from './lib/keys'
import mouse from './lib/mouse'
import output from './lib/output'
import spectrum from './lib/spectrum'

import { Hall } from './lib/hall'

import {
  requestAudioContext,
  lerp,
} from './lib/util'

// const root = 440

const HALLWAY_LENGTH = 147
// const SPEAKER_COUNT = 16

// let notes = [299, 336, 374, 399, 449, 498, 561, 598].map(i => i/2)
// notes = notes.concat(notes.map(i => i/2))

let samplers = {}
let sampler

requestAudioContext( () => {
  // sampler = samplers.misc = new Sampler('samples/misc/glass.mp3', 2)
  // sampler = samplers.smash = new Sampler('samples/smash/g{}.mp3', 12)
  sampler = samplers.earth = new Sampler('samples/earth/earth{}.mp3', 20)
  // sampler = samplers.glass = new Sampler('samples/glass/0{}Particle.mp3', 20)
  // sampler = samplers.bubbles = new Sampler('samples/bubbles/bubbles{}.mp3', 10)
  // sampler = samplers.kalimba = new Sampler('samples/kalimba/380731__cabled-mess__sansula-08-c-raw.wav', 10)

  samplers.choice = (m,n) => {
    const r = Math.random()
    if (r < m) return samplers.smash
    if (r < m+n) return samplers.kalimba
    return samplers.glass
  }
  Tone.Buffer.on('load', function(){
    console.log('all buffers are loaded.')
    // redraw()
  })
})

// const hall = new Hall ({
//   length: HALLWAY_LENGTH,
//   speakers: SPEAKER_COUNT,
// })

function redraw(){
  draw.clear()
}

let last_lin_bins, last_sam
let timeout
function manipulate(x, y, pcm, spec){
  if (timeout) return null
  timeout = setTimeout( () => { timeout = null }, 20 )

  // reverseSpectrum,
  // shuffleSpectrum,
  // invertSpectrum
  // const pcm_rev = pcm.slice().reverse()
  // const spec_rev = spectrum.toSpectrum(pcm_rev, spec.sr)
  // return spec_rev
  // return spectrum.reverseSpectrum(spec)
  // const log_bins = spectrum.logarithmicBins(spec)

  // let lin_bins = (x * x * x * spec.fft_size/6)|0 
  // let sam = (y * sampler.length)|0 + 1
  // if (lin_bins == last_lin_bins && sam == last_sam) return null
  // last_lin_bins = lin_bins
  // last_sam = sam
  // const log_bins = spectrum.linearBins(spec, lin_bins)
  // const new_bins = spectrum.reverseBins(log_bins)
  // const concat_bins = spectrum.concatBins(new_bins)
  // const new_spec = spectrum.reorderBins(spec, concat_bins)
  // console.log(spec, new_spec)

  // let new_spec = spectrum.cloneSpectrum(spec)
  // new_spec = spectrum.rotatePhase(new_spec, x * Math.PI)

  let new_spec = spectrum.rotateSpectrum(spec, (y + 0.5)%1)

  return new_spec
}

keys.listen(i => {
  trigger(xx + i/50, yy, 0, sampler)
})

let xx = 0.5, yy = 0.5
mouse.register({
  down: (x, y) => {
    redraw()
    clearTimeout(timeout)
    timeout = null
    trigger(x, y, 0, sampler)
  },
  move: (x, y) => {
    xx = x
    yy = y
  },
  drag: (x, y) => {
    trigger(x, y, 0, sampler)
  },
  // up: (x, y) => {
  // },
})

function trigger(x, y, t, source){
  x = x || 0
  y = y || 0
  t = t || 0
  t += Tone.now()
  source = source || sampler
  // source = source || last_dist > 40
  // ? samplers.choice(0.2, 0.2)
  // : samplers.choice((1-y) * 0.2, y*0.02)
  // const freq = notes[Math.floor(x * notes.length)]
  // const { speaker, player } = hall.play(source, y, freq, x, t)

  const { pcm, spec } = source.getWaveAndSpectrum(x)
  if (! pcm) return

  const new_spec = manipulate(x, y, pcm, spec)
  if (! new_spec) return

  const audioBuffer = spectrum.fromSpectrum(new_spec)
  const player = new Tone.Player(audioBuffer)
  player.connect(output)
  player.start(Tone.now())

  draw.onFrame(() => {
    // INIT DRAWING PHASE
    draw.clear()

    // DRAW INDIVIDUAL UI ELEMENTS
    // draw.waveform(pcm)
    // draw.spectrum(spec, 0, window.innerHeight/4 + 20)

    draw.waveform(audioBuffer.getChannelData(0))

    // DRAW SPECTRUM
    // const new_spec = spectrum.toSpectrum(audioBuffer.getChannelData(0), sr)
    // draw.spectrum(new_spec, 0, window.innerHeight * 1/2 + 40)
    draw.spectrum(new_spec, 0, window.innerHeight * 1/4 + 20, 0, window.innerHeight * 1/2)

    // DRAW FLAIR
    draw.triangle(
      lerp(x, 0, 1) * window.innerWidth, 
      lerp(y, 0, 1) * window.innerHeight - 20,
      40
    )
  })
}