-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
205 lines (181 loc) · 4.86 KB
/
worker.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Code in ~2 hours by Bemmu, idea and sound code snippet from Viznut.
// 2011-09-30 - Modifications by raer.
// 2011-10-07 - Modifications by raer.
// Last modifications by budsan: https://github.com/budsan/funck
function ProgressPost(size, rate) {
this.sizeInv = 1.0/size;
this.rate = size * rate;
this.total = 0;
this.curr = 0;
}
ProgressPost.prototype.step = function() {
this.total++;
this.curr++;
if (this.curr >= this.rate) {
this.curr = 0;
var value = Math.min(1.0, this.total * this.sizeInv);
self.postMessage({msg:'progress', value: value});
}
}
function DummyProgressPost() {}
DummyProgressPost.prototype.step = function() {}
function generateSound(params, notificate) {
//private
var _rate = params.rate;
var _depth = params.depth;
var _seconds = params.duration;
var _channels = 1;
var _f1 = null, _f2 = null;
var _samples = [];
var _curChan = 0;
var _post = null;
var _buildNotificator = null;
if (typeof notificate === 'undefined') {
_buildNotificator = function(total) {
return new DummyProgressPost();
}
} else {
_buildNotificator = function(total) {
return new ProgressPost(total, 0.05);
}
}
//public
var sin = Math.sin;
var cos = Math.cos;
var tan = Math.tan;
var floor = Math.floor;
var ceil = Math.ceil;
var PI = Math.PI;
var t = 0;
var rate = _rate;
var s = function(i, chan) {
if (i >= 0 && i < _samples.length) return _samples[i];
return 0;
};
//userspace
var thisline = new Error().lineNumber;
try {eval (params.aux);}
catch(ex) {
if(thisline) {
throw "Aux in line " + (ex.lineNumber-thisline) + " | " + ex.toString();
}
else {
throw "Aux | " + ex.toString();
}
}
try {eval("_f1 = function () { return " + params.oneliner + ";}");}
catch(ex) {throw "Left | " + ex.toString();}
if (params.oneliner2 != "") {
try {eval("_f2 = function () { return " + params.oneliner2 + ";}");}
catch(ex) {throw "Right | " + ex.toString();}
_channels = 2;
s = function(i, chan) {
if (typeof chan === 'undefined') chan = _curChan;
i *= _channels;
if (i >= 0 && i < _samples.length) return _samples[i+chan];
return 0;
};
}
var _prog = _buildNotificator(_rate*_seconds);
for (var _t = 0; _t < _rate*_seconds; _t++) {
// just in case this vars are modified.
t = _t;
rate = _rate;
//left channel
var _sample;
try {_sample = _f1();}
catch(ex) {throw "Runtime Left | " + ex.toString();};
var _sample2;
if (_channels > 1 && _f2 != null) {
//right channel
_curChan = 1;
try {_sample2 = _f2();}
catch(ex) {throw "Runtime Right | " + ex.toString();};
_curChan = 0;
}
//store left sample
_samples.push(_sample);
//store right sample if any
if (_channels > 1 && _f2 != null) {
_samples.push(_sample2);
}
_prog.step();
}
switch(_depth) {
case 'fnorm':
_prog = _buildNotificator(_samples.length*2);
_min = 1.7976931348623157E+10308, // Infinity
_max = -1.7976931348623157E+10308; //-Infinity
for(var i = 0; i < _samples.length; i++) {
var _sample = _samples[i];
if (_sample < _min) _min = _sample;
if (_sample > _max) _max = _sample;
_prog.step();
}
_d = 1.0 / ((_max - _min) * 0.5);
for(var i = 0; i < _samples.length; i++) {
var _sample = _samples[i];
_samples[i] = ((_sample-_min)*_d) - 1.0;
_prog.step();
}
case 'fclamp':
_prog = _buildNotificator(_samples.length);
for(var i = 0; i < _samples.length; i++) {
var _sample = _samples[i];
if (_sample < -1.0) _sample = -1.0;
if (_sample > 1.0) _sample = 1.0;
_sample = parseInt(_sample * 32767);
if (_sample < 0) _sample += 65536
_samples[i] = _sample;
_prog.step();
}
break;
case '16bits':
_prog = _buildNotificator(_samples.length);
for(var i = 0; i < _samples.length; i++) {
var _sample = parseInt(_samples[i]);
_sample = (_sample & 0xffff);
if (_sample < 0) _sample = 0;
if (_sample > 65535) _sample = 65535;
_samples[i] = _sample;
_prog.step();
}
break;
case '8bits':
default:
_prog = _buildNotificator(_samples.length);
for(var i = 0; i < _samples.length; i++) {
var _sample = parseInt(_samples[i]);
_sample = (_sample & 0xff) * 256;
if (_sample < 0) _sample = 0;
if (_sample > 65535) _sample = 65535;
_samples[i] = _sample;
_prog.step();
}
break;
}
return [_rate, _samples, _channels];
}
self.addEventListener('message', function(e)
{
try
{
var params = e.data;
var generated = generateSound(params, true);
self.postMessage({msg:'result', value: generated});
}
catch(err)
{
if (typeof err === 'string') {
self.postMessage({msg:'error', value: err});
}
else {
var vDebug = "";
for (var prop in err) {
vDebug += "property: "+ prop+ " value: ["+ err[prop]+ "]<br/>\n";
}
vDebug += "toString(): " + " value: [" + err.toString() + "]";
self.postMessage({msg:'error', value: vDebug});
}
}
}, false);