-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
276 lines (261 loc) · 8.07 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0" />
<title>哼唱识别</title>
<style type="text/css">
body{
background-color: rgba(0,0,0,1);
}
#rec-box{
top:50%;
left:50%;
transform: translate(-50%, -50%);
position: fixed;
z-index:20;
}
.circle{
width: 100px;
height: 100px;
background-color:rgba(0,255,255,0.3);
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
top:50%;
left:50%;
transform: translate(-50%, -50%);
position: fixed;
z-index:19;
}
#rec-box a{
color:rgba(128,128,128,1);
text-decoration:none;
font-size:30px;
}
#rel-value{
top:70%;
left:50%;
color:rgba(128,128,128,1);
font-size:15px;
transform: translate(-50%, -50%);
position: fixed;
z-index:20;
}
</style>
</head>
<body class="keBody">
<div class="kePublic">
<canvas id='canvas' width="1000" height="600"></canvas>
<br>
<div class="circle">
</div>
<div id="rec-box">
<a href="javascript:rec.start()" id="rec-node">录制</a>
</div>
<span id="rel-value"></span>
<br>
<script>
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
class noteParser{
constructor(ser){
this.serious = ser;
this.merge();
this.toRelative();
var relstr="0 "
for(var i=0;i<this.relative.length;++i){
relstr+=this.relative[i][0]+" ";
if(i>=31)
break;
}
document.getElementById("rel-value").innerText="relative:"+relstr;
}
toRelative(){
this.relative=[];
var last = null;
for(var i=0;i<this.merged.length;++i){
if(i!=0){
if(this.merged[i-1][1]<=1)
continue;
var delta = Math.round(this.merged[i][0]-this.merged[i-1][0]);
if(delta!=0)
this.relative.push([delta,this.merged[i-1][1]]);
}
}
}
pushNotes(ser){
var sum = 0;
for(var i=0;i<ser.length;++i){
sum+=ser[i];
}
this.merged.push([sum/ser.length,ser.length]);
}
merge(){
var arr=[];
this.merged= [];
for (var i=0;i<this.serious.length;++i){
if(i!=0){
if(Math.abs(this.serious[i]-this.serious[i-1])<1){
arr.push(this.serious[i]);
this.serious[i] = this.serious[i-1];
}else {
if(arr.length>0){
this.pushNotes(arr);
arr = [];
}
}
}
}
if(arr.length>0)
this.pushNotes(arr);
}
}
var rec={
"recMode":false,
"last" : null,
"base" : 0,
"noteList" : null,
"log2" :Math.log(2),
"pushNote" : function(fq,vol) {
if(!this.recMode)
return;
if(fq<=0)
return;
if(this.last == null){
base=fq;
}else{
if(vol<this.last[1]/2)
return;
this.noteList.push((Math.log(fq/base)/this.log2)*12);
}
this.last=[fq,vol];
},
"start" : function(){
if(this.recMode){
this.recMode=false;
var p = new noteParser(this.noteList);
document.getElementById("rec-node").innerText="录制";
}else{
this.recMode=true;
this.noteList = [];
this.last = null;
this.base = 0;
document.getElementById("rec-node").innerText="停止";
}
}
};
function lagInt4(x0,x1,x2,x3,y0,y1,y2,y3,x) {
var res=(((x-x1)*(x-x2)*(x-x3))/((x0-x1)*(x0-x2)*(x0-x3)))*y0;
res +=(((x-x0)*(x-x2)*(x-x3))/((x1-x0)*(x1-x2)*(x1-x3)))*y1;
res +=(((x-x0)*(x-x1)*(x-x3))/((x2-x0)*(x2-x1)*(x2-x3)))*y2;
res +=(((x-x0)*(x-x1)*(x-x2))/((x3-x0)*(x3-x1)*(x3-x2)))*y3;
return res;
}
window.onload = function() {
navigator.mozGetUserMedia({
audio: true
},
function(stream) {
var audioContext = new AudioContext();
inputPoint = audioContext.createGain();
var realAudioInput = audioContext.createMediaStreamSource(stream);
realAudioInput.connect(inputPoint);
analyserNode = audioContext.createAnalyser();
analyserNode.fftSize = 2048;
analyserNode.smoothingTimeConstant = 0.1;
inputPoint.connect( analyserNode );
var frequencyData = new Uint8Array(analyserNode.frequencyBinCount);
// we're ready to receive some data!
var canvas = document.getElementById('canvas'),
cwidth = canvas.width,
cheight = canvas.height - 2,
meterWidth = 1,
//width of the meters in the spectrum
gap = 2,
//gap between meters
capHeight = 2,
capStyle = '#fff',
meterNum = 1000,
//count of the meters
capYPositionArray = []; ////store the vertical position of hte caps for the preivous frame
var ctx = canvas.getContext('2d'),
gradient = ctx.createLinearGradient(0, 0, 0, 600);
gradient.addColorStop(1, '#0f0');
gradient.addColorStop(0.5, '#ff0');
gradient.addColorStop(0, '#f00');
// loop
var j=0;
var maxEle=document.getElementById("max-posi");
function renderFrame() {
var oarray = new Uint8Array(analyserNode.frequencyBinCount);
analyserNode.getByteFrequencyData(oarray);
var array = new Uint8Array(600);
for(var i=0;i<600;i++){
var ox = i/6.0;
var ox1 = Math.floor(ox);
var ox0 = ox1-1;
var ox2 = ox1+1;
var ox3 = ox1+2;
var y3 = oarray[ox3];
var y2 = oarray[ox2];
var y1 = oarray[ox1];
var y0 = 0;
if(ox0>= 0)
y0 = oarray[ox0];
array[i] = lagInt4(ox0,ox1,ox2,ox3,y0,y1,y2,y3,ox);
}
for(var i=1;i<600;i++){//滤掉突变
if((array[i]-array[i-1])>32)
array[i]=array[i-1];
}
//ctx.clearRect(0, 0, cwidth, cheight);
var maxValue=0;
var maxPosi=0;
for (var i = 16; i < 600; i++) {
var value = array[i];
var fstatus = new Uint8Array(600);
ctx.fillStyle = "rgba(0,0,"+value+",1)";
if(i>0 && i<600 && value>32
&& value>array[i-1] && value>=array[i+1]
&& value>array[i-2] && value>=array[i+2]
&& value>array[i-3] && value>=array[i+3]
&& array[i-2]>array[i-3]
&& array[i+2]>array[i+3]
){
if(value>maxValue){
maxValue = value;
maxPosi = i;
}
fstatus[i] = value;
var low = i>>1;
if(fstatus[low+1]!=0 || fstatus[low]!=0 || fstatus[low-1]!=0){
}else
ctx.fillStyle = "rgba(0,"+value+",0,1)";
}
ctx.fillRect(j
/*meterWidth+gap*/
, 600-i, 1, 1); //the meter
}
ctx.fillStyle = "rgba(255,0,0,1)";
ctx.fillRect(j, 600-maxPosi, 1, 1);
++j;
if(j>=1000)
j=0;
try{
rec.pushNote(maxPosi,maxValue);
}catch (e) {
console.log(e);
}
setTimeout(renderFrame,20);
}
renderFrame();
},
function(error) {
document.getElementById("rel-value").innerText="调用麦克风失败";
});
};
</script>
<div class="clear"></div>
</div>
</body>
</html>