summaryrefslogtreecommitdiff
path: root/client/lib
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-10-08 01:05:13 +0200
committerJules Laplace <julescarbon@gmail.com>2018-10-08 01:05:13 +0200
commit9e824cc6d358fc902b27e00e5a1e8f88e3610823 (patch)
treeb2aa1f0e514ddcc9a6843ba42481b75ae7127329 /client/lib
parent81adbc30b2dbf9a24917694e22042762a930e00d (diff)
group and midi stuff
Diffstat (limited to 'client/lib')
-rw-r--r--client/lib/midi.js13
-rw-r--r--client/lib/ui.js2
-rw-r--r--client/lib/util.js46
3 files changed, 38 insertions, 23 deletions
diff --git a/client/lib/midi.js b/client/lib/midi.js
index 05fd708..f2e0295 100644
--- a/client/lib/midi.js
+++ b/client/lib/midi.js
@@ -1,10 +1,13 @@
import Tone from 'tone'
import WebMidi from 'webmidi'
import scales from './scales'
-import { ftom } from './util'
+import { ftom, norm } from './util'
import kalimba from './kalimba'
+import { nx } from './ui'
+
let midiDevice
+let sendPitchBend = false
export const MidiWriter = require('midi-writer-js')
@@ -81,7 +84,7 @@ export function play_note(index, duration, channel="all", exporting=false){
/* play the next note in sequence */
-function play_sequence(i, bounds, note_time, channel="all") {
+export function play_sequence(i, bounds, diff, note_time, channel="all", exporting) {
const { rows, min, max } = bounds
const count = rows.length * rows[0].length
if (i >= count) i = 0
@@ -97,7 +100,7 @@ function play_sequence(i, bounds, note_time, channel="all") {
/* play the next row as an interval */
-function play_interval_sequence(i, bounds, note_time, channel="all") {
+export function play_interval_sequence(i, bounds, diff, note_time, channel="all", exporting) {
const { rows, min, max } = bounds
const count = rows.length
if (i >= count) i = 0
@@ -118,7 +121,7 @@ function play_interval_sequence(i, bounds, note_time, channel="all") {
/* generate a 1-track midi file by calling the play function repeatedly */
-function export_pattern_as_midi(datasetName, bounds, tempo, timingIndex, play_fn) {
+export function export_pattern_as_midi(datasetName, bounds, diff, tempo, timingIndex, play_fn) {
const behavior = document.querySelector('#behavior').value
const { rows } = bounds
let count = behavior === 'sequence' ? rows[0].length * rows.length : rows.length
@@ -128,7 +131,7 @@ function export_pattern_as_midi(datasetName, bounds, tempo, timingIndex, play_fn
let midi_track = new MidiWriter.Track()
midi_track.setTempo(tempo)
for (let i = 0, len = count; i < len; i++) {
- notes = play_fn(i, bounds, exporting = true)[1]
+ notes = play_fn(i, bounds, note_time, "all", true)[1]
if (timing.length) {
note_time = timing[i % timing.length]
} else {
diff --git a/client/lib/ui.js b/client/lib/ui.js
index f344f0e..b0909cd 100644
--- a/client/lib/ui.js
+++ b/client/lib/ui.js
@@ -1,3 +1,5 @@
+export const nx = window.nx = {}
+
/* ui - update an int/float value */
export function update_value_on_change(el, id, is_int, fn) {
diff --git a/client/lib/util.js b/client/lib/util.js
index 0685b9d..f33146f 100644
--- a/client/lib/util.js
+++ b/client/lib/util.js
@@ -1,21 +1,21 @@
import Tone from 'tone'
import StartAudioContext from './startAudioContext'
-const isIphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))
-const isIpad = (navigator.userAgent.match(/iPad/i))
-const isAndroid = (navigator.userAgent.match(/Android/i))
-const isMobile = isIphone || isIpad || isAndroid
-const isDesktop = ! isMobile
+export const isIphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))
+export const isIpad = (navigator.userAgent.match(/iPad/i))
+export const isAndroid = (navigator.userAgent.match(/Android/i))
+export const isMobile = isIphone || isIpad || isAndroid
+export const isDesktop = ! isMobile
document.body.classList.add(isMobile ? 'mobile' : 'desktop')
-const browser = { isIphone, isIpad, isMobile, isDesktop }
+export const browser = { isIphone, isIpad, isMobile, isDesktop }
-function choice (a){ return a[ Math.floor(Math.random() * a.length) ] }
-function mod(n,m){ return n-(m * Math.floor(n/m)) }
-function norm(n, min, max){ return (n - min) / (max - min) }
+export function choice (a){ return a[ Math.floor(Math.random() * a.length) ] }
+export function mod(n,m){ return n-(m * Math.floor(n/m)) }
+export function norm(n, min, max){ return (n - min) / (max - min) }
-function requestAudioContext (fn) {
+export function requestAudioContext (fn) {
if (isMobile) {
const container = document.createElement('div')
const button = document.createElement('div')
@@ -55,7 +55,7 @@ function requestAudioContext (fn) {
}
}
-function dataURItoBlob(dataURI) {
+export function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
@@ -79,14 +79,14 @@ function dataURItoBlob(dataURI) {
return blob;
}
-function ftom(f) {
+export function ftom(f) {
// return (Math.log(f) - Math.log(261.626)) / Math.log(2) + 4.0
return 69 + 12 * Math.log2(f / 440)
}
-function mtof(m) {
+export function mtof(m) {
return 440 * Math.pow(2, (m - 69) / 12)
}
-function tap (fn) {
+export function tap (fn) {
return (e) => {
if (browser.isMobile) fn()
else if (e.press) fn()
@@ -95,7 +95,7 @@ function tap (fn) {
/* get minimum and maximum variance from row-to-row */
-function get_diff_bounds(rows){
+export function get_diff_bounds(rows){
const diffs = rows.map(row => {
const row_min = Math.min.apply(Math, row)
const row_max = Math.max.apply(Math, row)
@@ -108,7 +108,7 @@ function get_diff_bounds(rows){
/* get minimum and maximum values from a dataset */
-function get_bounds(dataset){
+export function get_bounds(dataset){
let rows = dataset.lines
// rows.forEach(row => row.shift())
rows = rows.map(a => a.map(n => parseFloat(n)))
@@ -125,7 +125,17 @@ function get_bounds(dataset){
return { rows, max, min }
}
+/* transpose a 2D array */
-
-export { choice, mod, norm, browser, get_bounds, get_diff_bounds, requestAudioContext, ftom, mtof, tap, dataURItoBlob }
+export function transpose(a) {
+ let i_len = a.length, j_len = a[0].length
+ let T = new Array(i_len)
+ for (let i = 0; i < i_len; i++) {
+ T[i] = new Array(j_len)
+ for (var j = 0; j < j_len; j++) {
+ T[i][j] = a[j][i]
+ }
+ }
+ return T
+}