Certainly! Below is a simple example of a chiptune-style monophonic synth sequencer built with the Web Audio API in JavaScript, featuring:
- Sequencer: Step sequencer with 8 steps
- ADSR envelope
- Oscillator: Square wave (chiptune classic)
- Filter: Low-pass filter
- FM synthesis: Basic frequency modulation with a modulator oscillator
- Detune
- Synth controls exposed via simple UI
- Track share: You can export/import track data as JSON via text area
This example includes an HTML file with embedded script, so you can save it as index.html and open in any modern browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Chiptune Sequencer Synth</title>
<style>
body {
font-family: monospace;
background: #121212;
color: #eee;
padding: 20px;
}
.step {
display: inline-block;
width: 24px;
height: 24px;
margin: 2px;
background: #333;
border-radius: 4px;
cursor: pointer;
user-select: none;
}
.step.active {
background: #0f0;
}
.step.playing {
border: 2px solid #0f0;
}
label {
display: block;
margin: 10px 0 4px;
}
input[type=range] {
width: 100%;
}
textarea {
width: 100%;
height: 80px;
font-family: monospace;
}
button {
margin: 6px 2px 12px 0;
padding: 6px 12px;
border: none;
background: #0f0;
color: #030;
font-weight: bold;
border-radius: 4px;
cursor: pointer;
}
button:disabled {
background: #444;
color: #222;
cursor: default;
}
.container {
max-width: 600px;
}
</style>
</head>
<body>
<h1>Chiptune Step Sequencer Synth</h1>
<div class="container">
<div id="sequencer">
<!-- Steps go here -->
</div>
<label for="tempo">Tempo (BPM): <span id="tempoValue">120</span></label>
<input type="range" id="tempo" min="60" max="240" value="120">
<label>ADSR Envelope</label>
<label for="attack">Attack (s): <span id="attackValue">0.01</span></label>
<input type="range" id="attack" min="0" max="1" step="0.01" value="0.01">
<label for="decay">Decay (s): <span id="decayValue">0.1</span></label>
<input type="range" id="decay" min="0" max="1" step="0.01" value="0.1">
<label for="sustain">Sustain (level): <span id="sustainValue">0.6</span></label>
<input type="range" id="sustain" min="0" max="1" step="0.01" value="0.6">
<label for="release">Release (s): <span id="releaseValue">0.2</span></label>
<input type="range" id="release" min="0" max="2" step="0.01" value="0.2">
<label for="filterCutoff">Filter Cutoff (Hz): <span id="filterCutoffValue">800</span></label>
<input type="range" id="filterCutoff" min="100" max="5000" step="1" value="800">
<label for="filterQ">Filter Q: <span id="filterQValue">1</span></label>
<input type="range" id="filterQ" min="0.1" max="20" step="0.1" value="1">
<label for="fmDepth">FM Depth (Hz): <span id="fmDepthValue">100</span></label>
<input type="range" id="fmDepth" min="0" max="400" step="1" value="100">
<label for="fmFreq">FM Frequency (Hz): <span id="fmFreqValue">200</span></label>
<input type="range" id="fmFreq" min="0" max="1000" step="1" value="200">
<label for="detune">Detune (cents): <span id="detuneValue">0</span></label>
<input type="range" id="detune" min="-100" max="100" step="1" value="0">
<div style="margin-top:16px;">
<button id="playBtn">Play</button>
<button id="stopBtn" disabled>Stop</button>
</div>
<h2>Sequence (Step notes)</h2>
<p>Click steps to toggle notes on/off. Each step plays one note from a scale.</p>
<div id="notesInfo" style="margin-bottom:12px;">
Notes:
<span>0: Off</span>,
<span>1: C4</span>,
<span>2: D4</span>,
<span>3: E4</span>,
<span>4: G4</span>,
<span>5: A4</span>
</div>
<div id="sequenceSteps">
<!-- Will be a grid of step selectors -->
</div>
<label for="shareData">Track JSON Data:</label>
<textarea id="shareData" placeholder="No data loaded"></textarea>
<div>
<button id="exportBtn">Export Track</button>
<button id="importBtn">Import Track</button>
<button id="clearBtn">Clear Track</button>
</div>
</div>
<script>
(async () => {
const AudioContext = window.AudioContext || window.webkitAudioContext;
let audioCtx = null;
const NOTES = {
0: null, // Off
1: 261.63, // C4
2: 293.66, // D4
3: 329.63, // E4
4: 392.0, // G4
5: 440.0 // A4
};
// Sequencer length and steps
const STEPS = 8;
// For each step store note index (0=off)
let sequence = new Array(STEPS).fill(0);
// Current step while playing
let currentStep = 0;
let isPlaying = false;
let intervalId;
// UI elements references
const sequencerEl = document.getElementById('sequenceSteps');
const playBtn = document.getElementById('playBtn');
const stopBtn = document.getElementById('stopBtn');
const shareData = document.getElementById('shareData');
const exportBtn = document.getElementById('exportBtn');
const importBtn = document.getElementById('importBtn');
const clearBtn = document.getElementById('clearBtn');
const tempoSlider = document.getElementById('tempo');
const tempoValue = document.getElementById('tempoValue');
const attackSlider = document.getElementById('attack');
const attackValue = document.getElementById('attackValue');
const decaySlider = document.getElementById('decay');
const decayValue = document.getElementById('decayValue');
const sustainSlider = document.getElementById('sustain');
const sustainValue = document.getElementById('sustainValue');
const releaseSlider = document.getElementById('release');
const releaseValue = document.getElementById('releaseValue');
const filterCutoffSlider = document.getElementById('filterCutoff');
const filterCutoffValue = document.getElementById('filterCutoffValue');
const filterQSlider = document.getElementById('filterQ');
const filterQValue = document.getElementById('filterQValue');
const fmDepthSlider = document.getElementById('fmDepth');
const fmDepthValue = document.getElementById('fmDepthValue');
const fmFreqSlider = document.getElementById('fmFreq');
const fmFreqValue = document.getElementById('fmFreqValue');
const detuneSlider = document.getElementById('detune');
const detuneValue = document.getElementById('detuneValue');
// Initialize UI steps selector: For each step, create a small grid where you pick 0..5 note value
// We'll create for each step a dropdown/select so user picks the note for that step.
// Create steps selection UI
for(let i=0; i < STEPS; i++) {
let container = document.createElement('div');
container.style.display = 'inline-block';
container.style.margin = '0 8px 8px 0';
container.style.textAlign = 'center';
container.style.width = '48px';
let label = document.createElement('div');
label.textContent = `Step ${i+1}`;
label.style.fontSize = '10px';
container.appendChild(label);
let select = document.createElement('select');
select.dataset.stepIndex = i;
for(let n=0; n<=5; n++) {
let option = document.createElement('option');
option.value = n;
option.textContent = n === 0 ? "-" : Object.keys(NOTES)[n] + ": " +
{
1: "C4",
2: "D4",
3: "E4",
4: "G4",
5: "A4"
}[n];
select.appendChild(option);
}
select.value = 0;
container.appendChild(select);
sequencerEl.appendChild(container);
}
// Update sequence array on change
sequencerEl.querySelectorAll('select').forEach(sel => {
sel.addEventListener('change', e => {
let idx = e.target.dataset.stepIndex;
let val = parseInt(e.target.value);
sequence[idx] = val;
});
});
// Update slider labels on input
function bindSliderSliderLabel(slider, label) {
slider.addEventListener('input', () => {
label.textContent = slider.value;
});
label.textContent = slider.value;
}
bindSliderSliderLabel(tempoSlider, tempoValue);
bindSliderSliderLabel(attackSlider, attackValue);
bindSliderSliderLabel(decaySlider, decayValue);
bindSliderSliderLabel(sustainSlider, sustainValue);
bindSliderSliderLabel(releaseSlider, releaseValue);
bindSliderSliderLabel(filterCutoffSlider, filterCutoffValue);
bindSliderSliderLabel(filterQSlider, filterQValue);
bindSliderSliderLabel(fmDepthSlider, fmDepthValue);
bindSliderSliderLabel(fmFreqSlider, fmFreqValue);
bindSliderSliderLabel(detuneSlider, detuneValue);
function createSynthNode(freq) {
if (!audioCtx) return null;
// Carrier oscillator (square wave)
const carrier = audioCtx.createOscillator();
carrier.type = 'square';
carrier.frequency.value = freq;
// Detune in cents
carrier.detune.value = parseFloat(detuneSlider.value);
// FM oscillator
const fmOsc = audioCtx.createOscillator();
fmOsc.type = 'sine';
const fmGain = audioCtx.createGain();
fmOsc.frequency.value = parseFloat(fmFreqSlider.value);
fmGain.gain.value = parseFloat(fmDepthSlider.value);
// Connect FM oscillator to frequency param of carrier
fmOsc.connect(fmGain);
fmGain.connect(carrier.frequency);
// Filter
const filter = audioCtx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.value = parseFloat(filterCutoffSlider.value);
filter.Q.value = parseFloat(filterQSlider.value);
// Gain node for ADSR envelope
const gainNode = audioCtx.createGain();
gainNode.gain.value = 0;
carrier.connect(filter);
filter.connect(gainNode);
gainNode.connect(audioCtx.destination);
return {
carrier,
fmOsc,
fmGain,
filter,
gainNode
};
}
function playNoteAtTime(freq, time) {
if (!freq || !audioCtx) return;
const { carrier, fmOsc, filter, gainNode } = createSynthNode(freq);
const attack = parseFloat(attackSlider.value);
const decay = parseFloat(decaySlider.value);
const sustain = parseFloat(sustainSlider.value);
const release = parseFloat(releaseSlider.value);
carrier.start(time);
fmOsc.start(time);
// Envelope ADSR
const g = gainNode.gain;
g.cancelScheduledValues(time);
g.setValueAtTime(0, time);
g.linearRampToValueAtTime(1, time + attack);
g.linearRampToValueAtTime(sustain, time + attack + decay);
// Note length = a 16th note duration assuming 4 steps per beat and tempo
const tempo = parseInt(tempoSlider.value);
const beatDuration = 60 / tempo;
// Step length: 1/4 beat (16th note)
const stepDuration = beatDuration / 2;
const releaseStart = time + stepDuration;
g.cancelScheduledValues(releaseStart);
g.setValueAtTime(g.value, releaseStart);
g.linearRampToValueAtTime(0, releaseStart + release);
carrier.stop(releaseStart + release + 0.05);
fmOsc.stop(releaseStart + release + 0.05);
}
function playStep() {
// Update UI highlight
sequencerEl.querySelectorAll('select').forEach((sel, idx) => {
if (idx === currentStep) {
sel.style.borderColor = '#0f0';
sel.style.backgroundColor = '#222';
} else {
sel.style.borderColor = '';
sel.style.backgroundColor = '';
}
});
const noteIndex = sequence[currentStep];
const freq = NOTES[noteIndex];
if (freq) {
const now = audioCtx.currentTime;
playNoteAtTime(freq, now);
}
currentStep = (currentStep + 1) % STEPS;
}
function startPlaying() {
if (!audioCtx) {
audioCtx = new AudioContext();
} else if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
if (isPlaying) return;
isPlaying = true;
currentStep = 0;
// Start interval for sequencer
const tempo = parseInt(tempoSlider.value);
const intervalMs = (60 / tempo / 2) * 1000; // 16th note intervals
playStep(); // play immediately
intervalId = setInterval(playStep, intervalMs);
playBtn.disabled = true;
stopBtn.disabled = false;
}
function stopPlaying() {
if (!isPlaying) return;
isPlaying = false;
clearInterval(intervalId);
currentStep = 0;
sequencerEl.querySelectorAll('select').forEach((sel) => {
sel.style.borderColor = '';
sel.style.backgroundColor = '';
});
playBtn.disabled = false;
stopBtn.disabled = true;
}
playBtn.addEventListener('click', () => {
startPlaying();
});
stopBtn.addEventListener('click', () => {
stopPlaying();
});
// Export track JSON
exportBtn.addEventListener('click', () => {
const data = {
sequence,
tempo: tempoSlider.value,
attack: attackSlider.value,
decay: decaySlider.value,
sustain: sustainSlider.value,
release: releaseSlider.value,
filterCutoff: filterCutoffSlider.value,
filterQ: filterQSlider.value,
fmDepth: fmDepthSlider.value,
fmFreq: fmFreqSlider.value,
detune: detuneSlider.value
};
shareData.value = JSON.stringify(data, null, 2);
});
// Import track JSON
importBtn.addEventListener('click', () => {
try {
const data = JSON.parse(shareData.value);
if (!data.sequence || !Array.isArray(data.sequence)) throw new Error('Invalid data');
sequence = data.sequence;
// Update UI with loaded sequence and settings
sequencerEl.querySelectorAll('select').forEach((sel, idx) => {
sel.value = sequence[idx] || 0;
});
tempoSlider.value = data.tempo || tempoSlider.value;
attackSlider.value = data.attack || attackSlider.value;
decaySlider.value = data.decay || decaySlider.value;
sustainSlider.value = data.sustain || sustainSlider.value;
releaseSlider.value = data.release || releaseSlider.value;
filterCutoffSlider.value = data.filterCutoff || filterCutoffSlider.value;
filterQSlider.value = data.filterQ || filterQSlider.value;
fmDepthSlider.value = data.fmDepth || fmDepthSlider.value;
fmFreqSlider.value = data.fmFreq || fmFreqSlider.value;
detuneSlider.value = data.detune || detuneSlider.value;
// Update labels
tempoValue.textContent = tempoSlider.value;
attackValue.textContent = attackSlider.value;
decayValue.textContent = decaySlider.value;
sustainValue.textContent = sustainSlider.value;
releaseValue.textContent = releaseSlider.value;
filterCutoffValue.textContent = filterCutoffSlider.value;
filterQValue.textContent = filterQSlider.value;
fmDepthValue.textContent = fmDepthSlider.value;
fmFreqValue.textContent = fmFreqSlider.value;
detuneValue.textContent = detuneSlider.value;
alert('Track imported successfully!');
} catch(e) {
alert('Invalid JSON data');
}
});
// Clear track
clearBtn.addEventListener('click', () => {
sequence.fill(0);
sequencerEl.querySelectorAll('select').forEach((sel) => {
sel.value = 0;
});
shareData.value = '';
});
})();
</script>
</body>
</html>
How to use this:
- Save the above as
index.html
- Open in Chrome/Firefox or any modern browser
- Click steps, select notes per step (0=off, 1=C4, 2=D4, etc)
- Adjust ADSR, filter, FM, detune, tempo sliders
- Click Play to start sequencer
- Export the track JSON data to share or save your sequence
- Import JSON data to load sequences and settings
- Clear to reset sequence
Features included:
- Step sequencer with per-step note selection from a simple 5-note scale
- ADSR envelope applied to gain node
- Square oscillator carrier (classic chiptune tone)
- FM modulator oscillator modulating frequency of carrier
- Detune applied to carrier oscillator
- Lowpass filter with cutoff and Q control
- Tempo control (BPM), sequencer steps play 16th notes (~1/8th step length)
- Track export/import JSON serialization for sharing
Feel free to expand this with more notes, different waveform options, polyphony, or effects! This should provide a solid base for a chiptune synth with sequencing in pure HTML/JavaScript.