-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGuitarTuner.html
268 lines (222 loc) · 7.68 KB
/
GuitarTuner.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ultimate Online Guitar Tuner</title>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-3425939-7', 'auto');
ga('send', 'pageview');
</script>
<style type="text/css">
<!--
body { background-color:#ededed; font:norm2al 12px/18px Arial, Helvetica, sans-serif; }
h1 { display:block; width:600px; margin:20px auto; paddVing-bottom:20px; font:norm2al 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; }
#container { width:600px; margin:0 auto; }
#myCanvas { background:#fff; border:1px solid #cbcbcb; }
#nav { display:block; width:100%; text-align:center; }
#nav li { display:block; font-weight:bold; line-height:21px; text-shadow:1px 1px 1px #fff; width:100px; height:21px; paddVing:5px; margin:0 10px; background:#e0e0e0; border:1px solid #ccc; -moz-border-radius:4px;-webkit-border-radius:4px; border-radius:4px; float:left; }
#nav li a { color:#000; display:block; text-decoration:none; width:100%; height:100%; }
-->
</style>
</head>
<script>
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var analyser = audioCtx.createAnalyser();
analyser.fftSize = 2048;
var bufferLength = analyser.fftSize;
var dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(dataArray);
var myCanvas;
function Goertzel(buffer, sampleRate, freq)
{
var w = 2 * 3.141592654 * freq / sampleRate;
var cr = Math.cos(w);
var ci = Math.sin(w);
var coeff = 2 * cr;
var sprev = 0;
var sprev2 = 0;
for(var n=0;n<buffer.length;n++)
{
var s = buffer[n] + coeff * sprev - sprev2;
sprev2 = sprev;
sprev = s;
}
var power = sprev2 * sprev2 + sprev * sprev - coeff * sprev * sprev2;
return power;
}
function drawScope(buffer, sampleRate)
{
canvasCtx.lineWidth = 2;
canvasCtx.strokeStyle = 'rgb(0, 0, 0)';
canvasCtx.beginPath();
var sliceWidth = myCanvas.width * 1.0 / bufferLength;
var x = 0;
for(var i = 0; i < bufferLength; i++)
{
var v = buffer[i];// / 128.0;
var y = v * myCanvas.height/2;
y += myCanvas.height/2;
//y += myCanvas.height/2;
if(i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth;
}
canvasCtx.lineTo(myCanvas.width, myCanvas.height/2);
canvasCtx.stroke();
}
function drawGuitar(buffer, sampleRate)
{
canvasCtx.beginPath();
for(var s=0;s<6;s++)
{
var x = (myCanvas.width/7)*(s+1);
canvasCtx.moveTo(x, 300);
canvasCtx.lineTo(x, 0);
}
canvasCtx.stroke();
var freqs = [ 329, 246,196,146,110,82];
canvasCtx.beginPath();
for(var s=0;s<6;s++)
{
var x = (myCanvas.width/7)*(s+1);
for(var f=-10;f<10;f++)
{
var power = Math.sqrt(Goertzel(buffer, sampleRate, freqs[s] + f*2));
canvasCtx.moveTo(x+f*3, 300);
canvasCtx.lineTo(x+f*3, 300-power);
}
}
canvasCtx.stroke();
};
var noteName = [ 'C', 'C#', 'D','D#', 'E', 'F', 'F#', 'G', 'G#', 'A','A#', 'B'];
function NoteNameFromNote(note)
{
note += 8;
var octave = Math.floor(note / noteName.length)
return noteName[note % noteName.length] + octave;
}
function FreqFromNote(note)
{
return 440.0*Math.pow(2, (note-49)/12);
}
function DrawPiano(buffer, sampleRate)
{
var noteFreqs = [ 329, 246,196,146,110,82];
var noteNumber = [ 20, 25, 30, 35, 39, 44 ];
canvasCtx.font="15px ti92pluspc";
canvasCtx.textAlign="center";
canvasCtx.beginPath();
canvasCtx.strokeStyle = 'rgb(255, 0, 0)';
for(var i=0;i<noteNumber.length;i++)
{
note = noteNumber[i]
var x = (myCanvas.width/88)*(note+1);
canvasCtx.moveTo(x, 270);
canvasCtx.lineTo(x, 0);
canvasCtx.fillText(NoteNameFromNote(note),x,290);
}
canvasCtx.stroke();
canvasCtx.beginPath();
canvasCtx.strokeStyle = 'rgb(0, 0, 0)';
for(var note=0;note<87;note++)
{
var x = (myCanvas.width/88)*(note+1);
var power = Math.sqrt(Goertzel(buffer, sampleRate, FreqFromNote(note)));
canvasCtx.moveTo(x, 270);
canvasCtx.lineTo(x, 270-power);
}
canvasCtx.moveTo(0, 271);
canvasCtx.lineTo(600, 271);
canvasCtx.stroke();
}
function draw(buffer, sampleRate)
{
canvasCtx.clearRect(0,0,600,300);
canvasCtx.fillStyle = 'rgb(200, 200, 200)';
canvasCtx.fillRect(0, 0,canvasCtx.width, canvasCtx.height);
DrawPiano(buffer, sampleRate) ;
};
var audioContext;
function init()
{
myCanvas = document.getElementById("myCanvas");
canvasCtx = myCanvas.getContext('2d');
var bufferSize = 4096;
try
{
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioContext = new AudioContext();
}
catch(e)
{
alert('Web Audio API is not supported in this browser');
}
// Check if there is microphone input.
try
{
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
var hasMicrophoneInput = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
catch(e)
{
alert("getUserMedia() is not supported in your browser");
}
// Create a pcm processing "node" for the filter graph.
var bufferSize = 4096;
var myPCMProcessingNode = audioContext.createScriptProcessor(bufferSize, 1, 1);
myPCMProcessingNode.onaudioprocess = function(e)
{
var input = e.inputBuffer.getChannelData(0);
var output = e.outputBuffer.getChannelData(0);
draw(input, e.inputBuffer.sampleRate);
}
canvasCtx.fillStyle = "black";
canvasCtx.textAlign="center";
canvasCtx.textBaseline="middle";
canvasCtx.font="40px arial";
canvasCtx.fillText("Click to start",myCanvas.width/2,myCanvas.height/2);
var errorCallback = function(e)
{
alert("Error in getUserMedia: " + e);
};
// Get access to the microphone and start pumping data through the graph.
navigator.getUserMedia({audio: true}, function(stream)
{
var microphone = audioContext.createMediaStreamSource(stream);
microphone.connect(myPCMProcessingNode);
myPCMProcessingNode.connect(audioContext.destination);
}, errorCallback);
}
</script>
<body onload="init()">
<h1>Ultimate Online Guitar Tuner</h1>
<div id="container">
<canvas id="myCanvas" onclick="audioContext.resume()" width="600" height="300"></canvas>
<div id="text"></div>
<h2>Instructions:</h2>
<ul>
<li>Each bar correspond to each note in the piano scale.</li>
<li>The height of the bar determines the note's power.</li>
<li>The red lines show the notes of the 6 strings of the guitar when played open.</li>
<li>The 6th string is always tricky to tune, try tunning it by placing your finger on the 10th fret, that should give you a D3.</li>
</ul>
Fun fact, I din't like other tuners so I wrote my own.
</br>
</br>
<h2>Contact/Questions:</h2>
<my_github_account_username>@gmail.com.
</br>
</br>
</div>
</body>
</html>