gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
chiptune sequencer adsr osc filter fm detune synth track pattern share "/#" html
- User
chiptune sequencer adsr osc filter fm detune synth track pattern share "/#" html
- Assistant
chiptune sequencer adsr osc filter fm detune synth track pattern share "/#" html
Sure! Below is a complete HTML example of a simple chiptune-style synth with a sequencer, ADSR envelope, oscillator, filter, FM synthesis, and detune capabilities. It allows you to create patterns, play them back, and share patterns via URL hash.
Features included:
You can copy-paste this into a single .html file and open it in a modern browser (Chrome, Firefox, Edge):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Chiptune Synth Sequencer with ADSR, FM, and Filter</title>
<style>
body {
font-family: monospace, monospace;
background: #111;
color: #eee;
margin: 0;
padding: 1rem;
}
h1 {
text-align: center;
}
.sequencer {
display: grid;
grid-template-columns: repeat(16, 2rem);
gap: 2px;
justify-content: center;
margin-bottom: 1rem;
}
.step {
width: 28px;
height: 28px;
background: #333;
border-radius: 3px;
border: 1px solid #444;
cursor: pointer;
user-select: none;
display: flex;
align-items: center;
justify-content: center;
font-size: 1rem;
}
.step.active {
background: #39f;
box-shadow: 0 0 6px #39f88f;
}
.step.playing {
border-color: #f93;
}
.controls {
max-width: 600px;
margin: 0 auto 2rem;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 1rem;
}
label {
display: flex;
flex-direction: column;
font-size: 0.9rem;
}
input[type=range], select {
margin-top: 0.25rem;
width: 100%;
}
button {
background: #39f;
border: none;
padding: 0.7rem 1.5rem;
color: white;
font-weight: bold;
border-radius: 0.3rem;
cursor: pointer;
margin: 1rem auto;
display: block;
}
button:active {
background: #28c;
}
.footer {
text-align: center;
font-size: 0.8rem;
color: #666;
}
a {
color: #39f;
}
</style>
</head>
<body>
<h1>Chiptune Synth Sequencer</h1>
<div class="sequencer" id="sequencer"></div>
<div class="controls">
<label>Base Note
<select id="note-select" title="Set base note">
<option value="60">C4</option>
<option value="62">D4</option>
<option value="64" selected>E4</option>
<option value="65">F4</option>
<option value="67">G4</option>
<option value="69">A4</option>
<option value="71">B4</option>
<option value="72">C5</option>
</select>
</label>
<label>Waveform Oscillator 1
<select id="osc1-waveform">
<option value="square" selected>Square</option>
<option value="triangle">Triangle</option>
<option value="sawtooth">Sawtooth</option>
<option value="sine">Sine</option>
</select>
</label>
<label>Waveform Oscillator 2 (FM)
<select id="osc2-waveform">
<option value="square" selected>Square</option>
<option value="triangle">Triangle</option>
<option value="sawtooth">Sawtooth</option>
<option value="sine">Sine</option>
</select>
</label>
<label>FM Amount (Hz)
<input type="range" id="fm-amount" min="0" max="1000" step="1" value="0" />
</label>
<label>FM Frequency Ratio
<input type="range" id="fm-ratio" min="0.1" max="5" step="0.01" value="1" />
</label>
<label>Detune Osc2 (cents)
<input type="range" id="detune" min="-100" max="100" step="1" value="0" />
</label>
<label>Filter Cutoff (Hz)
<input type="range" id="filter-cutoff" min="200" max="8000" step="1" value="8000" />
</label>
<label>Filter Q (Resonance)
<input type="range" id="filter-q" min="0.1" max="20" step="0.1" value="1" />
</label>
<label>Attack (ms)
<input type="range" id="adsr-attack" min="0" max="1000" step="10" value="10" />
</label>
<label>Decay (ms)
<input type="range" id="adsr-decay" min="0" max="2000" step="10" value="100" />
</label>
<label>Sustain (0-1)
<input type="range" id="adsr-sustain" min="0" max="1" step="0.01" value="0.5" />
</label>
<label>Release (ms)
<input type="range" id="adsr-release" min="0" max="2000" step="10" value="200" />
</label>
</div>
<button id="play-stop-btn">Play</button>
<div class="footer">
Pattern saved in URL hash. <br />
Click steps to toggle notes. <br />
Share URL to share pattern. <br />
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API" target="_blank" rel="noopener noreferrer">Web Audio API</a> used.
</div>
<script>
(() => {
const AudioContext = window.AudioContext || window.webkitAudioContext;
const ctx = new AudioContext();
const NUM_STEPS = 16;
let currentStep = 0;
let isPlaying = false;
let timerID = null;
// Pattern: array of booleans for steps
// We'll store note on/off for each step
// For simplicity, just one note tone per step (base note)
let pattern = new Array(NUM_STEPS).fill(false);
// DOM references
const sequencerEl = document.getElementById('sequencer');
const playStopBtn = document.getElementById('play-stop-btn');
const controls = {
noteSelect: document.getElementById('note-select'),
osc1Waveform: document.getElementById('osc1-waveform'),
osc2Waveform: document.getElementById('osc2-waveform'),
fmAmount: document.getElementById('fm-amount'),
fmRatio: document.getElementById('fm-ratio'),
detune: document.getElementById('detune'),
filterCutoff: document.getElementById('filter-cutoff'),
filterQ: document.getElementById('filter-q'),
adsrAttack: document.getElementById('adsr-attack'),
adsrDecay: document.getElementById('adsr-decay'),
adsrSustain: document.getElementById('adsr-sustain'),
adsrRelease: document.getElementById('adsr-release'),
};
// Create sequencer buttons
const stepButtons = [];
for(let i=0; i < NUM_STEPS; i++) {
const step = document.createElement('div');
step.classList.add('step');
step.textContent = i+1;
step.dataset.index = i;
step.addEventListener('click', () => {
pattern[i] = !pattern[i];
updateStepUI(i);
savePatternToHash();
});
sequencerEl.appendChild(step);
stepButtons.push(step);
}
function updateStepUI(index) {
const btn = stepButtons[index];
if (pattern[index]) {
btn.classList.add('active');
} else {
btn.classList.remove('active');
}
}
function updateAllStepsUI() {
for(let i=0; i < NUM_STEPS; i++) {
updateStepUI(i);
}
}
// Pattern serialization/deserialization for URL hash
// Store pattern as hex string or base64? We'll do simple 16-char base64-ish with '0' and '1'
function patternToString() {
return pattern.map(p => (p ? '1' : '0')).join('');
}
function stringToPattern(str) {
if(typeof str !== 'string' || str.length !== NUM_STEPS) return;
pattern = str.split('').map(c => c === '1');
}
function savePatternToHash() {
location.hash = patternToString();
}
function loadPatternFromHash() {
if(location.hash.length >= NUM_STEPS+1) {
const str = location.hash.substr(1, NUM_STEPS);
stringToPattern(str);
updateAllStepsUI();
}
}
loadPatternFromHash();
// Audio nodes helper per note play
// We'll create a voice for each note played during triggering, which cleans itself after release
function centsToRatio(cents) {
return Math.pow(2, cents/1200);
}
function playNote(midiNote) {
if(ctx.state === 'suspended') ctx.resume();
// Frequency of base note
const baseFreq = 440 * Math.pow(2, (midiNote - 69)/12);
// Oscillator 2: FM modulator
const osc2Freq = baseFreq * parseFloat(controls.fmRatio.value);
const fmAmountHz = parseFloat(controls.fmAmount.value);
// Create nodes
const osc1 = ctx.createOscillator();
const osc2 = ctx.createOscillator();
const fmGain = ctx.createGain();
const detuneCents = parseFloat(controls.detune.value);
osc1.type = controls.osc1Waveform.value;
osc2.type = controls.osc2Waveform.value;
// Detune oscillator 2
osc2.detune.value = detuneCents;
// FM setup: osc2 modulates oscillator 1 frequency by fmAmount
osc2.frequency.value = osc2Freq;
fmGain.gain.value = fmAmountHz;
osc2.connect(fmGain);
fmGain.connect(osc1.frequency);
osc1.frequency.value = baseFreq;
// Filter
const filter = ctx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.value = parseFloat(controls.filterCutoff.value);
filter.Q.value = parseFloat(controls.filterQ.value);
// ADSR Envelope on gain node
const gainNode = ctx.createGain();
gainNode.gain.value = 0;
osc1.connect(filter);
filter.connect(gainNode);
gainNode.connect(ctx.destination);
osc1.start();
osc2.start();
// ADSR parameters
const now = ctx.currentTime;
const a = parseFloat(controls.adsrAttack.value) / 1000;
const d = parseFloat(controls.adsrDecay.value) / 1000;
const s = parseFloat(controls.adsrSustain.value);
const r = parseFloat(controls.adsrRelease.value) / 1000;
// Attack
gainNode.gain.cancelScheduledValues(now);
gainNode.gain.setValueAtTime(0, now);
gainNode.gain.linearRampToValueAtTime(1, now + a);
// Decay to Sustain
gainNode.gain.linearRampToValueAtTime(s, now + a + d);
// Return an object with stop function to trigger release
function stopNote() {
const stopTime = ctx.currentTime;
gainNode.gain.cancelScheduledValues(stopTime);
gainNode.gain.setValueAtTime(gainNode.gain.value, stopTime);
gainNode.gain.linearRampToValueAtTime(0, stopTime + r);
osc1.stop(stopTime + r + 0.05);
osc2.stop(stopTime + r + 0.05);
setTimeout(() => {
// Disconnect nodes
fmGain.disconnect();
osc2.disconnect();
osc1.disconnect();
filter.disconnect();
gainNode.disconnect();
}, (r + 0.1) * 1000);
}
return {
stop: stopNote
};
}
// Sequencer scheduler
const STEP_TIME = 0.125; // 1/8 note assuming ~120 BPM (approx), 125ms per step
let activeVoices = [];
function advanceStep() {
if(!isPlaying) return;
// Clear previous step highlight
stepButtons.forEach(btn => btn.classList.remove('playing'));
stepButtons[currentStep].classList.add('playing');
// Stop previous note voices
activeVoices.forEach(v => v.stop());
activeVoices = [];
// Play note if active in pattern
if(pattern[currentStep]) {
const baseNote = parseInt(controls.noteSelect.value);
activeVoices.push(playNote(baseNote));
}
currentStep = (currentStep + 1) % NUM_STEPS;
}
function startSequencer() {
if(ctx.state === 'suspended') ctx.resume();
if(isPlaying) return;
isPlaying = true;
currentStep = 0;
advanceStep();
timerID = setInterval(advanceStep, STEP_TIME*1000);
playStopBtn.textContent = "Stop";
}
function stopSequencer() {
if(!isPlaying) return;
isPlaying = false;
clearInterval(timerID);
timerID = null;
stepButtons.forEach(btn => btn.classList.remove('playing'));
activeVoices.forEach(v => v.stop());
activeVoices = [];
playStopBtn.textContent = "Play";
}
playStopBtn.addEventListener('click', () => {
if(isPlaying) {
stopSequencer();
} else {
startSequencer();
}
});
// On URL hash change, reload pattern
window.addEventListener('hashchange', () => {
loadPatternFromHash();
});
// Initialize UI steps based on loaded pattern
updateAllStepsUI();
})();
</script>
</body>
</html>
This small synth uses the Web Audio API to create a retro chiptune vibe with flexible synthesis parameters and pattern sequencing, all in one HTML file.
Enjoy making tunes! 🎶
If you want me to add more features or export/import pattern JSON, just ask!
Not yet included in a published release.