/
SoundRenderer.js
260 lines (202 loc) · 5.23 KB
/
SoundRenderer.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
This is taken from the sound-rich-BK 0010 version.
May be used for various Covox or AY developments.
Now it is poor speaker only.
*/
SoundRenderer = function()
{
var self = this;
// blank AY object, can be developed
var synth = {
On: false, /* can set that there is a AY8910 attached somewhere, or other, and develop various sounds */
mixed: false, /*3 channels as one */
nextSample:function() {/*get next buffer of values*/}
};
this.allowClear = true; // can set not to clear buffer frequently
var /*int*/xCPS = 0;
var P = null;
var B = []; // real buffer to play
var Bpos = 0;
var Bz = 0; // flag-count lasting same sound
var Bclr = 0; // flag-count should clear buffers
var adjspd = 0; // counter for speed adjusting
var /*int*/ofs = 0;
var /*int*/xAcc = 0;
var /*int*/val = -16;
var /*int*/synthVal = 0;
var /*int*/covoxVal = 0;
var /*int*/bitVal = -16;
var Chan = 1; // Channels
var context = null;
self.On = false; // sound on or off
self.covox = false;
self.cycles = 0;
self.initpause = 0; // pause, if too slow
this.setSynth = function(S) { synth = S; }
this.setSound = function(on) {
self.On = on;
if(on && context == null) {
var A = (window.AudioContext || window.webkitAudioContext ||
window.WebkitAudioContext);
if(A==null) { soundOn=0; return; } // wtf
context = new A();
if(typeof(context.createScriptProcessor)=="undefined") {
context = null; soundOn=0; return;
}
P = context.createScriptProcessor(4096, 3, 3);
if(P!=null) P.onaudioprocess = onAudio;
}
if(context != null && P!=null) {
if(on) P.connect(context.destination);
else {
P.disconnect();
clear2();
}
}
}
function clear2() {
if(!self.initpause) { B = []; Bpos = 0; Bz = 0; }
adjustSpeed();
adjspd=0;
ofs = 0;
Bclr = 0;
}
this.clear = function(a) {
if(a) clear2();
else {
if(self.allowClear || B.length&0x100000) Bclr++; // now will wait for silence and then clear
}
}
//~every .0232s
function onAudio(e) {
var p = Bpos, O, O2, c12 = (Chan==1);
for(var C=0; C<Chan; C++) {
O = e.outputBuffer.getChannelData(C);
// if one channel, then the second is copied
if(c12) O2=e.outputBuffer.getChannelData(1); // if speaker, then write on both the same
var j=0, Sz = O.length, L = B.length;
if(self.initpause)
{
self.initpause--;
while(j<Sz) {
if(c12) O2[j]=0;
O[j++]=0;
}
}
else
{
p = Bpos;
if(c12) { /* 1 channel */
while(j<Sz && p<L) {
O2[j]=B[p]; // simply copy 2nd from 1st
O[j++]=B[p++];
}
}
else { /* 3channels */
while(j<Sz && p<L) O[j++]=B[p++][C];
}
if(j>0) {
Bz=0;
if(j<Sz && p>1 /*&& !synth.On*/) Bz=1;
}
// smooth sound
var last = (p==0 ? 0 :(Chan==1 ? B[p-1] : B[p-1][C])); // the lasting sound cases
if(Bz) {
// little lasting sound, if emulator too slow
while(j<Sz) {
if(c12) O2[j]=last;
O[j++]=last;
}
}
if(C==(Chan-1)) { // adjust counters and buffers
if(Bclr) {
switch(Bclr) {
case 1: if(j<Sz) clear2(); break; // if nothing to play, clear
case 2: if(last==0) clear2(); break; // if last sound is 0, also clear
}
}
}
while(j<Sz) { // fill 0s till end
if(c12) O2[j]=0;
O[j++]=0;
}
}
} // each channel
Bpos = p;
}
function adjustSpeed(c) {
var spd = (c ? speed.CpuMHZ : speed.avg_Hz);
/* cycles per sample */
var C = (spd / 48010)|0; /*better be prepared on time >=48000*/
xCPS = (C * 4096);
}
this.adjConstSpeed = function() { adjustSpeed(true); };
// This generates the sound buffer while running present values */
/*void*/this.updateTimer = function()
{
var /*long*/cy = 1200; // a constant value connected to cpu cycles timings
var /*int*/xStep = /*(int)*/cy * 4096;
var /*int*/xRem = xCPS - ofs;
if (xStep < xRem)
{
xAcc += val * xStep;
ofs += xStep;
}
else
{
xAcc += val * xRem;
cSum( xAcc );
xStep -= xRem;
ofs = 0;
xAcc = 0;
while (xStep >= xCPS) {
cSum(val * xCPS);
xStep -= xCPS;
}
xAcc = (val * xStep);
ofs = xStep;
if(!self.initpause && (++adjspd)>50000) self.clear();
}
if(!self.covox) covoxVal = 0;
if(!synth.On) synthVal = 0;
if(!self.On) bitVal = -16;
}
function cSum(A) {
var g,c;
if(synth.On) {
synthVal = synth.nextSample(); // 1 mixed or 3 channels
if(synth.mixed) {
c = synthVal-val;
val+=(c>32?32: (c<-32?-32:c)); // simple mixed distorted value
g = val;
}
else {
g = synthVal; // independent 3 channels, may be slow
synthVal=0; val=0;
}
}
else if(self.covox) // 1 channel
{ //...2)phase
c = covoxVal-val;
val+=(c>32?32: (c<-32?-32:c)); // smoothing
g = val;
}
else g = (A/xCPS); // 1 channel
var q = Chan;
Chan = (synth.On && !synth.mixed ? 3 : 1);
if(Chan!=q) clear2();
/* The correct float values should be [-1.0 ... 1.0], but ok anyway. */
B.push(g);
}
/*void*/this.updateBit = function(/*int*/maskedVal) {
self.updateTimer();
bitVal = ((maskedVal==0) ? -16/*Off*/ : 16/*On*/);
val = (bitVal + covoxVal + synthVal);
}
/*void*/this.updateCovox = function(/*int*/value) {
self.updateTimer();
var v = value&255;
covoxVal = (v&128 ?v-256:v)/2; //1)phase
}
return self;
}