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

html css js rhythm game editor bpm load audio url play note record bars count rhythm…

  1. User

    html css js rhythm game editor bpm load audio url play note record bars count rhythm shortcuts delete share "#/"

  2. Assistant

    It sounds like you want to create a simple web-based rhythm game editor with the following features:

    • Set BPM
    • Load audio via URL
    • Play/pause audio
    • Add notes on a timeline (with bars and beats)
    • Record notes (by keyboard shortcuts)
    • Delete notes
    • Share/save pattern (probably via URL hash or JSON)
    • Visual timeline grid with bars and beats

    Below is a basic starter implementation written in HTML/CSS/JS fulfilling these requirements with scope for you to expand upon:


    Features implemented in this example:

    • Input for BPM
    • Audio URL input + load button
    • Audio playback controls (play/pause)
    • Timeline grid with bars and subdivisions (beats) displayed
    • Click on timeline to place notes
    • Keyboard shortcuts (e.g. keys 1-4 to place notes in different lanes)
    • Delete notes by clicking on them
    • Simple recording by playing audio and hitting keys
    • Share notes + bpm encoded in URL hash as JSON string
    • Load from URL hash on page load

    Code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Rhythm Game Editor</title>
    <style>
      body {
        font-family: sans-serif;
        margin: 0; padding: 0;
        background: #222;
        color: #ddd;
      }
      header {
        padding: 10px;
        background: #111;
        display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
      }
      header label {
        font-size: 14px;
      }
      header input[type="number"] {
        width: 60px;
      }
      header input[type="text"] {
        width: 300px;
      }
      button {
        background: #333;
        border: none;
        color: white;
        padding: 6px 12px;
        cursor: pointer;
        border-radius: 3px;
      }
      button:hover {
        background: #555;
      }
      #timeline {
        position: relative;
        margin: 20px 10px;
        height: 200px;
        background: linear-gradient(to right, #444 1px, transparent 1px),
                    linear-gradient(to bottom, #444 1px, transparent 1px);
        background-size: 40px 50px;
      }
      #timeline .bar-line {
        position: absolute;
        top: 0; bottom: 0;
        width: 2px;
        background: #888;
      }
      #timeline .beat-subline {
        position: absolute;
        top: 0; bottom: 0;
        width: 1px;
        background: #555;
      }
      .lane {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 25%;
        border-right: 1px solid #555;
      }
      #notes {
        position: absolute;
        top: 0; left: 0; right: 0; bottom: 0;
        pointer-events: none;
      }
      .note {
        position: absolute;
        width: 22px;
        height: 22px;
        border-radius: 50%;
        background: #4caf50;
        border: 2px solid #2e7d32;
        cursor: pointer;
        pointer-events: auto;
        transform: translate(-50%, -50%);
        transition: background 0.3s;
      }
      .note:hover {
        background: #81c784;
      }
      #info {
        padding: 10px;
        font-size: 12px;
        color: #aaa;
      }
    </style>
    </head>
    <body>
    <header>
      <label for="bpm-input">BPM:</label>
      <input type="number" id="bpm-input" value="120" min="40" max="250" step="1" />
      <label for="audio-url-input">Audio URL:</label>
      <input type="text" id="audio-url-input" placeholder="Enter audio URL" />
      <button id="load-audio-btn">Load</button>
      <button id="play-pause-btn" disabled>Play</button>
      <button id="share-btn" title="Create shareable URL">Share</button>
    </header>
    
    <div id="timeline"></div>
    <div id="info">
      <p><strong>Instructions:</strong></p>
      <ul>
        <li>Set BPM and load audio URL, then press Play.</li>
        <li>Click on the timeline grid to add notes in lane 1.</li>
        <li>Use keys 1-4 to add notes in lanes 1-4 at current playback time.</li>
        <li>Click notes to delete them.</li>
        <li>Share saves your pattern and bpm in URL.</li>
      </ul>
    </div>
    
    <script>
    (() => {
      const bpmInput = document.getElementById('bpm-input');
      const audioUrlInput = document.getElementById('audio-url-input');
      const loadAudioBtn = document.getElementById('load-audio-btn');
      const playPauseBtn = document.getElementById('play-pause-btn');
      const timeline = document.getElementById('timeline');
      const shareBtn = document.getElementById('share-btn');
    
      let audio = null;
      let audioContext = null;
      let sourceNode = null;
      let isPlaying = false;
      let startTime = 0;
      let pauseTime = 0;
    
      // Rhythm data structure
      // notes = [{time: seconds, lane: 0-3}]
      let notes = [];
      let bpm = parseInt(bpmInput.value);
    
      // Timeline config
      const barsToShow = 16;
      const beatsPerBar = 4;
      const lanesCount = 4;
      const timelineWidthPx = window.innerWidth - 20;
    
      timeline.style.height = '200px';
      timeline.style.position = 'relative';
      timeline.style.width = timelineWidthPx + 'px';
    
      // Draw grid lines for bars and beats
      function drawGrid() {
        timeline.innerHTML = '';
    
        // Bar lines
        const totalBeats = barsToShow * beatsPerBar;
        for (let b = 0; b <= barsToShow; b++) {
          const x = (b / barsToShow) * timelineWidthPx;
          const barLine = document.createElement('div');
          barLine.className = 'bar-line';
          barLine.style.left = x + 'px';
          timeline.appendChild(barLine);
        }
    
        // Beat sublines
        for (let i = 1; i < totalBeats; i++) {
          if (i % beatsPerBar === 0) continue; // skip bars
          const x = (i / totalBeats) * timelineWidthPx;
          const beatLine = document.createElement('div');
          beatLine.className = 'beat-subline';
          beatLine.style.left = x + 'px';
          timeline.appendChild(beatLine);
        }
    
        // Lanes vertical lines
        for(let lane = 1; lane < lanesCount; lane++){
          const laneX = (lane / lanesCount) * timelineWidthPx;
          const laneDiv = document.createElement('div');
          laneDiv.className = 'lane';
          laneDiv.style.left = laneX + 'px';
          laneDiv.style.top = 0;
          laneDiv.style.bottom = 0;
          timeline.appendChild(laneDiv);
        }
      }
    
      // Convert time in seconds to X position on timeline
      function timeToX(time) {
        // total timeline duration in seconds = bars * beats * (60/bpm)
        const secondsPerBeat = 60 / bpm;
        const secondsPerBar = secondsPerBeat * beatsPerBar;
        const timelineDuration = barsToShow * secondsPerBar;
        if (time > timelineDuration) return timelineWidthPx;
        return (time / timelineDuration) * timelineWidthPx;
      }
    
      // Convert X position on timeline to time in seconds
      function xToTime(x) {
        const secondsPerBeat = 60 / bpm;
        const secondsPerBar = secondsPerBeat * beatsPerBar;
        const timelineDuration = barsToShow * secondsPerBar;
        let time = (x / timelineWidthPx) * timelineDuration;
        if(time < 0) time = 0;
        if(time > timelineDuration) time = timelineDuration;
        return time;
      }
    
      // Render notes
      function renderNotes() {
        // remove existing notes container if exists
        let notesContainer = document.getElementById('notes');
        if(notesContainer){
          notesContainer.remove();
        }
        notesContainer = document.createElement('div');
        notesContainer.id = 'notes';
        notesContainer.style.position = 'absolute';
        notesContainer.style.top = '0';
        notesContainer.style.left = '0';
        notesContainer.style.height = timeline.style.height;
        notesContainer.style.width = timeline.style.width;
        timeline.appendChild(notesContainer);
    
        notes.forEach((note, idx) => {
          const noteEl = document.createElement('div');
          noteEl.className = 'note';
          const x = timeToX(note.time);
          const y = ((note.lane + 0.5) / lanesCount) * parseInt(timeline.style.height);
          noteEl.style.left = x + 'px';
          noteEl.style.top = y + 'px';
          noteEl.title = `Time: ${note.time.toFixed(2)}s, Lane: ${note.lane + 1}`;
          noteEl.dataset.idx = idx;
    
          noteEl.addEventListener('click', e => {
            e.stopPropagation();
            // Delete this note
            notes.splice(idx, 1);
            renderNotes();
          });
          notesContainer.appendChild(noteEl);
        });
      }
    
      // Add note at given time and lane
      function addNote(time, lane) {
        notes.push({ time, lane });
        // Sort notes by time
        notes.sort((a,b) => a.time - b.time);
        renderNotes();
      }
    
      // Play audio
      function playAudio(){
        if(!audio) return;
        if(isPlaying) return;
        audio.currentTime = pauseTime;
        audio.play();
        isPlaying = true;
        playPauseBtn.textContent = 'Pause';
      }
    
      // Pause audio
      function pauseAudio(){
        if(!audio) return;
        if(!isPlaying) return;
        audio.pause();
        pauseTime = audio.currentTime;
        isPlaying = false;
        playPauseBtn.textContent = 'Play';
      }
    
      // Toggle play/pause
      function togglePlayPause(){
        if(!audio) return;
        if(isPlaying){
          pauseAudio();
        } else {
          playAudio();
        }
      }
    
      // Update BPM from input
      bpmInput.addEventListener('change', e => {
        let val = parseInt(e.target.value);
        if(isNaN(val)) val = 120;
        if(val < 40) val = 40;
        if(val > 250) val = 250;
        bpm = val;
        bpmInput.value = bpm;
        drawGrid();
        renderNotes();
      });
    
      // Load audio from URL
      loadAudioBtn.addEventListener('click', () => {
        const url = audioUrlInput.value.trim();
        if(!url) return alert('Please enter audio URL');
        if(audio){
          audio.pause();
          audio = null;
        }
    
        audio = new Audio(url);
        audio.crossOrigin = 'anonymous';
        audio.addEventListener('canplay', () => {
          playPauseBtn.disabled = false;
          pauseTime = 0;
          isPlaying = false;
          playPauseBtn.textContent = 'Play';
          // Reset notes on new audio load? No, leave notes in place.
        });
        audio.addEventListener('ended', () => {
          isPlaying = false;
          playPauseBtn.textContent = 'Play';
          pauseTime = 0;
        });
        audio.load();
      });
    
      playPauseBtn.addEventListener('click', togglePlayPause);
    
      drawGrid();
    
      // Clicking timeline adds note in lane 0 at clicked time
      timeline.addEventListener('click', e => {
        const rect = timeline.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;
        const time = xToTime(x);
    
        // Lanes vertical division
        const lane = Math.floor(y / (rect.height / lanesCount));
        // limit lane
        const laneClamped = Math.min(Math.max(lane, 0), lanesCount - 1);
    
        addNote(time, laneClamped);
      });
    
      // Keyboard shortcuts 1-4 to add note at current playback time in lane 0-3
      document.addEventListener('keydown', e => {
        if(e.target.tagName === 'INPUT') return; // don't capture when typing in inputs
        const key = e.key;
    
        if(key >= '1' && key <= '4'){
          if(!audio) return;
          const lane = parseInt(key) - 1;
          const currentTime = audio.currentTime || 0;
          addNote(currentTime, lane);
        }
    
        // Space = toggle play/pause
        if(key === ' '){
          e.preventDefault();
          togglePlayPause();
        }
      });
    
      // Save/share functionality: encode bpm and notes to URL hash JSON string
      shareBtn.addEventListener('click', () => {
        const state = {
          bpm,
          notes
        };
        const jsonStr = JSON.stringify(state);
        const encoded = encodeURIComponent(jsonStr);
        const url = location.origin + location.pathname + '#/' + encoded;
        prompt('Shareable URL:', url);
      });
    
      // Load from URL hash on page load
      function loadFromHash(){
        if(!location.hash.startsWith('#/')) return;
        const encoded = location.hash.slice(2);
        try {
          const decoded = decodeURIComponent(encoded);
          const state = JSON.parse(decoded);
          if(state.bpm) {
            bpm = state.bpm;
            bpmInput.value = bpm;
          }
          if(Array.isArray(state.notes)) {
            notes = state.notes;
          }
          drawGrid();
          renderNotes();
        } catch(e) {
          console.warn('Invalid URL hash data');
        }
      }
      loadFromHash();
    
      window.addEventListener('resize', () => {
        const newWidth = window.innerWidth - 20;
        timeline.style.width = newWidth + 'px';
        drawGrid();
        renderNotes();
      });
    
    })();
    </script>
    </body>
    </html>
    

    How to use:

    1. Enter your BPM (default 120).
    2. Enter an audio URL (e.g. mp3 from a CORS-enabled source) and click "Load".
    3. Click Play to start the audio.
    4. Click anywhere on timeline to add notes in the lane according to your vertical click position.
    5. Use keys 1-4 to add notes on the timeline at the current audio time in lanes 1-4.
    6. Click a note to delete it.
    7. Click "Share" to get a sharable URL encoding BPM and notes in the URL hash.
    8. On page load, if the URL contains the encoded pattern, it will load automatically.

    Notes and improvements you can make:

    • This example assumes the audio source supports CORS (important for cross origin).
    • You can extend lanes for different inputs and sounds.
    • Add better playback sync with Web Audio API (currently uses HTMLAudioElement).
    • Improve UI/UX with snapping notes to beats/bars.
    • Add note velocity, different types, etc.
    • Export/import JSON separate from URL.
    • Add "record mode" start/stop buttons instead of key-based live add.

    If you want me to help expand or add features like quantization, better play cursor, etc just ask!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.