Here's a single-file web app that allows you to record your voice and adjust its pitch using a slider, using only HTML, CSS, and JavaScript:
html Copy code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Voice Pitch Shifter</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
text-align: center;
}
button {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
}
input[type="range"] {
width: 80%;
}
#pitchValue {
display: inline-block;
width: 30px;
font-weight: bold;
}
#status {
margin-top: 20px;
color: #666;
}
</style>
</head>
<body>
<h1>Voice Pitch Shifter</h1>
<button id="record">Record</button>
<button id="stop" disabled>Stop</button>
<div style="margin-top: 30px;">
<label for="pitch">Pitch Shift (semitones):</label><br>
<input type="range" id="pitch" min="-12" max="12" value="0">
<span id="pitchValue">0</span>
</div>
<p id="status">Status: Ready</p>
<script>
let mediaRecorder;
let audioChunks = [];
let audioBuffer;
let currentSource = null;
let audioContext = null;
const status = document.getElementById('status');
const recordButton = document.getElementById('record');
const stopButton = document.getElementById('stop');
const pitchSlider = document.getElementById('pitch');
const pitchValue = document.getElementById('pitchValue');
// Initialize pitch display
pitchValue.textContent = pitchSlider.value;
// Request audio access and start recording
recordButton.onclick = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
audioChunks = [];
mediaRecorder.ondataavailable = e => audioChunks.push(e.data);
mediaRecorder.onstop = async () => {
try {
// Create audio blob and decode
const blob = new Blob(audioChunks, { type: 'audio/wav' });
audioContext = new (window.AudioContext || window.webkitAudioContext)();
const arrayBuffer = await blob.arrayBuffer();
audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
status.textContent = 'Status: Audio loaded. Adjust pitch to play.';
} catch (err) {
console.error('Audio processing error:', err);
status.textContent = 'Status: Error processing audio';
}
};
mediaRecorder.start();
status.textContent = 'Status: Recording in progress...';
recordButton.disabled = true;
stopButton.disabled = false;
} catch (err) {
console.error('Microphone access denied:', err);
status.textContent = 'Status: Microphone access denied';
}
};
// Stop recording
stopButton.onclick = () => {
mediaRecorder.stop();
status.textContent = 'Status: Processing recording...';
recordButton.disabled = false;
stopButton.disabled = true;
};
// Handle pitch changes
pitchSlider.oninput = () => {
pitchValue.textContent = pitchSlider.value;
if (!audioBuffer) return;
try {
// Create new audio context if needed
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
// Stop previous playback
if (currentSource) {
currentSource.stop();
currentSource.disconnect();
}
// Create new source with pitch shift
currentSource = audioContext.createBufferSource();
currentSource.buffer = audioBuffer;
const semitones = parseFloat(pitchSlider.value);
currentSource.playbackRate.value = Math.pow(2, semitones / 12);
currentSource.connect(audioContext.destination);
currentSource.start();
} catch (err) {
console.error('Playback error:', err);
status.textContent = 'Status: Error during playback';
}
};
</script>
</body>
</html>
Features:
Recording : Uses WebRTC to capture microphone input
Audio Processing : Converts recorded audio to an AudioBuffer
Pitch Shifting : Uses Web Audio API's playbackRate property to shift pitch
UI Controls :
Record/Stop buttons
Pitch adjustment slider (-12 to +12 semitones)
Real-time pitch display
How It Works:
Click "Record" to start capturing audio from your microphone
Speak into your microphone, then click "Stop" to end recording
Use the pitch slider to hear your voice at different pitches
Limitations:
Basic pitch shifting (changes speed slightly along with pitch)
No waveform visualization
Limited to ~60 seconds of recording (browser-dependent)
Simple interface with minimal styling
This implementation uses only standard web APIs and works in modern browsers without requiring any external libraries. The pitch shifting effect is achieved by modifying the playbackRate property of the AudioBufferSourceNode, which provides a simple but effective way to change pitch in web applications.