-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio Cutter.html
379 lines (337 loc) · 13.7 KB
/
Audio Cutter.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Audio Cutter</title>
<style>
:root {
--primary: #ff4d4d;
--secondary: #4d4dff;
--background: #1a1a2e;
--text: #ffffff;
}
body {
font-family: 'Arial', sans-serif;
background-color: var(--background);
color: var(--text);
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background: linear-gradient(145deg, #2a2a3e, #1a1a2e);
border-radius: 20px;
padding: 30px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
max-width: 600px;
width: 100%;
}
h1 {
text-align: center;
margin-bottom: 30px;
font-size: 2.5em;
text-transform: uppercase;
letter-spacing: 2px;
background: linear-gradient(to right, var(--primary), var(--secondary));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.upload-container {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
#fileInput {
display: none;
}
.upload-btn {
background: linear-gradient(to right, var(--primary), var(--secondary));
color: var(--text);
border: none;
padding: 10px 20px;
border-radius: 30px;
cursor: pointer;
font-size: 1em;
transition: all 0.3s ease;
display: flex;
align-items: center;
gap: 10px;
}
.upload-btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(255, 77, 77, 0.4);
}
.controls {
display: flex;
justify-content: center;
gap: 20px;
margin-bottom: 20px;
}
.btn {
background: none;
border: 2px solid var(--primary);
color: var(--text);
padding: 10px;
border-radius: 50%;
cursor: pointer;
transition: all 0.3s ease;
}
.btn:hover {
background-color: var(--primary);
transform: scale(1.1);
}
.btn svg {
width: 24px;
height: 24px;
}
.range-container {
margin-bottom: 20px;
}
.range-label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
}
input[type="range"] {
width: 100%;
-webkit-appearance: none;
background: linear-gradient(to right, var(--primary), var(--secondary));
border-radius: 10px;
outline: none;
opacity: 0.7;
transition: opacity 0.2s;
}
input[type="range"]:hover {
opacity: 1;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
background: var(--text);
border-radius: 50%;
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 20px;
height: 20px;
background: var(--text);
border-radius: 50%;
cursor: pointer;
}
#cutBtn {
background: linear-gradient(to right, var(--primary), var(--secondary));
color: var(--text);
border: none;
padding: 10px 20px;
border-radius: 30px;
cursor: pointer;
font-size: 1em;
transition: all 0.3s ease;
display: block;
margin: 0 auto;
}
#cutBtn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(255, 77, 77, 0.4);
}
</style>
</head>
<body>
<div class="container">
<h1>Audio Cutter</h1>
<div class="upload-container">
<input type="file" id="fileInput" accept="audio/*">
<label for="fileInput" class="upload-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>
Upload Audio
</label>
</div>
<audio id="audio" style="display: none;"></audio>
<div class="controls">
<button id="playPauseBtn" class="btn">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>
</button>
<button id="loopBtn" class="btn">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 2.1l4 4-4 4"></path><path d="M3 12.2v-2a4 4 0 0 1 4-4h12.8M7 21.9l-4-4 4-4"></path><path d="M21 11.8v2a4 4 0 0 1-4 4H4.2"></path></svg>
</button>
</div>
<div class="range-container">
<div class="range-label">
<span>Start: <span id="startTime">0:00</span></span>
<span>End: <span id="endTime">0:00</span></span>
</div>
<input type="range" id="startPoint" min="0" max="100" value="0">
<input type="range" id="endPoint" min="0" max="100" value="100">
</div>
<div class="range-container">
<div class="range-label">
<span>Speed: <span id="speedValue">1x</span></span>
</div>
<input type="range" id="speed" min="0.5" max="2" step="0.1" value="1">
</div>
<button id="cutBtn">Cut & Download</button>
</div>
<script>
const audio = document.getElementById('audio');
const fileInput = document.getElementById('fileInput');
const playPauseBtn = document.getElementById('playPauseBtn');
const loopBtn = document.getElementById('loopBtn');
const startPoint = document.getElementById('startPoint');
const endPoint = document.getElementById('endPoint');
const speed = document.getElementById('speed');
const cutBtn = document.getElementById('cutBtn');
const startTimeDisplay = document.getElementById('startTime');
const endTimeDisplay = document.getElementById('endTime');
const speedValueDisplay = document.getElementById('speedValue');
let audioContext;
let sourceNode;
let isPlaying = false;
let isLooping = false;
fileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
audio.src = e.target.result;
audio.load();
audio.addEventListener('loadedmetadata', function() {
startPoint.max = endPoint.max = audio.duration;
endPoint.value = audio.duration;
updateTimeDisplay();
});
};
reader.readAsDataURL(file);
});
playPauseBtn.addEventListener('click', togglePlayPause);
loopBtn.addEventListener('click', toggleLoop);
startPoint.addEventListener('input', updateTimeDisplay);
endPoint.addEventListener('input', updateTimeDisplay);
speed.addEventListener('input', updateSpeed);
cutBtn.addEventListener('click', cutAudio);
function togglePlayPause() {
if (isPlaying) {
audio.pause();
playPauseBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>';
} else {
audio.currentTime = startPoint.value;
audio.play();
playPauseBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>';
}
isPlaying = !isPlaying;
}
function toggleLoop() {
isLooping = !isLooping;
loopBtn.style.backgroundColor = isLooping ? 'var(--primary)' : 'transparent';
}
function updateTimeDisplay() {
startTimeDisplay.textContent = formatTime(startPoint.value);
endTimeDisplay.textContent = formatTime(endPoint.value);
}
function updateSpeed() {
audio.playbackRate = speed.value;
speedValueDisplay.textContent = speed.value + 'x';
}
function formatTime(time) {
const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
function cutAudio() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
fetch(audio.src)
.then(response => response.arrayBuffer())
.then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer))
.then(audioBuffer => {
const start = parseFloat(startPoint.value);
const end = parseFloat(endPoint.value);
const duration = end - start;
const offlineContext = new OfflineAudioContext(
audioBuffer.numberOfChannels,
duration * audioBuffer.sampleRate,
audioBuffer.sampleRate
);
const source = offlineContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(offlineContext.destination);
source.start(0, start, duration);
offlineContext.startRendering().then(renderedBuffer => {
const wav = audioBufferToWav(renderedBuffer);
const blob = new Blob([new DataView(wav)], { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'cut_audio.wav';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
});
}
function audioBufferToWav(buffer) {
const numOfChan = buffer.numberOfChannels;
const length = buffer.length * numOfChan * 2 + 44;
const out = new ArrayBuffer(length);
const view = new DataView(out);
const channels = [];
let i;
let sample;
let offset = 0;
let pos = 0;
// write WAVE header
setUint32(0x46464952);
setUint32(length - 8);
setUint32(0x45564157);
setUint32(0x20746d66);
setUint32(16);
setUint16(1);
setUint16(numOfChan);
setUint32(buffer.sampleRate);
setUint32(buffer.sampleRate * 2 * numOfChan);
setUint16(numOfChan * 2);
setUint16(16);
setUint32(0x61746164);
setUint32(length - pos - 4);
for (i = 0; i < buffer.numberOfChannels; i++)
channels.push(buffer.getChannelData(i));
while (pos < length) {
for (i = 0; i < numOfChan; i++) {
sample = Math.max(-1, Math.min(1, channels[i][offset]));
sample = (0.5 + sample < 0 ? sample * 32768 : sample * 32767) | 0;
view.setInt16(pos, sample, true);
pos += 2;
}
offset++;
}
return out;
function setUint16(data) {
view.setUint16(pos, data, true);
pos += 2;
}
function setUint32(data) {
view.setUint32(pos, data, true);
pos += 4;
}
}
audio.addEventListener('timeupdate', function() {
if (audio.currentTime >= endPoint.value) {
if (isLooping) {
audio.currentTime = startPoint.value;
} else {
audio.pause();
isPlaying = false;
playPauseBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>';
}
}
});
</script>
</body>
</html>