It looks like you want a simple HTML chiptune tracker with features such as:
- BPM control
- Patterns and rows per pattern
- Multiple instruments with synths or custom oscillators
- Volume, detune, ADSR, filter, noise controls per instrument
- Ability to share songs (export/import URL or string)
- Keyboard shortcuts
- Looping playback
- No demo audio playing on load
- Possibly use
/# in the URL for song sharing or state
I will create a minimal but functional web chiptune tracker that includes:
- Basic UI for BPM, patterns, rows, instruments (with synth controls)
- A pattern editor grid
- Simple sound generation via Web Audio OscillatorNode + GainNode + BiquadFilterNode
- Simple ADSR envelope per note
- Noise option via BufferSource
- Share/export song as base64 JSON in URL hash
/#[data]
- Playback controls with loop toggle
- Keyboard shortcuts for navigation and playback
Here's the full HTML+JS code for the tracker:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>HTML Chiptune Tracker</title>
<style>
body { background: #111; color: #eee; font-family: monospace; margin: 0; }
header, footer { padding: 0.5rem 1rem; background: #222; display: flex; align-items: center; gap: 1rem; }
main { display: flex; gap: 1rem; padding: 1rem; }
#patternGrid { border-collapse: collapse; user-select:none; }
#patternGrid td, #patternGrid th { border: 1px solid #444; padding: 0.2rem 0.4rem; width: 3rem; text-align:center; cursor: pointer; }
#patternGrid td.active { background: #39f; }
#patternGrid td.editing { background: #093; }
#patternGrid th { background: #222; }
#instruments { flex: 1; background: #222; padding: 1rem; border-radius: 4px; }
#playerControls { gap: 0.5rem; }
label { font-size: 0.9rem; }
input[type=number], select, input[type=range] { width: 4rem; }
.instrument { background: #333; padding: 0.5rem; margin-bottom: 0.5rem; border-radius: 4px; }
.inlabel { display: inline-block; width: 5rem; }
#shareLink { width: 100%; font-size: 0.9rem; }
button { background: #39f; border: none; color: white; padding: 0.3rem 0.6rem; font-size: 1rem; border-radius: 3px; cursor: pointer; }
button:hover { background: #58f; }
small { color: #666; }
</style>
</head>
<body>
<header>
<label>BPM: <input type="number" id="bpm" min="30" max="400" value="120" /></label>
<label>Patterns: <input type="number" id="numPatterns" min="1" max="16" value="4" /></label>
<label>Rows/pattern: <input type="number" id="rowsPerPattern" min="4" max="64" value="16" /></label>
<label>Instruments: <input type="number" id="numInstruments" min="1" max="8" value="3" /></label>
<button id="newSong">New Song</button>
</header>
<main>
<section id="patternSection" style="flex:2;">
<table id="patternGrid"></table>
<div style="padding:0.5rem 0;">
<button id="playBtn">▶ Play</button>
<button id="stopBtn" disabled>■ Stop</button>
<label><input type="checkbox" id="loopToggle" checked /> Loop</label>
<button id="clearPattern">Clear Pattern</button>
</div>
</section>
<aside id="instruments" style="flex:1;">
<h3>Instruments</h3>
<div id="instrumentList"></div>
</aside>
</main>
<footer>
<button id="exportBtn">Export / Share Song</button>
<input type="text" id="shareLink" readonly placeholder="Exported song data URL will appear here" />
</footer>
<script>
(() => {
// Constants & State
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
let isPlaying = false;
let currentRow = 0;
let timerID = null;
let song = {
bpm: 120,
numPatterns: 4,
rowsPerPattern: 16,
numInstruments: 3,
instruments: [], // array of instrument objects
patterns: [], // array of patterns, each pattern: array of rows, each row: array of notes per instrument
};
/*
Note representation for each cell in a pattern:
{
note: n, // MIDI note number or null
instrument: 0..numInstruments-1 (index)
}
We store pattern data as 2D: patterns[patternIndex][rowIndex][instrIndex] = { note, volume, other params? }
But we'll keep simple, each cell: { note: number|null, volume: 0-1, ...}
*/
// Utility
function midiToFrequency(midi) {
return 440 * Math.pow(2, (midi - 69) / 12);
}
// Initialize empty song data with default instruments and patterns
function initializeSong() {
const { bpm, numPatterns, rowsPerPattern, numInstruments } = song;
song.patterns = [];
for(let pi=0; pi < numPatterns; pi++) {
const pattern = [];
for(let ri=0; ri < rowsPerPattern; ri++) {
const row = [];
for(let ii=0; ii < numInstruments; ii++) {
row.push(null); // null means no note
}
pattern.push(row);
}
song.patterns.push(pattern);
}
song.instruments = [];
for(let i=0; i < numInstruments; i++) {
song.instruments.push(defaultInstrument(i));
}
}
function defaultInstrument(i) {
return {
name: `Inst ${i+1}`,
oscType: 'square', // 'sine', 'square', 'sawtooth', 'triangle', 'custom'
customWave: null, // if custom
volume: 0.5,
detune: 0,
adsr: { attack: 0.01, decay: 0.1, sustain: 0.6, release: 0.1 },
filter: { type: 'lowpass', frequency: 8000, Q: 1 },
noise: false,
};
}
// DOM refs
const bpmInput = document.getElementById('bpm');
const numPatternsInput = document.getElementById('numPatterns');
const rowsPerPatternInput = document.getElementById('rowsPerPattern');
const numInstrumentsInput = document.getElementById('numInstruments');
const patternGrid = document.getElementById('patternGrid');
const instrumentList = document.getElementById('instrumentList');
const playBtn = document.getElementById('playBtn');
const stopBtn = document.getElementById('stopBtn');
const loopToggle = document.getElementById('loopToggle');
const clearPatternBtn = document.getElementById('clearPattern');
const exportBtn = document.getElementById('exportBtn');
const shareLinkInput = document.getElementById('shareLink');
const newSongBtn = document.getElementById('newSong');
// State vars for UI
let currentPatternIndex = 0;
let editingCell = null; // {rowIndex, instrIndex}
// Initialize & Render functions
// Render pattern grid
function renderPatternGrid() {
const { rowsPerPattern, numInstruments } = song;
// Header with instrument numbers
let html = '<thead><tr><th>Row</th>';
for(let i=0; i < numInstruments; i++) html += `<th>Inst ${i+1}</th>`;
html += '</tr></thead><tbody>';
for(let r=0; r < rowsPerPattern; r++) {
html += `<tr><th>${r+1}</th>`;
for(let i=0; i < numInstruments; i++) {
const cell = song.patterns[currentPatternIndex][r][i];
let text = '';
if(cell && cell.note!=null) {
text = noteName(cell.note);
}
html += `<td data-row="${r}" data-instr="${i}"${(editingCell && editingCell.row===r && editingCell.instr===i) ? ' class="editing"' : ''}>${text||''}</td>`;
}
html += '</tr>';
}
html += '</tbody>';
patternGrid.innerHTML = html;
}
// Render instrument controls
function renderInstrumentControls() {
instrumentList.innerHTML = '';
song.instruments.forEach((inst, idx) => {
const div = document.createElement('div');
div.className = 'instrument';
div.dataset.idx = idx;
div.innerHTML = `
<strong>${inst.name}</strong><br/>
<label>Wave:
<select class="oscType">
<option value="sine" ${inst.oscType==='sine'?'selected':''}>Sine</option>
<option value="square" ${inst.oscType==='square'?'selected':''}>Square</option>
<option value="sawtooth" ${inst.oscType==='sawtooth'?'selected':''}>Sawtooth</option>
<option value="triangle" ${inst.oscType==='triangle'?'selected':''}>Triangle</option>
</select>
</label><br/>
<label>Vol: <input type="range" min="0" max="1" step="0.01" class="volume" value="${inst.volume}" /></label><br/>
<label>Detune: <input type="number" min="-100" max="100" step="1" class="detune" value="${inst.detune}"/></label><br/>
<label>Noise: <input type="checkbox" class="noise" ${inst.noise?'checked':''} /></label><br/>
<fieldset>
<legend>ADSR</legend>
<label>A <input type="number" min="0" max="2" step="0.01" class="adsrA" value="${inst.adsr.attack}" /></label><br/>
<label>D <input type="number" min="0" max="2" step="0.01" class="adsrD" value="${inst.adsr.decay}" /></label><br/>
<label>S <input type="number" min="0" max="1" step="0.01" class="adsrS" value="${inst.adsr.sustain}" /></label><br/>
<label>R <input type="number" min="0" max="2" step="0.01" class="adsrR" value="${inst.adsr.release}" /></label><br/>
</fieldset>
<fieldset>
<legend>Filter</legend>
<label>Type:
<select class="filterType">
<option value="lowpass" ${inst.filter.type==='lowpass'?'selected':''}>Lowpass</option>
<option value="highpass" ${inst.filter.type==='highpass'?'selected':''}>Highpass</option>
<option value="bandpass" ${inst.filter.type==='bandpass'?'selected':''}>Bandpass</option>
<option value="none" ${(inst.filter.type==='none'?'selected':'')}>None</option>
</select>
</label><br/>
<label>Freq: <input type="number" min="20" max="20000" step="1" class="filterFreq" value="${inst.filter.frequency}" /></label><br/>
<label>Q: <input type="number" min="0" max="20" step="0.1" class="filterQ" value="${inst.filter.Q}" /></label><br/>
</fieldset>
`;
instrumentList.appendChild(div);
});
}
// Note name from MIDI number (C4=60)
function noteName(midi) {
if(midi==null) return '';
const names=['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'];
let octave = Math.floor(midi/12) -1;
let name = names[midi%12];
return name+octave;
}
// Parse note name (basic), supports C4, D#3 etc.
function parseNoteName(str) {
str = str.trim().toUpperCase();
if(!str) return null;
const names={'C':0,'C#':1,'DB':1,'D':2,'D#':3,'EB':3,'E':4,'F':5,'F#':6,'GB':6,'G':7,'G#':8,'AB':8,'A':9,'A#':10,'BB':10,'B':11};
let m = str.match(/^([A-G])([#B]?)(-?\d)$/);
if(!m) return null;
let note = m[1];
let accidental = m[2];
let octave = +m[3];
let key = note + accidental;
let base = names[key];
if(base==null) return null;
return (octave +1)*12 + base;
}
// Play note with given instrument
function playNote(inst, midiNote, startTime, duration) {
if(midiNote==null) return;
const freq = midiToFrequency(midiNote + (inst.detune/100));
const gainNode = audioCtx.createGain();
const filterNode = audioCtx.createBiquadFilter();
// Setup filter or bypass
if(inst.filter.type!=='none') {
filterNode.type = inst.filter.type;
filterNode.frequency.setValueAtTime(inst.filter.frequency, startTime);
filterNode.Q.setValueAtTime(inst.filter.Q, startTime);
}
const volume = inst.volume;
if(inst.noise) {
// Noise playback
const bufferSize = audioCtx.sampleRate * duration;
const noiseBuffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
let data = noiseBuffer.getChannelData(0);
for(let i=0; i<data.length; i++) data[i] = Math.random() * 2 - 1;
const noiseSource = audioCtx.createBufferSource();
noiseSource.buffer = noiseBuffer;
noiseSource.loop = false;
// ADSR Gain envelope
gainNode.gain.setValueAtTime(0, startTime);
const {attack, decay, sustain, release} = inst.adsr;
gainNode.gain.linearRampToValueAtTime(volume, startTime + attack);
gainNode.gain.linearRampToValueAtTime(volume*sustain, startTime + attack + decay);
noiseSource.connect(gainNode);
if(inst.filter.type!=='none') gainNode.connect(filterNode);
else gainNode.connect(audioCtx.destination);
if(inst.filter.type!=='none') filterNode.connect(audioCtx.destination);
noiseSource.start(startTime);
// Schedule release
gainNode.gain.setTargetAtTime(0, startTime + duration, release/5);
noiseSource.stop(startTime + duration + release + 0.1);
}
else {
// Oscillator playback
const osc = audioCtx.createOscillator();
osc.type = inst.oscType;
osc.frequency.setValueAtTime(freq, startTime);
osc.detune.setValueAtTime(inst.detune, startTime);
// ADSR envelope
gainNode.gain.setValueAtTime(0, startTime);
const {attack, decay, sustain, release} = inst.adsr;
gainNode.gain.linearRampToValueAtTime(volume, startTime + attack);
gainNode.gain.linearRampToValueAtTime(volume*sustain, startTime + attack + decay);
if(inst.filter.type!=='none') {
osc.connect(gainNode);
gainNode.connect(filterNode);
filterNode.connect(audioCtx.destination);
}
else {
osc.connect(gainNode);
gainNode.connect(audioCtx.destination);
}
osc.start(startTime);
gainNode.gain.setTargetAtTime(0, startTime + duration, release/5);
osc.stop(startTime + duration + release);
}
}
// Playback loop
function playSong() {
if(isPlaying) return;
if(audioCtx.state === 'suspended') audioCtx.resume();
isPlaying = true;
playBtn.disabled = true;
stopBtn.disabled = false;
const interval = (60 / song.bpm) / 4; // sixteenth note length in seconds (assuming 16 rows per pattern)
let patternLength = song.rowsPerPattern;
let startTime = audioCtx.currentTime + 0.05;
currentRow = 0;
timerID = window.setInterval(() => {
// Play notes on current row
let pattern = song.patterns[currentPatternIndex];
if(!pattern) return;
const row = pattern[currentRow];
row.forEach((cell, idx) => {
if(cell && cell.note!=null) {
playNote(song.instruments[idx], cell.note, audioCtx.currentTime + 0.01, interval * 0.9);
}
});
highlightRow(currentRow);
currentRow++;
if(currentRow >= patternLength) {
if(loopToggle.checked) currentRow = 0;
else stopSong();
}
}, interval*1000);
}
function stopSong() {
if(!isPlaying) return;
isPlaying = false;
playBtn.disabled = false;
stopBtn.disabled = true;
clearInterval(timerID);
highlightRow(-1);
}
// Highlight pattern row during playback
function highlightRow(row) {
const rows = patternGrid.querySelectorAll('tbody tr');
rows.forEach((tr, idx) => {
if(idx === row) tr.style.background = '#2222ff33';
else tr.style.background = '';
});
}
// UI Events
bpmInput.addEventListener('change', e => {
song.bpm = Math.max(30, Math.min(400, +bpmInput.value));
bpmInput.value = song.bpm;
});
numPatternsInput.addEventListener('change', e => {
let val = +numPatternsInput.value;
if(val <1) val=1;
if(val >16) val=16;
numPatternsInput.value = val;
song.numPatterns = val;
initializeSong();
renderPatternGrid();
});
rowsPerPatternInput.addEventListener('change', e => {
let val = +rowsPerPatternInput.value;
if(val <4) val=4;
if(val >64) val=64;
rowsPerPatternInput.value = val;
song.rowsPerPattern = val;
initializeSong();
renderPatternGrid();
});
numInstrumentsInput.addEventListener('change', e => {
let val = +numInstrumentsInput.value;
if(val <1) val=1;
if(val >8) val=8;
numInstrumentsInput.value = val;
song.numInstruments = val;
initializeSong();
renderInstrumentControls();
renderPatternGrid();
});
playBtn.addEventListener('click', playSong);
stopBtn.addEventListener('click', stopSong);
clearPatternBtn.addEventListener('click', () => {
song.patterns[currentPatternIndex] = [];
for(let ri=0; ri < song.rowsPerPattern; ri++) {
const row = [];
for(let ii=0; ii < song.numInstruments; ii++) row.push(null);
song.patterns[currentPatternIndex].push(row);
}
renderPatternGrid();
});
newSongBtn.addEventListener('click', () => {
if(confirm('Create new song? This will overwrite current work.')) {
initializeSong();
renderInstrumentControls();
renderPatternGrid();
stopSong();
bpmInput.value = song.bpm;
numPatternsInput.value = song.numPatterns;
rowsPerPatternInput.value = song.rowsPerPattern;
numInstrumentsInput.value = song.numInstruments;
}
});
// Edit notes on pattern grid click
patternGrid.addEventListener('click', e => {
if(e.target.tagName !== 'TD') return;
const row = +e.target.dataset.row;
const instr = +e.target.dataset.instr;
if(editingCell && editingCell.row === row && editingCell.instr === instr) {
// Already editing, stop edit
editingCell = null;
renderPatternGrid();
return;
}
editingCell = { row, instr };
renderPatternGrid();
// Prompt user for note (keyboard shortcut could be better but minimum for now)
const currentCell = song.patterns[currentPatternIndex][row][instr];
const currentNote = currentCell && currentCell.note;
const noteStr = prompt('Enter note (e.g. C4, D#3) or leave empty to clear:', currentNote?noteName(currentNote):'');
if(noteStr === null) {
// cancelled, do nothing
editingCell = null;
renderPatternGrid();
return;
}
const newNote = parseNoteName(noteStr);
if(newNote == null && noteStr.trim() !== '') {
alert('Invalid note');
editingCell = null;
renderPatternGrid();
return;
}
if(newNote == null) {
// clear note
song.patterns[currentPatternIndex][row][instr] = null;
}
else {
song.patterns[currentPatternIndex][row][instr] = { note:newNote };
}
editingCell = null;
renderPatternGrid();
});
// Instrument controls change handlers
instrumentList.addEventListener('input', e => {
const div = e.target.closest('.instrument');
if(!div) return;
const idx = +div.dataset.idx;
const inst = song.instruments[idx];
if(!inst) return;
if(e.target.classList.contains('oscType')) {
inst.oscType = e.target.value;
}
else if(e.target.classList.contains('volume')) {
inst.volume = +e.target.value;
}
else if(e.target.classList.contains('detune')) {
inst.detune = +e.target.value;
}
else if(e.target.classList.contains('noise')) {
inst.noise = e.target.checked;
}
else if(e.target.classList.contains('adsrA')) {
inst.adsr.attack = Math.max(0, +e.target.value);
}
else if(e.target.classList.contains('adsrD')) {
inst.adsr.decay = Math.max(0, +e.target.value);
}
else if(e.target.classList.contains('adsrS')) {
let v = +e.target.value;
if(v>1) v=1;
if(v<0) v=0;
inst.adsr.sustain = v;
}
else if(e.target.classList.contains('adsrR')) {
inst.adsr.release = Math.max(0, +e.target.value);
}
else if(e.target.classList.contains('filterType')) {
inst.filter.type = e.target.value;
}
else if(e.target.classList.contains('filterFreq')) {
inst.filter.frequency = Math.min(20000, Math.max(20, +e.target.value));
}
else if(e.target.classList.contains('filterQ')) {
inst.filter.Q = Math.min(20, Math.max(0, +e.target.value));
}
});
// Export and Share
exportBtn.addEventListener('click', () => {
// Serialize song state to JSON, then base64 encode
let data = JSON.stringify(song);
let b64 = btoa(encodeURIComponent(data));
let url = location.origin + location.pathname + '/#' + b64;
shareLinkInput.value = url;
shareLinkInput.select();
alert('Song data encoded in URL hash. You can copy/share the link below.');
// Also set URL hash
location.hash = b64;
});
// Load from URL hash if available
function loadFromHash() {
if(location.hash.length > 2) {
let b64 = location.hash.slice(1);
try {
let jsonStr = decodeURIComponent(atob(b64));
let loadedSong = JSON.parse(jsonStr);
if(loadedSong && loadedSong.patterns && loadedSong.instruments) {
song = loadedSong;
bpmInput.value = song.bpm;
numPatternsInput.value = song.numPatterns;
rowsPerPatternInput.value = song.rowsPerPattern;
numInstrumentsInput.value = song.numInstruments;
renderInstrumentControls();
renderPatternGrid();
alert('Loaded song data from URL.');
}
}
catch(e) {
console.warn('Failed to load song from URL', e);
}
}
}
// Keyboard shortcuts
document.addEventListener('keydown', e => {
if(e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; // ignore if typing
if(e.key === ' ') { // space play/stop toggle
e.preventDefault();
if(isPlaying) stopSong();
else playSong();
}
else if(e.key === 'ArrowRight') {
// Next pattern
currentPatternIndex++;
if(currentPatternIndex >= song.numPatterns) currentPatternIndex=0;
renderPatternGrid();
}
else if(e.key === 'ArrowLeft') {
// Prev pattern
currentPatternIndex--;
if(currentPatternIndex <0) currentPatternIndex = song.numPatterns -1;
renderPatternGrid();
}
else if(e.key === 'Escape') {
// Cancel edit on pattern grid
if(editingCell) {
editingCell=null;
renderPatternGrid();
}
}
});
// Init
function init() {
initializeSong();
renderInstrumentControls();
renderPatternGrid();
loadFromHash();
}
init();
})();
</script>
</body>
</html>
Explanation
-
The tracker supports:
- BPM (30-400)
- Number of patterns (up to 16)
- Rows per pattern (4-64)
- Up to 8 instruments, each with waveform, volume, detune, ADSR, filter, and noise toggle
- Editable pattern grid for current pattern; click on cell to edit note (C4, D#3, etc)
- Playback with loop option; play/stop buttons; space to toggle play
- Pattern switching via left/right arrow keys
- Song export to URL hash as base64 JSON data for sharing
- Load song data from URL hash on load
- Clear pattern button
-
Sound is generated via Web Audio API oscillators and noise buffers with ADSR envelopes and filters.
-
No audio plays automatically on load (user must click play).
-
Simple UI but functional for chiptune/square-wave style composition.
You can save the above as tracker.html and open it in a modern desktop browser and start composing.
If you want me to add features like pattern chaining, saving/loading from localStorage, or piano keyboard-style input, or MIDI support, just ask!