-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
274 lines (233 loc) · 7.61 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<html>
<head>
<style type="text/css">
body {
}
.visualizer {
height: 200;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
padding-bottom: 50px;
}
.logo {
background: blue;
min-width: 1.5%;
}
.mic {
border-radius: 20px;
background: red;
min-height: 20;
min-width: 10;
position: absolute;
left: 50;
}
.sql {
position: absolute;
left: 50;
}
.settings
{
margin-top: 50px;
border-radius: 20px;
}
.audios_history
{
padding-top: 50px;
}
h1 {
color: #000;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Start</h1>
</body>
<script type="text/javascript">
var create_checkbox = function(id, text, parent)
{
var check = document.createElement('input');
check.type = 'checkbox';
check.id = id;
check.style = style="margin-left: 20px;"
parent.appendChild(check);
var label = document.createElement("label");
label.for = id;
label.textContent = text;
parent.appendChild(label);
return check;
};
window.onload = function()
{
// к этому моменту страница загружена
/*СОЗДАЁМ ОСНОВНЫЕ ПЕРЕМЕННЫЕ*/
var body, num, array, context, myElements, analyser, frequencyBinCount, mic, sql, mediaRecorder,settings_div, audios_history,repeat_checkbox,save_checkbox;
/*ЗАПИСЫВАЕМ В ПЕРЕМЕННУЮ body ЭЛЕМЕНТ body*/
body = document.querySelector('body');
var audioChunks = [];
/*УКАЗЫВАЕМ КОЛИЧЕСТВО СТОЛБЦОВ НА ИНДИКАТОРЕ*/
num = 64;
var record_enabled = false;
var record_start_time = 0;
var record_stop_time = 0;
var signal_time = 0;
var waiting_time = 0;
var silent_time = 2;
var min_record_time = 2;
/*ПРИ НАЖАТИИ НА КНОПКУ МЫШИ В ДОКУМЕНТЕ*/
window.onclick = function()
{
if(context) return;
context = new AudioContext(); /*СОЗДАЕМ НОВЫЙ ЭКЗЕМПЛЯР КЛАССА AudioContext*/
/*СОЗДАЁМ АНАЛАЙЗЕР*/
analyser = context.createAnalyser();
frequencyBinCount = analyser.frequencyBinCount;
// num = frequencyBinCount;
console.log(num, frequencyBinCount);
/*УДАЛЯЕМ ЭЛЕМЕНТ С ТЕКСТОМ*/
body.querySelector('h1').remove();
/*ПРОПИСЫВАЕМ ЦИКЛ ВНУТРИ КОТОРОГО БУДЕМ СОЗДАВАТЬ ЭЛЕМЕНТЫ НАШИХ СТОЛБИКОВ*/
var vis = document.createElement('div');
vis.className = 'visualizer';
for(var i = 0 ; i < num ; i++){
var logo = document.createElement('div'); /*КАЖДЫЙ ЭЛЕМЕНТ БУДЕТ ЗАПИСЫВАТЬСЯ ВНУТРИ ПЕРЕМЕННОЙ logo*/
logo.className = 'logo'; /*ДЛЯ ВЫШЕ СОЗДАННОГО ЭЛЕМЕНТА МЫ ПРОПИСЫВАЕМ КЛАСС logo*/
vis.appendChild(logo); /*ДОБАВЛЯЕМ ЭЛЕМЕНТ ВО ВНУТРЬ ЭЛЕМЕНТА body*/
}
body.appendChild(vis);
myElements = document.getElementsByClassName('logo');/*ЗАПИСЫВАЕМ ЭЛЕМЕНТ logo В ПЕРЕМЕННУЮ myElement*/
// средний уровень
// mic = document.getElementById('mic');
mic = document.createElement('div');
mic.className = 'mic';
body.appendChild(mic);
// sql
// sql = document.getElementById('sql');
sql = document.createElement('input');
sql.type = 'range';
sql.className = 'sql';
sql.min = 0;
sql.max = 1;
sql.step = 0.01;
sql.value = 0.5;
body.appendChild(sql);
settings_div = document.createElement('div');
settings_div.className = 'settings';
body.appendChild(settings_div);
repeat_checkbox = create_checkbox('repeat', 'repeater', settings_div);
save_checkbox = create_checkbox('save_files', 'auto save files', settings_div);
audios_history = document.createElement('div');
audios_history.className = 'audios_history';
body.appendChild(audios_history);
/*СОЗДАЕМ МАССИВ*/
array = new Uint8Array(frequencyBinCount);
/*СОЗДАЁМ ЗАПРОС ДОСТУПА К МИКРОФОНУ*/
navigator.mediaDevices.getUserMedia({
audio: true
}).then(stream => {
var src = context.createMediaStreamSource(stream);
src.connect(analyser);
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.addEventListener("dataavailable",function(event) {
audioChunks.push(event.data);
console.log(audioChunks);
});
mediaRecorder.addEventListener("stop", function() {
if (record_stop_time - record_start_time > min_record_time)
{
waiting_time = repeat_checkbox.checked ? record_stop_time + (record_stop_time - record_start_time) + silent_time * 2 : record_stop_time + silent_time;
const audioBlob = new Blob(audioChunks, {
type: 'audio/wav'
});
var audio_container = document.createElement('div');
audio_container.value = Date();
var audio_date = document.createElement('div');
audio_date.textContent = Date();
audio_container.appendChild(audio_date);
const audioUrl = URL.createObjectURL(audioBlob);
var audio = document.createElement('audio');
audio.src = audioUrl;
audio.controls = true;
audio.autoplay = repeat_checkbox.checked;
audio_container.appendChild(audio);
audios_history.appendChild(audio_container);
if (save_checkbox.checked)
{
var link = document.createElement('a');
document.body.appendChild(link);
link.download = Date();
link.href = window.URL.createObjectURL(audioBlob);
link.click();
document.body.removeChild(link); //remove the link when done
}
}
audioChunks = [];
});
loop();
}).catch(error => {
console.log(error);
console.log(navigator.mediaDevices);
alert(error + '\r\n\ Rejected!!! cant get access to navigator.mediaDevices.getUserMedia');
// location.reload();
});
}
function loop() {
window.requestAnimationFrame(loop);
analyser.getByteFrequencyData(array);
// console.log(analyser);
var v = 0;
var step = Math.floor(frequencyBinCount / num);
// var step = 1
for(var i = 0 ; i < num ; i++){
var height = 0;
for (var j = 0; j < step; j++)
{
height += array[i * step + j];
}
height /= step;
myElements[i].style.minHeight = height+'px';
myElements[i].style.opacity = 0.008*height;
v += height;
}
v /= num;
v /= 256; // normalize
mic.style.minWidth = v * (document.body.clientWidth - 100);
sql.style.minWidth = document.body.clientWidth - 100;
var current_time = Date.now()/1000;
if (current_time > waiting_time)
{
waiting_time = 0;
if (v > sql.value)
{
signal_time = current_time;
mic.style.background = "red";
if (!record_enabled)
{
mediaRecorder.start();
record_start_time = current_time;
record_enabled = true;
settings_div.style.background = "red";
console.log("media start!!!!!!");
}
}
else
{
mic.style.background = "yellow";
}
if ((current_time - signal_time > silent_time) && record_enabled)
{
console.log("media stop!!!!!!");
record_stop_time = signal_time;
record_enabled = false;
settings_div.style.background = '#00ff00';
mediaRecorder.stop();
}
if (audios_history.childElementCount > 10)
audios_history.removeChild(audios_history.firstChild);
}
}
};
</script>
</html>