-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwavepot.js
127 lines (101 loc) · 2.78 KB
/
wavepot.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var readFile = require('fs').readFile;
var electron = require('electron');
var ipc = electron.ipcRenderer;
console.log = electron.remote.getGlobal('console').log;
console.error = electron.remote.getGlobal('console').error;
window.addEventListener('error', e => {
e.preventDefault();
console.error(e.error.stack);
});
var audio = new AudioContext;
window.sampleRate = audio.sampleRate;
var bpm = 60;
var sources = {};
var beatTime;
clock();
connect();
function clock() {
beatTime = 1 / (bpm / 60);
}
function connect() {
ipc.on('sourcefile', getSourceFile)
ipc.on('source', getSource)
}
function getSourceFile(event, filename) {
readFile(filename, 'utf8', build);
}
function getSource(event, js) {
build(null, js);
}
function build(err, js) {
if (err) console.log(err.stack);
else compile(js);
}
function compile(js) {
var mod = { exports: {} };
var fn = new Function('module', 'exports', 'require', js);
fn(mod, mod.exports);
console.log('ok');
if ('bpm' in mod.exports) {
console.log('set bpm:', mod.exports.bpm);
bpm = mod.exports.bpm;
clock();
}
for (var key in sources) {
if (!(key in mod.exports)) sources[key].stop();
}
for (var key in mod.exports) {
if ('function' === typeof mod.exports[key]) {
console.log('compile:', key);
play(key, mod.exports[key]);
} else if (Array.isArray(mod.exports[key])) {
console.log('compile:', key);
play(key, mod.exports[key][1], mod.exports[key][0])
}
}
}
function createSource(key) {
var source = audio.createBufferSource();
source.loop = true;
source.onended = disconnect;
source.connect(audio.destination);
return source;
}
function createBuffer(fn, multiplier) {
var channels = 2;
var beatFrames = Math.floor(audio.sampleRate * beatTime);
var blockFrames = Math.floor(beatFrames * multiplier);
var buffer = audio.createBuffer(2, blockFrames, audio.sampleRate);
var sample = 0;
var L = buffer.getChannelData(0);
var R = buffer.getChannelData(1);
for (var i = 0; i < blockFrames; i++) {
sample = fn(1 + i / beatFrames, i);
L[i] = R[i] = normalize(sample);
}
return buffer;
}
function play(key, fn, multiplier) {
multiplier = multiplier || 4;
var buffer = createBuffer(fn, multiplier);
var source = createSource();
var syncTime = calcSyncTime(multiplier);
if (key in sources) sources[key].stop(syncTime);
sources[key] = source;
source.buffer = buffer;
source.start(syncTime);
console.log('playing:', key);
}
function calcSyncTime(multiplier) {
return normalize(
audio.currentTime +
(multiplier * beatTime -
(audio.currentTime % (multiplier * beatTime)))
);
}
function disconnect() {
this.disconnect();
}
function normalize(number) {
return number === Infinity || number === -Infinity || isNaN(number) ? 0 : number;
}