-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtuner.html
237 lines (203 loc) · 8.29 KB
/
tuner.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
<html>
<head>
<title>Meet Vyas</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,700" rel="stylesheet">
<link rel="shortcut icon" type="image/png" href="pics/astronaut_favicon.png">
<link rel="stylesheet" href="css/open-iconic-bootstrap.min.css">
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/all.css">
<link rel="stylesheet" href="css/jquery.timepicker.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<link rel="stylesheet" href="css/magnific-popup.css">
<link rel="stylesheet" href="css/aos.css">
<link rel="stylesheet" href="css/ionicons.min.css">
<link rel="stylesheet" href="css/bootstrap-datepicker.css">
<link rel="stylesheet" type="text/css" href="css/flaticon.css">
<link rel="stylesheet" href="css/icomoon.css">
<link rel="stylesheet" href="css/style.css">
<script>// Define the set of test frequencies that we'll use to analyze microphone data.
var C2 = 65.41; // C2 note, in Hz.
var notes = [ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" ];
var test_frequencies = [];
for (var i = 0; i < 30; i++)
{
var note_frequency = C2 * Math.pow(2, i / 12);
var note_name = notes[i % 12];
var note = { "frequency": note_frequency, "name": note_name };
var just_above = { "frequency": note_frequency * Math.pow(2, 1 / 48), "name": note_name + " (a bit sharp)" };
var just_below = { "frequency": note_frequency * Math.pow(2, -1 / 48), "name": note_name + " (a bit flat)" };
test_frequencies = test_frequencies.concat([ just_below, note, just_above ]);
}
window.addEventListener("load", initialize);
var correlation_worker = new Worker("js/correlation_worker.js");
// Set up a message listener to receive messages from the worker
correlation_worker.onmessage = function(e) {
console.log('Message from worker:', e.data);
};
// Send a test message to the worker
correlation_worker.postMessage('Test message');
correlation_worker.addEventListener("message", interpret_correlation_result);
function initialize()
{
var get_user_media = navigator.getUserMedia;
get_user_media = get_user_media || navigator.webkitGetUserMedia;
get_user_media = get_user_media || navigator.mozGetUserMedia;
get_user_media.call(navigator, { "audio": true }, use_stream, function() {});
document.getElementById("play-note").addEventListener("click", toggle_playing_note);
}
function use_stream(stream)
{
var audio_context = new AudioContext();
var microphone = audio_context.createMediaStreamSource(stream);
window.source = microphone; // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=934512
var script_processor = audio_context.createScriptProcessor(1024, 1, 1);
script_processor.connect(audio_context.destination);
microphone.connect(script_processor);
var buffer = [];
var sample_length_milliseconds = 100;
var recording = true;
// Need to leak this function into the global namespace so it doesn't get
// prematurely garbage-collected.
// http://lists.w3.org/Archives/Public/public-audio/2013JanMar/0304.html
window.capture_audio = function(event)
{
if (!recording)
return;
buffer = buffer.concat(Array.prototype.slice.call(event.inputBuffer.getChannelData(0)));
// Stop recording after sample_length_milliseconds.
if (buffer.length > sample_length_milliseconds * audio_context.sampleRate / 1000)
{
recording = false;
correlation_worker.postMessage
(
{
"timeseries": buffer,
"test_frequencies": test_frequencies,
"sample_rate": audio_context.sampleRate
}
);
buffer = [];
setTimeout(function() { recording = true; }, 250);
}
};
script_processor.onaudioprocess = window.capture_audio;
}
function interpret_correlation_result(event)
{
var timeseries = event.data.timeseries;
var frequency_amplitudes = event.data.frequency_amplitudes;
// Compute the (squared) magnitudes of the complex amplitudes for each
// test frequency.
var magnitudes = frequency_amplitudes.map(function(z) { return z[0] * z[0] + z[1] * z[1]; });
// Find the maximum in the list of magnitudes.
var maximum_index = -1;
var maximum_magnitude = 0;
for (var i = 0; i < magnitudes.length; i++)
{
if (magnitudes[i] <= maximum_magnitude)
continue;
maximum_index = i;
maximum_magnitude = magnitudes[i];
}
// Compute the average magnitude. We'll only pay attention to frequencies
// with magnitudes significantly above average.
var average = magnitudes.reduce(function(a, b) { return a + b; }, 0) / magnitudes.length;
var confidence = maximum_magnitude / average;
var confidence_threshold = 10; // empirical, arbitrary.
if (confidence > confidence_threshold)
{
var dominant_frequency = test_frequencies[maximum_index];
document.getElementById("note-name").textContent = dominant_frequency.name;
document.getElementById("frequency").textContent = dominant_frequency.frequency;
}
}
// Unnecessary addition of button to play an E note.
var note_context = new AudioContext();
var note_node = note_context.createOscillator();
var gain_node = note_context.createGain();
note_node.frequency = C2 * Math.pow(2, 4 / 12); // E, ~82.41 Hz.
gain_node.gain.value = 0;
note_node.connect(gain_node);
gain_node.connect(note_context.destination);
note_node.start();
var playing = false;
function toggle_playing_note()
{
playing = !playing;
if (playing)
gain_node.gain.value = 0.1;
else
gain_node.gain.value = 0;
}
</script>
<style>.tuner-container {
text-align: center;
background-color: rgba(0, 0, 0, 0.8);
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.tuner-display {
position: relative;
margin-bottom: 20px;
}
.dial-container {
position: relative;
}
.dial {
width: 200px;
height: 100px;
background: rgba(255, 255, 255, 0.1);
border-top-left-radius: 100px;
border-top-right-radius: 100px;
position: relative;
overflow: hidden;
}
.needle {
width: 2px;
height: 100%;
background: red;
position: absolute;
bottom: 0;
left: 50%;
transform: rotate(0deg);
transform-origin: bottom;
}
.note-display {
margin-top: -20px;
font-size: 24px;
}
button {
background-color: #333;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
border-radius: 5px;
}
button:hover {
background-color: #555;
}
</style>
</head>
<body>
<div class="ftco-section">
<div class="container">
<div class="row justify-content-center mb-5 pb-5">
<div class="note-display">
<p>You are playing: </p>
<h1 id="note-name"></h1>
<p>
<span>frequency (Hz):</span>
<span id="frequency"></span>
</p>
</div>
</div>
</div>
</div>
</body>
</html>