Files
manifest.json
pitch-worklet.js
popup.html
popup.js
studio.html
studio.js
style.css
Code
7 247
studio.js
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const recordButton = document.getElementById('record');
const finishButton = document.getElementById('finish');
const effect = document.getElementById('effect');
const monitor = document.getElementById('monitor');
const status = document.getElementById('status');
const download = document.getElementById('download');
let context, stream, source, pitch, filter, modulator, modGain, voiceGain, delay, feedback, echoGain, output, monitorGain, recordingDestination;
let recorder, chunks = [], downloadUrl;
function setEffect() {
if (!context) return;
const selected = effect.value;
const now = context.currentTime;
pitch.parameters.get('ratio').setValueAtTime(selected === 'high' ? 1.5 : selected === 'low' ? 0.7 : 1, now);
filter.type = selected === 'telephone' ? 'bandpass' : 'lowpass';
filter.frequency.setValueAtTime(selected === 'telephone' ? 1500 : 20000, now);
filter.Q.setValueAtTime(selected === 'telephone' ? 0.8 : 0.0001, now);
voiceGain.gain.setValueAtTime(selected === 'robot' ? 0.5 : 1, now);
modGain.gain.setValueAtTime(selected === 'robot' ? 0.5 : 0, now);
echoGain.gain.setValueAtTime(selected === 'echo' ? 0.4 : 0, now);
feedback.gain.setValueAtTime(selected === 'echo' ? 0.35 : 0, now);
}
startButton.addEventListener('click', async () => {
startButton.disabled = true;
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
context = new AudioContext();
await context.audioWorklet.addModule('pitch-worklet.js');
source = context.createMediaStreamSource(stream);
pitch = new AudioWorkletNode(context, 'pitch-processor');
filter = context.createBiquadFilter();
voiceGain = context.createGain();
modulator = context.createOscillator();
modGain = context.createGain();
modulator.frequency.value = 35;
modulator.connect(modGain).connect(voiceGain.gain);
modulator.start();
delay = context.createDelay(1);
delay.delayTime.value = 0.25;
feedback = context.createGain();
echoGain = context.createGain();
output = context.createGain();
monitorGain = context.createGain();
recordingDestination = context.createMediaStreamDestination();
source.connect(pitch).connect(filter).connect(voiceGain);
voiceGain.connect(output);
voiceGain.connect(delay);
delay.connect(feedback).connect(delay);
delay.connect(echoGain).connect(output);
output.connect(monitorGain).connect(context.destination);
output.connect(recordingDestination);
monitorGain.gain.value = monitor.checked ? 1 : 0;
setEffect();
await context.resume();
stopButton.disabled = false;
recordButton.disabled = typeof MediaRecorder === 'undefined';
status.textContent = 'Microphone active. Choose an effect or start recording.';
} catch (error) {
console.error('Failed to start microphone:', error);
if (stream) stream.getTracks().forEach(track => track.stop());
if (context) await context.close().catch(closeError => console.error('Failed to close audio:', closeError));
stream = context = null;
startButton.disabled = false;
status.textContent = 'Could not start microphone: ' + error.message;
}
});
effect.addEventListener('change', setEffect);
monitor.addEventListener('change', () => {
if (monitorGain && context) monitorGain.gain.setValueAtTime(monitor.checked ? 1 : 0, context.currentTime);
});
recordButton.addEventListener('click', () => {
try {
chunks = [];
recorder = new MediaRecorder(recordingDestination.stream);
recorder.ondataavailable = event => { if (event.data.size) chunks.push(event.data); };
recorder.onerror = event => {
console.error('Recording failed:', event.error);
status.textContent = 'Recording failed.';
};
recorder.onstop = () => {
if (!chunks.length) return;
if (downloadUrl) URL.revokeObjectURL(downloadUrl);
downloadUrl = URL.createObjectURL(new Blob(chunks, { type: recorder.mimeType || 'audio/webm' }));
download.href = downloadUrl;
download.download = 'voice-recording.webm';
download.hidden = false;
status.textContent = 'Recording ready to download.';
};
recorder.start();
recordButton.disabled = true;
finishButton.disabled = false;
status.textContent = 'Recording…';
} catch (error) {
console.error('Failed to start recording:', error);
status.textContent = 'Could not start recording: ' + error.message;
}
});
finishButton.addEventListener('click', () => {
if (recorder && recorder.state !== 'inactive') recorder.stop();
finishButton.disabled = true;
recordButton.disabled = !context;
});
stopButton.addEventListener('click', async () => {
if (recorder && recorder.state !== 'inactive') recorder.stop();
finishButton.disabled = true;
recordButton.disabled = true;
stopButton.disabled = true;
stream.getTracks().forEach(track => track.stop());
try { await context.close(); } catch (error) { console.error('Failed to close audio:', error); }
context = stream = null;
startButton.disabled = false;
status.textContent = 'Microphone stopped.';
});