summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-10-10 14:58:24 +0200
committerJules Laplace <julescarbon@gmail.com>2018-10-10 14:58:24 +0200
commit31117e3b786e4d24a7dc43163362ab4a4d3050fd (patch)
treef11f19a71c0d4b40f31c4bd8d5dab9efbbfa8294
parentec4db2e8a67ca063737c696716e64496470ebbb3 (diff)
exporting all midi notes... add pedal tone option
-rw-r--r--client/index.js53
-rw-r--r--client/lib/midi.js15
-rw-r--r--index.html13
3 files changed, 65 insertions, 16 deletions
diff --git a/client/index.js b/client/index.js
index 3f48bd3..fef9b0c 100644
--- a/client/index.js
+++ b/client/index.js
@@ -30,6 +30,8 @@ const DEFAULT_BPM = 60
let recorder = null
let recording = false
+let paused = false
+let playTimeout
midi_init()
@@ -44,7 +46,8 @@ const mass_fields = [
return a
}, {})
-let i = 0, mass_i = 0, datasets = {}, dataset = {}, bounds = {}, diff = []
+let i = 0, max_i = 0, mass_i = 0
+let datasets = {}, dataset = {}, bounds = {}, diff = []
let play_fn = play_sequence
data.load().then(lists => {
console.log(lists)
@@ -63,12 +66,15 @@ data.load().then(lists => {
datasets["Mass Shootings"].name = "Mass Shootings"
datasets["Mass Shootings"].play_fn = play_mass_shootings
const lines = datasets["Mass Shootings"].lines.reverse()
- const [min_y, ...rest] = lines[0][mass_fields.date].split('/')
+ const [min_y, ...rest_a] = lines[0][mass_fields.date].split('/')
+ const [max_y, ...rest_b] = lines[lines.length-1][mass_fields.date].split('/')
datasets["Mass Shootings"].dates = lines.map(row => {
const [y, m, d] = row[mass_fields.date].split('/')
return (parseInt(y) - parseInt(min_y)) * 12 + parseInt(m)
})
+ max_i = (parseInt(max_y) - parseInt(min_y)) * 12 + parseInt(12)
+ console.log('max i', max_i)
datasets["Mass Shootings"].data = lines
datasets["Mass Shootings"].lines = [lines.map(row => row[mass_fields.total_victims])]
requestAudioContext(ready)
@@ -102,7 +108,7 @@ function play_mass_shootings(i, bounds, diff, note_time, channel="all", exportin
const n = rows[y][x]
const total = dataset.dates.length
let notes = [], midi_notes = [], cases = [], timings
- console.log(i, mass_i, dataset.dates[mass_i])
+ console.log(i, mass_i, dataset.dates[mass_i], channel, exporting)
while (i >= dataset.dates[mass_i] && mass_i < total) {
notes.push(dataset.lines[0][mass_i])
cases.push(dataset.data[mass_i][mass_fields.date] + ' ' + dataset.data[mass_i][mass_fields.case] +
@@ -148,7 +154,9 @@ function play_mass_shootings(i, bounds, diff, note_time, channel="all", exportin
} else {
i += 1
}
- kalimba.play(220, -12)
+
+ const pedal_freq = scales.current().index(nx.pedal_tone.value, nx.octave.value)
+ kalimba.play(pedal_freq, -12)
if (notes.length) {
mass_rest = 0
return [i, midi_notes, timings, mass_rest]
@@ -160,8 +168,10 @@ function play_mass_shootings(i, bounds, diff, note_time, channel="all", exportin
/* play next note according to sonification */
function play_next(){
+ if (paused) return
let note_time = 120000 / Tone.Transport.bpm.value * note_values[nx.timing.active][0]
- setTimeout(play_next, note_time)
+ clearTimeout(playTimeout)
+ playTimeout = setTimeout(play_next, note_time)
let [new_i, notes, timings] = play_fn(i, bounds, diff, note_time)
i = new_i
if (recording) {
@@ -171,6 +181,16 @@ function play_next(){
}
}
+function play() {
+ i = 0
+ mass_i = 0
+ paused = false
+ play_next()
+}
+function pause() {
+ paused = true
+}
+
/* bind selects */
function pick_dataset(key){
@@ -254,9 +274,28 @@ function ready() {
})
update_value_on_change(nx.interval, '#interval', true)
+ nx.pedal_tone = new Nexus.Dial('#pedal_tone', {
+ size: dial_size,
+ min: -24,
+ max: 24,
+ step: 1,
+ value: -7,
+ })
+ update_value_on_change(nx.pedal_tone, '#pedal_tone', true)
+
+ const play_button = document.querySelector('#play')
+ play_button.addEventListener('click', () => {
+ play()
+ })
+
+ const pause_button = document.querySelector('#pause')
+ pause_button.addEventListener('click', () => {
+ pause()
+ })
+
const export_midi_button = document.querySelector('#export_midi')
export_midi_button.addEventListener('click', () => {
- export_pattern_as_midi(dataset.name, bounds, diff, nx.tempo.value, nx.timing.active, play_fn)
+ export_pattern_as_midi(dataset.name, bounds, diff, nx.tempo.value, nx.timing.active, play_fn, max_i)
})
const record_midi_button = document.querySelector('#record_midi')
@@ -285,7 +324,7 @@ function ready() {
document.querySelector('#scale').value = '14'
scales.pick(14)
- play_next()
+ // play_next()
}
/* keys */
diff --git a/client/lib/midi.js b/client/lib/midi.js
index d51aeff..449abc0 100644
--- a/client/lib/midi.js
+++ b/client/lib/midi.js
@@ -142,31 +142,34 @@ export function play_interval_sequence(i, bounds, diff, note_time, channel="all"
/* generate a 1-track midi file by calling the play function repeatedly */
-export function export_pattern_as_midi(datasetName, bounds, diff, tempo, timingIndex, play_fn) {
+export function export_pattern_as_midi(datasetName, bounds, diff, tempo, timingIndex, play_fn, max_i) {
// const behavior = document.querySelector('#behavior').value
const { rows } = bounds
// let count = behavior === 'sequence' ? rows[0].length * rows.length : rows.length
- let count = rows[0].length
+ max_i = max_i || rows[0].length
let notes, timings, wait
let note_time
// let timing = note_values[timingIndex][2]
let midi_track = new MidiWriter.Track()
+ let next_i
midi_track.setTempo(tempo)
- for (let i = 0, len = count; i < len; i++) {
- [i, notes, timings, wait] = play_fn(i, bounds, note_time, "all", true)
+ for (let i = 0, len = max_i; i < len; i++) {
+ [next_i, notes, timings, wait] = play_fn(i, bounds, diff, note_time, "all", true)
// if (timing.length) {
// note_time = timing[i % timing.length]
// } else {
// note_time = timing
// }
// midi_track.addEvent(new MidiWriter.NoteEvent({ pitch: notes, duration: 't' + note_time }))
- console.log(i, notes, timings, wait)
+ // console.log(i, notes, timings, wait)
for (let j = 0; j < notes.length; j++) {
+ console.log(i, j, notes[j], timings[j], wait)
midi_track.addEvent(new MidiWriter.NoteEvent({
pitch: notes[j],
duration: 't' + timings[j],
- wait: (j === 0) ? wait : 0,
+ wait,
}))
+ wait = 0
}
}
const writer = new MidiWriter.Writer([midi_track])
diff --git a/index.html b/index.html
index 8ea82b8..5aa9922 100644
--- a/index.html
+++ b/index.html
@@ -9,14 +9,16 @@
<div class='tools'>
<div>
<h2>Sonification of gun violence</h2>
+ <div class='row'>
+ <div id='cases'></div>
+ </div>
<div class='buttons'>
+ <button id="play">Play</button>
+ <button id="pause">Pause</button>
<button id="export_midi">Export dataset as MIDI</button>
<button id="record_midi">Record MIDI</button>
<span id='recording_msg'>Recording</span>
</div>
- <div class='row'>
- <div id='cases'></div>
- </div>
<label>
<span>scale</span>
<select id="scale"></select>
@@ -60,6 +62,11 @@
<span class='val'></span>
<span>Interval</span>
</div>
+ <div class='block'>
+ <div id="pedal_tone"></div>
+ <span class='val'></span>
+ <span>Pedal tone</span>
+ </div>
</div>
</div>
</div>