You want to create a rhythm game editor that:
- Loads a song from a URL (MP3)
- Allows setting BPM
- Supports sharing via URL hash
- Has shortcuts and controls for bars, note count, combo, delete, play
- Displays a note grid for placing notes
I can help you with a minimal implementation to get started with these features using HTML, CSS, and JavaScript.
Features to include:
- Load MP3 from URL input
- Set BPM input
- Display a grid aligned with the BPM (e.g., bars and beats)
- Add/delete notes on the grid
- Play the song + playback cursor with sound and visualization
- Share editor state in URL hash (e.g., song URL, BPM, notes)
- Keyboard shortcuts for adding/removing notes, play/pause, delete
Basic plan:
- HTML
- Inputs: MP3 URL, BPM
- Buttons: Play/Pause, Add/Delete notes
- Display grid: horizontally represents time / beats, vertically can represent instruments/notes
- Show bars, beats as vertical lines on grid
- CSS
- Grid appearance with lines
- Cursor for current playback position
- JS
- Load and play audio
- Draw grid and notes
- Handle user actions (mouse click, keyboard shortcuts)
- Encode/decode editor state in URL hash
Here’s a minimal example:
<!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: 20px; }
#controls { margin-bottom: 10px; }
input[type="text"], input[type="number"] { width: 300px; }
#grid {
position: relative;
border: 1px solid #ccc;
height: 200px;
user-select: none;
background: repeating-linear-gradient(
to right,
#eee 0,
#eee 40px,
transparent 40px,
transparent 60px);
}
.bar-line {
position: absolute;
top: 0; bottom: 0;
width: 2px;
background: black;
opacity: 0.2;
}
.note {
position: absolute;
width: 20px;
height: 20px;
background: #3498db;
border-radius: 3px;
cursor: pointer;
}
#play-cursor {
position: absolute;
top: 0;
bottom: 0;
width: 2px;
background: red;
pointer-events: none;
}
</style>
</head>
<body>
<div id="controls">
<label>
MP3 URL: <input type="text" id="song-url" placeholder="Enter MP3 URL" />
</label>
<label>
BPM: <input type="number" id="bpm" value="120" min="30" max="300" />
</label>
<button id="load-song">Load</button>
<button id="play-pause">Play</button>
<button id="clear-notes">Clear Notes</button>
<div>
<small>Shortcuts: Space = Play/Pause, Click note = delete, Click grid = add note</small>
</div>
</div>
<div id="grid"></div>
<script>
const grid = document.getElementById('grid');
const playPauseButton = document.getElementById('play-pause');
const loadSongButton = document.getElementById('load-song');
const clearNotesButton = document.getElementById('clear-notes');
const bpmInput = document.getElementById('bpm');
const songUrlInput = document.getElementById('song-url');
let audioCtx = null;
let audioBuffer = null;
let sourceNode = null;
let startTime = 0;
let isPlaying = false;
let notes = []; // [{time, lane}]
const lanes = 4;
const gridWidth = 800;
const laneHeight = 50;
const beatsPerBar = 4;
const barsCount = 8;
// Playback state
let animationFrameId;
// Playback cursor
const playCursor = document.createElement('div');
playCursor.id = 'play-cursor';
grid.appendChild(playCursor);
// Set grid size
grid.style.width = gridWidth + 'px';
grid.style.height = laneHeight * lanes + 'px';
// Draw bar lines every beat and bar
function drawGridLines() {
// remove old bars
grid.querySelectorAll('.bar-line').forEach(el => el.remove());
const bpm = parseInt(bpmInput.value, 10);
const secondsPerBeat = 60 / bpm;
const beatsTotal = beatsPerBar * barsCount;
// We use fixed grid width representing total beats
// One beat = 60px wide (we set repeating-linear-gradient above — for beat background stripes)
// We'll add bars every 4 beats thicker lines
for(let i=0; i<=beatsTotal; i++) {
const x = i * 60;
const barLine = document.createElement('div');
barLine.classList.add('bar-line');
barLine.style.left = x + 'px';
barLine.style.height = laneHeight * lanes + 'px';
barLine.style.opacity = (i % beatsPerBar === 0) ? '0.5' : '0.2';
barLine.style.width = (i % beatsPerBar === 0) ? '2px' : '1px';
grid.appendChild(barLine);
}
}
drawGridLines();
// Render notes
function renderNotes() {
// Remove old notes
grid.querySelectorAll('.note').forEach(n => n.remove());
const bpm = parseInt(bpmInput.value, 10);
const beatsTotal = beatsPerBar * barsCount;
notes.forEach((note, i) => {
// Position: time in beats * 60px; lane * laneHeight
const noteEl = document.createElement('div');
noteEl.classList.add('note');
noteEl.style.left = (note.time * 60 - 10) + 'px'; // center note on beat (20px width)
noteEl.style.top = note.lane * laneHeight + 'px';
noteEl.title = `Time: ${note.time.toFixed(2)} beats, Lane: ${note.lane + 1}`;
// Click remove note
noteEl.addEventListener('click', e => {
e.stopPropagation();
notes.splice(i,1);
updateHash();
renderNotes();
});
grid.appendChild(noteEl);
});
}
// Add note at beat/lane
function addNoteAtPosition(x, y) {
const lane = Math.floor(y / laneHeight);
if(lane < 0 || lane >= lanes) return;
const timeInBeats = x / 60; // since 60px per beat
if(timeInBeats < 0 || timeInBeats > beatsPerBar * barsCount) return;
// Avoid duplicate note in same lane/time +/- small epsilon
const exists = notes.some(n => Math.abs(n.time - timeInBeats) < 0.3 && n.lane === lane);
if(!exists) {
notes.push({ time: timeInBeats, lane });
notes.sort((a,b) => a.time - b.time);
updateHash();
renderNotes();
}
}
// Play audio and sync play cursor
function playSong() {
if(!audioBuffer) return;
if(!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
sourceNode = audioCtx.createBufferSource();
sourceNode.buffer = audioBuffer;
sourceNode.connect(audioCtx.destination);
startTime = audioCtx.currentTime;
sourceNode.start();
isPlaying = true;
playPauseButton.textContent = 'Pause';
animatePlayCursor();
sourceNode.onended = () => {
isPlaying = false;
playPauseButton.textContent = 'Play';
cancelAnimationFrame(animationFrameId);
playCursor.style.left = '0px';
};
}
function pauseSong() {
if(sourceNode) {
sourceNode.stop();
sourceNode = null;
}
if(audioCtx && audioCtx.state !== 'closed') {
audioCtx.suspend();
}
isPlaying = false;
playPauseButton.textContent = 'Play';
cancelAnimationFrame(animationFrameId);
}
// Animate cursor during playback
function animatePlayCursor() {
if(!isPlaying) return;
const bpm = parseInt(bpmInput.value, 10);
const secondsPerBeat = 60 / bpm;
const durationBeats = beatsPerBar * barsCount;
const durationSeconds = durationBeats * secondsPerBeat;
const currentTime = audioCtx.currentTime - startTime;
if(currentTime > durationSeconds) {
playCursor.style.left = '0px';
isPlaying = false;
playPauseButton.textContent = 'Play';
return;
}
// Map currentTime to px
const pxPerBeat = 60;
const left = (currentTime / secondsPerBeat) * pxPerBeat;
playCursor.style.left = left + 'px';
animationFrameId = requestAnimationFrame(animatePlayCursor);
}
// Load audio buffer from URL
async function loadAudio(url) {
try {
if(!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
alert('Audio loaded successfully.');
} catch (error) {
alert('Error loading audio: ' + error.message);
}
}
// Update everything when BPM changes (grid lines, notes position)
function onBpmChange() {
drawGridLines();
renderNotes();
updateHash();
}
// Encode state in URL hash
function updateHash() {
const state = {
songUrl: songUrlInput.value,
bpm: bpmInput.value,
notes: notes.map(n => [n.time.toFixed(2), n.lane])
};
const encoded = btoa(JSON.stringify(state));
location.hash = encoded;
}
// Load state from URL hash
function loadFromHash() {
if(location.hash.length < 2) return;
try {
const json = atob(location.hash.substring(1));
const state = JSON.parse(json);
songUrlInput.value = state.songUrl || '';
bpmInput.value = state.bpm || '120';
notes = (state.notes || []).map(n => ({time: parseFloat(n[0]), lane: n[1]}));
renderNotes();
drawGridLines();
if(state.songUrl) {
loadAudio(state.songUrl);
}
} catch(e) {
console.log('Invalid hash state');
}
}
// Event listeners
loadSongButton.onclick = () => {
const url = songUrlInput.value.trim();
if(!url) return alert('Please enter an MP3 URL');
loadAudio(url).then(() => {
updateHash();
});
};
bpmInput.onchange = onBpmChange;
bpmInput.oninput = onBpmChange;
playPauseButton.onclick = () => {
if(isPlaying) {
pauseSong();
} else {
playSong();
}
};
clearNotesButton.onclick = () => {
notes = [];
updateHash();
renderNotes();
};
grid.onclick = e => {
const rect = grid.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
addNoteAtPosition(x,y);
};
// Keyboard shortcuts
window.addEventListener('keydown', e => {
if(e.target.tagName === 'INPUT') return; // ignore if typing in input
if(e.code === 'Space') {
e.preventDefault();
if(isPlaying) pauseSong();
else playSong();
}
});
// Load state on start
loadFromHash();
// Update URL hash when inputs change (debounce)
let debounceTimer;
[songUrlInput, bpmInput].forEach(input => {
input.addEventListener('input', () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(updateHash, 500);
});
});
</script>
</body>
</html>
How this works:
- Enter an MP3 URL (make sure CORS allows fetching that resource from your domain or use a CORS proxy)
- Set BPM
- Click "Load" to load audio and visualize grid
- Click on the grid to add notes to lanes (4 lanes)
- Click on a note to delete it
- Use Space to play/pause the audio and see the playback cursor move
- Clear notes with the button
- The current editor state (song URL, bpm, notes) is encoded in the URL hash for sharing.
- Loading a URL with the hash restores the state.
Notes / Improvements:
- You can extend note lanes vertically for different instruments
- Use canvas or SVG for better performance and visuals
- Add note types/combo count UI
- Share with URL shortening
- Add save/load JSON functionality
- Responsive design and touch support
If you want me to add any specific feature or improve parts, just ask!