Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

html chiptune tracker bpm rows per tracks patterns rows per pattern custom osc volume…

  1. User

    html chiptune tracker bpm rows per tracks patterns rows per pattern custom osc volume detune adsr filter noise share shortcuts "/#" no demo

  2. Assistant

    It looks like you want an HTML chiptune tracker that supports features like:

    • BPM control
    • Rows per track
    • Patterns
    • Rows per pattern
    • Custom oscillator options
    • Volume & detune control
    • ADSR envelope
    • Filter and noise
    • Sharing via URL hash (/#)
    • Keyboard shortcuts
    • And you want it without a demo (likely meaning minimal UI or no preset sounds)

    I can help you build a basic prototype of such a chiptune tracker using Web Audio API and HTML/JS. Due to complexity, I'll provide a foundation with the features you asked for — you can expand/customize it later.


    Basic Chiptune Tracker Prototype 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; display:flex; flex-direction: column; align-items: center; margin:0; padding:1rem; }
      #tracker { display:flex; flex-wrap: wrap; max-width: 800px; }
      .track {
        border: 1px solid #555;
        background: #111;
        margin: 0.5rem;
        padding: 0.5rem;
        width: 180px;
        display: flex;
        flex-direction: column;
      }
      input[type=number], select {
        width: 100%;
        margin-bottom: 0.5rem;
      }
      label {
        font-size: 0.8rem;
      }
      button {
        margin-top: 0.5rem;
      }
      #playBtn {
        margin-top: 1rem;
        padding: 0.5rem 1rem;
      }
      #shareLink {
        margin-top: 1rem;
        width: 100%;
        word-break: break-all;
        background: #333;
        padding: 0.3rem;
      }
    </style>
    </head>
    <body>
    
    <h1>HTML Chiptune Tracker</h1>
    
    <div>
      <label for="bpm">BPM:</label>
      <input type="number" id="bpm" value="120" min="30" max="300" />
      <label for="patternRows">Rows / Pattern:</label>
      <input type="number" id="patternRows" value="16" min="4" max="64" />
      <label for="tracksCount">Tracks:</label>
      <input type="number" id="tracksCount" value="3" min="1" max="8" />
      <button id="initTracks">Init Tracks</button>
    </div>
    
    <div id="tracker"></div>
    
    <button id="playBtn">Play</button>
    <button id="stopBtn" disabled>Stop</button>
    <button id="shareBtn">Share Pattern</button>
    
    <textarea id="shareLink" readonly placeholder="Share link will appear here"></textarea>
    
    <script>
    (() => {
      const AudioContext = window.AudioContext || window.webkitAudioContext;
      let ctx = null;
      let isPlaying = false;
      let timerId = null;
      let currentRow = 0;
    
      // Params for tracker
      let bpm = 120;
      let rowsPerPattern = 16;
      let tracksCount = 3;
    
      // Data structure to hold patterns: 
      // patterns[patternIndex][trackIndex][rowIndex] = { note: frequency in Hz or null, oscType, volume, detune, adsr, filter, noise }
      let patterns = [];
    
      // We'll start with 1 pattern only
      let currentPattern = 0;
    
      const trackerDiv = document.getElementById('tracker');
      const bpmInput = document.getElementById('bpm');
      const patternRowsInput = document.getElementById('patternRows');
      const tracksCountInput = document.getElementById('tracksCount');
      const initTracksBtn = document.getElementById('initTracks');
      const playBtn = document.getElementById('playBtn');
      const stopBtn = document.getElementById('stopBtn');
      const shareBtn = document.getElementById('shareBtn');
      const shareLinkArea = document.getElementById('shareLink');
    
      // Scale and notes for basic chiptune freq calculation (C4=261.6256 Hz)
      const notesMap = {
        "C": 261.6256,
        "C#": 277.1826,
        "D": 293.6648,
        "D#": 311.1270,
        "E": 329.6276,
        "F": 349.2282,
        "F#": 369.9944,
        "G": 391.9954,
        "G#": 415.3047,
        "A": 440,
        "A#": 466.1638,
        "B": 493.8833,
        "R": null  // Rest
      };
    
      // List of oscillator types for selection
      const oscTypes = ['sine', 'square', 'sawtooth', 'triangle'];
    
      // Initialize or reset the tracker UI and data
      function initTracker() {
        bpm = parseInt(bpmInput.value);
        rowsPerPattern = parseInt(patternRowsInput.value);
        tracksCount = parseInt(tracksCountInput.value);
    
        // Initialize pattern data: one pattern of [tracks][rows]
        patterns = [];
        let pattern = [];
        for(let t=0; t < tracksCount; t++) {
          let trackRows = [];
          for(let r=0; r < rowsPerPattern; r++) {
            // Each row is an object with minimal/chiptune params
            trackRows.push({
              note: null,         // frequency in Hz or null
              oscType: 'square',  // default chiptune osc
              volume: 0.5,
              detune: 0,
              adsr: { a:0.01, d:0.05, s:0.5, r:0.2 },
              filter: null,  // null or {type, freq, Q}
              noise: false,
            });
          }
          pattern.push(trackRows);
        }
        patterns.push(pattern);
    
        // Reset UI tracker
        trackerDiv.innerHTML = '';
        for(let t=0; t < tracksCount; t++) {
          const trackDiv = document.createElement('div');
          trackDiv.className = 'track';
          trackDiv.dataset.trackIndex = t;
    
          trackDiv.innerHTML = `<strong>Track ${t+1}</strong>`;
    
          // Add rows
          for(let r=0; r < rowsPerPattern; r++) {
            let row = pattern[t][r];
            const rowDiv = document.createElement('div');
            rowDiv.style.marginBottom = '0.15rem';
            rowDiv.style.display = 'flex';
            rowDiv.style.justifyContent = 'space-between';
            rowDiv.style.alignItems = 'center';
    
            // Note selector (text input accepts like "C4", "A#4", "R" for rest)
            const noteInput = document.createElement('input');
            noteInput.type = 'text';
            noteInput.maxLength = 3;
            noteInput.size = 3;
            noteInput.value = '';
            noteInput.placeholder = 'Note';
            noteInput.dataset.track = t;
            noteInput.dataset.row = r;
    
            // Oscillator selector
            const oscSelect = document.createElement('select');
            for(let o of oscTypes) {
              let opt = document.createElement('option');
              opt.value = o;
              opt.textContent = o;
              oscSelect.appendChild(opt);
            }
            oscSelect.value = row.oscType;
            oscSelect.style.width = '70px';
            oscSelect.dataset.track = t;
            oscSelect.dataset.row = r;
    
            // Volume input (0-1 float)
            const volInput = document.createElement('input');
            volInput.type = 'number';
            volInput.min = 0;
            volInput.max = 1;
            volInput.step = 0.1;
            volInput.value = row.volume;
            volInput.style.width = '50px';
            volInput.dataset.track = t;
            volInput.dataset.row = r;
    
            // Detune input (-50 to 50 cents)
            const detuneInput = document.createElement('input');
            detuneInput.type = 'number';
            detuneInput.min = -50;
            detuneInput.max = 50;
            detuneInput.step = 1;
            detuneInput.value = row.detune;
            detuneInput.style.width = '50px';
            detuneInput.dataset.track = t;
            detuneInput.dataset.row = r;
    
            // Noise checkbox
            const noiseInput = document.createElement('input');
            noiseInput.type = 'checkbox';
            noiseInput.dataset.track = t;
            noiseInput.dataset.row = r;
            noiseInput.checked = row.noise;
    
            rowDiv.appendChild(noteInput);
            rowDiv.appendChild(oscSelect);
            rowDiv.appendChild(volInput);
            rowDiv.appendChild(detuneInput);
            rowDiv.appendChild(noiseInput);
    
            trackDiv.appendChild(rowDiv);
          }
          trackerDiv.appendChild(trackDiv);
        }
      }
    
      // Convert note string to frequency, assuming octave 4 if none given
      function noteToFreq(noteStr) {
        // Examples: C, C4, A#4, R (rest)
        noteStr = noteStr.toUpperCase();
        if(noteStr === 'R' || noteStr === '') return null;
    
        const regex = /^([A-G]#?)(\d?)$/;
        const m = noteStr.match(regex);
        if(!m) return null;
    
        let note = m[1];
        let octave = m[2] === '' ? 4 : parseInt(m[2]);
        let baseFreq = notesMap[note];
        if(baseFreq == null) return null;
    
        // multiply or divide frequency by 2^(octave-4)
        const freq = baseFreq * Math.pow(2, octave - 4);
        return freq;
      }
    
      // Read from inputs into patterns data structure
      function readTrackerInputs() {
        for(let t=0; t < tracksCount; t++) {
          for(let r=0; r < rowsPerPattern; r++) {
            let row = patterns[0][t][r];
    
            // Find inputs by dataset attributes
            const inputSelector = `input[type=text][data-track="${t}"][data-row="${r}"]`;
            const noteInput = trackerDiv.querySelector(inputSelector);
            row.note = noteToFreq(noteInput.value.trim());
    
            const oscSelector = `select[data-track="${t}"][data-row="${r}"]`;
            const oscSelect = trackerDiv.querySelector(oscSelector);
            row.oscType = oscSelect.value;
    
            const volSelector = `input[type=number][data-track="${t}"][data-row="${r}"]`;
            // There are multiple number inputs, volume is the first (index 0), detune second (index 1)
            const numberInputs = trackerDiv.querySelectorAll(`input[type=number][data-track="${t}"][data-row="${r}"]`);
            row.volume = parseFloat(numberInputs[0].value);
            row.detune = parseFloat(numberInputs[1].value);
    
            const noiseSelector = `input[type=checkbox][data-track="${t}"][data-row="${r}"]`;
            const noiseInput = trackerDiv.querySelector(noiseSelector);
            row.noise = noiseInput.checked;
          }
        }
      }
    
      // Play a single note with given params for a short duration based on ADSR envelope
      function playNote(freq, oscType, volume, detune, adsr, filterParams, noise) {
        if (!ctx) ctx = new AudioContext();
    
        if(!freq && !noise) return; // no note or noise? skip
    
        if(noise) {
          // Play noise - create noise source, filter it if filterParams exist
          const bufferSize = ctx.sampleRate * 0.2; // 0.2s noise buffer
          const buffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
          let data = buffer.getChannelData(0);
          for(let i=0; i < bufferSize; i++) {
            data[i] = Math.random() * 2 -1;
          }
          const noiseSource = ctx.createBufferSource();
          noiseSource.buffer = buffer;
    
          let gainNode = ctx.createGain();
          gainNode.gain.setValueAtTime(0, ctx.currentTime);
          gainNode.gain.linearRampToValueAtTime(volume, ctx.currentTime + adsr.a);
          gainNode.gain.linearRampToValueAtTime(adsr.s * volume, ctx.currentTime + adsr.a + adsr.d);
          gainNode.gain.setValueAtTime(adsr.s * volume, ctx.currentTime + adsr.a + adsr.d + 0.1);
          gainNode.gain.linearRampToValueAtTime(0, ctx.currentTime + adsr.a + adsr.d + adsr.r + 0.1);
    
          if(filterParams) {
            const filter = ctx.createBiquadFilter();
            filter.type = filterParams.type;
            filter.frequency.setValueAtTime(filterParams.freq, ctx.currentTime);
            filter.Q.setValueAtTime(filterParams.Q, ctx.currentTime);
    
            noiseSource.connect(filter);
            filter.connect(gainNode);
          } else {
            noiseSource.connect(gainNode);
          }
    
          gainNode.connect(ctx.destination);
          noiseSource.start();
          noiseSource.stop(ctx.currentTime + adsr.a + adsr.d + adsr.r + 0.1);
    
          return;
        }
    
        // Oscillator note
        const osc = ctx.createOscillator();
        osc.type = oscType;
        osc.frequency.setValueAtTime(freq, ctx.currentTime);
        osc.detune.setValueAtTime(detune, ctx.currentTime);
    
        // ADSR gain envelope
        const gainNode = ctx.createGain();
        gainNode.gain.setValueAtTime(0, ctx.currentTime);
    
        // Attack
        gainNode.gain.linearRampToValueAtTime(volume, ctx.currentTime + adsr.a);
        // Decay
        gainNode.gain.linearRampToValueAtTime(adsr.s * volume, ctx.currentTime + adsr.a + adsr.d);
        // Sustain level held (no ramp), release called on stop
    
        // Filter if any
        let outputNode = gainNode;
        if(filterParams){
          const filter = ctx.createBiquadFilter();
          filter.type = filterParams.type;
          filter.frequency.setValueAtTime(filterParams.freq, ctx.currentTime);
          filter.Q.setValueAtTime(filterParams.Q, ctx.currentTime);
    
          osc.connect(filter);
          filter.connect(gainNode);
        } else {
          osc.connect(gainNode);
        }
    
        gainNode.connect(ctx.destination);
        osc.start();
    
        // Auto stop after envelope: attack + decay + sustain + release (~0.4s)
        const dur = adsr.a + adsr.d + adsr.r + 0.2;
        gainNode.gain.setTargetAtTime(0, ctx.currentTime + adsr.a + adsr.d + 0.2, adsr.r);
    
        osc.stop(ctx.currentTime + dur);
    
        return;
      }
    
      // Playback loop
      function playPattern() {
        if(!ctx) ctx = new AudioContext();
    
        const intervalMs = 60000 / (bpm * 4); // 4 rows per beat (assuming 16 ticks pattern / 4 beats)
    
        if(currentRow >= rowsPerPattern) {
          currentRow = 0;
        }
    
        // Read current rows note per track
        for(let t=0; t < tracksCount; t++) {
          let rowData = patterns[0][t][currentRow];
          playNote(rowData.note, rowData.oscType, rowData.volume, rowData.detune, rowData.adsr, rowData.filter, rowData.noise);
        }
        currentRow++;
      }
    
      // Start playback
      function start() {
        if(isPlaying) return;
        isPlaying = true;
        currentRow = 0;
    
        if(!ctx || ctx.state === 'closed') {
          ctx = new AudioContext();
        }
        
        timerId = setInterval(playPattern, 60000/(bpm*4));
        playBtn.disabled = true;
        stopBtn.disabled = false;
      }
    
      // Stop playback
      function stop() {
        isPlaying = false;
        clearInterval(timerId);
        playBtn.disabled = false;
        stopBtn.disabled = true;
      }
    
      // Serialize pattern as JSON and encode in base64 for sharing
      function serializePattern() {
        const data = {
          bpm,
          rowsPerPattern,
          tracksCount,
          patterns,
        };
        return btoa(JSON.stringify(data));
      }
    
      // Deserialize pattern from hash
      function deserializePattern(base64str) {
        try {
          const jsonstr = atob(base64str);
          const data = JSON.parse(jsonstr);
          bpm = data.bpm || 120;
          rowsPerPattern = data.rowsPerPattern || 16;
          tracksCount = data.tracksCount || 3;
    
          if(data.patterns && data.patterns.length>0){
            patterns = data.patterns;
            currentPattern = 0;
          } else {
            patterns = [];
          }
          
          bpmInput.value = bpm;
          patternRowsInput.value = rowsPerPattern;
          tracksCountInput.value = tracksCount;
    
          renderLoadedPattern();
        } catch(e) {
          alert('Failed to load shared pattern: '+e.message);
        }
      }
    
      // Render loaded pattern into UI inputs
      function renderLoadedPattern() {
        trackerDiv.innerHTML = '';
        for(let t=0; t < tracksCount; t++) {
          const trackDiv = document.createElement('div');
          trackDiv.className = 'track';
          trackDiv.dataset.trackIndex = t;
    
          trackDiv.innerHTML = `<strong>Track ${t+1}</strong>`;
    
          for(let r=0; r < rowsPerPattern; r++) {
            let row = patterns[0][t][r];
            const rowDiv = document.createElement('div');
            rowDiv.style.marginBottom = '0.15rem';
            rowDiv.style.display = 'flex';
            rowDiv.style.justifyContent = 'space-between';
            rowDiv.style.alignItems = 'center';
    
            // Reverse freq back to note string: no perfect mapping, so keep freq approximate & empty
            let noteStr = '';
            if(row.note) {
              noteStr = 'C4'; // fallback
              // Try reverse note for freq near notesMap
              const baseOctave = 4;
              for(let n in notesMap) {
                if(notesMap[n] === null) continue;
                for(let o=0; o<=8; o++) {
                  let f = notesMap[n] * Math.pow(2, o - baseOctave);
                  if(Math.abs(f - row.note) < 5) {
                    noteStr = n + o;
                    break;
                  }
                }
              }
            } else if(row.noise){
              noteStr = 'R';
            }
    
            const noteInput = document.createElement('input');
            noteInput.type = 'text';
            noteInput.value = noteStr;
            noteInput.maxLength = 3;
            noteInput.size = 3;
            noteInput.placeholder = 'Note';
            noteInput.dataset.track = t;
            noteInput.dataset.row = r;
    
            const oscSelect = document.createElement('select');
            for(let o of oscTypes) {
              let opt = document.createElement('option');
              opt.value = o;
              opt.textContent = o;
              oscSelect.appendChild(opt);
            }
            oscSelect.value = row.oscType || 'square';
            oscSelect.style.width = '70px';
            oscSelect.dataset.track = t;
            oscSelect.dataset.row = r;
    
            const volInput = document.createElement('input');
            volInput.type = 'number';
            volInput.min = 0;
            volInput.max = 1;
            volInput.step = 0.1;
            volInput.value = row.volume || 0.5;
            volInput.style.width = '50px';
            volInput.dataset.track = t;
            volInput.dataset.row = r;
    
            const detuneInput = document.createElement('input');
            detuneInput.type = 'number';
            detuneInput.min = -50;
            detuneInput.max = 50;
            detuneInput.step = 1;
            detuneInput.value = row.detune || 0;
            detuneInput.style.width = '50px';
            detuneInput.dataset.track = t;
            detuneInput.dataset.row = r;
    
            const noiseInput = document.createElement('input');
            noiseInput.type = 'checkbox';
            noiseInput.dataset.track = t;
            noiseInput.dataset.row = r;
            noiseInput.checked = row.noise || false;
    
            rowDiv.appendChild(noteInput);
            rowDiv.appendChild(oscSelect);
            rowDiv.appendChild(volInput);
            rowDiv.appendChild(detuneInput);
            rowDiv.appendChild(noiseInput);
    
            trackDiv.appendChild(rowDiv);
          }
          trackerDiv.appendChild(trackDiv);
        }
      }
    
      // Handle shortcuts - for example space toggles play/stop
      function handleShortcut(e) {
        if(e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; // ignore input focused
    
        if(e.code === 'Space') {
          e.preventDefault();
          if(isPlaying) stop();
          else {
            readTrackerInputs();
            start();
          }
        }
      }
    
      // On page load, init tracker with defaults or hash data
      function pageLoad() {
        if(location.hash.startsWith('#data=')) {
          const base64pattern = location.hash.substring(6);
          deserializePattern(base64pattern);
        } else {
          initTracker();
        }
      }
    
      // Event Listeners
      initTracksBtn.addEventListener('click', e => {
        initTracker();
      });
    
      playBtn.addEventListener('click', e => {
        readTrackerInputs();
        start();
      });
    
      stopBtn.addEventListener('click', e => {
        stop();
      });
    
      shareBtn.addEventListener('click', e => {
        readTrackerInputs();
        const serialized = serializePattern();
        const url = location.origin + location.pathname + '#data=' + serialized;
        shareLinkArea.value = url;
        // Optionally copy to clipboard
        navigator.clipboard.writeText(url).then(() => alert('Pattern link copied to clipboard!'));
      });
    
      window.addEventListener('keydown', handleShortcut);
    
      // Update BPM changes live
      bpmInput.addEventListener('change', e => {
        bpm = parseInt(bpmInput.value);
        if(isPlaying){
          stop();
          start();
        }
      });
    
      // On load
      pageLoad();
    
    })();
    </script>
    
    </body>
    </html>
    

    How to Use

    • Set BPM, rows/pattern, and number of tracks.
    • Click Init Tracks to generate the grid.
    • Fill each row for every track with:
      • Note: C4, D#4, A3, R (rest)
      • Osc type: sine, square, sawtooth, triangle
      • Volume (0.0 - 1.0)
      • Detune (-50 to 50 cents)
      • Noise toggle (if checked plays noise instead of oscillator)
    • Click Play to hear.
    • Click Stop to stop.
    • Keyboard shortcut: Space toggles play/stop.
    • Click Share Pattern to generate a URL with data encoded in hash (/#data=). Share this URL to reload pattern.

    Notes & Limitations

    • This is a minimal, basic chiptune tracker demo.
    • ADSR envelope is fixed per note but you can extend the UI to customize ADSR.
    • Filter support is stubbed; filter params not yet exposed to UI but data structure is ready.
    • Volume and detune are per note.
    • Noise is a simple noise burst.
    • Sharing uses base64 encoded JSON in the URL hash.
    • Keyboard shortcuts limited to space play/stop for now.
    • No demo audio included (sounds are generated live).

    If you want me to add more features or polish UI/audio engine, let me know!