html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playable Synth Keyboard</title>
<style>
:root {
--bg-color: #2c3e50;
--control-bg: #34495e;
--key-white: #ecf0f1;
--key-black: #2c3e50;
--key-active: #3498db;
--text-color: #ecf0f1;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
user-select: none;
-webkit-user-select: none;
}
#synth-container {
background-color: var(--control-bg);
padding: 20px;
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
width: 90%;
max-width: 800px;
}
h1 {
text-align: center;
margin-bottom: 20px;
color: #3498db;
}
#controls {
display: flex;
justify-content: space-around;
align-items: center;
margin-bottom: 25px;
padding-bottom: 20px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.control-group {
display: flex;
flex-direction: column;
align-items: center;
}
label {
font-size: 0.9rem;
margin-bottom: 5px;
}
select, input[type="range"] {
padding: 5px;
border-radius: 4px;
border: 1px solid var(--key-white);
background-color: var(--bg-color);
color: var(--text-color);
cursor: pointer;
}
#keyboard {
display: flex;
position: relative;
height: 180px;
padding: 5px 0;
}
.key {
box-sizing: border-box;
cursor: pointer;
transition: background-color 0.1s, transform 0.1s;
display: flex;
align-items: flex-end;
justify-content: center;
padding-bottom: 10px;
font-size: 0.8rem;
font-weight: bold;
}
.key.white {
height: 100%;
width: calc(100% / 14);
background-color: var(--key-white);
border: 1px solid #bdc3c7;
border-radius: 0 0 5px 5px;
color: var(--bg-color);
z-index: 1;
}
.key.black {
position: absolute;
height: 60%;
width: calc(100% / 24);
background-color: var(--key-black);
color: var(--text-color);
border-radius: 0 0 5px 5px;
z-index: 2;
top: 0;
box-shadow: 0 2px 5px rgba(0,0,0,0.4);
}
.key.black[data-note="C#4"] { left: calc((100% / 14) * 0.7); }
.key.black[data-note="D#4"] { left: calc((100% / 14) * 1.8); }
.key.black[data-note="F#4"] { left: calc((100% / 14) * 3.65); }
.key.black[data-note="G#4"] { left: calc((100% / 14) * 4.75); }
.key.black[data-note="A#4"] { left: calc((100% / 14) * 5.85); }
.key.black[data-note="C#5"] { left: calc((100% / 14) * 7.7); }
.key.black[data-note="D#5"] { left: calc((100% / 14) * 8.8); }
.key.black[data-note="F#5"] { left: calc((100% / 14) * 10.65); }
.key.black[data-note="G#5"] { left: calc((100% / 14) * 11.75); }
.key.black[data-note="A#5"] { left: calc((100% / 14) * 12.85); }
.key.active {
background-color: var(--key-active);
transform: translateY(2px);
box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}
.key.black.active {
background-color: #2980b9;
}
#status {
text-align: center;
margin-top: 15px;
font-size: 0.8rem;
color: #95a5a6;
}
</style>
</head>
<body>
<div id="synth-container">
<h1>Simple Web Synth</h1>
<div id="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.3">
</div>
</div>
<div id="keyboard">
<div class="key white" data-note="C4" data-key="a">A</div>
<div class="key black" data-note="C#4" data-key="w">W</div>
<div class="key white" data-note="D4" data-key="s">S</div>
<div class="key black" data-note="D#4" data-key="e">E</div>
<div class="key white" data-note="E4" data-key="d">D</div>
<div class="key white" data-note="F4" data-key="f">F</div>
<div class="key black" data-note="F#4" data-key="t">T</div>
<div class="key white" data-note="G4" data-key="g">G</div>
<div class="key black" data-note="G#4" data-key="y">Y</div>
<div class="key white" data-note="A4" data-key="h">H</div>
<div class="key black" data-note="A#4" data-key="u">U</div>
<div class="key white" data-note="B4" data-key="j">J</div>
<div class="key white" data-note="C5" data-key="k">K</div>
<div class="key black" data-note="C#5" data-key="o">O</div>
<div class="key white" data-note="D5" data-key="l">L</div>
<div class="key black" data-note="D#5" data-key="p">P</div>
<div class="key white" data-note="E5" data-key=";">;</div>
<div class="key white" data-note="F5" data-key="'">'</div>
</div>
<div id="status">Click a key or use your computer keyboard (A, W, S, E, D, F...)</div>
</div>
<script>
const AudioContext = window.AudioContext || window.webkitAudioContext;
let audioCtx = null;
let masterGain = null;
const noteFrequencies = {
'C4': 261.63, 'C#4': 277.18, 'D4': 293.66, 'D#4': 311.13, 'E4': 329.63,
'F4': 349.23, 'F#4': 369.99, 'G4': 392.00, 'G#4': 415.30, 'A4': 440.00,
'A#4': 466.16, 'B4': 493.88, 'C5': 523.25, 'C#5': 554.37, 'D5': 587.33,
'D#5': 622.25, 'E5': 659.25, 'F5': 698.46
};
const activeOscillators = {};
const pressedKeys = {};
const keys = document.querySelectorAll('.key');
const volumeControl = document.getElementById('volume');
const waveformSelector = document.getElementById('waveform');
const statusDisplay = document.getElementById('status');
const keyboardMap = {};
keys.forEach(key => { const computerKey = key.dataset.key; if (computerKey) keyboardMap[computerKey] = key; });
function initializeAudio() {
if (audioCtx) return;
try {
audioCtx = new AudioContext();
masterGain = audioCtx.createGain();
masterGain.connect(audioCtx.destination);
updateVolume();
statusDisplay.textContent = "Synth Ready! Use mouse or keyboard.";
} catch (e) {
statusDisplay.textContent = "Error: Web Audio API not supported.";
}
}
function updateVolume() {
if (masterGain) masterGain.gain.setValueAtTime(volumeControl.value, audioCtx.currentTime);
}
volumeControl.addEventListener('input', updateVolume);
function startNote(noteName, keyElement) {
if (!audioCtx) initializeAudio();
if (audioCtx.state === 'suspended') audioCtx.resume();
if (activeOscillators[noteName]) return;
const frequency = noteFrequencies[noteName];
const oscillator = audioCtx.createOscillator();
oscillator.type = waveformSelector.value;
oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime);
oscillator.connect(masterGain);
oscillator.start();
activeOscillators[noteName] = oscillator;
if (keyElement) keyElement.classList.add('active');
}
function stopNote(noteName, keyElement) {
if (activeOscillators[noteName]) {
const osc = activeOscillators[noteName];
osc.stop(audioCtx.currentTime + 0.05);
osc.onended = () => osc.disconnect();
delete activeOscillators[noteName];
if (keyElement) keyElement.classList.remove('active');
}
}
keys.forEach(key => {
const note = key.dataset.note;
key.addEventListener('pointerdown', () => startNote(note, key));
key.addEventListener('pointerup', () => stopNote(note, key));
key.addEventListener('pointerleave', () => stopNote(note, key));
key.addEventListener('pointerenter', e => { if (e.buttons > 0) startNote(note, key); });
});
document.addEventListener('keydown', e => {
if (!audioCtx) initializeAudio();
const key = e.key.toLowerCase();
if (pressedKeys[key] || e.repeat) return;
const visualKeyElement = keyboardMap[key];
if (visualKeyElement) {
const note = visualKeyElement.dataset.note;
startNote(note, visualKeyElement);
pressedKeys[key] = true;
}
});
document.addEventListener('keyup', e => {
const key = e.key.toLowerCase();
const visualKeyElement = keyboardMap[key];
if (visualKeyElement) {
const note = visualKeyElement.dataset.note;
stopNote(note, visualKeyElement);
delete pressedKeys[key];
}
});
document.body.addEventListener('click', initializeAudio, { once: true });
document.body.addEventListener('touchstart', initializeAudio, { once: true, passive: true });
</script>
</body>
</html>