-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
184 lines (148 loc) · 6.39 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
<!DOCTYPE html>
<html>
<head>
<title>Sine Wave Interference Warbling Demonstration</title>
</head>
<link rel="stylesheet" href="styles.css">
<body style="zoom: 110%">
<h1>Sine Wave Interference Warbling Demonstration</h1>
<p>This website demonstrates how audio waves of different frequencies blend together and cancel each other out, and how the closer two frequencies are, the slower the warbling will be.</p>
<p>Use the sliders below to adjust the frequency of the two audio waves:</p>
<div id="controls">
<label for="frequency1">Frequency 1:</label>
<input type="range" id="frequency1" min="0" max="20000" value="115" step="1" style="width: calc(100% - 50px)">
<input type="text" id="frequency1-display" value="115"> Hz
<br>
<label for="frequency2">Frequency 2:</label>
<input type="range" id="frequency2" min="0" max="20000" value="290" step="1" style="width: calc(100% - 50px)">
<input type="text" id="frequency2-display" value="290"> Hz
</div>
<button id="mute-button">Mute</button>
<h3>Time Domain</h3>
<canvas id="waveform" width="800" height="400"></canvas>
<h3>Frequency Domain</h3>
<canvas id="frequencyform" width="800" height="400"></canvas>
<h3>Try All Of These Combinations</h3>
<p>
<button class="set-frequency" data-freq1="129" data-freq2="130">Set to 129 Hz and 130 Hz</button>
</p>
<p>
<button class="set-frequency" data-freq1="129" data-freq2="131">Set to 129 Hz and 131 Hz</button>
</p>
<div class="centered-container">
<button class="set-frequency" data-freq1="129" data-freq2="129.1">Set to 129 Hz and 129.1 Hz</button>
<span class="message">(Wait it out, notice the changes)</span>
</div>
<p>Singers can use sine waves to train pitch accuracy, as they sing one note against a sine wave and aim to minimize the warbling.</p>
<h3>Additional</h3>
<p>This project was made with the help of ChatGPT. It's impressive to see what it's capable of!</p>
<p>I did learn some things along the way as I tried to fix things</p>
<script>
var canvasTime = document.getElementById('waveform');
var canvasFreq = document.getElementById('frequencyform');
var frequency1Slider = document.getElementById('frequency1');
var frequency1Display = document.getElementById('frequency1-display');
var frequency2Slider = document.getElementById('frequency2');
var frequency2Display = document.getElementById('frequency2-display');
var muteButton = document.getElementById('mute-button');
var audioContext = new AudioContext();
var oscillator1 = audioContext.createOscillator();
var oscillator2 = audioContext.createOscillator();
var gainNode = audioContext.createGain();
oscillator1.frequency.value = frequency1Slider.value;
oscillator2.frequency.value = frequency2Slider.value;
oscillator1.connect(gainNode);
oscillator2.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator1.start();
oscillator2.start();
var analyser = audioContext.createAnalyser();
gainNode.connect(analyser);
function drawTimeDomain() {
var data = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteTimeDomainData(data);
canvasTime.getContext('2d').clearRect(0, 0, canvasTime.width, canvasTime.height);
canvasTime.getContext('2d').beginPath();
var width = canvasTime.width;
var height = canvasTime.height;
var sliceWidth = width * 1.0 / data.length;
var x = 0;
for (var i = 1; i < data.length; i++) {
var v = data[i] / 128.0;
var y = v * height/2;
if(i === 0) {
canvasTime.getContext('2d').moveTo(x, y);
} else {
canvasTime.getContext('2d').lineTo(x, y);
}
x += sliceWidth;
}
canvasTime.getContext('2d').stroke();
requestAnimationFrame(drawTimeDomain);
}
function drawFrequencyDomain() {
var data = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(data);
canvasFreq.getContext('2d').clearRect(0, 0, canvasFreq.width, canvasFreq.height);
var barWidth = (canvasFreq.width / data.length);
var barHeight;
var x = 0;
for(var i = 0; i < data.length; i++) {
barHeight = data[i];
canvasFreq.getContext('2d').fillStyle = 'rgb(' + (barHeight+100) + ',50,50)';
canvasFreq.getContext('2d').fillRect(x,canvasFreq.height-barHeight/2,barWidth,barHeight/2);
x += barWidth + 1;
}
requestAnimationFrame(drawFrequencyDomain);
}
drawTimeDomain();
drawFrequencyDomain();
frequency1Slider.addEventListener('input', function() {
oscillator1.frequency.value = frequency1Slider.value;
frequency1Display.value = frequency1Slider.value;
});
frequency2Slider.addEventListener('input', function() {
oscillator2.frequency.value = frequency2Slider.value;
frequency2Display.value = frequency2Slider.value;
});
frequency1Display.addEventListener('input', function() {
frequency1Slider.value = frequency1Display.value;
oscillator1.frequency.value = frequency1Display.value;
});
frequency2Display.addEventListener('input', function() {
frequency2Slider.value = frequency2Display.value;
oscillator2.frequency.value = frequency2Display.value;
});
muteButton.addEventListener('click', function() {
if (muteButton.textContent === 'Mute') {
gainNode.gain.value = 0;
muteButton.textContent = 'Unmute';
} else {
gainNode.gain.value = 1;
muteButton.textContent = 'Mute';
}
});
function startAudio() {
audioContext.resume().then(() => {
// start the oscillators and draw the waveform here
});
}
document.addEventListener('click', startAudio);
document.querySelectorAll('.set-frequency').forEach(button => {
button.addEventListener('click', function() {
// Get the frequencies from the data attributes
let freq1 = this.getAttribute('data-freq1');
let freq2 = this.getAttribute('data-freq2');
// Set the oscillator frequencies
oscillator1.frequency.value = freq1;
oscillator2.frequency.value = freq2;
// Update the slider and display values
frequency1Slider.value = freq1;
frequency2Slider.value = freq2;
frequency1Display.value = freq1;
frequency2Display.value = freq2;
});
});
</script>
</body>
</html>