diff options
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 184 |
1 files changed, 84 insertions, 100 deletions
diff --git a/client/index.js b/client/index.js index e0f44bd..5ad002e 100644 --- a/client/index.js +++ b/client/index.js @@ -4,7 +4,6 @@ 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 output from './lib/output' import spectrum from './lib/spectrum' @@ -12,34 +11,27 @@ import spectrum from './lib/spectrum' import { Hall } from './lib/hall' import { - browser, requestAudioContext, - randint, randrange, choice, clamp, lerp, dist, shuffle, - isMobile, + requestAudioContext, + lerp, } 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 root = 440 const HALLWAY_LENGTH = 147 -const SPEAKER_COUNT = 16 +// 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)) -notes = notes.concat(notes.map(i => i*2)) -notes = shuffle(notes) +// 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( () => { - // samplers.smash = new Sampler('samples/smash/g{}.mp3', 12) - samplers.earth = new Sampler('samples/earth/earth{}.mp3', 20) - // samplers.glass = new Sampler('samples/glass/0{}Particle.mp3', 20) - // samplers.kalimba = new Sampler('samples/kalimba/380731__cabled-mess__sansula-08-c-raw.wav', 10) - sampler = samplers.earth + 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.bong = new Sampler('samples/bong/bong{}.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 @@ -52,111 +44,103 @@ requestAudioContext( () => { }) }) - -const hall = new Hall ({ - length: HALLWAY_LENGTH, - speakers: SPEAKER_COUNT, -}) +// const hall = new Hall ({ +// length: HALLWAY_LENGTH, +// speakers: SPEAKER_COUNT, +// }) function redraw(){ draw.clear() } -function manipulate(spec){ - const data = spec.data - const sr = spec.sr - const fft_size = spec.fft_size - const fft_overlap = spec.fft_overlap - const spec_len = data.length +// let last_lin_bins, last_sam +let timeout +function manipulate(x, y, pcm, spec){ + if (timeout) return null + timeout = setTimeout( () => { timeout = null }, 20 ) - let i, j, u, v, _r, _i + // 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 aa = [] - for (i = 0; i < fft_size; i++) { - aa[i] = i - } - shuffle(aa) + // 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_data = [], new_col, col - let bands = 2 << 4 - let band, band_index - let band_size = Math.floor(fft_size / bands) - for (i = 0; i < spec_len; i++) { - col = data[i] - new_col = new_data[i] = data[i].concat() - data[i][2] = 0 - for (j = 0; j < fft_size; j++) { - band = Math.floor(j / band_size) * band_size - band_index = j % band_size + // let new_spec = spectrum.cloneSpectrum(spec) + // new_spec = spectrum.rotatePhase(new_spec, x * Math.PI) - new_col[j] = col[ band + (band_size - band_index) ] + let new_spec = spectrum.rotateSpectrum(spec, x) - // spectrum inversion - // new_col[j] = data[i][ fft_size - j - 1] - - // erase mirrored half of fft - new_col[j + fft_size] = 0 - } - new_col[2] = 0 - } - - spec.data = new_data //.reverse() - // col = data[i] - // for (j = 0; j < fft_size; j++) { - // _r = j*2 - // _i = j*2+1 - // col[_r] = col[_r] / 2 - // col[_i] = col[_i] - // } - // } + return new_spec } -keys.listen(index => { - // trigger(Math.random(), ((index+7) % SPEAKER_COUNT) / SPEAKER_COUNT, 0, samplers.smash) - const sample = sampler.play(100, Tone.now(), output) - const buf = sample._buffer.get() - if (! buf) return - const pcm = buf.getChannelData(0) - const sr = buf.sampleRate - const duration = buf.duration - const spec = spectrum.toSpectrum(pcm, sr) - - draw.clear() - draw.waveform(pcm) - draw.spectrum(spec, 0, window.innerHeight/4 + 20) - - manipulate(spec) - - const audioBuffer = spectrum.fromSpectrum(spec) - const player = new Tone.Player(audioBuffer) - player.connect(output) - player.start(Tone.now() + pcm.length / sr) - - // const new_spec = spectrum.toSpectrum(audioBuffer.getChannelData(0), sr) - draw.spectrum(spec, 0, window.innerHeight * 1/2 + 40) +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, dx, dy) => { + move: (x, y) => { + xx = x + yy = y }, - up: (x, y) => { + drag: (x, y) => { + trigger(x, y, 0, sampler) }, + // up: (x, y) => { + // }, }) -let timeout, px = 0, py = 0 -function play(x, y){ -} -function trigger(x, y, t, sampler){ +function trigger(x, y, t, source){ + x = x || 0 + y = y || 0 t = t || 0 t += Tone.now() - sampler = sampler || 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 = hall.play(sampler, y, freq, x, t) + 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(y) + if (! pcm) return + + const new_spec = manipulate(x, y, pcm, spec) + if (! new_spec) return + + draw.clear() + // draw.waveform(pcm) + draw.spectrum(spec, 0, window.innerHeight/4 + 20) + + const audioBuffer = spectrum.fromSpectrum(new_spec) + draw.waveform(audioBuffer.getChannelData(0)) + + const player = new Tone.Player(audioBuffer) + player.connect(output) + player.start(Tone.now()) + + // const new_spec = spectrum.toSpectrum(audioBuffer.getChannelData(0), sr) + draw.spectrum(new_spec, 0, window.innerHeight * 1/2 + 40) draw.triangle( lerp(x, 0, 1) * window.innerWidth, |
