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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
| function createAudioMeter(audioContext, clipLevel, averaging, clipLag)
{
var processor = audioContext.createScriptProcessor(256); //512,fix
processor.onaudioprocess = volumeAudioProcess;
processor.clipping = false;
processor.lastClip = 0;
processor.volume = 0;
processor.clipLevel = clipLevel || 0.98;
processor.averaging = averaging || 0.95;
processor.clipLag = clipLag || 750;
// this will have no effect, since we don't copy the input to the output,
// but works around a current Chrome bug.
processor.connect(audioContext.destination);
processor.checkClipping =function()
{
if (!this.clipping) return false;
if ((this.lastClip + this.clipLag) < window.performance.now()) this.clipping = false;
return this.clipping;
};
processor.shutdown =function(){ this.disconnect(); this.onaudioprocess = null; };
return processor;
}
function volumeAudioProcess( event ) {
var buf = event.inputBuffer.getChannelData(0);
var bufLength = buf.length;
var sum = 0;
var x;
// Do a root-mean-square on the samples: sum up the squares...
for (var i=0; i<bufLength; i++) {
x = buf[i];
if (Math.abs(x)>=this.clipLevel) { this.clipping = true; this.lastClip = window.performance.now(); }
sum += x * x;
}
var rms = Math.sqrt(sum / bufLength);
this.volume = Math.max(rms, this.volume*this.averaging);
}
var audioContext = null;
var meter = null;
var canvasContext = null;
var audioCtxGen =null;
var oscillator =null;
var freqmin=50.0;
var freqmax=10000.0;
var currfreq= 50.0;
var fstep =50.0;
var WIDTH=1000;
var HEIGHT=300;
var timeoutID=null;
var delay=100;
var LevelIn=50;
var LevelOut=50;
var stepnum=0;
var dBmode=0;
var streammode=0;
function SetLevelIn()
{
LevelIn=document.getElementById("LevelInput").value;
document.getElementById("LevelIn1").innerHTML =LevelIn;
}
/*
function SetLevelOut()
{
LevelOut=document.getElementById("LevelOutput").value;
document.getElementById("LevelOut1").innerHTML =LevelOut;
}
*/
function UpdateLabels()
{
document.getElementById("minfreq").innerHTML =freqmin;
document.getElementById("maxfreq").innerHTML =freqmax;
document.getElementById("delay1").innerHTML =delay;
document.getElementById("step1").innerHTML =fstep;
}
window.onload = function Init() {UpdateLabels(); }
function OnChangeFmin(){freqmin=parseFloat(document.getElementById("FminIn").value);UpdateLabels();}
function OnChangeFmax(){ freqmax =parseFloat(document.getElementById("FmaxIn").value); UpdateLabels(); }
function OnChangeDelay(){ delay =parseFloat(document.getElementById("DelayIn").value) ; UpdateLabels(); }
function OnChangeStep(){ fstep =parseFloat(document.getElementById("StepIn").value) ;UpdateLabels(); }
//window.onload = functionOnClick()
function didntGetStream() { alert('Stream generation failed.'); }
function CreateStream()
{
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioContext = new AudioContext();
try {
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
navigator.getUserMedia(
{
"audio": {
"mandatory": {
"googEchoCancellation": "false",
"googAutoGainControl": "false",
"googNoiseSuppression": "false",
"googHighpassFilter": "false"
},
"optional": []
},
}, gotStream, didntGetStream);
} catch (e) { alert('getUserMedia threw exception :' + e); }
}
var mediaStreamSource = null;
/*
function CreateGainFun( )
{
var gainNode = null;
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
var myAudio = document.querySelector('audio');
gainNode =audioCtx.createGain();
gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
gainNode.gain.linearRampToValueAtTime(0.5, audioCtx.currentTime );
}
*/
function CreateOsc( )
{
audioCtxGen = new (window.AudioContext || window.webkitAudioContext)();
oscillator = audioCtxGen.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(currfreq, audioCtxGen.currentTime); // value in hertz
oscillator.connect(audioCtxGen.destination);
oscillator.start();
}
function CreateInputLevelMeter(stream)
{
mediaStreamSource = audioContext.createMediaStreamSource(stream);
meter = createAudioMeter(audioContext);
mediaStreamSource.connect(meter);
}
var Coeffs= new Float64Array(2048);
Coeffs.fill(1);
function GetLevel(){ guard_delay(); return (meter.volume *1.41) *(LevelIn*0.01); }
function GetValue(Ldata , nstep1, modedB, mode1 )
{
var datadBIn=20*Math.log10(Ldata );
var dataIn=Ldata ;
if (mode1==1) { datadBIn+= Coeffs [nstep1] ; dataIn*= Coeffs[ nstep1] ; }
document.getElementById("Vinp1").innerHTML =dataIn.toExponential(4);
document.getElementById("Vinp2").innerHTML =datadBIn.toFixed(1);
if (modedB==1) { return datadBIn} else { return dataIn ; }
return dataIn ;
}
function CreateNormCoefsArray(stepnum, mode, indata )
{
var coeftmp=1;
var i=stepnum;
if (mode==0){ coeftmp=1/(indata+(1e-12)); } //obtain coeff. for Level*Coeffs=1return;
if (mode==1){ coeftmp=(-indata); } //obtain coeff. for Level+Coeffs =return;
Coeffs[i]=coeftmp;
}
function dsleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds) { break; } }
}
function guard_delay(){ dsleep(delay); }
function UpdateFreqValue()
{
document.getElementById("curfreq1").innerHTML =currfreq.toFixed(2);
oscillator.frequency.value=currfreq;
//oscillator.frequency.setValueAtTime(currfreq, audioCtxGen.currentTime);
// oscillator.frequency.linearRampToValueAtTime(currfreq, audioCtxGen.currentTime);
}
function SetPoint(numstep, V , num)
{
var dxPos=2;
var xPos=numstep*dxPos;
var yPos ;
if (dBmode==0) { yPos=HEIGHT*(1-V); } else { yPos=HEIGHT*(1-0.7)- V *(HEIGHT/120); }
canvasContext.beginPath();
canvasContext.moveTo( xPos, HEIGHT);
canvasContext.lineTo( xPos, yPos); //fix
// canvasContext.moveTo( xPos, yPos);
//canvasContext.lineTo( xPos+1, yPos); //fix
if (meter.checkClipping()){ canvasContext.strokeStyle ="red"; } // тёмно-синий цвет
else { canvasContext.strokeStyle ="green"; }
canvasContext.lineWidth =dxPos;
canvasContext.stroke();
}
function NormLoop( )
{
var tmpdata;
if (currfreq<=freqmax)
{
document.getElementById("nstep").innerHTML =stepnum ;
UpdateFreqValue();
tmpdata=GetLevel();
CreateNormCoefsArray(stepnum, dBmode , GetValue(tmpdata, stepnum, dBmode, 0 ) );
if ( document.getElementById("NormCoefs").checked ) { tmpdata =GetValue(tmpdata, stepnum , dBmode , 1 ) ; } else
{ tmpdata =GetValue(tmpdata, stepnum , dBmode , 0 ) ; }
SetPoint( stepnum, tmpdata);
currfreq +=fstep;
stepnum++;
}
else{ StopOnClick(); }
}
function drawLoop( )
{
var tmpdata;
if (currfreq<=freqmax)
{
document.getElementById("nstep").innerHTML =stepnum ;
UpdateFreqValue();
tmpdata=GetLevel();
if ( document.getElementById("NormCoefs").checked ) { tmpdata =GetValue(tmpdata, stepnum , dBmode , 1 ) ;
} else
{ tmpdata =GetValue(tmpdata, stepnum , dBmode , 0 ) ; }
SetPoint( stepnum, tmpdata);
currfreq += fstep;
stepnum++;
}
else{ StopOnClick(); }
}
function drawLoop1( )
{
var tmpdata;
document.getElementById("nstep").innerHTML =0 ;
UpdateFreqValue();
tmpdata=(meter.volume *1.41)*(LevelIn*0.01);
tmpdata =GetValue(tmpdata, 0 , 0 , 0 ) ;
var yPos1=HEIGHT*(1-tmpdata);
canvasContext.clearRect(0,0,WIDTH, HEIGHT);
canvasContext.beginPath();
canvasContext.moveTo( 100, HEIGHT);
canvasContext.lineTo(100, yPos1); //fix
if (meter.checkClipping()){ canvasContext.strokeStyle ="red"; } // тёмно-синий цвет
else { canvasContext.strokeStyle ="green"; }
canvasContext.lineWidth =20;
canvasContext.stroke();
}
function GetNumberofPoints(){ return 1+Math.ceil ((freqmax-freqmin)/fstep); }
function gotStream(stream)
{
CreateInputLevelMeter(stream);
CreateOsc( );
Coeffs.length=GetNumberofPoints();
if (streammode==0){ timeoutID=setInterval( drawLoop , 50); }
if (streammode==1){ if (dBmode==1) { Coeffs.fill(0) ; } else { Coeffs.fill(1) ; } timeoutID=setInterval( NormLoop , 50 ); }
if (streammode==2){ timeoutID=setInterval( drawLoop1 , 50 ); }
}
function StopOnClick()
{
clearInterval( timeoutID);
timeoutID=null ;
oscillator.stop();
oscillator.disconnect();
currfreq=freqmin;
meter =null;
mediaStreamSource = null;
audioCtxGen =null;
audioContext =null;
stepnum=0;
if(streammode==2) canvasContext.clearRect(0,0,WIDTH, HEIGHT);
}
function ClearOnClick(){ StopOnClick(); canvasContext.clearRect(0,0,WIDTH, HEIGHT);}
function ClearCoefsOnClick(){ Coeffs.length=GetNumberofPoints() ; if (dBmode==1) { Coeffs.fill(0) ; } else { Coeffs.fill(1) ; } }
function RunOnClick()
{
(document.getElementById("LogMode").checked==true) ? dBmode=1 : dBmode=0;
if (dBmode==1) { document.getElementById("currmode").innerHTML ="Log. Mode " ;}
if (dBmode==0) { document.getElementById("currmode").innerHTML ="Lin. Mode " ; }
currfreq=freqmin;
streammode=0;
UpdateLabels();
canvasContext = document.getElementById( "meter" ).getContext("2d");
canvasContext.clearRect(0,0,WIDTH, HEIGHT);
CreateStream();
}
function NormOnClick()
{
(document.getElementById("LogMode").checked==true) ? dBmode=1 : dBmode=0;
if (dBmode==1) { document.getElementById("currmode").innerHTML ="Log. Mode " ;}
if (dBmode==0) { document.getElementById("currmode").innerHTML ="Lin. Mode " ; }
currfreq=freqmin;
streammode=1;
UpdateLabels();
canvasContext = document.getElementById( "meter" ).getContext("2d");
canvasContext.clearRect(0,0,WIDTH, HEIGHT);
CreateStream();
}
function LevelOnClick(){
currfreq=parseFloat(prompt('Input frequency, Hz ?', 1000.0) );
streammode=2;
UpdateLabels();
document.getElementById("currmode").innerHTML ="Log. Mode ";
canvasContext = document.getElementById( "meter" ).getContext("2d");
canvasContext.clearRect(0,0,WIDTH, HEIGHT);
CreateStream();
} |