summaryrefslogtreecommitdiff
path: root/client/lib/util.js
blob: e47b3438bdcaf0c2934fb15ef29a9227323c77ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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

document.body.classList.add(isMobile ? 'mobile' : 'desktop')

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 requestAudioContext (fn) {
	if (isMobile) {
    const container = document.createElement('div')
    const button = document.createElement('div')
    button.innerHTML = 'Tap to start - please unmute your phone'
    Object.assign(container.style, {
      position: 'absolute',
      width: '100%',
      height: '100%',
      zIndex: '10000',
      top: '0px',
      left: '0px',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
    })
		Object.assign(button.style, {
		  position: 'absolute',
		  left: '50%',
		  top: '50%',
			padding: '20px',
		  backgroundColor: '#7F33ED',
		  color: 'white',
		  fontFamily: 'monospace',
		  borderRadius: '3px',
		  transform: 'translate3D(-50%,-50%,0)',
		  textAlign: 'center',
			lineHeight: '1.5',
    })
    container.appendChild(button)
    document.body.appendChild(container)
    StartAudioContext.setContext(Tone.context)
    StartAudioContext.on(button)
    StartAudioContext.onStarted(_ => {
      container.remove()
			fn()
  	})
	} else {
		fn()
	}
}

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]);

  // separate out the mime component
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

  // write the bytes of the string to an ArrayBuffer
  var ab = new ArrayBuffer(byteString.length);

  // create a view into the buffer
  var ia = new Uint8Array(ab);

  // set the bytes of the buffer to the correct values
  for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }

  // write the ArrayBuffer to a blob, and you're done
  var blob = new Blob([ab], {type: mimeString});
  return blob;

}
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) {
  return 440 * Math.pow(2, (m - 69) / 12)
}
function tap (fn) {
  return (e) => {
    if (browser.isMobile) fn()
    else if (e.press) fn()
  }
}

export { choice, mod, browser, requestAudioContext, ftom, mtof, tap, dataURItoBlob }