From 0ba30e46c0bb9cd8a4ee74cbb0e215ebab1263c4 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 14 Aug 2018 02:18:35 +0200 Subject: switch seq/ interv behavior --- client/index.js | 82 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 29 deletions(-) (limited to 'client/index.js') diff --git a/client/index.js b/client/index.js index da7b72a..3e0e171 100644 --- a/client/index.js +++ b/client/index.js @@ -49,28 +49,43 @@ function midi_ready(err) { if (filtered.length) { midi = filtered[0] } - } else { - midi = WebMidi.outputs[0] } + midi = midi || WebMidi.outputs[0] console.log(midi.name) } -let i = 0 +let i = 0, datasets = {}, dataset = {}, bounds = {}, diff = [] +let play_fn = play_sequence data.load().then(lists => { // nx.dataset.choices = Object.keys(lists) - console.log(lists) - const list = lists.weekly_earnings - document.querySelector('#dataset_name').innerHTML = list.name.replace(/-/g, ' ') - // playSequence(list) - playIntervalSequence(list) + console.log(lists) + datasets = lists + requestAudioContext(ready) + pick_dataset('housing costs and income inequality') }) - -function playSequence(list){ - let { rows, min, max } = get_bounds(list) - let count = rows.length * rows[0].length - playNext() - function playNext() { - let note_time = 120000 / Tone.Transport.bpm.value * note_values[nx.timing.active][0] - setTimeout(playNext, note_time) +function pick_dataset(key){ + console.log('pick dataset:', key) + i = 0 + dataset = datasets[key] + bounds = get_bounds(dataset) + diff = get_diff_bounds(bounds.rows) +} +var behaviors = { + sequence: { name: 'Sequence', fn: play_sequence }, + interval: { name: 'Intervals', fn: play_interval_sequence }, +} +function pick_behavior(name){ + behaviors[name].fn() +} +function play_next(){ + let note_time = 120000 / Tone.Transport.bpm.value * note_values[nx.timing.active][0] + setTimeout(play_next, note_time) + play_fn(note_time) +} +function play_sequence(){ + play_fn = (note_time) => { + const { rows, min, max } = bounds + const count = rows.length * rows[0].length + if (i >= count) i = 0 const y = Math.floor(i / rows[0].length) const x = i % rows[0].length if (!x) console.log(y) @@ -80,27 +95,23 @@ function playSequence(list){ play( norm(n, min, max) * nx.multiply.value, note_time * nx.duration.value) } } - -function playIntervalSequence(list){ - let { rows, min, max } = get_bounds(list) - let diff = get_diff_bounds(rows) - let count = rows.length - playNext() - function playNext() { - let note_time = 120000 / Tone.Transport.bpm.value * note_values[nx.timing.active][0] - setTimeout(playNext, note_time) +function play_interval_sequence(){ + play_fn = (note_time) => { + const { rows, min, max } = bounds + const count = rows.length + if (i >= count) i = 0 const y = i % count const row = rows[y] + if (! row) { i = 0; return } const row_min = Math.min.apply(Math, row) const row_max = Math.max.apply(Math, row) const row_f0 = norm(row_min, min, max) const row_root = row_f0 * nx.multiply.value - rows[y].forEach(n => { + row.forEach(n => { const note = row_root + norm(n - row_min, diff.min, diff.max) * nx.interval.value play(note, note_time * nx.duration.value) }) i += 1 - if (i > count) i = 0; } } @@ -156,7 +167,6 @@ function play(index, duration){ kalimba.play(freq) } } -requestAudioContext(ready) function update_value_on_change(el, id, is_int, fn) { const label = document.querySelector(id + ' + .val') @@ -184,8 +194,22 @@ function update_radio_value_on_change(el, id, values, fn) { update(el.active) el.update = update } +function build_options(el, lists, fn) { + Object.keys(lists).forEach( (key, i) => { + const list = lists[key] + const option = document.createElement('option') + option.innerHTML = list.name + option.value = key + el.appendChild(option) + }) + el.addEventListener('input', function(e){ + fn(e.target.value) + }) +} function ready () { scales.build_options(document.querySelector('#scale')) + build_options(document.querySelector('#dataset'), datasets, pick_dataset) + build_options(document.querySelector('#behavior'), behaviors, pick_behavior) // nx.colorize('#f4d142') Tone.Transport.bpm.value = DEFAULT_BPM @@ -244,8 +268,8 @@ function ready () { }) update_value_on_change(nx.interval, '#interval', true) - Tone.Transport.start() document.querySelector('.loading').classList.remove('loading') + play_next() } keys.listen(index => { -- cgit v1.2.3-70-g09d2