It sounds like you want to create a simple rhythm game editor in HTML/CSS/JS that supports:
- Loading an MP3 from a URL
- Setting BPM (beats per minute)
- Displaying bars/measures and counts, allowing note placement (combo notes)
- Supporting keyboard shortcuts for play/delete/etc.
- Sharing the created chart via URL hash (“#/”)
- Playing the beat with notes in sync
I’ll provide you the foundation of such an editor that:
- Lets you input a URL to an MP3 and a BPM
- Creates a timeline divided into bars and beats
- Allows clicking to place/remove notes on the timeline
- Plays the audio and highlights notes on the beat
- Allows sharing the note chart encoded in the URL hash
- Supports keyboard shortcuts like space to play/pause, delete to remove selected note
Here is a fully commented/simple implementation:
<!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: 10px;
background: #222;
color: #eee;
}
#controls {
margin-bottom: 10px;
}
input[type=text], input[type=number] {
padding: 5px;
margin-right: 10px;
font-size: 1em;
}
button {
padding: 6px 12px;
font-size: 1em;
}
#timeline {
margin-top: 20px;
user-select: none;
position: relative;
}
.bar {
display: flex;
border-bottom: 1px solid #555;
margin-bottom: 5px;
position: relative;
}
.beat {
flex: 1;
height: 40px;
border-left: 1px solid #555;
cursor: pointer;
position: relative;
}
.beat:first-child {
border-left: none;
}
.note {
position: absolute;
top: 5px;
left: 50%;
transform: translateX(-50%);
width: 14px;
height: 14px;
background: #09f;
border-radius: 50%;
}
.highlight {
border-color: #0f9 !important;
background-color: #0f9 !important;
}
#status {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Rhythm Game Editor</h1>
<div id="controls">
<label>MP3 URL: <input type="text" id="mp3url" placeholder="Enter MP3 URL" size="50" /></label>
<label>BPM: <input type="number" id="bpm" value="120" min="20" max="400" /></label>
<label>Bars: <input type="number" id="barsCount" value="4" min="1" max="64" /></label>
<button id="loadBtn">Load & Render</button>
<button id="playPauseBtn" disabled>Play</button>
<button id="shareBtn">Share</button>
</div>
<div id="timeline"></div>
<div id="status"></div>
<script>
(() => {
// === State ===
let audio = null;
let context = null;
let source = null;
let startTime = 0; // AudioContext start time reference
let playing = false;
let scheduledNotes = [];
let animationFrameId = null;
let bpm = 120;
let barsCount = 4;
const beatsPerBar = 4;
let notes = []; // Array of {bar, beat} objects
const mp3urlInput = document.getElementById("mp3url");
const bpmInput = document.getElementById("bpm");
const barsInput = document.getElementById("barsCount");
const timelineEl = document.getElementById("timeline");
const loadBtn = document.getElementById("loadBtn");
const playPauseBtn = document.getElementById("playPauseBtn");
const shareBtn = document.getElementById("shareBtn");
const statusEl = document.getElementById("status");
// ========
// UTIL - encode notes to string and from string for URL hash
// Format: bar-beat pairs joined by commas, e.g. "0-1,1-0,3-3"
function encodeNotes(notes) {
return notes.map(n => n.bar + "-" + n.beat).join(",");
}
function decodeNotes(str) {
const arr = str.split(",");
const result = [];
for (const pair of arr) {
if (!pair) continue;
const [bar, beat] = pair.split("-").map(x => parseInt(x));
if (!isNaN(bar) && !isNaN(beat)) result.push({bar, beat});
}
return result;
}
// ========
// Load MP3 using Web Audio API
async function loadAudio(url) {
if (audio) {
audio.pause();
audio = null;
}
if (context) {
try { context.close(); } catch {}
context = null;
}
context = new (window.AudioContext || window.webkitAudioContext)();
let buffer;
try {
let response = await fetch(url);
let arrayBuffer = await response.arrayBuffer();
buffer = await context.decodeAudioData(arrayBuffer);
} catch (e) {
alert("Error loading audio: " + e.message);
return null;
}
return buffer;
}
// ========
// Render timeline bars and beats
function renderTimeline() {
timelineEl.innerHTML = "";
for(let bar=0; bar<barsCount; bar++) {
const barEl = document.createElement("div");
barEl.className = "bar";
for(let beat=0; beat<beatsPerBar; beat++) {
const beatEl = document.createElement("div");
beatEl.className = "beat";
beatEl.dataset.bar = bar;
beatEl.dataset.beat = beat;
beatEl.title = `Bar ${bar+1} Beat ${beat+1}`;
// show note if exists
if (notes.some(n => n.bar===bar && n.beat===beat)) {
const noteEl = document.createElement("div");
noteEl.className = "note";
beatEl.appendChild(noteEl);
}
barEl.appendChild(beatEl);
}
timelineEl.appendChild(barEl);
}
}
// ========
// Toggle note at specified bar/beat
function toggleNote(bar, beat) {
const idx = notes.findIndex(n => n.bar === bar && n.beat === beat);
if(idx >= 0) {
// remove note
notes.splice(idx, 1);
} else {
notes.push({bar, beat});
// sort notes by bar then beat to keep consistent
notes.sort((a,b) => (a.bar - b.bar) || (a.beat - b.beat));
}
renderTimeline();
}
// ========
// Play audio and start beat sync
function play() {
if (!audioBuffer) return;
if (!context) {
context = new (window.AudioContext || window.webkitAudioContext)();
}
source = context.createBufferSource();
source.buffer = audioBuffer;
source.connect(context.destination);
startTime = context.currentTime;
source.start(startTime);
playing = true;
playPauseBtn.textContent = "Pause";
animationFrameId = requestAnimationFrame(update);
}
// ========
// Stop playback
function stop() {
if (source) {
source.stop(0);
source.disconnect();
source = null;
}
playing = false;
playPauseBtn.textContent = "Play";
clearNoteHighlight();
cancelAnimationFrame(animationFrameId);
}
// ========
// Highlight current beat in timeline during playback
function update() {
if (!playing) return;
const elapsed = context.currentTime - startTime;
const secondsPerBeat = 60 / bpm;
const totalBeats = barsCount * beatsPerBar;
let currentBeatFloat = elapsed / secondsPerBeat;
if (currentBeatFloat >= totalBeats) {
stop();
return;
}
const currentBeatIndex = Math.floor(currentBeatFloat);
const currentBar = Math.floor(currentBeatIndex / beatsPerBar);
const currentBeat = currentBeatIndex % beatsPerBar;
highlightCurrentBeat(currentBar, currentBeat);
animationFrameId = requestAnimationFrame(update);
}
// ========
// Clear all highlights
function clearNoteHighlight() {
const beats = document.querySelectorAll(".beat");
beats.forEach(el => el.classList.remove("highlight"));
}
// Highlight current beat and notes in that beat
function highlightCurrentBeat(bar, beat) {
clearNoteHighlight();
const selector = `.beat[data-bar="${bar}"][data-beat="${beat}"]`;
const el = timelineEl.querySelector(selector);
if(el) {
el.classList.add("highlight");
}
}
// ========
// Share notes + bpm + bars in URL hash
function updateHash() {
const parts = [];
parts.push("bpm="+bpm);
parts.push("bars="+barsCount);
parts.push("notes="+encodeNotes(notes));
location.hash = "#/" + parts.join("&");
}
// Parse hash to restore state
function parseHash() {
if (!location.hash.startsWith("#/")) return;
const q = location.hash.slice(2);
const params = {};
q.split("&").forEach(part => {
const [k,v] = part.split("=");
if(k && v) params[k] = v;
});
if (params.bpm) {
const bpmVal = parseInt(params.bpm);
if(!isNaN(bpmVal)) bpm = bpmVal;
bpmInput.value = bpm;
}
if (params.bars) {
const barsVal = parseInt(params.bars);
if(!isNaN(barsVal)) barsCount = barsVal;
barsInput.value = barsCount;
}
if (params.notes) {
notes = decodeNotes(params.notes);
}
}
// === Initial vars ===
let audioBuffer = null;
// === Event Listeners ===
loadBtn.addEventListener("click", async () => {
bpm = parseInt(bpmInput.value);
barsCount = parseInt(barsInput.value);
if (!mp3urlInput.value) {
alert("Please enter a valid MP3 URL");
return;
}
statusEl.textContent = "Loading audio...";
audioBuffer = await loadAudio(mp3urlInput.value);
if (!audioBuffer) {
statusEl.textContent = "Failed to load audio";
playPauseBtn.disabled = true;
return;
}
statusEl.textContent = "Audio loaded, render timeline";
notes = [];
renderTimeline();
playPauseBtn.disabled = false;
updateHash();
});
timelineEl.addEventListener("click", e => {
if(!e.target.classList.contains("beat")) return;
const bar = parseInt(e.target.dataset.bar);
const beatVal = parseInt(e.target.dataset.beat);
toggleNote(bar, beatVal);
updateHash();
});
playPauseBtn.addEventListener("click", () => {
if (playing) {
stop();
} else {
if (!audioBuffer) return;
play();
}
});
shareBtn.addEventListener("click", () => {
updateHash();
navigator.clipboard.writeText(location.href).then(() => {
alert("Link copied to clipboard!");
}, (e) => {
alert("Failed to copy link:"+e);
});
});
// Shortcut keys:
// Space = play/pause
// Delete = delete latest note (last in list)
document.addEventListener("keydown", e => {
if (e.target.tagName === "INPUT") return; // skip when typing
if(e.code === "Space") {
e.preventDefault();
playPauseBtn.click();
}
else if(e.code === "Delete") {
if(notes.length > 0) {
notes.pop();
renderTimeline();
updateHash();
}
}
});
// On page load, parse hash and restore state if available
window.addEventListener("load", () => {
parseHash();
bpm = parseInt(bpmInput.value);
barsCount = parseInt(barsInput.value);
renderTimeline();
if (notes.length > 0) {
playPauseBtn.disabled = false;
}
});
})();
</script>
</body>
</html>
How this works:
- You enter an MP3 URL, BPM, and number of bars
- Click “Load & Render” to fetch and decode the audio, then a grid appears with bars and 4 beats each
- Click any beat cell to toggle placing a note (blue dot)
- Press Play to play the song - the current beat column is highlighted and notes are shown visually
- Space bar toggles play/pause, Delete removes the last placed note
- Click “Share” to update the URL hash with the current chart and copy the link to clipboard
- On page load, if URL has hash with data, it will restore the chart, bpm, bars count
Notes:
- This is a basic editor. You can extend it with more features: velocity, note types, dragging notes, etc.
- The audio playback is synchronized roughly using AudioContext time and animations.
- Sharing is done encoding the notes simple bar-beat CSV in the hash.
- The MP3 URL must permit CORS or be from your server (otherwise browser won’t allow fetch).
If you want me to help with expanding features or improving UI, let me know!