forked from QSOLKCB/lostsoundtech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
263 lines (212 loc) · 7.99 KB
/
app.js
File metadata and controls
263 lines (212 loc) · 7.99 KB
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
// LST - MediaRecorder Pipeline
// VP9/VP8 fallback, timesliced 100ms, auto-stop on audio end
let audioContext;
let analyser;
let audioSource;
let audioElement;
let mediaRecorder;
let recordedChunks = [];
let canvas;
let canvasContext;
let animationId;
let mediaStream;
let isRecording = false;
const statusEl = document.getElementById('status');
const uploadBtn = document.getElementById('uploadBtn');
const audioFileInput = document.getElementById('audioFile');
const recordBtn = document.getElementById('recordBtn');
const stopBtn = document.getElementById('stopBtn');
const downloadBtn = document.getElementById('downloadBtn');
// Initialize canvas
function initCanvas() {
canvas = document.getElementById('visualizer');
canvasContext = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 400;
}
// Draw initial frame before captureStream
function drawInitialFrame() {
canvasContext.fillStyle = '#000';
canvasContext.fillRect(0, 0, canvas.width, canvas.height);
canvasContext.strokeStyle = '#0f0';
canvasContext.lineWidth = 2;
canvasContext.strokeRect(10, 10, canvas.width - 20, canvas.height - 20);
canvasContext.fillStyle = '#0f0';
canvasContext.font = '20px Courier New';
canvasContext.textAlign = 'center';
canvasContext.fillText('LST - Ready to Record', canvas.width / 2, canvas.height / 2);
}
// Upload audio file
audioFileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
try {
statusEl.textContent = `Loading: ${file.name}`;
// Create audio element
if (audioElement) {
audioElement.pause();
if (audioElement.src) {
URL.revokeObjectURL(audioElement.src);
}
audioElement.src = '';
}
audioElement = new Audio();
audioElement.src = URL.createObjectURL(file);
audioElement.controls = false;
// Initialize Web Audio API
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
// Create analyser
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
// Create source from audio element
audioSource = audioContext.createMediaElementSource(audioElement);
// Create destination for recording
const destination = audioContext.createMediaStreamDestination();
// Connect: source -> analyser -> destination
audioSource.connect(analyser);
analyser.connect(destination);
// Also connect to speakers for monitoring
analyser.connect(audioContext.destination);
// Store the audio stream
mediaStream = destination.stream;
statusEl.textContent = `Audio loaded: ${file.name}. Ready to record!`;
recordBtn.disabled = false;
// Draw initial frame
drawInitialFrame();
} catch (error) {
statusEl.textContent = `Error loading audio: ${error.message}`;
console.error(error);
}
});
// Visualize audio on canvas
function visualize() {
if (!analyser) return;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
function draw() {
animationId = requestAnimationFrame(draw);
if (!isRecording) return;
analyser.getByteFrequencyData(dataArray);
// Clear canvas
canvasContext.fillStyle = '#000';
canvasContext.fillRect(0, 0, canvas.width, canvas.height);
// Draw frequency bars
const barWidth = (canvas.width / bufferLength) * 2.5;
let barHeight;
let x = 0;
for (let i = 0; i < bufferLength; i++) {
barHeight = (dataArray[i] / 255) * canvas.height * 0.8;
const green = Math.floor((dataArray[i] / 255) * 255);
canvasContext.fillStyle = `rgb(0, ${green}, 0)`;
canvasContext.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
x += barWidth + 1;
}
// Draw time indicator
if (audioElement) {
canvasContext.fillStyle = '#0f0';
canvasContext.font = '16px Courier New';
canvasContext.textAlign = 'left';
const timeText = `${audioElement.currentTime.toFixed(1)}s / ${audioElement.duration.toFixed(1)}s`;
canvasContext.fillText(timeText, 10, 30);
}
}
draw();
}
// Get supported video codec (VP9/VP8 fallback)
function getSupportedCodec() {
const codecs = [
'video/webm;codecs=vp9,opus',
'video/webm;codecs=vp8,opus',
'video/webm;codecs=vp9',
'video/webm;codecs=vp8',
'video/webm'
];
for (const codec of codecs) {
if (MediaRecorder.isTypeSupported(codec)) {
return codec;
}
}
return 'video/webm';
}
// Start recording
recordBtn.addEventListener('click', async () => {
try {
recordedChunks = [];
// Get canvas stream
const canvasStream = canvas.captureStream(30); // 30 FPS
// Combine canvas video and audio
const combinedStream = new MediaStream([
...canvasStream.getVideoTracks(),
...mediaStream.getAudioTracks()
]);
// Get supported codec
const mimeType = getSupportedCodec();
statusEl.textContent = `Using codec: ${mimeType}`;
// Create MediaRecorder with timeslice
mediaRecorder = new MediaRecorder(combinedStream, {
mimeType: mimeType,
videoBitsPerSecond: 2500000
});
// Handle data available (timesliced)
mediaRecorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
recordedChunks.push(event.data);
}
};
// Handle stop
mediaRecorder.onstop = () => {
const blob = new Blob(recordedChunks, { type: mimeType });
const url = URL.createObjectURL(blob);
downloadBtn.href = url;
downloadBtn.download = `LST-recording-${Date.now()}.webm`;
downloadBtn.style.display = 'inline-block';
statusEl.textContent = 'Recording complete! Click download to save.';
isRecording = false;
};
// Start recording with 100ms timeslice
mediaRecorder.start(100);
isRecording = true;
// Auto-stop when audio ends
audioElement.addEventListener('ended', () => {
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
statusEl.textContent = 'Recording stopped (audio ended)';
}
}, { once: true });
// Start audio playback
await audioElement.play();
// Start visualization
visualize();
statusEl.textContent = 'Recording... Audio playing.';
recordBtn.disabled = true;
stopBtn.disabled = false;
} catch (error) {
statusEl.textContent = `Error starting recording: ${error.message}`;
console.error(error);
isRecording = false;
}
});
// Stop recording manually
stopBtn.addEventListener('click', () => {
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
if (audioElement) {
audioElement.pause();
audioElement.currentTime = 0;
}
if (animationId) {
cancelAnimationFrame(animationId);
}
statusEl.textContent = 'Recording stopped manually';
recordBtn.disabled = false;
stopBtn.disabled = true;
}
});
// Initialize on load
window.addEventListener('DOMContentLoaded', () => {
initCanvas();
drawInitialFrame();
statusEl.textContent = 'Ready. Upload an audio file to begin.';
});