summaryrefslogtreecommitdiff
path: root/client/index.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-08-14 02:18:35 +0200
committerJules Laplace <julescarbon@gmail.com>2018-08-14 02:18:35 +0200
commit0ba30e46c0bb9cd8a4ee74cbb0e215ebab1263c4 (patch)
tree595348b286b3c22e5b6702e223c7dcf5b5fc6bf9 /client/index.js
parent71853816c933e95ee23ba741c4c8d6c97246681a (diff)
switch seq/ interv behavior
Diffstat (limited to 'client/index.js')
-rw-r--r--client/index.js82
1 files changed, 53 insertions, 29 deletions
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 => {