diff options
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 74 |
1 files changed, 68 insertions, 6 deletions
diff --git a/client/index.js b/client/index.js index 7a01b55..2b09f5f 100644 --- a/client/index.js +++ b/client/index.js @@ -7,6 +7,7 @@ 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' import { Hall } from './lib/hall' @@ -34,9 +35,11 @@ let samplers = {} let sampler requestAudioContext( () => { - samplers.smash = new Sampler('samples/smash/g{}.mp3', 12) + // samplers.smash = new Sampler('samples/smash/g{}.mp3', 12) + samplers.earth = new Sampler('samples/earth/earth{}.wav', 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 samplers.choice = (m,n) => { const r = Math.random() if (r < m) return samplers.smash @@ -45,7 +48,7 @@ requestAudioContext( () => { } Tone.Buffer.on('load', function(){ console.log('all buffers are loaded.') - redraw() + // redraw() }) }) @@ -59,19 +62,78 @@ 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 i, j, u, v, _r, _i + + let aa = [] + for (i = 0; i < fft_size; i++) { + aa[i] = i + } + shuffle(aa) + + 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 + + new_col[j] = col[ band + (band_size - band_index) ] + + // 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] + // } + // } +} + keys.listen(index => { // trigger(Math.random(), ((index+7) % SPEAKER_COUNT) / SPEAKER_COUNT, 0, samplers.smash) - const sample = samplers.smash.play(100, Tone.now(), output) - console.log(Tone.now()) + 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 - console.log(duration.toFixed(2) + " s.") + const spec = spectrum.toSpectrum(pcm, sr) + draw.clear() draw.waveform(pcm) - draw.spectrum(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) }) mouse.register({ |
