I’ll provide a minimal but functional example of a chiptune tracker app using HTML/CSS/JS. This version will be extendable and cover:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Chiptune Tracker</title>
<style>
body {
font-family: monospace, monospace;
background: #111;
color: #eee;
margin: 0; padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
header, footer {
background: #222;
padding: 8px 16px;
}
main {
flex: 1;
display: flex;
overflow: hidden;
}
#pattern-editor {
flex: 1;
overflow: auto;
border-right: 1px solid #444;
background: #222;
display: grid;
grid-template-columns: 50px repeat(4, 60px);
user-select: none;
}
#pattern-editor div {
border-bottom: 1px solid #333;
border-right: 1px solid #333;
line-height: 1.3;
padding: 2px 4px;
text-align: center;
cursor: pointer;
}
#pattern-editor div.header {
background: #333;
font-weight: bold;
cursor: default;
}
#pattern-editor div.selected {
background: #0a74da;
color: #fff;
}
#pattern-editor div.editing {
outline: 1px solid #fff;
background: #1256b0;
}
#controls-panel {
width: 320px;
background: #222;
padding: 12px;
overflow-y: auto;
}
label, select, input {
display: block;
margin-bottom: 8px;
font-size: 14px;
}
input[type=range], input[type=number] {
width: 100%;
}
button {
background: #0a74da;
border: none;
color: white;
padding: 6px 12px;
margin: 4px 2px;
cursor: pointer;
font-weight: bold;
}
button:disabled {
background: #444;
cursor: default;
}
#share-area {
background: #111;
border: 1px solid #333;
padding: 4px;
font-size: 12px;
margin-top: 12px;
width: 100%;
box-sizing: border-box;
color: #eee;
}
</style>
</head>
<body>
<header>
<h1>Chiptune Tracker</h1>
</header>
<main>
<div id="pattern-editor" tabindex="0" aria-label="Pattern Editor"></div>
<div id="controls-panel">
<h3>Song Controls</h3>
<label for="bpm">BPM:</label>
<input type="number" id="bpm" min="30" max="300" value="120" />
<label for="pattern-size">Rows per Pattern:</label>
<input type="number" id="pattern-size" min="8" max="64" step="8" value="16"/>
<h3>Playback</h3>
<button id="play-btn">Play</button>
<button id="stop-btn" disabled>Stop</button>
<button id="loop-btn">Toggle Loop</button>
<div>Looping: <span id="loop-status">Off</span></div>
<h3>Instrument Settings</h3>
<label for="inst-select">Instrument:</label>
<select id="inst-select"></select>
<label for="waveform-select">Waveform:</label>
<select id="waveform-select">
<option value="sine">Sine</option>
<option value="square" selected>Square</option>
<option value="triangle">Triangle</option>
<option value="sawtooth">Sawtooth</option>
</select>
<label for="volume">Volume:</label>
<input type="range" id="volume" min="0" max="1" step="0.01" value="0.7" />
<label for="detune">Detune (cents):</label>
<input type="range" id="detune" min="-1200" max="1200" step="1" value="0" />
<label>ADSR Envelope</label>
<label for="attack">Attack (s):</label>
<input type="range" id="attack" min="0" max="1" step="0.01" value="0.1" />
<label for="decay">Decay (s):</label>
<input type="range" id="decay" min="0" max="1" step="0.01" value="0.1" />
<label for="sustain">Sustain (0-1):</label>
<input type="range" id="sustain" min="0" max="1" step="0.01" value="0.7" />
<label for="release">Release (s):</label>
<input type="range" id="release" min="0" max="2" step="0.01" value="0.2" />
<label for="filter-freq">Filter Cutoff (Hz):</label>
<input type="range" id="filter-freq" min="20" max="10000" step="1" value="8000" />
<label for="noise-amount">Noise Amount:</label>
<input type="range" id="noise-amount" min="0" max="1" step="0.01" value="0"/>
<button id="add-instrument-btn">Add Instrument</button>
<button id="remove-instrument-btn" disabled>Remove Instrument</button>
<h3>Patterns</h3>
<button id="copy-pattern-btn">Copy Pattern</button>
<button id="paste-pattern-btn">Paste Pattern</button>
<button id="duplicate-pattern-btn">Duplicate Pattern</button>
<button id="delete-pattern-btn" disabled>Delete Pattern</button>
<h3>Share Song</h3>
<textarea id="share-area" readonly rows="4"></textarea>
<button id="copy-share-btn">Copy Share Link</button>
</div>
</main>
<footer>
<small>Use arrow keys to navigate, enter to edit notes (C#4, D-5, etc). Use buttons or keyboard shortcuts (Ctrl+C: copy, Ctrl+V: paste, Ctrl+D: duplicate, Del: delete pattern)</small>
</footer>
<script>
// Constants and Utilities
const NOTE_NAMES = ['C-', 'C#', 'D-', 'D#', 'E-', 'F-', 'F#', 'G-', 'G#', 'A-', 'A#', 'B-'];
const NOTES_OCTAVE_MIN = 0;
const NOTES_OCTAVE_MAX = 8;
// Default Instrument
const DEFAULT_INST = {
name: 'Inst 1',
waveform: 'square',
volume: 0.7,
detune: 0,
adsr: { attack: 0.1, decay: 0.1, sustain: 0.7, release: 0.2 },
filterFreq: 8000,
noiseAmount: 0,
};
// Note helper - parse and format
function noteToFreq(note) {
// note: e.g. "C-4", returns frequency in Hz
if (!note || note.length < 2) return null;
const n = note.slice(0,2);
const octave = Number(note.slice(2));
const noteIndex = NOTE_NAMES.indexOf(n);
if(noteIndex < 0 || isNaN(octave)) return null;
// MIDI note number for C-0 = 12
const midi = 12 + octave*12 + noteIndex;
return 440 * Math.pow(2, (midi - 69) / 12);
}
function freqToNote(freq) {
if (!freq) return null;
const midi = Math.round(12 * (Math.log2(freq/440)) + 69);
const octave = Math.floor(midi/12) - 1;
const noteIndex = midi % 12;
return NOTE_NAMES[noteIndex] + octave;
}
function isValidNote(note) {
if(!note) return false;
if(note.toLowerCase() === '---') return true; // no note
if(note.length < 3) return false;
const base = note.slice(0,2);
const octave = note.slice(2);
if(!NOTE_NAMES.includes(base)) return false;
const octNum = Number(octave);
if(isNaN(octNum) || octNum < NOTES_OCTAVE_MIN || octNum > NOTES_OCTAVE_MAX) return false;
return true;
}
// --- Tracker Data Model ---
class Instrument {
constructor(id, data) {
this.id = id;
this.name = data.name || `Inst ${id+1}`;
this.waveform = data.waveform || 'square';
this.volume = data.volume != null ? data.volume : 0.7;
this.detune = data.detune || 0;
this.adsr = Object.assign({attack:0.1,decay:0.1,sustain:0.7,release:0.2}, data.adsr || {});
this.filterFreq = data.filterFreq != null ? data.filterFreq : 8000;
this.noiseAmount = data.noiseAmount || 0;
}
}
class Pattern {
constructor(rows, channels) {
this.rows = rows;
this.channels = channels;
// pattern data: for each row an array of objects {note, chord, inst}
this.data = [];
for(let r=0;r<rows;r++){
let row = [];
for(let c=0;c<channels;c++){
// empty cell
row.push({ note: '---', inst: 0 });
}
this.data.push(row);
}
}
clone() {
const copy = new Pattern(this.rows, this.channels);
copy.data = this.data.map(row => row.map(cell => ({...cell})));
return copy;
}
}
class Song {
constructor(bpm=120, rowsPerPattern=16, channels=4) {
this.bpm = bpm;
this.rowsPerPattern = rowsPerPattern;
this.channels = channels;
this.instruments = [new Instrument(0, DEFAULT_INST)];
this.patterns = [new Pattern(rowsPerPattern, channels)];
this.currentPatternIndex = 0;
this.loop = false;
}
serialize() {
// Encode song to JSON string (compact)
return btoa(JSON.stringify({
bpm: this.bpm,
rowsPerPattern: this.rowsPerPattern,
channels: this.channels,
instruments: this.instruments,
patterns: this.patterns.map(p => p.data),
currentPatternIndex: this.currentPatternIndex,
loop: this.loop,
}));
}
static deserialize(str) {
try {
const obj = JSON.parse(atob(str));
const song = new Song(obj.bpm, obj.rowsPerPattern, obj.channels);
song.instruments = obj.instruments.map((x,i)=>new Instrument(i, x));
song.patterns = obj.patterns.map(d => {
const p = new Pattern(obj.rowsPerPattern, obj.channels);
p.data = d;
return p;
});
song.currentPatternIndex = obj.currentPatternIndex || 0;
song.loop = obj.loop || false;
return song;
} catch(e) {
return null;
}
}
}
// --- Audio Synth ---
class Synth {
constructor(audioCtx, instrument) {
this.ctx = audioCtx;
this.instrument = instrument;
}
playNote(freq, startTime, duration) {
if(!freq) return;
const ctx = this.ctx;
const osc = ctx.createOscillator();
osc.type = this.instrument.waveform;
osc.frequency.setValueAtTime(freq, startTime);
osc.detune.setValueAtTime(this.instrument.detune, startTime);
const gainNode = ctx.createGain();
// ADSR envelope applied to gain
let atkEnd = startTime + this.instrument.adsr.attack;
gainNode.gain.setValueAtTime(0.001, startTime);
gainNode.gain.linearRampToValueAtTime(this.instrument.volume, atkEnd);
let decayEnd = atkEnd + this.instrument.adsr.decay;
gainNode.gain.linearRampToValueAtTime(this.instrument.volume * this.instrument.adsr.sustain, decayEnd);
let releaseStart = startTime + duration;
gainNode.gain.setValueAtTime(this.instrument.volume * this.instrument.adsr.sustain, releaseStart);
gainNode.gain.linearRampToValueAtTime(0.001, releaseStart + this.instrument.adsr.release);
// Filter
const filter = ctx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.setValueAtTime(this.instrument.filterFreq, startTime);
// Noise (white noise)
let noiseNode = null;
if(this.instrument.noiseAmount > 0){
const bufferSize = ctx.sampleRate * duration;
const noiseBuffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
let output = noiseBuffer.getChannelData(0);
for(let i=0; i<bufferSize; i++){
output[i] = (Math.random() * 2 - 1) * this.instrument.noiseAmount * this.instrument.volume;
}
noiseNode = ctx.createBufferSource();
noiseNode.buffer = noiseBuffer;
noiseNode.loop = false;
const noiseGain = ctx.createGain();
noiseGain.gain.setValueAtTime(1, startTime);
noiseGain.gain.linearRampToValueAtTime(0, startTime + duration + this.instrument.adsr.release);
noiseNode.connect(noiseGain).connect(filter).connect(gainNode).connect(ctx.destination);
noiseNode.start(startTime);
noiseNode.stop(startTime + duration + this.instrument.adsr.release);
}
osc.connect(filter).connect(gainNode).connect(ctx.destination);
osc.start(startTime);
osc.stop(startTime + duration + this.instrument.adsr.release);
}
}
// --- UI and App Logic ---
const patternEditorElem = document.getElementById('pattern-editor');
const instSelectElem = document.getElementById('inst-select');
const waveformSelectElem = document.getElementById('waveform-select');
const bpmElem = document.getElementById('bpm');
const volumeElem = document.getElementById('volume');
const detuneElem = document.getElementById('detune');
const attackElem = document.getElementById('attack');
const decayElem = document.getElementById('decay');
const sustainElem = document.getElementById('sustain');
const releaseElem = document.getElementById('release');
const filterFreqElem = document.getElementById('filter-freq');
const noiseAmountElem = document.getElementById('noise-amount');
const playBtn = document.getElementById('play-btn');
const stopBtn = document.getElementById('stop-btn');
const loopBtn = document.getElementById('loop-btn');
const loopStatusElem = document.getElementById('loop-status');
const addInstBtn = document.getElementById('add-instrument-btn');
const removeInstBtn = document.getElementById('remove-instrument-btn');
const patternSizeElem = document.getElementById('pattern-size');
const copyPatternBtn = document.getElementById('copy-pattern-btn');
const pastePatternBtn = document.getElementById('paste-pattern-btn');
const duplicatePatternBtn = document.getElementById('duplicate-pattern-btn');
const deletePatternBtn = document.getElementById('delete-pattern-btn');
const shareArea = document.getElementById('share-area');
const copyShareBtn = document.getElementById('copy-share-btn');
let audioCtx = null;
let song = new Song();
let currentInstrumentIndex = 0;
let isPlaying = false;
let playTimer = null;
let playRow = 0;
let copyBuffer = null;
// Editor selection state
let sel = {patternIndex: 0, row: 0, col: 0};
const colsCount = 4; // channels
function buildPatternEditor() {
patternEditorElem.innerHTML = '';
// header row with col labels
patternEditorElem.style.gridTemplateColumns = `50px repeat(${colsCount}, 60px)`;
patternEditorElem.appendChild(createDiv('Row', 'header'));
for(let c=0;c<colsCount;c++) {
patternEditorElem.appendChild(createDiv(`Ch${c+1}`, 'header'));
}
// pattern rows
const pat = song.patterns[song.currentPatternIndex];
for(let r=0; r<pat.rows; r++) {
patternEditorElem.appendChild(createDiv(String(r+1).padStart(2,'0'), 'rownum'));
for(let c=0; c<colsCount; c++) {
const cellData = pat.data[r][c];
const cellDiv = createDiv(cellData.note, 'note-cell');
cellDiv.dataset.row = r;
cellDiv.dataset.col = c;
cellDiv.tabIndex = 0;
if(r === sel.row && c === sel.col) cellDiv.classList.add('selected');
cellDiv.addEventListener('click', () => {
sel.row = r;
sel.col = c;
sel.patternIndex = song.currentPatternIndex;
updatePatternEditorSelection();
});
patternEditorElem.appendChild(cellDiv);
}
}
}
function createDiv(text, className) {
const div = document.createElement('div');
div.textContent = text;
if(className) div.classList.add(className);
return div;
}
function updatePatternEditorSelection() {
[...patternEditorElem.querySelectorAll('div.note-cell')].forEach(div => {
div.classList.toggle('selected',
+div.dataset.row === sel.row && +div.dataset.col === sel.col && song.currentPatternIndex === sel.patternIndex);
});
}
function updateInstrumentList() {
instSelectElem.innerHTML = '';
song.instruments.forEach((inst, i) => {
const opt = document.createElement('option');
opt.value = i;
opt.textContent = inst.name;
instSelectElem.appendChild(opt);
});
instSelectElem.value = currentInstrumentIndex;
}
function updateInstrumentControls() {
const inst = song.instruments[currentInstrumentIndex];
waveformSelectElem.value = inst.waveform;
volumeElem.value = inst.volume;
detuneElem.value = inst.detune;
attackElem.value = inst.adsr.attack;
decayElem.value = inst.adsr.decay;
sustainElem.value = inst.adsr.sustain;
releaseElem.value = inst.adsr.release;
filterFreqElem.value = inst.filterFreq;
noiseAmountElem.value = inst.noiseAmount;
}
function saveInstrumentControls() {
const inst = song.instruments[currentInstrumentIndex];
inst.waveform = waveformSelectElem.value;
inst.volume = +volumeElem.value;
inst.detune = +detuneElem.value;
inst.adsr.attack = +attackElem.value;
inst.adsr.decay = +decayElem.value;
inst.adsr.sustain = +sustainElem.value;
inst.adsr.release = +releaseElem.value;
inst.filterFreq = +filterFreqElem.value;
inst.noiseAmount = +noiseAmountElem.value;
}
function updateSongParams() {
song.bpm = +bpmElem.value;
song.rowsPerPattern = +patternSizeElem.value;
}
function playSong() {
if (isPlaying) return;
if(!audioCtx) audioCtx = new AudioContext();
isPlaying = true;
playBtn.disabled = true;
stopBtn.disabled = false;
const pat = song.patterns[song.currentPatternIndex];
const synths = song.instruments.map(inst => new Synth(audioCtx, inst));
const secondsPerRow = 60 / (song.bpm * 4);
playRow = 0;
function scheduleRow(r) {
if(r >= pat.rows){
if (song.loop) {
playRow = 0;
scheduleRow(playRow);
} else {
stopSong();
return;
}
}
const startTime = audioCtx.currentTime + 0.05;
for(let c=0; c<colsCount; c++) {
const cell = pat.data[r][c];
if(isValidNote(cell.note) && cell.note !== '---'){
const freq = noteToFreq(cell.note);
const synth = synths[cell.inst || 0];
if(synth && freq) {
synth.playNote(freq, startTime, secondsPerRow*0.9);
}
}
}
updateVisualPlayPosition(r);
playRow++;
playTimer = setTimeout(() => scheduleRow(playRow), secondsPerRow * 1000);
}
scheduleRow(playRow);
}
function stopSong() {
if(!isPlaying) return;
isPlaying = false;
playBtn.disabled = false;
stopBtn.disabled = true;
clearTimeout(playTimer);
updateVisualPlayPosition(-1);
}
function updateVisualPlayPosition(row) {
[...patternEditorElem.querySelectorAll('div.note-cell')].forEach(div => {
div.classList.toggle('playing', +div.dataset.row === row);
});
}
function handleNoteInput(note, row, col) {
const pat = song.patterns[song.currentPatternIndex];
if(row < 0 || row >= pat.rows || col < 0 || col >= pat.channels) return;
note = note.toUpperCase();
if(note === '' || note === '---' || isValidNote(note)) {
pat.data[row][col].note = note === '' ? '---' : note;
}
buildPatternEditor();
}
// -- Event Listeners --
patternEditorElem.addEventListener('keydown', e => {
const pat = song.patterns[song.currentPatternIndex];
switch(e.key) {
case 'ArrowUp':
sel.row = (sel.row - 1 + pat.rows) % pat.rows;
e.preventDefault();
break;
case 'ArrowDown':
sel.row = (sel.row + 1) % pat.rows;
e.preventDefault();
break;
case 'ArrowLeft':
sel.col = (sel.col - 1 + pat.channels) % pat.channels;
e.preventDefault();
break;
case 'ArrowRight':
sel.col = (sel.col + 1) % pat.channels;
e.preventDefault();
break;
case 'Enter':
// Enter edit mode: simple prompt
const oldNote = pat.data[sel.row][sel.col].note;
const newNote = prompt('Enter note (e.g. C-4, D#5, or --- for empty):', oldNote);
if(newNote !== null) {
handleNoteInput(newNote, sel.row, sel.col);
}
e.preventDefault();
break;
case 'Delete':
deletePattern();
e.preventDefault();
break;
case 'c':
case 'C':
if(e.ctrlKey) {
copyPattern();
e.preventDefault();
}
break;
case 'v':
case 'V':
if(e.ctrlKey) {
pastePattern();
e.preventDefault();
}
break;
case 'd':
case 'D':
if(e.ctrlKey) {
duplicatePattern();
e.preventDefault();
}
break;
}
updatePatternEditorSelection();
buildPatternEditor();
});
bpmElem.addEventListener('change', () => {
updateSongParams();
});
patternSizeElem.addEventListener('change', () => {
updateSongParams();
// Adjust all patterns length
song.patterns.forEach(p => {
if(p.rows !== song.rowsPerPattern){
// extend or truncate $p.data
const diff = song.rowsPerPattern - p.rows;
if(diff > 0){
for(let i=0;i<diff;i++){
const newRow = [];
for(let c=0;c<p.channels;c++) newRow.push({note:'---',inst:0});
p.data.push(newRow);
}
} else if(diff < 0){
p.data.splice(diff);
}
p.rows = song.rowsPerPattern;
}
});
buildPatternEditor();
});
instSelectElem.addEventListener('change', () => {
saveInstrumentControls();
currentInstrumentIndex = +instSelectElem.value;
updateInstrumentControls();
});
waveformSelectElem.addEventListener('change', () => {
saveInstrumentControls();
});
volumeElem.addEventListener('input', () => {
saveInstrumentControls();
});
detuneElem.addEventListener('input', () => {
saveInstrumentControls();
});
attackElem.addEventListener('input', () => {
saveInstrumentControls();
});
decayElem.addEventListener('input', () => {
saveInstrumentControls();
});
sustainElem.addEventListener('input', () => {
saveInstrumentControls();
});
releaseElem.addEventListener('input', () => {
saveInstrumentControls();
});
filterFreqElem.addEventListener('input', () => {
saveInstrumentControls();
});
noiseAmountElem.addEventListener('input', () => {
saveInstrumentControls();
});
playBtn.addEventListener('click', () => {
playSong();
});
stopBtn.addEventListener('click', () => {
stopSong();
});
loopBtn.addEventListener('click', () => {
song.loop = !song.loop;
loopStatusElem.textContent = song.loop ? 'On' : 'Off';
});
// Instruments add/remove
addInstBtn.addEventListener('click', () => {
const newInst = new Instrument(song.instruments.length, DEFAULT_INST);
song.instruments.push(newInst);
currentInstrumentIndex = song.instruments.length -1;
updateInstrumentList();
updateInstrumentControls();
removeInstBtn.disabled = song.instruments.length <= 1;
});
removeInstBtn.addEventListener('click', () => {
if(song.instruments.length > 1){
song.instruments.splice(currentInstrumentIndex,1);
currentInstrumentIndex = 0;
updateInstrumentList();
updateInstrumentControls();
removeInstBtn.disabled = song.instruments.length <= 1;
}
});
// Pattern copy/paste/dup/delete
function copyPattern() {
copyBuffer = song.patterns[song.currentPatternIndex].clone();
alert('Pattern copied');
}
function pastePattern() {
if(copyBuffer){
song.patterns[song.currentPatternIndex] = copyBuffer.clone();
buildPatternEditor();
}
}
function duplicatePattern() {
const newPattern = song.patterns[song.currentPatternIndex].clone();
song.patterns.push(newPattern);
song.currentPatternIndex = song.patterns.length - 1;
sel.patternIndex = song.currentPatternIndex;
sel.row = 0; sel.col=0;
buildPatternEditor();
updateDeletePatternBtn();
}
function deletePattern() {
if(song.patterns.length <= 1) {
alert('Cannot delete last pattern');
return;
}
song.patterns.splice(song.currentPatternIndex,1);
song.currentPatternIndex = Math.max(0, song.currentPatternIndex-1);
sel.patternIndex = song.currentPatternIndex;
sel.row = 0; sel.col=0;
buildPatternEditor();
updateDeletePatternBtn();
}
copyPatternBtn.addEventListener('click', copyPattern);
pastePatternBtn.addEventListener('click', pastePattern);
duplicatePatternBtn.addEventListener('click', duplicatePattern);
deletePatternBtn.addEventListener('click', deletePattern);
function updateDeletePatternBtn() {
deletePatternBtn.disabled = song.patterns.length <= 1;
}
updateDeletePatternBtn();
// Share link functionality
function updateShareArea() {
const ser = song.serialize();
const url = location.origin + location.pathname + '#'+ ser;
shareArea.value = url;
}
copyShareBtn.addEventListener('click', () => {
shareArea.select();
document.execCommand('copy');
alert('Share link copied to clipboard!');
});
window.addEventListener('hashchange', () => {
const hash = location.hash.slice(1);
if(hash) {
const loadedSong = Song.deserialize(hash);
if(loadedSong) {
song = loadedSong;
currentInstrumentIndex = 0;
sel.patternIndex = song.currentPatternIndex;
sel.row = 0; sel.col = 0;
bpmElem.value = song.bpm;
patternSizeElem.value = song.rowsPerPattern;
loopStatusElem.textContent = song.loop ? 'On' : 'Off';
updateInstrumentList();
updateInstrumentControls();
buildPatternEditor();
alert('Loaded song from URL');
}
}
});
// On load
function init() {
updateInstrumentList();
updateInstrumentControls();
buildPatternEditor();
loopStatusElem.textContent = song.loop ? 'On' : 'Off';
updateDeletePatternBtn();
updateShareArea();
}
init();
// Update share link when something changes
[
bpmElem, patternSizeElem,
waveformSelectElem, volumeElem, detuneElem,
attackElem, decayElem, sustainElem, releaseElem,
filterFreqElem, noiseAmountElem,
instSelectElem
].forEach(el => {
el.addEventListener('input', updateShareArea);
el.addEventListener('change', updateShareArea);
});
patternEditorElem.addEventListener('keydown', updateShareArea);
</script>
</body>
</html>