Spaces:
Running
Running
File size: 8,552 Bytes
5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 621a9fb 5454980 |
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 |
<!DOCTYPE html>
<html>
<head>
<title>Audio Recorder with Silence Detection</title>
<style>
body {
font-family: system-ui, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.controls {
margin: 20px 0;
}
button {
padding: 10px 20px;
margin: 5px;
cursor: pointer;
}
#recordingsList {
margin-top: 20px;
border: 1px solid #ccc;
padding: 10px;
min-height: 100px;
}
.recording-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #eee;
}
#timer {
font-size: 1.2em;
margin: 10px 0;
}
#volumeMeter {
width: 300px;
height: 20px;
border: 1px solid #ccc;
margin: 10px 0;
}
#volumeBar {
height: 100%;
width: 0%;
background-color: #4CAF50;
transition: width 0.1s;
}
</style>
</head>
<body>
<h1>Audio Recorder with Silence Detection</h1>
<div class="controls">
<button id="startButton">Start Recording</button>
<button id="stopButton" disabled>Stop Recording</button>
</div>
<div id="timer">00:00</div>
<div id="volumeMeter">
<div id="volumeBar"></div>
</div>
<div id="recordingsList"></div>
<script>
let mediaRecorder;
let audioChunks = [];
let recordings = [];
let startTime;
let timerInterval;
let silenceTimeout;
let audioContext;
let analyser;
let isRecording = false;
let totalDuration = 0;
const SILENCE_THRESHOLD = -50; // dB
const SILENCE_DURATION = 1000; // 1 second
document.getElementById('startButton').addEventListener('click', startRecording);
document.getElementById('stopButton').addEventListener('click', stopRecording);
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
setupAudioAnalysis(stream);
mediaRecorder = new MediaRecorder(stream);
audioChunks = [];
isRecording = true;
startTime = Date.now();
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
if (audioChunks.length > 0) {
saveRecording();
}
};
mediaRecorder.start();
updateUI(true);
startTimer();
} catch (err) {
console.error('Error:', err);
alert('Error accessing microphone');
}
}
function setupAudioAnalysis(stream) {
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
const source = audioContext.createMediaStreamSource(stream);
source.connect(analyser);
analyser.fftSize = 2048;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Float32Array(bufferLength);
function checkAudioLevel() {
if (!isRecording) return;
analyser.getFloatTimeDomainData(dataArray);
let sum = 0;
for (let i = 0; i < bufferLength; i++) {
sum += Math.abs(dataArray[i]);
}
const average = sum / bufferLength;
const db = 20 * Math.log10(average);
// Update volume meter
const volumeBar = document.getElementById('volumeBar');
const normalizedVolume = Math.max(0, (db + 90) / 90) * 100;
volumeBar.style.width = `${normalizedVolume}%`;
if (db < SILENCE_THRESHOLD) {
if (!silenceTimeout) {
silenceTimeout = setTimeout(() => {
if (mediaRecorder.state === 'recording') {
mediaRecorder.stop();
startNewRecording();
}
}, SILENCE_DURATION);
}
} else {
if (silenceTimeout) {
clearTimeout(silenceTimeout);
silenceTimeout = null;
}
if (mediaRecorder.state === 'inactive' && isRecording) {
startNewRecording();
}
}
requestAnimationFrame(checkAudioLevel);
}
checkAudioLevel();
}
function startNewRecording() {
if (isRecording) {
audioChunks = [];
mediaRecorder.start();
}
}
function stopRecording() {
isRecording = false;
if (mediaRecorder.state === 'recording') {
mediaRecorder.stop();
}
clearInterval(timerInterval);
updateUI(false);
if (audioContext) {
audioContext.close();
}
if (silenceTimeout) {
clearTimeout(silenceTimeout);
}
}
function saveRecording() {
const blob = new Blob(audioChunks, { type: 'audio/webm' });
const reader = new FileReader();
reader.onload = function() {
const base64String = reader.result.split(',')[1];
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `recording-${timestamp}.webm`;
recordings.push({
filename,
base64: base64String,
duration: (Date.now() - startTime) / 1000
});
updateRecordingsList();
};
reader.readAsDataURL(blob);
}
function updateRecordingsList() {
const list = document.getElementById('recordingsList');
list.innerHTML = '';
recordings.forEach((recording, index) => {
const item = document.createElement('div');
item.className = 'recording-item';
const info = document.createElement('span');
info.textContent = `${recording.filename} (${recording.duration.toFixed(1)}s)`;
const downloadBtn = document.createElement('button');
downloadBtn.textContent = 'Download';
downloadBtn.onclick = () => downloadRecording(recording);
item.appendChild(info);
item.appendChild(downloadBtn);
list.appendChild(item);
});
}
function downloadRecording(recording) {
const link = document.createElement('a');
link.href = `data:audio/webm;base64,${recording.base64}`;
link.download = recording.filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function startTimer() {
const timerElement = document.getElementById('timer');
const startTime = Date.now();
timerInterval = setInterval(() => {
const elapsed = Date.now() - startTime;
const seconds = Math.floor(elapsed / 1000);
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
timerElement.textContent = `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}, 1000);
}
function updateUI(recording) {
document.getElementById('startButton').disabled = recording;
document.getElementById('stopButton').disabled = !recording;
}
</script>
</body>
</html> |