html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Synth Keyboard</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: #1a1a1a;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 20px;
}
h1 {
margin-bottom: 20px;
color: #4CAF50;
}
.synth-container {
width: 100%;
max-width: 800px;
background: #2a2a2a;
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
}
.controls {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #444;
}
.control-group {
display: flex;
flex-direction: column;
gap: 5px;
}
label {
font-size: 14px;
color: #aaa;
}
select, input[type="range"] {
background: #333;
color: white;
border: 1px solid #444;
padding: 5px;
border-radius: 4px;
}
.keyboard {
display: flex;
position: relative;
height: 200px;
margin-top: 10px;
}
.key {
position: relative;
cursor: pointer;
user-select: none;
}
.white-key {
height: 100%;
background: white;
border: 1px solid #ccc;
border-radius: 0 0 5px 5px;
flex: 1;
color: #333;
display: flex;
align-items: flex-end;
justify-content: center;
padding-bottom: 10px;
font-weight: bold;
z-index: 1;
}
.black-key {
position: absolute;
top: 0;
width: 40px;
height: 60%;
background: #111;
border-radius: 0 0 5px 5px;
z-index: 2;
color: white;
display: flex;
align-items: flex-end;
justify-content: center;
padding-bottom: 10px;
box-shadow: 0 2px 5px rgba(0,0,0,0.5);
}
.key.active {
background: #4CAF50;
}
.black-key.active {
background: #2E7D32;
}
.key-label {
font-size: 12px;
margin-bottom: 5px;
}
.status {
margin-top: 20px;
text-align: center;
font-size: 14px;
color: #888;
}
</style>
</head>
<body>
<div class="synth-container">
<h1>Web Synth Keyboard</h1>
<div class="controls">
<div class="control-group">
<label for="waveform">Waveform</label>
<select id="waveform">
<option value="sine">Sine</option>
<option value="square">Square</option>
<option value="sawtooth">Sawtooth</option>
<option value="triangle" selected>Triangle</option>
</select>
</div>
<div class="control-group">
<label for="volume">Volume</label>
<input type="range" id="volume" min="0" max="1" step="0.01" value="0.5">
</div>
<div class="control-group">
<label for="octave">Octave</label>
<select id="octave">
<option value="3">3</option>
<option value="4" selected>4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="keyboard" id="keyboard">
<!-- White keys will be generated by JavaScript -->
</div>
<div class="status" id="status">
Click or tap the keys to play. Computer keyboard also works (A,S,D,F,G,H,J,K,L).
</div>
</div>
<script>
// Audio context setup
let audioContext;
let masterGain;
const activeOscillators = {};
// Keyboard layout
const whiteKeys = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
const blackKeys = ['C#', 'D#', '', 'F#', 'G#', 'A#', ''];
const keyMap = {
'a': 'C', 'w': 'C#', 's': 'D', 'e': 'D#', 'd': 'E',
'f': 'F', 't': 'F#', 'g': 'G', 'y': 'G#', 'h': 'A',
'u': 'A#', 'j': 'B', 'k': 'C5', 'o': 'C#5', 'l': 'D5',
'p': 'D#5', ';': 'E5'
};
// Initialize the synth
function initSynth() {
try {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
masterGain = audioContext.createGain();
masterGain.gain.value = document.getElementById('volume').value;
masterGain.connect(audioContext.destination);
createKeyboard();
updateStatus("Synth ready! Play with mouse or keyboard.");
} catch (e) {
updateStatus("Error: Web Audio API not supported in this browser.");
console.error(e);
}
}
// Create the visual keyboard
function createKeyboard() {
const keyboard = document.getElementById('keyboard');
keyboard.innerHTML = '';
const octave = parseInt(document.getElementById('octave').value);
// Create white keys
whiteKeys.forEach((note, i) => {
const key = document.createElement('div');
key.className = 'key white-key';
key.dataset.note = note + octave;
key.dataset.key = Object.keys(keyMap).find(k => keyMap[k] === note + (note === 'C' && i === 6 ? octave + 1 : octave));
const label = document.createElement('div');
label.className = 'key-label';
label.textContent = key.dataset.key ? key.dataset.key.toUpperCase() : '';
key.appendChild(label);
keyboard.appendChild(key);
// Add event listeners
addKeyEvents(key);
});
// Create black keys
blackKeys.forEach((note, i) => {
if (!note) return;
const key = document.createElement('div');
key.className = 'key black-key';
key.dataset.note = note + octave;
key.dataset.key = Object.keys(keyMap).find(k => keyMap[k] === note + octave);
const label = document.createElement('div');
label.className = 'key-label';
label.textContent = key.dataset.key ? key.dataset.key.toUpperCase() : '';
key.appendChild(label);
// Position the black key
key.style.left = `${(i + 0.7) * (100 / whiteKeys.length)}%`;
keyboard.appendChild(key);
// Add event listeners
addKeyEvents(key);
});
}
// Add event listeners to a key
function addKeyEvents(key) {
key.addEventListener('mousedown', () => playNote(key.dataset.note, key));
key.addEventListener('mouseup', () => stopNote(key.dataset.note, key));
key.addEventListener('mouseleave', () => {
if (key.classList.contains('active')) {
stopNote(key.dataset.note, key);
}
});
// Touch support
key.addEventListener('touchstart', (e) => {
e.preventDefault();
playNote(key.dataset.note, key);
});
key.addEventListener('touchend', (e) => {
e.preventDefault();
stopNote(key.dataset.note, key);
});
}
// Play a note
function playNote(note, keyElement) {
if (!audioContext) initSynth();
if (audioContext.state === 'suspended') audioContext.resume();
if (activeOscillators[note]) return;
const freq = getNoteFrequency(note);