-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeFiveGrid.html
More file actions
205 lines (172 loc) · 5.28 KB
/
Copy paththreeFiveGrid.html
File metadata and controls
205 lines (172 loc) · 5.28 KB
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
<html>
<head><title>Tones</title>
<style>
body {
font-family: arial;
}
.tones {
margin: auto;
width: 90vw;
display: grid;
grid-template-rows: auto auto auto auto;
grid-template-columns: repeat(25, 3.6vw);
background-color: #2196F3;
padding: 1px;
}
.tone {
cursor:pointer;
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 5px;
font-size: 10px;
text-align: center;
}
.tone.tempered {
background-color: rgba(0, 255, 128);
}
.tone.pythagorean {
background-color: rgba(255, 127, 255, 0.8);
}
.tone.five-limit {
background-color: rgba(127, 255, 255, 0.8);
}
.tone.five-limit.pythagorean {
background-color: rgba(191, 191, 255, 0.8);
}
.tone.playing {
background-color: rgba(255, 255, 127, 0.8) !important;
}
.noteName {
font-size: 20px;
font-weight: bold;
}
#oscilloscope {
border: 1px solid rgba(0, 0, 0, 0.8);
width: 45vw;
height: 10vw;
display: block;
margin: auto;
}
</style>
</head>
<body>
<div id="tones" class="tones"></div>
<canvas id="oscilloscope"></canvas>
<script>
var sharp = '\u266f';
var flat = '\u266d';
var middleC = 261.625565;
var noteNames=['C','C'+sharp,'D','E'+flat,'E','F','F'+sharp,'G','A'+flat,'A','B'+flat,'B'];
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var analyser = audioCtx.createAnalyser();
analyser.fftSize = 8192;
var bufferLength = analyser.frequencyBinCount;
var dataArray = new Float32Array(bufferLength);
var prevArray = new Float32Array(bufferLength);
var canvas = document.getElementById("oscilloscope");
var canvasCtx = canvas.getContext("2d");
// draw an oscilloscope of the current audio source
const sq = x => x * x;
var prevMaxCorrelationIndex = 0;
function draw() {
setTimeout(function(){requestAnimationFrame(draw);}, 40);
var tmp = prevArray;
prevArray = dataArray;
dataArray = tmp;
analyser.getFloatTimeDomainData(dataArray);
// find the max cross correlation
var maxCorrelationIndex = 0;
var maxCorrelationValue = -Infinity;
for (var i = 0; i < bufferLength/2; i++) {
var curCorrelationValue = 0;
for (var j = 0; j < bufferLength/2; j++) {
curCorrelationValue += -sq(prevArray[j+prevMaxCorrelationIndex] - dataArray[j+i]);
}
if (curCorrelationValue > maxCorrelationValue) {
maxCorrelationValue = curCorrelationValue;
maxCorrelationIndex = i;
}
}
canvasCtx.fillStyle = "rgb(255, 255, 255)";
canvasCtx.fillRect(0, 0, canvas.width, canvas.height);
canvasCtx.lineWidth = 1.5;
canvasCtx.strokeStyle = "rgb(0, 0, 0)";
canvasCtx.beginPath();
var sliceWidth = canvas.width * 2 / bufferLength;
var x = 0;
for (var i = 0; i < bufferLength/2; i++) {
var v = dataArray[i + maxCorrelationIndex];
var y = ((v*0.5) + 1) * canvas.height / 2;
if (i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth;
}
canvasCtx.stroke();
prevMaxCorrelationIndex = maxCorrelationIndex;
}
draw();
function toggleTone(tone) {return function() {
var playing = tone.classList.toggle("playing");
if (playing) {
var oscillator = audioCtx.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = tone.freq;
oscillator.connect(audioCtx.destination);
oscillator.connect(analyser);
tone.oscillator = oscillator;
tone.oscillator.start();
} else {
tone.oscillator.stop();
}
}};
var hs = Math.pow(2, 1/12);
var tonesEl = document.getElementById("tones");
for (var r=-1; r<=1; r++) {
for (var c=-12; c<=12; c++) {
var tone = document.createElement('div');
tone.className="tone";
tone.row = r;
tone.col = c;
if (Math.abs(c) < 3) tone.classList.add("five-limit");
if ((Math.abs(c) < 7) && (r == 0)) tone.classList.add("pythagorean");
var note = (12+(r*4+c*7)%12)%12;
tone.noteName = noteNames[note];
var numerator = Math.pow(5,Math.max(r,0))*Math.pow(3,Math.max(c,0));
var denominator = Math.pow(5,Math.max(-r,0))*Math.pow(3,Math.max(-c,0));
var scale = Math.floor(Math.log2(numerator/denominator)+(1/24))
numerator *= Math.pow(2, Math.max(-scale,0));
denominator *= Math.pow(2, Math.max(scale,0));
tone.numerator = numerator;
tone.denominator = denominator;
var freq = middleC * numerator / denominator;
tone.freq = freq;
tone.temperedFreq = middleC * Math.pow(2, note/12);
var cents = (Math.log2(tone.freq/tone.temperedFreq)*1200);
tone.cents = cents;
tone.innerHTML = "<span class=\"noteName\">"+tone.noteName+"</span><br><br>"+
tone.numerator+"<br>——<br>"+tone.denominator+"<br><br>"+
tone.freq.toFixed(0)+"<br><br>"+((cents>=0)?"+":"")+cents.toFixed(0)+"¢";
tone.addEventListener("click", toggleTone(tone));
tonesEl.appendChild(tone);
}
}
for (var c=-12; c<=12; c++) {
var tone = document.createElement('div');
tone.className="tone tempered";
tone.col = c;
var note = ((c%12)+12)%12;
tone.noteName = noteNames[note];
tone.temperedFreq = middleC * Math.pow(2, note/12);
tone.freq = tone.temperedFreq;
tone.innerHTML = "<span class=\"noteName\">"+tone.noteName+"</span><br><br>"+
"2<sup>"+note+"/12</sup><br><br>"+
tone.freq.toFixed(0);
tone.addEventListener("click", toggleTone(tone));
tonesEl.appendChild(tone);
}
</script>
</body>
</html>