I'll create a simplified pitch correction app inspired by Melodyne. This will let you record audio and adjust the pitch to different notes using the Web Audio API.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Pitch Editor</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #1a1a2e;
color: #eee;
}
.container {
background: #16213e;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}
h1 {
text-align: center;
color: #4fbdba;
margin-bottom: 30px;
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
background: #4fbdba;
color: white;
font-weight: bold;
}
button:hover {
background: #3ca9a6;
transform: translateY(-2px);
}
button:disabled {
background: #555;
cursor: not-allowed;
transform: none;
}
.pitch-control {
margin: 20px 0;
background: #0f3460;
padding: 20px;
border-radius: 8px;
}
.pitch-slider {
width: 100%;
margin: 10px 0;
}
.pitch-value {
text-align: center;
font-size: 24px;
color: #4fbdba;
margin: 10px 0;
}
.note-buttons {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
gap: 5px;
margin: 20px 0;
}
.note-btn {
padding: 15px 10px;
font-size: 14px;
background: #0f3460;
border: 2px solid #4fbdba;
}
.note-btn:hover {
background: #4fbdba;
}
.note-btn.active {
background: #4fbdba;
}
.waveform-container {
background: #0f3460;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
min-height: 150px;
display: flex;
align-items: center;
justify-content: center;
}
#waveform {
width: 100%;
height: 150px;
background: #16213e;
border-radius: 5px;
}
.status {
text-align: center;
padding: 10px;
background: #0f3460;
border-radius: 5px;
margin: 10px 0;
}
.recording {
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
</style>
</head>
<body>
<div class="container">
<h1>🎵 Simple Pitch Editor</h1>
<div class="controls">
<button id="recordBtn">Start Recording</button>
<button id="stopBtn" disabled>Stop Recording</button>
<button id="playBtn" disabled>Play Original</button>
<button id="playPitchedBtn" disabled>Play Pitched</button>
<button id="downloadBtn" disabled>Download</button>
</div>
<div class="status" id="status">Ready to record</div>
<div class="waveform-container">
<canvas id="waveform"></canvas>
</div>
<div class="pitch-control">
<h3>Pitch Control</h3>
<div class="pitch-value" id="pitchValue">0 semitones</div>
<input type="range" class="pitch-slider" id="pitchSlider"
min="-12" max="12" value="0" step="0.1">
<h4>Quick Notes (relative to original)</h4>
<div class="note-buttons" id="noteButtons"></div>
</div>
</div>
<script>
let mediaRecorder;
let audioChunks = [];
let audioBlob;
let audioContext;
let sourceBuffer;
let currentPitch = 0;
const recordBtn = document.getElementById('recordBtn');
const stopBtn = document.getElementById('stopBtn');
const playBtn = document.getElementById('playBtn');
const playPitchedBtn = document.getElementById('playPitchedBtn');
const downloadBtn = document.getElementById('downloadBtn');
const pitchSlider = document.getElementById('pitchSlider');
const pitchValue = document.getElementById('pitchValue');
const status = document.getElementById('status');
const waveformCanvas = document.getElementById('waveform');
const ctx = waveformCanvas.getContext('2d');
// Musical notes
const notes = [
{ name: 'C', semitones: -12 },
{ name: 'C#', semitones: -11 },
{ name: 'D', semitones: -10 },
{ name: 'D#', semitones: -9 },
{ name: 'E', semitones: -8 },
{ name: 'F', semitones: -7 },
{ name: 'F#', semitones: -6 },
{ name: 'G', semitones: -5 },
{ name: 'G#', semitones: -4 },
{ name: 'A', semitones: -3 },
{ name: 'A#', semitones: -2 },
{ name: 'B', semitones: -1 },
{ name: 'C↑', semitones: 0 },
{ name: 'C#↑', semitones: 1 },
{ name: 'D↑', semitones: 2 },
{ name: 'D#↑', semitones: 3 },
{ name: 'E↑', semitones: 4 },
{ name: 'F↑', semitones: 5 },
{ name: 'F#↑', semitones: 6 },
{ name: 'G↑', semitones: 7 },
{ name: 'G#↑', semitones: 8 },
{ name: 'A↑', semitones: 9 },
{ name: 'A#↑', semitones: 10 },
{ name: 'B↑', semitones: 11 },
{ name: 'C↑↑', semitones: 12 }
];
// Create note buttons
const noteButtonsContainer = document.getElementById('noteButtons');
notes.forEach(note => {
const btn = document.createElement('button');
btn.className = 'note-btn';
btn.textContent = note.name;
btn.onclick = () => setPitch(note.semitones);
noteButtonsContainer.appendChild(btn);
});
// Initialize audio context
function initAudioContext() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
}
// Pitch shift using Web Audio API
async function pitchShift(audioBuffer, semitones) {
const playbackRate = Math.pow(2, semitones / 12);
const offlineContext = new OfflineAudioContext(
audioBuffer.numberOfChannels,
Math.ceil(audioBuffer.length / playbackRate),
audioBuffer.sampleRate
);
const source = offlineContext.createBufferSource();
source.buffer = audioBuffer;
source.playbackRate.value = playbackRate;
source.connect(offlineContext.destination);
source.start(0);
return await offlineContext.startRendering();
}
// Draw waveform
function drawWaveform(audioBuffer) {
const width = waveformCanvas.width = waveformCanvas.offsetWidth;
const height = waveformCanvas.height = waveformCanvas.offsetHeight;
const data = audioBuffer.getChannelData(0);
const step = Math.ceil(data.length / width);
const amp = height / 2;
ctx.fillStyle = '#16213e';
ctx.fillRect(0, 0, width, height);
ctx.beginPath();
ctx.moveTo(0, amp);
ctx.strokeStyle = '#4fbdba';
ctx.lineWidth = 2;
for (let i = 0; i < width; i++) {
let min = 1.0;
let max = -1.0;
for (let j = 0; j < step; j++) {
const value = data[i * step + j];
if (value < min) min = value;
if (value > max) max = value;
}
ctx.lineTo(i, (1 + min) * amp);
ctx.lineTo(i, (1 + max) * amp);
}
ctx.stroke();
}
// Set pitch
function setPitch(semitones) {
currentPitch = semitones;
pitchSlider.value = semitones;
updatePitchDisplay();
// Update active note button
document.querySelectorAll('.note-btn').forEach(btn => {
btn.classList.remove('active');
});
const activeNote = notes.find(n => n.semitones === Math.round(semitones));
if (activeNote) {
const index = notes.indexOf(activeNote);
document.querySelectorAll('.note-btn')[index].classList.add('active');
}
}
// Update pitch display
function updatePitchDisplay() {
const semitones = parseFloat(pitchSlider.value);
currentPitch = semitones;
pitchValue.textContent = `${semitones > 0 ? '+' : ''}${semitones.toFixed(1)} semitones`;
}
// Event listeners
pitchSlider.addEventListener('input', updatePitchDisplay);
recordBtn.addEventListener('click', async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
initAudioContext();
mediaRecorder = new MediaRecorder(stream);
audioChunks = [];
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = async () => {
audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
const arrayBuffer = await audioBlob.arrayBuffer();
sourceBuffer = await audioContext.decodeAudioData(arrayBuffer);
drawWaveform(sourceBuffer);
playBtn.disabled = false;
playPitchedBtn.disabled = false;
downloadBtn.disabled = false;
status.textContent = 'Recording saved - adjust pitch and play!';
status.classList.remove('recording');
stream.getTracks().forEach(track => track.stop());
};
mediaRecorder.start();
recordBtn.disabled = true;
stopBtn.disabled = false;
status.textContent = 'Recording...';
status.classList.add('recording');
} catch (err) {
console.error('Error accessing microphone:', err);
status.textContent = 'Error: Could not access microphone';
}
});
stopBtn.addEventListener('click', () => {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
recordBtn.disabled = false;
stopBtn.disabled = true;
}
});
playBtn.addEventListener('click', () => {
if (sourceBuffer) {
const source = audioContext.createBufferSource();
source.buffer = sourceBuffer;
source.connect(audioContext.destination);
source.start(0);
status.textContent = 'Playing original...';
}
});
playPitchedBtn.addEventListener('click', async () => {
if (sourceBuffer && currentPitch !== 0) {
status.textContent = 'Processing pitch shift...';
const pitchedBuffer = await pitchShift(sourceBuffer, currentPitch);
const source = audioContext.createBufferSource();
source.buffer = pitchedBuffer;
source.connect(audioContext.destination);
source.start(0);
status.textContent = `Playing with ${currentPitch > 0 ? '+' : ''}${currentPitch.toFixed(1)} semitones shift...`;
} else if (sourceBuffer) {
playBtn.click();
}
});
downloadBtn.addEventListener('click', async () => {
if (sourceBuffer) {
status.textContent = 'Preparing download...';
let bufferToDownload = sourceBuffer;
if (currentPitch !== 0) {
bufferToDownload = await pitchShift(sourceBuffer, currentPitch);
}
// Convert to WAV
const wav = audioBufferToWav(bufferToDownload);
const blob = new Blob([wav], { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `pitched_audio_${currentPitch.toFixed(1)}_semitones.wav`;
a.click();
URL.revokeObjectURL(url);
status.textContent = 'Download started!';
}
});
// Convert AudioBuffer to WAV
function audioBufferToWav(buffer) {
const length = buffer.length * buffer.numberOfChannels * 2 + 44;
const arrayBuffer = new ArrayBuffer(length);
const view = new DataView(arrayBuffer);
const channels = [];
let offset = 0;
let pos = 0;
// Write WAV header
setUint32(0x46464952); // "RIFF"
setUint32(length - 8); // file length - 8
setUint32(0x45564157); // "WAVE"
setUint32(0x20746d66); // "fmt " chunk
setUint32(16); // length = 16
setUint16(1); // PCM
setUint16(buffer.numberOfChannels);
setUint32(buffer.sampleRate);
setUint32(buffer.sampleRate * 2 * buffer.numberOfChannels); // avg. bytes/sec
setUint16(buffer.numberOfChannels * 2); // block-align
setUint16(16); // 16-bit
setUint32(0x61746164); // "data" - chunk
setUint32(length - pos - 4); // chunk length
// Write interleaved data
for (let i = 0; i < buffer.numberOfChannels; i++) {
channels.push(buffer.getChannelData(i));
}
while (pos < length - 44) {
for (let i = 0; i < buffer.numberOfChannels; i++) {
const sample = Math.max(-1, Math.min(1, channels[i][offset]));
view.setInt16(pos, sample * 0x7FFF, true);
pos += 2;
}
offset++;
}
return arrayBuffer;
function setUint16(data) {
view.setUint16(pos, data, true);
pos += 2;
}
function setUint32(data) {
view.setUint32(pos, data, true);
pos += 4;
}
}
</script>
</body>
</html>
This app provides:
- Recording: Click "Start Recording" to capture audio from your microphone
- Waveform Display: Visual representation of your recorded audio
- Pitch Control:
- Use the slider to adjust pitch by ±12 semitones (one octave up or down)
- Click note buttons for quick pitch changes to specific musical notes
- Playback: Compare original and pitch-shifted versions
- Download: Save your pitch-shifted audio as a WAV file
The app uses the Web Audio API's playback rate adjustment for pitch shifting. While this is simpler than Melodyne's advanced algorithms (which preserve formants and timing), it demonstrates the core concept of pitch manipulation in a browser.
To use it:
- Click "Start Recording" and allow microphone access
- Sing or speak
- Click "Stop Recording"
- Adjust the pitch slider or click a note button
- Click "Play Pitched" to hear the result
- Download if you like it!
Note: This works best in modern browsers like Chrome, Firefox, or Edge. The pitch shifting method changes both pitch and tempo (like speeding up/slowing down a tape), which is different from Melodyne's more sophisticated pitch correction that preserves timing.