forked from elgehelge/Browserbased-Random-Music-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavepot-runtime.js
More file actions
93 lines (84 loc) · 2.23 KB
/
Copy pathwavepot-runtime.js
File metadata and controls
93 lines (84 loc) · 2.23 KB
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
/**
*
* Wavepot playback engine
*
* This code was originally copied from https://github.com/possan/wavepot-runtime.
* It is modified to be able to receive a function instead of a string.
*
* Use: Instantiate a WavepotRuntime object using a Wavepot-like dsp() function.
*
* Examples:
* ---------
* function dsp(t) { return 0.1 * Math.sin(2 * Math.PI * t * 440);}
* var rt = new WavepotRuntime(dsp);
* rt.play();
*
* or
*
* var rt = new WavepotRuntime();
* var codeString = 'function dsp(t) { return 0.1 * Math.sin(2 * Math.PI * t * 440);}';
* rt.compile(codeString);
*
*/
function WavepotRuntime(dspFunction, context) {
this.scope = {};
this.time = 0;
this.context = context || new AudioContext();
this.playing = false;
this.bufferSize = 1024;
this.scriptnode = this.context.createScriptProcessor(this.bufferSize, 0, 1);
this._process = function (e) {
var out = e.outputBuffer.getChannelData(0);
var f = 0, t = 0, td = 1.0 / this.context.sampleRate;
if (this.scope && this.scope.dsp && this.playing) {
t = this.time;
for (var i = 0; i < out.length; i++) {
f = this.scope.dsp(t);
out[i] = f;
t += td;
}
this.time = t;
} else {
for (var i = 0; i < out.length; i++) {
out[i] = f;
}
}
};
this.feedDspFunction = function (dspFunction) {
var newscope = new Object();
var f = function () {
this.dsp = dspFunction;
};
var r = f.call(newscope);
if (newscope && typeof(newscope.dsp) == 'function') {
this.scope = newscope;
return true;
} else {
return false;
}
};
this.compile = function(code) {
try {
var f = new Function('var sampleRate=' + this.context.sampleRate+ ';\n\n' + code + '\n\nthis.dsp = dsp;');
} catch(e) {
console.error('WavepotRuntime: compilation error', e);
}
this.feedDspFunction(f);
};
this.play = function () {
// console.log('WavepotRuntime: play');
this.playing = true;
};
this.stop = function () {
// console.log('WavepotRuntime: stop');
this.playing = false;
};
this.reset = function () {
// console.log('WavepotRuntime: reset');
this.time = 0;
};
this.scriptnode.onaudioprocess = this._process.bind(this);
this.scriptnode.connect(this.context.destination);
// feed dsp function
this.feedDspFunction(dspFunction);
};