-
Notifications
You must be signed in to change notification settings - Fork 3
/
tr-303.html
253 lines (231 loc) · 7.6 KB
/
tr-303.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TB-303 Synth Emulator</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet" />
<style>
body {
background: #282c34;
color: white;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #4caf50;
outline: none;
opacity: 0.7;
-webkit-transition: 0.2s;
transition: opacity 0.2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb,
.slider::-moz-range-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #fff;
cursor: pointer;
}
table {
width: 100%;
table-layout: fixed;
}
th,
td {
padding: 8px;
text-align: center;
}
</style>
</head>
<body class="p-6">
<h1 class="mb-4 text-3xl">TB-303 Synth Emulator</h1>
<button class="rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700" onclick="synth.playPattern()">
Play Pattern
</button>
<button class="rounded bg-red-500 px-4 py-2 font-bold text-white hover:bg-red-700" onclick="synth.stopPattern()">
Stop Pattern
</button>
<div class="mt-4">
<label for="cutoff">Filter Cutoff Frequency:</label>
<input
type="range"
id="cutoff"
class="slider"
min="100"
max="5000"
value="1200"
step="10"
oninput="synth.updateFilterFrequency(this.value)"
/>
<span id="cutoffValue">1200 Hz</span>
</div>
<div class="mt-4">
<label for="resonance">Filter Resonance (Q):</label>
<input
type="range"
id="resonance"
class="slider"
min="1"
max="20"
value="10"
step="1"
oninput="synth.updateResonance(this.value)"
/>
<span id="resonanceValue">10</span>
</div>
<div class="mt-4">
<label for="volume">Master Volume:</label>
<input
type="range"
id="volume"
class="slider"
min="0"
max="1"
value="0.9"
step="0.01"
oninput="synth.updateVolume(this.value)"
/>
<span id="volumeValue">90%</span>
</div>
<h2 class="mb-3 mt-6 text-2xl">Pattern Editor</h2>
<table id="patternEditor">
<tr>
<th>Note Frequency (Hz)</th>
<th>Actions</th>
</tr>
</table>
<button onclick="addNote()">Add Note</button>
<script>
const synth = (() => {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
let oscillator, filter, envelope, gainNode;
let currentStep = 0;
let isPlaying = false;
let pattern = [65.41, 65.41, 65.41, 65.41, 65.41, 98.0, 87.31];
const tempo = 200;
const intervalTime = 60 / tempo / 2;
document.addEventListener("DOMContentLoaded", renderPatternEditor);
function createOscillator(frequency) {
oscillator = audioContext.createOscillator();
oscillator.type = "sawtooth";
oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
oscillator.start();
return oscillator;
}
function createFilter(frequency, q) {
filter = audioContext.createBiquadFilter();
filter.type = "lowpass";
filter.frequency.setValueAtTime(frequency, audioContext.currentTime);
filter.Q.setValueAtTime(q, audioContext.currentTime);
return filter;
}
function createEnvelope() {
envelope = audioContext.createGain();
envelope.gain.setValueAtTime(0.00001, audioContext.currentTime);
envelope.gain.exponentialRampToValueAtTime(1, audioContext.currentTime + 0.01);
envelope.gain.exponentialRampToValueAtTime(0.3, audioContext.currentTime + 0.2);
envelope.gain.exponentialRampToValueAtTime(0.0001, audioContext.currentTime + 1);
return envelope;
}
function createGainNode(volume) {
gainNode = audioContext.createGain();
gainNode.gain.setValueAtTime(volume, audioContext.currentTime);
return gainNode;
}
function playNote() {
const freq = pattern[currentStep % pattern.length];
const cutoff = parseInt(document.getElementById("cutoff").value);
const q = parseInt(document.getElementById("resonance").value);
const volume = parseFloat(document.getElementById("volume").value);
const osc = createOscillator(freq);
const filt = createFilter(cutoff, q);
const env = createEnvelope();
const gain = createGainNode(volume);
osc.connect(filt);
filt.connect(env);
env.connect(gain);
gain.connect(audioContext.destination);
setTimeout(() => {
osc.stop();
currentStep++;
}, intervalTime * 1000);
}
function playPattern() {
if (!isPlaying) {
isPlaying = true;
setInterval(() => {
if (isPlaying) playNote();
}, intervalTime * 1000);
}
}
function stopPattern() {
isPlaying = false;
}
function updateFilterFrequency(value) {
if (filter) {
filter.frequency.setValueAtTime(value, audioContext.currentTime);
}
document.getElementById("cutoffValue").textContent = value + " Hz";
}
function updateResonance(value) {
if (filter) {
filter.Q.setValueAtTime(value, audioContext.currentTime);
}
document.getElementById("resonanceValue").textContent = value;
}
function updateVolume(value) {
if (gainNode) {
gainNode.gain.setValueAtTime(value, audioContext.currentTime);
}
document.getElementById("volumeValue").textContent = Math.round(value * 100) + "%";
}
function renderPatternEditor() {
const table = document.getElementById("patternEditor");
table.innerHTML = `
<tr>
<th>Note Frequency (Hz)</th>
<th>Actions</th>
</tr>
`;
pattern.forEach((note, index) => {
const row = table.insertRow(-1);
const cell1 = row.insertCell(0);
const cell2 = row.insertCell(1);
cell1.innerHTML = `<input type='number' value='${note}' step='0.01' onchange='synth.updateNoteFrequency(${index}, this.value)' />`;
cell2.innerHTML = `<button onclick='synth.removeNote(${index})'>Remove</button>`;
});
}
function addNote() {
pattern.push(440); // Default to A4
renderPatternEditor();
}
function updateNoteFrequency(index, frequency) {
pattern[index] = parseFloat(frequency);
}
function removeNote(index) {
pattern.splice(index, 1);
renderPatternEditor();
}
return {
playPattern,
stopPattern,
updateFilterFrequency,
updateResonance,
updateVolume,
updateNoteFrequency,
addNote,
removeNote,
};
})();
</script>
</body>
</html>