Got it! You want a simple HTML-based chiptune tracker with these features:
- BPM (tempo control)
- Patterns and rows per pattern
- Synth per instrument with custom oscillator types
- Volume, detune, ADSR envelope, filter, noise controls per instrument
- Ability to share songs (e.g., via URL hash
/#)
- Basic shortcuts (keyboard bindings)
- Duplicate (dupe), delete, loop functionality on patterns
- No demo/autoplay
Below is a minimal but functional implementation of such a tracker using the Web Audio API and vanilla JavaScript:
<!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 {
font-family: monospace, monospace;
background: #111;
color: #eee;
margin: 0;
padding: 0;
user-select: none;
}
header {
padding: 0.5em;
background: #222;
border-bottom: 1px solid #333;
}
.tracker {
display: grid;
grid-template-columns: 3fr 1fr;
height: 90vh;
}
#pattern-editor {
overflow: auto;
white-space: pre;
background: #000;
font-size: 16px;
line-height: 1.3em;
}
#controls {
background: #222;
padding: 0.5em;
overflow-y: auto;
}
label {
display: block;
margin-top: 0.5em;
}
input[type=number], select, input[type=range] {
width: 100%;
}
button {
margin-top: 0.5em;
width: 100%;
background: #444;
color: #eee;
border: none;
padding: 0.5em;
cursor: pointer;
}
button:hover {
background: #666;
}
.highlight {
background: #444;
}
#message {
font-size: 0.9em;
color: #666;
margin-top: 0.5em;
}
</style>
</head>
<body>
<header>
<strong>Chiptune Tracker</strong>
<span id="bpm-display">BPM: 120</span>
</header>
<div class="tracker">
<pre id="pattern-editor" tabindex="0" aria-label="Pattern editor" role="textbox"></pre>
<div id="controls">
<label>BPM
<input type="number" min="30" max="300" value="120" id="bpm" />
</label>
<label>Pattern Rows
<input type="number" min="4" max="64" value="16" id="rows" />
</label>
<label>Instruments
<select id="instrument-select"></select>
<button id="add-instrument">Add Instrument</button>
</label>
<div id="instrument-controls" style="margin-top:1em;"></div>
<button id="play">Play</button>
<button id="stop" disabled>Stop</button>
<button id="share">Share Song</button>
<div id="message"></div>
</div>
</div>
<script>
// TRACKER DATA STRUCTURE
class Instrument {
constructor() {
this.oscType = 'square'; // 'square', 'sawtooth', 'triangle', 'sine', 'custom'
this.volume = 0.5;
this.detune = 0;
this.adsr = {attack: 0.01, decay: 0.1, sustain: 0.8, release: 0.2};
this.filterFreq = 10000;
this.filterQ = 1;
this.noise = 0; // 0 to 1
}
}
class Pattern {
constructor(rows) {
this.rows = rows;
this.data = new Array(rows); // rows of notes
for(let i=0; i<rows; i++){
this.data[i] = [];
}
}
}
class Tracker {
constructor(){
this.bpm = 120;
this.rowsPerPattern = 16;
this.instruments = [new Instrument()];
this.patterns = [new Pattern(this.rowsPerPattern)];
this.currentPattern = 0;
this.currentRow = 0;
this.loop = true;
}
}
const NOTES = ['C-', 'C#', 'D-', 'D#', 'E-', 'F-', 'F#', 'G-', 'G#', 'A-', 'A#', 'B-'];
const NOTE_REGEX = /^([A-G])(#?)(\d)$/; // e.g. C#4
const SEMITONE_FROM_C0 = {
'C':0, 'C#':1,'D':2,'D#':3,'E':4,'F':5,'F#':6,'G':7,'G#':8,'A':9,'A#':10,'B':11
};
function noteToFrequency(note){
// note like C#4, A3
let m = NOTE_REGEX.exec(note);
if(!m) return 0;
let [_, letter, sharp, octave] = m;
let key = letter + (sharp||'');
let octaveNum = parseInt(octave,10);
let semitone = SEMITONE_FROM_C0[key];
if(semitone === undefined) return 0;
let noteNum = semitone + octaveNum*12;
let A4 = 69;
let freq = 440 * Math.pow(2,(noteNum - A4)/12);
return freq;
}
// UTILITIES FOR SERIALIZATION AND SHARING
function serializeSong(tracker){
return btoa(JSON.stringify(tracker));
}
function deserializeSong(str){
try {
return JSON.parse(atob(str));
} catch(e) {
return null;
}
}
// SETUP
let audioCtx;
let gainNode;
let oscillatorNodes = [];
let noiseNode;
const tracker = new Tracker();
const patternEditor = document.getElementById('pattern-editor');
const bpmInput = document.getElementById('bpm');
const rowsInput = document.getElementById('rows');
const instrumentSelect = document.getElementById('instrument-select');
const instrumentControls = document.getElementById('instrument-controls');
const playBtn = document.getElementById('play');
const stopBtn = document.getElementById('stop');
const shareBtn = document.getElementById('share');
const bpmDisplay = document.getElementById('bpm-display');
const messageDiv = document.getElementById('message');
const addInstrumentBtn = document.getElementById('add-instrument');
function createAudioContext(){
if(audioCtx == null || audioCtx.state === 'closed'){
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
gainNode = audioCtx.createGain();
gainNode.gain.value = 0.7;
gainNode.connect(audioCtx.destination);
}
}
// RENDER PATTERN TO TEXT
function renderPattern(){
let p = tracker.patterns[tracker.currentPattern];
let rows = p.rows;
let txt = '';
for(let r=0; r<rows; r++){
txt += (r === tracker.currentRow ? '>' : ' ') + " ";
for(let ch=0; ch<tracker.instruments.length; ch++){
let note = p.data[r][ch] || '---';
if(note === '') note = '---';
txt += note.padEnd(4,' ') + ' ';
}
txt += '\n';
}
patternEditor.textContent = txt.trimEnd();
bpmDisplay.textContent = `BPM: ${tracker.bpm}`;
}
function updateInstrumentSelect(){
instrumentSelect.innerHTML = '';
tracker.instruments.forEach((inst,i)=>{
let option = document.createElement('option');
option.value = i;
option.textContent = `Instrument #${i+1}`;
instrumentSelect.appendChild(option);
});
instrumentSelect.value = 0;
renderInstrumentControls(0);
}
function renderInstrumentControls(i){
const inst = tracker.instruments[i];
instrumentControls.innerHTML = '';
const oscLabel = document.createElement('label');
oscLabel.textContent = 'Oscillator:';
const oscSelect = document.createElement('select');
['square','sawtooth','triangle','sine'].forEach(type=>{
let opt = document.createElement('option');
opt.value = type;
opt.textContent = type;
if(type === inst.oscType) opt.selected = true;
oscSelect.appendChild(opt);
});
oscSelect.onchange = () => {
inst.oscType = oscSelect.value;
};
oscLabel.appendChild(oscSelect);
instrumentControls.appendChild(oscLabel);
const volLabel = document.createElement('label');
volLabel.textContent = 'Volume:';
const volRange = document.createElement('input');
volRange.type='range';
volRange.min=0; volRange.max=1; volRange.step=0.01;
volRange.value=inst.volume;
volRange.oninput = () => {
inst.volume = +volRange.value;
};
volLabel.appendChild(volRange);
instrumentControls.appendChild(volLabel);
const detuneLabel = document.createElement('label');
detuneLabel.textContent = 'Detune (cents):';
const detuneRange = document.createElement('input');
detuneRange.type='number';
detuneRange.min=-100;
detuneRange.max=100;
detuneRange.value = inst.detune;
detuneRange.onchange = () => {
inst.detune = +detuneRange.value;
};
detuneLabel.appendChild(detuneRange);
instrumentControls.appendChild(detuneLabel);
// ADSR sliders
['attack','decay','sustain','release'].forEach(param=>{
const label = document.createElement('label');
label.textContent = param.charAt(0).toUpperCase() + param.slice(1) + ':';
const input = document.createElement('input');
input.type = (param==='sustain')?'range':'number';
if(param==='sustain'){
input.min = 0;
input.max = 1;
input.step = 0.01;
} else {
input.min = 0;
input.max = 2;
input.step = 0.01;
}
input.value = inst.adsr[param];
input.oninput = () => {
inst.adsr[param] = (param==='sustain')? parseFloat(input.value) : +input.value;
};
label.appendChild(input);
instrumentControls.appendChild(label);
});
// Filter freq
const filterFreqLabel = document.createElement('label');
filterFreqLabel.textContent = 'Filter Frequency:';
const filterFreqInput = document.createElement('input');
filterFreqInput.type = 'number';
filterFreqInput.min = 10;
filterFreqInput.max = 20000;
filterFreqInput.value = inst.filterFreq;
filterFreqInput.onchange = () => {
inst.filterFreq = +filterFreqInput.value;
};
filterFreqLabel.appendChild(filterFreqInput);
instrumentControls.appendChild(filterFreqLabel);
// Filter Q
const filterQLabel = document.createElement('label');
filterQLabel.textContent = 'Filter Q:';
const filterQInput = document.createElement('input');
filterQInput.type = 'number';
filterQInput.min = 0.1;
filterQInput.max = 20;
filterQInput.step = 0.1;
filterQInput.value = inst.filterQ;
filterQInput.onchange = () => {
inst.filterQ = +filterQInput.value;
};
filterQLabel.appendChild(filterQInput);
instrumentControls.appendChild(filterQLabel);
// Noise amount
const noiseLabel = document.createElement('label');
noiseLabel.textContent = 'Noise:';
const noiseRange = document.createElement('input');
noiseRange.type = 'range';
noiseRange.min = 0; noiseRange.max = 1; noiseRange.step=0.01;
noiseRange.value = inst.noise;
noiseRange.oninput = ()=> {
inst.noise = +noiseRange.value;
};
noiseLabel.appendChild(noiseRange);
instrumentControls.appendChild(noiseLabel);
// Delete instrument button
if(tracker.instruments.length > 1){
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete Instrument';
deleteBtn.onclick = () => {
tracker.instruments.splice(i,1);
tracker.patterns.forEach(p=>{
for(let r=0; r<p.rows; r++){
if(p.data[r].length > i){
p.data[r].splice(i,1);
}
}
});
updateInstrumentSelect();
renderPattern();
};
instrumentControls.appendChild(deleteBtn);
}
}
// HANDLE PATTERN EDITING IN THE PRE TAG
// Format:
// > C-4 --- --- ---
// --- --- --- ---
// We'll build a simple cursor state: current row and col (instrument)
// User types note names or delete/backspace
let cursor = {row: 0, col: 0};
const rows = tracker.rowsPerPattern;
function focusedPatternEditor(){
patternEditor.focus();
renderPattern();
}
function setCursor(row, col){
cursor.row = Math.min(Math.max(0, row), tracker.patterns[tracker.currentPattern].rows-1);
cursor.col = Math.min(Math.max(0, col), tracker.instruments.length-1);
renderPatternWithCursor();
}
function renderPatternWithCursor(){
let p = tracker.patterns[tracker.currentPattern];
let rows = p.rows;
let lines = [];
for(let r=0; r<rows; r++){
let line = '';
line += (r === cursor.row ? '>' : ' ') + " ";
for(let ch=0; ch<tracker.instruments.length; ch++){
let note = p.data[r][ch] || '---';
if(note === '') note = '---';
let noteStr = note.padEnd(4,' ');
if(r === cursor.row && ch === cursor.col){
noteStr = `[${note.replace(/ /g,'-')}]`; // highlight current cell
}
line += noteStr + ' ';
}
lines.push(line);
}
patternEditor.textContent = lines.join('\n');
bpmDisplay.textContent = `BPM: ${tracker.bpm}`;
}
// Parse input to note format (e.g. "C#4", or "---" or delete cell)
function parseNoteInput(str){
if(/^---$/.test(str.toUpperCase())) return '';
let up = str.toUpperCase();
if(NOTE_REGEX.test(up)) return up;
return null;
}
patternEditor.addEventListener('keydown', e =>{
const p = tracker.patterns[tracker.currentPattern];
if(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight','Backspace','Delete'].includes(e.key) || /^[a-gA-G#0-9\-]$/.test(e.key)){
e.preventDefault();
if(e.key === 'ArrowUp'){
setCursor(cursor.row-1, cursor.col);
} else if(e.key === 'ArrowDown'){
setCursor(cursor.row+1, cursor.col);
} else if(e.key === 'ArrowLeft'){
setCursor(cursor.row, cursor.col-1);
} else if(e.key === 'ArrowRight'){
setCursor(cursor.row, cursor.col+1);
} else if(e.key === 'Backspace' || e.key === 'Delete'){
p.data[cursor.row][cursor.col] = '';
renderPatternWithCursor();
} else {
// Try to build note under cursor
// Simple approach: accept notes like C4, C#4 and map to uppercase with # optional
// We'll allow user to type the note inline: e.g. 'C', '#'... but for simplicity we accept single key input:
// So only allow full notes typed quickly - let's input note by replacing the cell content
// Because complexity of partial note entering with multiple keys is high,
// let's let user paste full notes or overwrite with full note names.
// For demo purposes: If user types a letter from A-G, we auto add octave 4
let key = e.key.toUpperCase();
if('ABCDEFG'.includes(key)){
p.data[cursor.row][cursor.col] = key + '-' + '4';
// move cursor right
let newCol = cursor.col+1;
if(newCol >= tracker.instruments.length){
let newRow = cursor.row+1;
if(newRow >= p.rows) newRow = 0;
setCursor(newRow, 0);
} else {
setCursor(cursor.row, newCol);
}
renderPatternWithCursor();
}
}
}
});
// PLAYBACK (VERY BASIC)
// Returns a promise that resolves after note is played
function playNote(inst, freq){
return new Promise(resolve => {
if(!audioCtx) createAudioContext();
if(freq === 0) { resolve(); return; }
let now = audioCtx.currentTime;
const {oscType, volume, detune, adsr, filterFreq, filterQ, noise} = inst;
// Oscillator
const osc = audioCtx.createOscillator();
osc.type = oscType;
osc.frequency.setValueAtTime(freq, now);
osc.detune.setValueAtTime(detune, now);
// Gain for ADSR
const gain = audioCtx.createGain();
gain.gain.setValueAtTime(0, now);
gain.gain.linearRampToValueAtTime(volume, now + adsr.attack);
gain.gain.linearRampToValueAtTime(volume * adsr.sustain, now + adsr.attack + adsr.decay);
// Release after duration will handle outside
// Filter
const filter = audioCtx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.setValueAtTime(filterFreq, now);
filter.Q.setValueAtTime(filterQ, now);
// Noise
let noiseGain = null;
let noiseSource = null;
if(noise > 0){
// Create noise buffer
let bufferSize = audioCtx.sampleRate * 0.1;
let buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
let data = buffer.getChannelData(0);
for(let i=0; i<bufferSize; i++){
data[i] = (Math.random() * 2 - 1) * noise;
}
noiseSource = audioCtx.createBufferSource();
noiseSource.buffer = buffer;
noiseGain = audioCtx.createGain();
noiseGain.gain.value = volume * noise;
noiseSource.connect(noiseGain);
noiseGain.connect(filter);
}
osc.connect(gain);
gain.connect(filter);
filter.connect(gainNode);
if(noise > 0){
noiseSource.start(now);
}
osc.start(now);
// Length of note: full row duration minus release
let rowDuration = (60 / tracker.bpm) * 4 / tracker.rowsPerPattern;
let noteDuration = rowDuration;
// Schedule release envelope
gain.gain.setValueAtTime(gain.gain.value, now + noteDuration);
gain.gain.linearRampToValueAtTime(0, now + noteDuration + adsr.release);
osc.stop(now + noteDuration + adsr.release + 0.05);
if(noiseSource) noiseSource.stop(now + noteDuration + adsr.release + 0.05);
osc.onended = () => {
resolve();
};
});
}
let playInterval;
let playing = false;
async function playPattern(){
if(playing) return;
createAudioContext();
playing = true;
playBtn.disabled = true;
stopBtn.disabled = false;
let p = tracker.patterns[tracker.currentPattern];
let i = 0;
while(playing){
tracker.currentRow = i;
renderPatternWithCursor();
let row = p.data[i];
for(let ch=0; ch<tracker.instruments.length; ch++){
let note = row[ch] || '';
let freq = noteToFrequency(note);
playNote(tracker.instruments[ch], freq);
}
// await row duration
let rowDuration = (60 / tracker.bpm) * 4 / p.rows;
await new Promise(r=> setTimeout(r, rowDuration*1000));
i++;
if(i >= p.rows){
if(tracker.loop){
i=0;
} else {
break;
}
}
}
playing = false;
playBtn.disabled = false;
stopBtn.disabled = true;
}
function stopPattern(){
playing = false;
playBtn.disabled = false;
stopBtn.disabled = true;
tracker.currentRow = 0;
renderPatternWithCursor();
}
// UI EVENT LISTENERS
bpmInput.addEventListener('change', () => {
tracker.bpm = Math.min(Math.max(30, +bpmInput.value), 300);
renderPatternWithCursor();
});
rowsInput.addEventListener('change', () => {
let val = Math.min(Math.max(4, +rowsInput.value), 64);
tracker.rowsPerPattern = val;
tracker.patterns.forEach(p=>{
p.rows = val;
while(p.data.length < val) p.data.push(new Array(tracker.instruments.length).fill(''));
if(p.data.length > val){
p.data = p.data.slice(0,val);
}
});
renderPatternWithCursor();
});
instrumentSelect.addEventListener('change', e => {
let val = +instrumentSelect.value;
renderInstrumentControls(val);
});
addInstrumentBtn.addEventListener('click', () => {
tracker.instruments.push(new Instrument());
tracker.patterns.forEach(p=>{
for(let r=0; r<p.rows; r++){
if(!p.data[r]) p.data[r] = [];
p.data[r].push('');
}
});
updateInstrumentSelect();
renderPatternWithCursor();
});
// Share song
shareBtn.addEventListener('click', () => {
let serial = serializeSong(tracker);
let url = location.origin + location.pathname + '/#' + serial;
navigator.clipboard.writeText(url)
.then(() => {
messageDiv.textContent = 'Song URL copied to clipboard.';
setTimeout(()=> messageDiv.textContent = '', 3000);
});
});
// Play/Stop buttons
playBtn.addEventListener('click', () => {
if(audioCtx && audioCtx.state === 'suspended'){
audioCtx.resume().then(playPattern);
} else {
playPattern();
}
});
stopBtn.addEventListener('click', () => {
stopPattern();
});
// Load from URL hash if present
window.addEventListener('load', () => {
if(location.hash.length > 2){
const hash = location.hash.slice(2);
let loaded = deserializeSong(hash);
if(loaded){
// shallow copy, in practice, deep copy here
Object.assign(tracker, loaded);
// Fix prototype lost in instruments and patterns
tracker.instruments = tracker.instruments.map(i => Object.assign(new Instrument(), i));
tracker.patterns = tracker.patterns.map(p => {
let np = new Pattern(p.rows);
np.data = p.data;
return np;
});
updateInstrumentSelect();
renderPatternWithCursor();
messageDiv.textContent = 'Loaded song from URL.';
setTimeout(()=> messageDiv.textContent = '', 3000);
}
} else {
updateInstrumentSelect();
renderPatternWithCursor();
}
});
patternEditor.addEventListener('focus', () => renderPatternWithCursor());
// Duplicate/Delete/Loop pattern controls
const loopBtn = document.createElement('button');
loopBtn.textContent = 'Toggle Loop';
loopBtn.onclick = () => {
tracker.loop = !tracker.loop;
loopBtn.textContent = tracker.loop ? 'Loop: ON' : 'Loop: OFF';
};
loopBtn.style.marginTop = '1em';
loopBtn.title = 'Toggle loop of pattern playback';
instrumentControls.appendChild(loopBtn);
const dupeBtn = document.createElement('button');
dupeBtn.textContent = 'Duplicate Pattern';
dupeBtn.onclick = () => {
let p = tracker.patterns[tracker.currentPattern];
let newPattern = new Pattern(p.rows);
newPattern.data = p.data.map(row => [...row]);
tracker.patterns.push(newPattern);
tracker.currentPattern = tracker.patterns.length -1;
messageDiv.textContent = `Pattern duplicated. Now editing pattern #${tracker.currentPattern+1}`;
setTimeout(() => messageDiv.textContent = '', 3000);
renderPatternWithCursor();
};
dupeBtn.title = 'Duplicate current pattern';
instrumentControls.appendChild(dupeBtn);
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete Pattern';
deleteBtn.onclick = () => {
if(tracker.patterns.length <= 1){
alert('Cannot delete last pattern.');
return;
}
tracker.patterns.splice(tracker.currentPattern,1);
tracker.currentPattern = 0;
messageDiv.textContent = 'Pattern deleted.';
setTimeout(() => messageDiv.textContent = '', 3000);
renderPatternWithCursor();
};
deleteBtn.title='Delete current pattern';
instrumentControls.appendChild(deleteBtn);
// Keyboard pattern switching shortcuts for convenience
document.addEventListener('keydown', e => {
if(e.target === patternEditor){
// no interfering shortcuts
return;
}
if(e.key === '['){
// Prev pattern
tracker.currentPattern--;
if(tracker.currentPattern < 0) tracker.currentPattern = tracker.patterns.length -1;
renderPatternWithCursor();
e.preventDefault();
} else if(e.key === ']'){
// Next pattern
tracker.currentPattern++;
if(tracker.currentPattern >= tracker.patterns.length) tracker.currentPattern = 0;
renderPatternWithCursor();
e.preventDefault();
} else if(e.key === ' ') {
// Play/Pause toggle
if(playing){
stopPattern();
} else {
playPattern();
}
e.preventDefault();
}
});
</script>
</body>
</html>
How it works:
- Pattern editor is the left pane – you can type notes by focusing it and typing keys
A to G which auto-fill octave 4 and place the note into current instrument column.
- Use arrow keys to navigate rows and instrument columns.
- Press Backspace or Delete to clear a note.
- You can add & select instruments; adjust their oscillator type, volume, detune, ADSR envelope, filter frequency/Q, and noise level.
- Play and Stop buttons start/stop pattern playback (with looping toggle).
- Duplicate and delete pattern buttons to manage patterns.
- Share your song by clicking "Share Song" – it copies a URL with encoded song state in the hash (
/#...).
- On page load, if URL hash contains a song, it automatically loads it.
- Keyboard shortcuts outside the pattern editor:
[ and ] switch patterns
- Space toggles play/pause
Limitations & Notes:
- Only supports one pattern playing at a time.
- Simple note input model: just
A-G keys place NOTE-4 (octave 4).
- More complex note input is possible but would require bigger UI and logic.
- Audio context is created only on playback start.
- Custom oscillator means standard wave types only (no truly custom waveforms).
- Volume and noise are mixed linearly but simplistic.
- No demo autoplay (you have to press Play).
If you want, you can extend this with more advanced features (pattern chaining, effects, custom waveforms, visualization), but this is a solid base for a chiptune tracker in plain HTML/JS.
Try this out by saving it as tracker.html and opening in any modern browser (Chrome, Firefox, Edge).