summaryrefslogtreecommitdiff
path: root/bin/media.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-11-23 19:33:41 +0100
committerJules Laplace <julescarbon@gmail.com>2018-11-23 19:33:41 +0100
commit6fc8743f68648857ccef703bc4f2290b2cdac061 (patch)
tree00b5b3afc0e0a2f2e808fe36c5cd4565b84d9310 /bin/media.js
parentc1c058c662109696fda78b8512aebfd197974b5e (diff)
media sonification
Diffstat (limited to 'bin/media.js')
-rw-r--r--bin/media.js117
1 files changed, 117 insertions, 0 deletions
diff --git a/bin/media.js b/bin/media.js
new file mode 100644
index 0000000..b8742ac
--- /dev/null
+++ b/bin/media.js
@@ -0,0 +1,117 @@
+const fs = require('fs')
+const parse = require('csv-parse')
+const stringify = require('csv-stringify')
+
+const parklandInput = fs.createReadStream('./data/media_01_parkland_shooting.csv')
+const averageInput = fs.createReadStream('./data/media_02_average_of_seven_shootings.csv')
+const parklandParser = parse()
+const averageParser = parse()
+const stringifier = stringify()
+const output = fs.createWriteStream('./data/media.csv')
+
+let finishCount = 0
+let parkland = [], average = []
+function finish() {
+ finishCount += 1
+ console.log(finishCount)
+ if (finishCount < 2) { return }
+ console.log('finishing')
+ let a, b, nn
+ for (let n = 0; n < 12 * 8; n += 1) {
+ nn = n / 8
+ a = find(nn, parkland)
+ b = find(nn, average)
+ stringifier.write([n, a, b])
+ }
+ stringifier.end()
+}
+
+/*
+ Given the sequence A of pairs (i,j) describing coordinates and an offset n,
+ find i where A[i][0] <= n and n < A[i+1][0]
+ and use these to interpolate the value of A(n) -> j or whatever you call it.
+*/
+function find(n, a) {
+ let head, tail, dist
+ for (let i = 1, len = a.length; i < len; i++) {
+ if (a[i][0] > n) {
+ head = a[i-1]
+ tail = a[i]
+ dist = (n - head[0]) / (tail[0] - head[0])
+ console.log(n, head, tail)
+ return lerp(dist, head[1], tail[1])
+ }
+ }
+}
+function lerp(n, a, b) {
+ return (b-a) * n + a
+}
+
+parklandInput.on('readable', function() {
+ let buf
+ while ((buf = parklandInput.read()) !== null) {
+ parklandParser.write(buf)
+ }
+})
+parklandInput.on('error', function(err){
+ console.error('parklandInput error', err.message)
+})
+parklandInput.on('finish', function(){
+ parklandParser.end()
+})
+
+averageInput.on('readable', function() {
+ let buf
+ while ((buf = averageInput.read()) !== null) {
+ averageParser.write(buf)
+ }
+})
+averageInput.on('error', function(err){
+ console.error('averageInput error', err.message)
+})
+averageInput.on('finish', function(){
+ console.log('enddd')
+ averageParser.end()
+})
+
+parklandParser.on('readable', function(){
+ let row
+ while (row = parklandParser.read()) {
+ parkland.push(row.map(n => parseFloat(n)))
+ }
+ // console.log('p', parkland.length)
+ if (parkland.length == 108) finish()
+})
+parklandParser.on('error', function(err){
+ console.error('parklandParser error', err.message)
+})
+parklandParser.on('end', function(){
+ finish()
+})
+
+averageParser.on('readable', function(){
+ let row
+ while (row = averageParser.read()) {
+ average.push(row.map(n => parseFloat(n)))
+ }
+ if (average.length == 89) finish()
+})
+averageParser.on('error', function(err){
+ console.error('averageParser error', err.message)
+})
+averageParser.on('end', function(){
+ finish()
+})
+
+stringifier.on('readable', function(){
+ let row
+ while (row = stringifier.read()){
+ output.write(row)
+ }
+})
+stringifier.on('error', function(err){
+ console.error('stringifier error', err.message)
+})
+stringifier.on('finish', function(){
+ output.end()
+})