<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Minimal HTML Chiptune Tracker</title>
<style>
body { font-family: sans-serif; background: #222; color: #eee; }
input, select { margin: 0 5px; }
table { border-collapse: collapse; margin-top: 1em; }
td, th { border: 1px solid #555; padding: 4px; text-align: center; }
.note-cell { cursor: pointer; width: 30px; user-select: none;}
.note-cell.on { background: #4c4; color: black; }
</style>
</head>
<body>
<h1>Minimal HTML Chiptune Tracker</h1>
<div>
BPM: <input id="bpm" type="number" value="120" min="30" max="400" />
Rows per pattern: <input id="rowsPerPattern" type="number" value="16" min="4" max="64" />
Tracks (instruments): <input id="tracksCount" type="number" value="3" min="1" max="8" />
Patterns count: <input id="patternsCount" type="number" value="1" min="1" max="8" />
<button id="applySettings">Apply</button>
</div>
<div>
<button id="playBtn">Play</button>
<button id="stopBtn">Stop</button>
Loop: <input id="loopToggle" type="checkbox" checked />
</div>
<div id="patternEditor"></div>
<div>
<h2>Instrument Synth Settings</h2>
Select Track:
<select id="trackSelect"></select><br/>
Oscillator:
<select id="oscSelect">
<option value="square">Square</option>
<option value="triangle">Triangle</option>
<option value="sawtooth">Sawtooth</option>
<option value="custom">Custom (sine+noise)</option>
</select><br/>
Volume:
<input id="volume" type="range" min="0" max="1" step="0.01" value="0.2" /><br/>
Detune (cents):
<input id="detune" type="number" min="-100" max="100" value="0" /><br/>
ADSR (ms):
Attack <input id="attack" type="number" min="0" max="2000" value="10" />
Decay <input id="decay" type="number" min="0" max="2000" value="50" />
Sustain <input id="sustain" type="number" min="0" max="1" step="0.01" value="0.7" />
Release <input id="release" type="number" min="0" max="2000" value="100" /><br/>
Noise: <input id="noise" type="checkbox" />
</div>
<div>
<button id="copyLink">Copy shareable link</button>
</div>
<script>
// ===== Basic tracker implementation =====
class Tracker {
constructor() {
this.audioCtx = null;
this.bpm = 120;
this.rowsPerPattern = 16;
this.tracksCount = 3;
this.patternsCount = 1;
this.patterns = []; // format: patterns[patternIndex][row][track] = note (0=off, else MIDI note number)
this.instruments = []; // synth params per track
this.isPlaying = false;
this.timerId = null;
this.currentPattern = 0;
this.currentRow = 0;
this.loop = true;
this.noteFrequencies = {}; // MIDI note number => freq
for(let i=0;i<128;i++){
this.noteFrequencies[i] = 440 * Math.pow(2,(i-69)/12);
}
}
initialize() {
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
this.resetPatterns();
this.setupDefaultInstruments();
}
resetPatterns() {
this.patterns = [];
for(let p=0; p < this.patternsCount; p++) {
let pattern = [];
for(let r=0; r < this.rowsPerPattern; r++) {
let row = new Array(this.tracksCount).fill(0);
pattern.push(row);
}
this.patterns.push(pattern);
}
}
setupDefaultInstruments() {
this.instruments = [];
for(let i=0; i < this.tracksCount; i++) {
this.instruments.push({
oscillator: 'square',
volume: 0.2,
detune: 0,
adsr: {attack: 0.01, decay: 0.05, sustain: 0.7, release: 0.1},
noise: false
});
}
}
setBpm(bpm) { this.bpm = bpm; }
setRowsPerPattern(rows) { this.rowsPerPattern = rows; }
setTracksCount(count) { this.tracksCount = count; }
setPatternsCount(count) { this.patternsCount = count; }
play() {
if(this.isPlaying) return;
if(!this.audioCtx) this.initialize();
this.isPlaying = true;
this.currentPattern = 0;
this.currentRow = 0;
this.scheduleStep();
}
stop() {
this.isPlaying = false;
if(this.timerId) clearTimeout(this.timerId);
}
scheduleStep() {
if(!this.isPlaying) return;
this.playRow(this.currentPattern, this.currentRow);
this.currentRow++;
if(this.currentRow >= this.rowsPerPattern) {
this.currentRow = 0;
this.currentPattern++;
if(this.currentPattern >= this.patternsCount) {
if(this.loop) this.currentPattern = 0;
else {
this.stop();
return;
}
}
}
// Calculate ms per row
const msPerBeat = 60000 / this.bpm;
// Assuming 4 rows per beat as default
const msPerRow = msPerBeat / 4;
this.timerId = setTimeout(() => this.scheduleStep(), msPerRow);
}
playRow(patternIndex, rowIndex) {
let row = this.patterns[patternIndex][rowIndex];
for(let track = 0; track < this.tracksCount; track++){
let note = row[track];
if(note > 0){
this.playNote(track, note);
}
}
}
playNote(track, midiNote) {
const freq = this.noteFrequencies[midiNote];
const instrument = this.instruments[track];
const ctx = this.audioCtx;
// Create Oscillator
let osc;
if(instrument.oscillator === 'custom'){
// example custom - use noise + sine
osc = ctx.createOscillator();
osc.type = 'sine';
} else {
osc = ctx.createOscillator();
osc.type = instrument.oscillator;
}
osc.frequency.setValueAtTime(freq, ctx.currentTime);
osc.detune.value = instrument.detune;
// Gain for ADSR envelope and volume
const gainNode = ctx.createGain();
// Noise if enabled
let noiseSource;
if(instrument.noise){
const bufferSize = ctx.sampleRate * 0.1;
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)*0.2;
noiseSource = ctx.createBufferSource();
noiseSource.buffer = noiseBuffer;
noiseSource.loop = true;
noiseSource.start(ctx.currentTime);
}
osc.connect(gainNode);
if(noiseSource){
noiseSource.connect(gainNode);
}
gainNode.connect(ctx.destination);
// ADSR envelope
let now = ctx.currentTime;
const a = instrument.adsr.attack;
const d = instrument.adsr.decay;
const s = instrument.adsr.sustain;
const r = instrument.adsr.release;
gainNode.gain.setValueAtTime(0, now);
gainNode.gain.linearRampToValueAtTime(instrument.volume, now + a);
gainNode.gain.linearRampToValueAtTime(instrument.volume * s, now + a + d);
const noteDuration = 0.3; // 300 ms note length before release
gainNode.gain.setValueAtTime(instrument.volume * s, now + a + d + noteDuration);
gainNode.gain.linearRampToValueAtTime(0, now + a + d + noteDuration + r);
osc.start(now);
osc.stop(now + a + d + noteDuration + r);
if(noiseSource){
noiseSource.stop(now + a + d + noteDuration + r);
}
}
}
// ===== UI Logic ======
const tracker = new Tracker();
tracker.initialize();
const bpmInput = document.getElementById('bpm');
const rowsInput = document.getElementById('rowsPerPattern');
const tracksInput = document.getElementById('tracksCount');
const patternsInput = document.getElementById('patternsCount');
const applyBtn = document.getElementById('applySettings');
const playBtn = document.getElementById('playBtn');
const stopBtn = document.getElementById('stopBtn');
const loopToggle = document.getElementById('loopToggle');
const patternEditor = document.getElementById('patternEditor');
const trackSelect = document.getElementById('trackSelect');
const oscSelect = document.getElementById('oscSelect');
const volumeInput = document.getElementById('volume');
const detuneInput = document.getElementById('detune');
const attackInput = document.getElementById('attack');
const decayInput = document.getElementById('decay');
const sustainInput = document.getElementById('sustain');
const releaseInput = document.getElementById('release');
const noiseToggle = document.getElementById('noise');
const copyLinkBtn = document.getElementById('copyLink');
function buildPatternEditor() {
patternEditor.innerHTML = '';
const table = document.createElement('table');
const headerRow = document.createElement('tr');
headerRow.innerHTML = '<th>Row</th>';
for(let t=0; t<tracker.tracksCount; t++){
const th = document.createElement('th');
th.textContent = 'Track ' + (t+1);
headerRow.appendChild(th);
}
table.appendChild(headerRow);
for(let r=0; r < tracker.rowsPerPattern; r++){
const tr = document.createElement('tr');
const rowNumberCell = document.createElement('td');
rowNumberCell.textContent = r+1;
tr.appendChild(rowNumberCell);
for(let t=0; t < tracker.tracksCount; t++){
const td = document.createElement('td');
td.classList.add('note-cell');
td.dataset.row = r;
td.dataset.track = t;
td.textContent = '-';
td.addEventListener('click', () => {
// Cycle note from off to some notes
const currentNote = tracker.patterns[0][r][t];
let newNote;
if(currentNote === 0) newNote = 60; // C4
else if(currentNote === 60) newNote = 62; // D4
else if(currentNote === 62) newNote = 64; // E4
else if(currentNote === 64) newNote = 67; // G4
else newNote = 0;
tracker.patterns[0][r][t] = newNote;
updatePatternEditorCell(td, newNote);
});
tr.appendChild(td);
}
table.appendChild(tr);
}
patternEditor.appendChild(table);
}
function updatePatternEditorCell(cell, note) {
if(note === 0){
cell.classList.remove('on');
cell.textContent = '-';
} else {
cell.classList.add('on');
cell.textContent = midiNoteToName(note);
}
}
function midiNoteToName(midi) {
const notes = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'];
const octave = Math.floor(midi / 12) -1;
const name = notes[midi % 12];
return name + octave;
}
function refreshPatternEditor() {
const cells = patternEditor.querySelectorAll('.note-cell');
cells.forEach(cell => {
const r = Number(cell.dataset.row);
const t = Number(cell.dataset.track);
const note = tracker.patterns[0][r][t];
updatePatternEditorCell(cell, note);
});
}
function populateTrackSelect() {
trackSelect.innerHTML = '';
for(let i=0; i < tracker.tracksCount; i++){
const option = document.createElement('option');
option.value = i;
option.textContent = 'Track ' + (i+1);
trackSelect.appendChild(option);
}
}
function updateInstrumentUI(track) {
const inst = tracker.instruments[track];
oscSelect.value = inst.oscillator;
volumeInput.value = inst.volume;
detuneInput.value = inst.detune;
attackInput.value = inst.adsr.attack * 1000;
decayInput.value = inst.adsr.decay * 1000;
sustainInput.value = inst.adsr.sustain;
releaseInput.value = inst.adsr.release * 1000;
noiseToggle.checked = inst.noise;
}
function saveInstrumentParams(track) {
const inst = tracker.instruments[track];
inst.oscillator = oscSelect.value;
inst.volume = parseFloat(volumeInput.value);
inst.detune = parseFloat(detuneInput.value);
inst.adsr.attack = parseFloat(attackInput.value)/1000;
inst.adsr.decay = parseFloat(decayInput.value)/1000;
inst.adsr.sustain = parseFloat(sustainInput.value);
inst.adsr.release = parseFloat(releaseInput.value)/1000;
inst.noise = noiseToggle.checked;
}
applyBtn.onclick = () => {
tracker.setBpm(Number(bpmInput.value));
tracker.setRowsPerPattern(Number(rowsInput.value));
tracker.setTracksCount(Number(tracksInput.value));
tracker.setPatternsCount(Number(patternsInput.value));
tracker.resetPatterns();
tracker.setupDefaultInstruments();
populateTrackSelect();
buildPatternEditor();
refreshPatternEditor();
updateInstrumentUI(0);
};
playBtn.onclick = () => tracker.play();
stopBtn.onclick = () => tracker.stop();
loopToggle.onchange = () => tracker.loop = loopToggle.checked;
trackSelect.onchange = () => {
saveInstrumentParams(Number(trackSelect.value ^ 0));
updateInstrumentUI(Number(trackSelect.value));
};
oscSelect.onchange = () => {
saveInstrumentParams(Number(trackSelect.value));
updateInstrumentUI(Number(trackSelect.value));
};
volumeInput.oninput = () => {
saveInstrumentParams(Number(trackSelect.value));
};
detuneInput.onchange = () => {
saveInstrumentParams(Number(trackSelect.value));
};
attackInput.oninput = () => {
saveInstrumentParams(Number(trackSelect.value));
};
decayInput.oninput = () => {
saveInstrumentParams(Number(trackSelect.value));
};
sustainInput.oninput = () => {
saveInstrumentParams(Number(trackSelect.value));
};
releaseInput.oninput = () => {
saveInstrumentParams(Number(trackSelect.value));
};
noiseToggle.onchange = () => {
saveInstrumentParams(Number(trackSelect.value));
};
// Share song by encoding state in URL hash
function encodeSongState(){
let state = {
bpm: tracker.bpm,
rows: tracker.rowsPerPattern,
tracks: tracker.tracksCount,
patterns: tracker.patternsCount,
loop: tracker.loop,
patternsData: tracker.patterns,
instruments: tracker.instruments
};
return encodeURIComponent(JSON.stringify(state));
}
function decodeSongState(str){
try {
return JSON.parse(decodeURIComponent(str));
} catch(e) {
return null;
}
}
copyLinkBtn.onclick = () => {
saveInstrumentParams(Number(trackSelect.value));
const encoded = encodeSongState();
const url = window.location.origin + window.location.pathname + '/#' + encoded;
navigator.clipboard.writeText(url);
alert('Song link copied! Paste to load.');
};
// On page load, try to load shared song
window.onload = () => {
const hash = window.location.hash.slice(1);
if(hash){
const state = decodeSongState(hash);
if(state){
tracker.bpm = state.bpm || 120;
tracker.rowsPerPattern = state.rows || 16;
tracker.tracksCount = state.tracks || 3;
tracker.patternsCount = state.patterns || 1;
tracker.loop = state.loop !== undefined ? state.loop : true;
tracker.patterns = state.patternsData || tracker.patterns;
tracker.instruments = state.instruments || tracker.instruments;
bpmInput.value = tracker.bpm;
rowsInput.value = tracker.rowsPerPattern;
tracksInput.value = tracker.tracksCount;
patternsInput.value = tracker.patternsCount;
loopToggle.checked = tracker.loop;
populateTrackSelect();
buildPatternEditor();
refreshPatternEditor();
updateInstrumentUI(0);
} else {
applyBtn.click();
}
} else {
applyBtn.click();
}
};
</script>
</body>
</html>