-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
178 lines (157 loc) · 4.34 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
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title>Жуок</title>
<style type="text/css">
.wrapper {
width: 900px;
margin: 0 auto;
}
.wrapper div {
margin: 10px 0;
}
@media screen and (max-width: 900px) {
.wrapper {
width: calc(100% - 20px);
}
.wrapper div {
margin: 10px;
}
}
.header {
text-align: center;
}
input[type="range"] {
width: 100%;
}
.buttons {
display: flex;
flex-direction: row;
}
.button {
flex: 1;
}
.button button {
width: calc(100% - 20px);
margin: 10px;
font-size: 30px;
}
button#start {
background-color: chartreuse;
color: aliceblue;
}
button#stop {
background-color: coral;
color: aliceblue;
}
</style>
<script>
function generateFrequencies(f) {
const frequencies = [];
const i1 = 0.03;
const i2 = i1 * 2;
const i3 = i1 * 3;
const i4 = i1 * 4;
for (const i of [f, f + i1, f + i2, f + i3, f + i4, f - i1, f - i2, f - i3, f - i4]) {
for (const j of [i, 5 * i, 6 * i, 7 * i, 8 * i, 9 * i, 10 * i]) {
frequencies.push(j);
}
}
return frequencies;
}
document.addEventListener("DOMContentLoaded", function () {
let f = 50;
let volume = 9;
let frequencies = generateFrequencies(f);
// Добавление 63 (9 * 7) частот в массив
generateFrequencies(f);
const oscillators = [], gains = [];
const context = new AudioContext();
const start = document.getElementById("start");
const stop = document.getElementById("stop");
const volumeEl = document.getElementById("volume");
const freqEl = document.getElementById("freq");
start.addEventListener("click", function () {
for (let i = 0; i < frequencies.length; i++) {
const o = context.createOscillator();
o.frequency.value = frequencies[i];
const g = context.createGain();
o.connect(g);
g.connect(context.destination);
g.gain.value = (volume / 10) / frequencies.length;
o.start(0);
oscillators[i] = o;
gains[i] = g;
}
start.disabled = true;
stop.disabled = false;
});
stop.addEventListener("click", function () {
for (let i = 0; i < frequencies.length; i++) {
gains[i].gain.exponentialRampToValueAtTime(
0.00001, context.currentTime + 0.2
);
oscillators[i].stop(context.currentTime + 0.4);
}
start.disabled = false;
stop.disabled = true;
});
freqEl.addEventListener("input", function (event) {
f = Number.parseInt(event.target.value);
frequencies = generateFrequencies(f);
for (let i = 0; i < frequencies.length; i++) {
oscillators[i].frequency.value = frequencies[i];
}
});
volumeEl.addEventListener("input", function (event) {
volume = Number.parseInt(event.target.value);
for (let i = 0; i < frequencies.length; i++) {
gains[i].gain.exponentialRampToValueAtTime(
(volume / 10) / frequencies.length, context.currentTime + 0.1
);
}
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="header">
<h1>
Жуок
</h1>
</div>
<div class="block">
<div class="buttons">
<div class="button">
<button id="start">start</button>
</div>
<div class="button">
<button id="stop" disabled>stop</button>
</div>
</div>
</div>
<div class="block">
<p>
Базовая частота
</p>
<input id="freq" type="range" max="100" min="1" value="50"/>
</div>
<div class="block">
<p>
Громкость
</p>
<input id="volume" type="range" max="13" min="0" value="9"/>
</div>
<div class="block">
<p><b>Объяснение:</b></p>
<p>Для генерации звука используется
<a href="https://ru.wikipedia.org/wiki/%D0%A2%D0%BE%D0%BD_%D0%A8%D0%B5%D0%BF%D0%B0%D1%80%D0%B4%D0%B0">
непрерывный ряд Рисе
</a>.
</p>
</div>
</div>
</body>
</html>