-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
552 lines (451 loc) · 20 KB
/
script.js
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Elements for Drag and Drop & File Upload Logic
const dropZone = document.getElementById('dropZone');
const audioElement = document.getElementById('audioElement');
const audioFileInput = document.getElementById('audioFileInput');
const audioCanvas = document.getElementById('Audio_waveform');
const audioCanvasContext = audioCanvas.getContext('2d');
// Elements for Recording Logic
let mediaRecorder;
let audioChunks = [];
const recordedAudio = document.getElementById('recordedAudio');
const startRecordingButton = document.getElementById('startRecording');
const stopRecordingButton = document.getElementById('stopRecording');
const saveRecordingButton = document.getElementById('saveRecording');
const recordCanvas = document.getElementById('waveform');
const recordCanvasContext = recordCanvas.getContext('2d');
// Elements for Playback Logic
const playbackSpeedSlider = document.getElementById('playbackSpeed');
const speedDisplay = document.getElementById('speedDisplay');
const localStorageKey = 'preferredPlaybackSpeed';
// Elements for recorded audio playback speed control
const recordPlaybackSpeedSlider = document.getElementById('recordPlaybackSpeed');
const recordSpeedDisplay = document.getElementById('recordSpeedDisplay');
const recordedAudioStorageKey = 'preferredRecordedPlaybackSpeed';
// Elements for Volume Control
const volumeControlSlider = document.getElementById('volumeControl');
const volumeDisplay = document.getElementById('volumeDisplay');
// Elements for Recorded Audio Volume Control
const recordVolumeControlSlider = document.getElementById('recordVolumeControl');
const recordVolumeDisplay = document.getElementById('recordVolumeDisplay');
// Web Audio API context will be created upon user interaction
let audioContext;
// Declare global variables for recorded audio visualization
let recordedAudioSource;
let recordedAudioAnalyser;
let recordedAudioAnimationId;
// Audio Recording Button
const recordPlayButton = document.getElementById('recordPlayButton');
let isRecordedPlaying = false;
// Function to create AudioContext
function createAudioContext() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
}
// Initialize AudioContext and resume it as early as possible
function initializeAudioContext() {
createAudioContext();
if (audioContext.state === 'suspended') {
audioContext.resume();
}
}
// Function to set up visualization components for recorded audio
function setupRecordedAudioVisualization() {
// Ensure AudioContext is ready
initializeAudioContext();
if (!recordedAudioSource) {
recordedAudioSource = audioContext.createMediaElementSource(recordedAudio);
}
if (!recordedAudioAnalyser) {
recordedAudioAnalyser = audioContext.createAnalyser();
recordedAudioAnalyser.fftSize = 2048;
recordedAudioSource.connect(recordedAudioAnalyser);
recordedAudioAnalyser.connect(audioContext.destination);
}
}
// Function to visualize the recorded audio during playback
function visualizeRecordedAudio() {
// Visualization setup has already been done in setupRecordedAudioVisualization()
const dataArray = new Uint8Array(recordedAudioAnalyser.frequencyBinCount);
function draw() {
recordedAudioAnimationId = requestAnimationFrame(draw);
recordedAudioAnalyser.getByteTimeDomainData(dataArray);
recordCanvasContext.fillStyle = 'white';
recordCanvasContext.fillRect(0, 0, recordCanvas.width, recordCanvas.height);
recordCanvasContext.lineWidth = 2;
recordCanvasContext.strokeStyle = '#000000';
recordCanvasContext.beginPath();
const amplitudeScaleFactor = 1.5; // Adjust if needed
const sliceWidth = recordCanvas.width / dataArray.length;
let x = 0;
for (let i = 0; i < dataArray.length; i++) {
const v = (dataArray[i] - 128) / 128.0;
const y = (v * amplitudeScaleFactor * (recordCanvas.height / 2)) + (recordCanvas.height / 2);
if (i === 0) {
recordCanvasContext.moveTo(x, y);
} else {
recordCanvasContext.lineTo(x, y);
}
x += sliceWidth;
}
recordCanvasContext.lineTo(recordCanvas.width, recordCanvas.height / 2);
recordCanvasContext.stroke();
}
draw();
}
// Event listeners for recordedAudio
recordedAudio.addEventListener('loadeddata', () => {
if (audioContext && audioContext.state === 'suspended') {
audioContext.resume();
}
});
recordedAudio.addEventListener('play', () => {
// Visualize recorded audio when it starts playing
visualizeRecordedAudio();
});
recordedAudio.addEventListener('pause', () => {
if (recordedAudioAnimationId) {
cancelAnimationFrame(recordedAudioAnimationId);
recordedAudioAnimationId = null;
// Optional: Clear the canvas
recordCanvasContext.clearRect(0, 0, recordCanvas.width, recordCanvas.height);
}
});
recordedAudio.addEventListener('ended', () => {
recordPlayButton.textContent = '▶';
isRecordedPlaying = false;
if (recordedAudioAnimationId) {
cancelAnimationFrame(recordedAudioAnimationId);
recordedAudioAnimationId = null;
// Optional: Clear the canvas
recordCanvasContext.clearRect(0, 0, recordCanvas.width, recordCanvas.height);
}
});
// Function to visualize audio element (drag-and-drop file)
function visualizeAudioElement(audioElement) {
createAudioContext();
const track = audioContext.createMediaElementSource(audioElement);
const analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
track.connect(analyser);
analyser.connect(audioContext.destination); // Connect analyser to the output (speakers)
const dataArray = new Uint8Array(analyser.frequencyBinCount);
function draw() {
requestAnimationFrame(draw);
analyser.getByteTimeDomainData(dataArray);
audioCanvasContext.fillStyle = 'white';
audioCanvasContext.fillRect(0, 0, audioCanvas.width, audioCanvas.height);
audioCanvasContext.lineWidth = 2;
audioCanvasContext.strokeStyle = '#000000';
audioCanvasContext.beginPath();
const sliceWidth = audioCanvas.width / dataArray.length;
let x = 0;
for (let i = 0; i < dataArray.length; i++) {
const v = dataArray[i] / 128.0;
const y = (v * audioCanvas.height) / 2;
if (i === 0) {
audioCanvasContext.moveTo(x, y);
} else {
audioCanvasContext.lineTo(x, y);
}
x += sliceWidth;
}
audioCanvasContext.lineTo(audioCanvas.width, audioCanvas.height / 2);
audioCanvasContext.stroke();
}
draw();
}
// Function to visualize recording audio
function visualizeRecording(stream) {
createAudioContext();
const analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
const source = audioContext.createMediaStreamSource(stream);
source.connect(analyser);
const dataArray = new Uint8Array(analyser.frequencyBinCount);
function draw() {
requestAnimationFrame(draw);
analyser.getByteTimeDomainData(dataArray);
recordCanvasContext.fillStyle = 'white';
recordCanvasContext.fillRect(0, 0, recordCanvas.width, recordCanvas.height);
recordCanvasContext.lineWidth = 2;
recordCanvasContext.strokeStyle = '#000000';
recordCanvasContext.beginPath();
const amplitudeScaleFactor = 1.5; // Adjust this factor to make the waveform taller
const sliceWidth = recordCanvas.width / dataArray.length;
let x = 0;
for (let i = 0; i < dataArray.length; i++) {
const v = (dataArray[i] - 128) / 128.0; // Center the waveform vertically
const y = (v * amplitudeScaleFactor * (recordCanvas.height / 2)) + (recordCanvas.height / 2);
if (i === 0) {
recordCanvasContext.moveTo(x, y);
} else {
recordCanvasContext.lineTo(x, y);
}
x += sliceWidth;
}
recordCanvasContext.lineTo(recordCanvas.width, recordCanvas.height / 2);
recordCanvasContext.stroke();
}
draw();
}
// Add click functionality to dropZone
dropZone.addEventListener('click', () => {
audioFileInput.click(); // Trigger the hidden file input click
});
// Drag-and-drop functionality
dropZone.addEventListener('dragover', (event) => {
event.preventDefault(); // Prevent default behavior to allow drop
dropZone.style.borderColor = 'green';
});
dropZone.addEventListener('dragleave', () => {
dropZone.style.borderColor = '#aaa';
});
dropZone.addEventListener('drop', (event) => {
event.preventDefault();
dropZone.style.borderColor = '#aaa';
const file = event.dataTransfer.files[0];
handleAudioFile(file);
});
audioFileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
handleAudioFile(file);
});
function handleAudioFile(file) {
if (file && file.type.startsWith('audio')) {
const url = URL.createObjectURL(file);
audioElement.src = url;
// Force the audio element to load the new file
audioElement.load();
// Display the uploaded file name in the drop zone
dropZone.textContent = `${file.name}`;
// Apply the current playback speed from the slider or stored value
const storedSpeed = localStorage.getItem(localStorageKey);
const speed = storedSpeed ? parseFloat(storedSpeed) : 1.0;
audioElement.playbackRate = speed;
playbackSpeedSlider.value = speed;
speedDisplay.textContent = `${speed}x`;
// Apply the current volume setting
const storedVolume = localStorage.getItem('preferredVolume');
const volume = storedVolume ? parseFloat(storedVolume) : 1.0;
audioElement.volume = volume;
volumeControlSlider.value = volume;
volumeDisplay.textContent = `${Math.round(volume * 100)}%`;
// Resume the AudioContext if it is suspended
if (audioContext && audioContext.state === 'suspended') {
audioContext.resume().then(() => {
console.log('Audio context resumed.');
});
}
// Start visualizing the drag-and-drop audio file when it plays
audioElement.addEventListener('play', () => {
visualizeAudioElement(audioElement);
});
audioElement.addEventListener('ended', () => {
// Optional: Clear the canvas when audio ends
audioCanvasContext.clearRect(0, 0, audioCanvas.width, audioCanvas.height);
});
// Autoplay the audio file after loading
audioElement.play().catch(err => console.error('Error playing audio:', err));
} else {
alert('Please upload a valid audio file.');
}
}
// Recording functionality
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
startRecordingButton.addEventListener('click', () => {
// Must resume the AudioContext inside user-initiated event
initializeAudioContext();
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
// Explicitly set MIME type for MediaRecorder
let options = { mimeType: 'audio/mp3;codecs=opus' };
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
console.warn(`${options.mimeType} is not supported, trying 'audio/ogg; codecs=opus'.`);
options = { mimeType: 'audio/ogg; codecs=opus' };
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
console.warn(`${options.mimeType} is not supported, using default codec.`);
options = {};
}
}
mediaRecorder = new MediaRecorder(stream, options);
audioChunks = []; // Reset audio chunks
// Pre-buffering the audio stream by a small delay
const startDelay = 200; // Adjust delay in milliseconds if necessary
setTimeout(() => {
mediaRecorder.start();
console.log("Recording started after delay");
startRecordingButton.disabled = true;
stopRecordingButton.disabled = false;
saveRecordingButton.disabled = true;
visualizeRecording(stream); // Start waveform visualization for recording
}, startDelay);
mediaRecorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
audioChunks.push(event.data);
console.log("Audio chunk collected:", event.data);
} else {
console.warn("Empty audio data received.");
}
};
mediaRecorder.onstop = () => {
console.log("Recording stopped, processing data.");
console.log("Audio chunks collected:", audioChunks.length);
const audioBlob = new Blob(audioChunks, { type: mediaRecorder.mimeType || 'audio/mp3' });
const audioUrl = URL.createObjectURL(audioBlob);
recordedAudio.src = audioUrl;
// Apply the current volume setting
const storedVolume = localStorage.getItem('preferredRecordedVolume');
const volume = storedVolume ? parseFloat(storedVolume) : 1.0;
recordedAudio.volume = volume;
recordVolumeControlSlider.value = volume;
recordVolumeDisplay.textContent = `${Math.round(volume * 100)}%`;
// Apply the current playback speed
const storedSpeed = localStorage.getItem(recordedAudioStorageKey);
const speed = storedSpeed ? parseFloat(storedSpeed) : 1.0;
recordedAudio.playbackRate = speed;
recordPlaybackSpeedSlider.value = speed;
recordSpeedDisplay.textContent = `${speed}x`;
console.log("Recording saved and available for playback");
// Set up AudioContext and visualization components
setupRecordedAudioVisualization();
// Ensure the recorded audio is ready to play
recordedAudio.load();
};
mediaRecorder.onerror = (event) => {
console.error("MediaRecorder error:", event.error);
};
})
.catch(err => {
console.error("Error accessing microphone:", err);
alert("Error accessing microphone. Please check your permissions.");
});
});
stopRecordingButton.addEventListener('click', () => {
mediaRecorder.stop();
console.log("Recording stopped by user.");
// Stop all media tracks to release the microphone
mediaRecorder.stream.getTracks().forEach(track => track.stop());
stopRecordingButton.disabled = true;
startRecordingButton.disabled = false;
saveRecordingButton.disabled = false;
// Optional: Clear the canvas when recording stops
recordCanvasContext.clearRect(0, 0, recordCanvas.width, recordCanvas.height);
});
saveRecordingButton.addEventListener('click', () => {
const audioBlob = new Blob(audioChunks, { type: mediaRecorder.mimeType || 'audio/mp3' });
const audioUrl = URL.createObjectURL(audioBlob);
const a = document.createElement('a');
a.href = audioUrl;
a.download = 'recording.mp3';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
console.log("Recording downloaded as 'recording.mp3'");
});
} else {
alert('Your browser does not support audio recording.');
}
// Function to initialize recorded audio playback speed from localStorage
function initializeRecordedPlaybackSpeed() {
const storedSpeed = localStorage.getItem(recordedAudioStorageKey);
const speed = storedSpeed ? parseFloat(storedSpeed) : 1.0;
recordedAudio.playbackRate = speed;
recordPlaybackSpeedSlider.value = speed;
recordSpeedDisplay.textContent = `${speed}x`;
recordSpeedDisplay.style.color = '#000000';
}
// Store the preferred playback speed for recorded audio in localStorage when the slider changes
recordPlaybackSpeedSlider.addEventListener('input', () => {
const speed = recordPlaybackSpeedSlider.value;
recordedAudio.playbackRate = speed;
recordSpeedDisplay.textContent = `${speed}x`;
localStorage.setItem(recordedAudioStorageKey, speed);
});
// Function to initialize volume controls from localStorage
function initializeVolumeControls() {
// Initialize volume for drag-and-drop audio
const storedVolume = localStorage.getItem('preferredVolume');
const volume = storedVolume ? parseFloat(storedVolume) : 1.0;
audioElement.volume = volume;
volumeControlSlider.value = volume;
volumeDisplay.textContent = `${Math.round(volume * 100)}%`;
// Initialize volume for recorded audio
const storedRecordedVolume = localStorage.getItem('preferredRecordedVolume');
const recordedVolume = storedRecordedVolume ? parseFloat(storedRecordedVolume) : 1.0;
recordedAudio.volume = recordedVolume;
recordVolumeControlSlider.value = recordedVolume;
recordVolumeDisplay.textContent = `${Math.round(recordedVolume * 100)}%`;
}
// Store the preferred volume in localStorage when the slider changes
volumeControlSlider.addEventListener('input', () => {
const volume = volumeControlSlider.value;
audioElement.volume = volume;
volumeDisplay.textContent = `${Math.round(volume * 100)}%`;
localStorage.setItem('preferredVolume', volume);
});
// Store the preferred volume for recorded audio in localStorage when the slider changes
recordVolumeControlSlider.addEventListener('input', () => {
const volume = recordVolumeControlSlider.value;
recordedAudio.volume = volume;
recordVolumeDisplay.textContent = `${Math.round(volume * 100)}%`;
localStorage.setItem('preferredRecordedVolume', volume);
});
const playButton = document.getElementById('playButton');
let isPlaying = false;
// Add event listener for the play button
playButton.addEventListener('click', () => {
if (isPlaying) {
audioElement.pause();
playButton.textContent = '▶';
} else {
audioElement.play();
playButton.textContent = '⏸︎';
}
isPlaying = !isPlaying;
});
// Play and pause the audio along with visualizing the waveform
audioElement.addEventListener('play', () => {
visualizeAudioElement(audioElement);
});
audioElement.addEventListener('pause', () => {
playButton.textContent = '▶';
isPlaying = false;
});
audioElement.addEventListener('ended', () => {
playButton.textContent = '▶';
isPlaying = false;
});
// Event listener for the recordPlayButton
recordPlayButton.addEventListener('click', () => {
if (isRecordedPlaying) {
// Pause and reset the audio to the beginning
recordedAudio.pause();
recordedAudio.currentTime = 0;
recordPlayButton.textContent = '▶';
isRecordedPlaying = false;
// Stop visualization
if (recordedAudioAnimationId) {
cancelAnimationFrame(recordedAudioAnimationId);
recordedAudioAnimationId = null;
recordCanvasContext.clearRect(0, 0, recordCanvas.width, recordCanvas.height);
}
} else {
// Play the audio from the beginning
recordedAudio.currentTime = 0;
recordedAudio.play();
recordPlayButton.textContent = '⏸︎';
isRecordedPlaying = true;
// Start visualization
visualizeRecordedAudio();
}
});
// Resume AudioContext when the page gains focus (optional, improves responsiveness)
window.addEventListener('focus', () => {
if (audioContext && audioContext.state === 'suspended') {
audioContext.resume();
}
});
// Initialize recorded playback speed and volume controls
initializeRecordedPlaybackSpeed();
initializeVolumeControls();