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 ) }) }