diff options
Diffstat (limited to 'client/lib/util.js')
| -rw-r--r-- | client/lib/util.js | 46 |
1 files changed, 28 insertions, 18 deletions
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 +} |
