This example will be very simplified. A full tracker is a big app, this is a starter base you can extend.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple HTML Chiptune Tracker</title>
<style>
body { font-family: monospace; background: #222; color: #eee; padding: 1em; }
.tracker { display: flex; flex-direction: row; }
.pattern, .instruments { margin: 0 1em; }
table { border-collapse: collapse; }
td { border: 1px solid #555; width: 30px; height: 22px; text-align: center; cursor: pointer; }
td.active { background: yellow; color: black; }
input, select { background: #333; color: #eee; border: none; padding: 0.2em; width: 100%; margin-bottom: 0.4em; }
button { margin: 0.5em 0; padding: 0.4em 0.8em; background: #444; border: none; color: #eee; cursor: pointer; }
button:hover { background: #666; }
.controls { margin-top: 1em; }
</style>
</head>
<body>
<h1>Simple HTML Chiptune Tracker</h1>
<div>
<label>BPM: <input id="bpm" type="number" min="30" max="300" value="125"></label>
<label>Rows per Pattern: <input id="rowsPerPattern" type="number" min="4" max="64" value="16"></label>
<button id="playBtn">▶ Play</button>
<button id="stopBtn" disabled>■ Stop</button>
<button id="shareBtn">🔗 Share Song</button>
</div>
<div class="tracker">
<div class="pattern">
<h2>Pattern</h2>
<table id="patternTable"></table>
</div>
<div class="instruments">
<h2>Instruments</h2>
<div>
<label>Oscillator:
<select id="oscType">
<option value="square">Square</option>
<option value="sawtooth">Sawtooth</option>
<option value="triangle">Triangle</option>
<option value="sine">Sine</option>
</select>
</label>
<label>Volume: <input id="volume" type="range" min="0" max="1" step="0.01" value="0.5"></label>
<label>Detune (cents): <input id="detune" type="range" min="-100" max="100" step="1" value="0"></label>
<label>ADSR Attack (sec): <input id="attack" type="range" min="0" max="2" step="0.01" value="0.05"></label>
<label>ADSR Decay (sec): <input id="decay" type="range" min="0" max="2" step="0.01" value="0.1"></label>
<label>ADSR Sustain (0-1): <input id="sustain" type="range" min="0" max="1" step="0.01" value="0.3"></label>
<label>ADSR Release (sec): <input id="release" type="range" min="0" max="3" step="0.01" value="0.3"></label>
<label>Filter cutoff (Hz): <input id="filterCutoff" type="range" min="100" max="10000" step="10" value="7000"></label>
<label><input id="noiseChannel" type="checkbox"> Enable Noise at row</label>
</div>
</div>
</div>
<p>Click cells to toggle note on/off. Keyboard shortcuts: Space=Play/Pause, C=Clear pattern, L=Loop toggle.</p>
<script>
// Basic constants and state
const notes = [
'C4', 'C#4', 'D4', 'D#4', 'E4', 'F4', 'F#4', 'G4',
'G#4', 'A4', 'A#4', 'B4',
]; // 12 notes per octave, here octave4 only for simplicity
// Setup tracking rows and pattern size
let bpmInput = document.getElementById('bpm');
let rowsPerPatternInput = document.getElementById('rowsPerPattern');
let playBtn = document.getElementById('playBtn');
let stopBtn = document.getElementById('stopBtn');
let shareBtn = document.getElementById('shareBtn');
let patternTable = document.getElementById('patternTable');
// Instrument controls
const oscTypeInput = document.getElementById('oscType');
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 filterCutoffInput = document.getElementById('filterCutoff');
const noiseChannelInput = document.getElementById('noiseChannel');
let audioCtx = null;
// State: pattern data as an array of cells per row. Each cell: note index or null.
let patternData = [];
let rowsPerPattern = parseInt(rowsPerPatternInput.value || 16);
// Loop control
let isPlaying = false;
let currentRow = 0;
let scheduleId = null;
let loopEnabled = true;
// Load pattern data from URL hash or initialize empty
function loadPatternFromHash() {
// hash format: /#rowsPerPattern,bpm;note1,note2,... (notes separated by commas)
let hash = location.hash.slice(2);
if(!hash) return false;
try {
let parts = hash.split(';');
if(parts.length < 2) return false;
let settings = parts[0].split(',');
if(settings.length >= 2) {
rowsPerPattern = parseInt(settings[0]);
bpmInput.value = parseInt(settings[1]);
rowsPerPatternInput.value = rowsPerPattern;
}
let notes = parts[1].split(',');
if(notes.length != rowsPerPattern) return false;
patternData = notes.map(n => {
if(n === 'x') return null;
let idx = parseInt(n);
if(isNaN(idx) || idx < 0 || idx >= notes.length) return null;
return idx;
});
return true;
} catch(e) {
return false;
}
}
// Save pattern data to URL hash
function savePatternToHash() {
// format: /#rowsPerPattern,bpm;note1,note2...
let notes = patternData.map(x => x === null ? 'x' : x);
let hashStr = `/${rowsPerPattern},${bpmInput.value};${notes.join(',')}`;
history.replaceState(null, '', hashStr);
alert('Song URL copied! You can share the URL to reproduce this pattern');
}
// Initialize empty pattern data
function initPattern() {
patternData = [];
for(let i=0; i<rowsPerPattern; i++) {
patternData.push(null); // no note by default
}
}
// Build pattern table UI
function buildPatternTable() {
patternTable.innerHTML = '';
let tr = document.createElement('tr');
for(let r=0; r<rowsPerPattern; r++) {
let td = document.createElement('td');
td.dataset.row = r;
td.textContent = patternData[r] !== null ? notes[patternData[r]] : '';
td.addEventListener('click', () => {
// toggle note for simplicity: cycle through notes on repeated clicks
let current = patternData[r];
let next = current === null ? 0 : (current + 1) % notes.length;
patternData[r] = next;
td.textContent = notes[next];
if(current !== null && next === 0) { // allow cycles to null by clicking again
// Actually cycle one step further to null
patternData[r] = null;
td.textContent = '';
}
});
tr.appendChild(td);
}
patternTable.appendChild(tr);
}
// Convert note name to frequency - simple equal temperament
function noteToFreq(note) {
const semitonesMap = {
'C':0,'C#':1,'D':2,'D#':3,'E':4,'F':5,'F#':6,'G':7,'G#':8,'A':9,'A#':10,'B':11
};
let m = note.match(/^([A-G]#?)(\d)$/);
if(!m) return 440;
let noteName = m[1];
let octave = parseInt(m[2]);
let semitoneIndex = semitonesMap[noteName];
let noteIndex = (octave + 1) * 12 + semitoneIndex;
let A4Index = (4+1)*12 + semitonesMap['A'];
return 440 * Math.pow(2, (noteIndex - A4Index)/12);
}
// Play a note on AudioContext
function playNote(freq, time, duration) {
if(!audioCtx) return;
// Create oscillator
let osc = audioCtx.createOscillator();
osc.type = oscTypeInput.value;
osc.frequency.value = freq;
osc.detune.value = parseFloat(detuneInput.value);
// Create gain for ADSR
let gainNode = audioCtx.createGain();
// Create Biquad Filter
let filter = audioCtx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.value = parseFloat(filterCutoffInput.value);
// Connect nodes
osc.connect(filter);
filter.connect(gainNode);
gainNode.connect(audioCtx.destination);
// ADSR setup
let now = time;
let attack = parseFloat(attackInput.value);
let decay = parseFloat(decayInput.value);
let sustain = parseFloat(sustainInput.value);
let release = parseFloat(releaseInput.value);
gainNode.gain.setValueAtTime(0, now);
gainNode.gain.linearRampToValueAtTime(volumeInput.value, now + attack);
gainNode.gain.linearRampToValueAtTime(volumeInput.value * sustain, now + attack + decay);
// sustain until note end - release will be scheduled after duration
osc.start(now);
gainNode.gain.setTargetAtTime(0, now + duration, release / 10); // reasonable release timing
osc.stop(now + duration + release + 0.1);
}
// Play noise for short burst
function playNoise(time, duration) {
if(!audioCtx) return;
let bufferSize = audioCtx.sampleRate * duration;
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) * volumeInput.value;
}
let noiseNode = audioCtx.createBufferSource();
noiseNode.buffer = buffer;
// Filter noise
let filter = audioCtx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.value = parseFloat(filterCutoffInput.value);
noiseNode.connect(filter).connect(audioCtx.destination);
noiseNode.start(time);
noiseNode.stop(time + duration);
}
// Scheduler function to play pattern rows
function schedulePattern() {
if(!isPlaying) return;
let bpm = parseFloat(bpmInput.value);
let beatDuration = 60 / bpm;
let secondsPerRow = beatDuration / 4; // 4 rows per beat approximation
let startTime = audioCtx.currentTime + 0.1;
// Schedule future rows
for(let i=0; i<rowsPerPattern; i++) {
let row = (currentRow + i) % rowsPerPattern;
let noteIdx = patternData[row];
let playTime = startTime + i * secondsPerRow;
if(noteIdx !== null) {
playNote(noteToFreq(notes[noteIdx]), playTime, secondsPerRow * 0.8);
}
if(noiseChannelInput.checked) {
if(row === currentRow) playNoise(playTime, secondsPerRow * 0.5);
}
}
// Update currentRow
currentRow = (currentRow + 1) % rowsPerPattern;
// Schedule next call
scheduleId = setTimeout(schedulePattern, secondsPerRow * 1000);
}
// Play/pause control
function play() {
if (!audioCtx) audioCtx = new AudioContext();
isPlaying = true;
playBtn.disabled = true;
stopBtn.disabled = false;
schedulePattern();
}
function stop() {
isPlaying = false;
playBtn.disabled = false;
stopBtn.disabled = true;
if(scheduleId) {
clearTimeout(scheduleId);
scheduleId = null;
}
currentRow = 0;
}
// Handlers
playBtn.addEventListener('click', play);
stopBtn.addEventListener('click', stop);
shareBtn.addEventListener('click', savePatternToHash);
rowsPerPatternInput.addEventListener('change', () => {
rowsPerPattern = parseInt(rowsPerPatternInput.value);
initPattern();
buildPatternTable();
});
bpmInput.addEventListener('change', () => {
if(isPlaying) {
stop();
play();
}
});
// Keyboard shortcuts
window.addEventListener('keydown', e => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT') return; // ignore typing
if (e.code === 'Space') {
e.preventDefault();
if (isPlaying) stop();
else play();
}
if (e.key.toLowerCase() === 'c') {
e.preventDefault();
initPattern();
buildPatternTable();
}
if (e.key.toLowerCase() === 'l') {
e.preventDefault();
loopEnabled = !loopEnabled;
alert('Loop ' + (loopEnabled ? 'enabled' : 'disabled'));
}
});
// Initialize on load
if(!loadPatternFromHash()) {
rowsPerPattern = parseInt(rowsPerPatternInput.value);
initPattern();
}
buildPatternTable();
</script>
</body>
</html>
If you want something more advanced or specific parts (like multi-track patterns or voice management), just ask!