Creating a basic rhythm game editor that loads audio from a URL, plays the audio, allows recording notes aligned to bars/beats, counts bars, supports rhythm shortcuts, deleting notes, and sharing the edited rhythm as a URL hash is a sizeable but fun project.
Below is a minimal implementation using HTML, CSS, and JavaScript that demonstrates these features:
Features covered:
Load audio from a URL input
Play / pause controls
Visual bars based on BPM and bars count
Record notes by clicking or keyboard shortcuts (e.g., spacebar)
Delete notes by clicking on them
Display notes on a timeline aligned to bars
Share the rhythm data via the URL hash (#/encodedData)
Parse URL hash on load to restore saved rhythm
Keyboard shortcuts for recording notes and playback
How it works:
The user inputs audio URL and sets BPM and bars count.
The timeline renders bars accordingly.
User presses spacebar or clicks "Record Note" to add notes at current playback time.
Notes show as dots on the timeline.
Clicking a note deletes it.
The rhythm data (note times) and settings are encoded into a base64 JSON string and put into the URL hash.
Loading such a URL restores the editor state.
Play/pause controls start/stop the audio.
Code:
html
<!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: Arial, sans-serif;
margin: 0; padding: 1rem;
background: #121212;
color: #eee;
}
.container {
max-width: 800px;
margin: auto;
}
label {
display: block;
margin-top: 1rem;
}
input[type="text"], input[type="number"] {
width: 100%;
padding: 0.5em;
background: #222;
border: 1px solid #444;
color: #eee;
}
button {
margin-top: 1rem;
padding: 0.5em 1em;
background: #2a8;
border: none;
color: white;
cursor: pointer;
font-weight: bold;
}
button:disabled {
background: #555;
cursor: default;
}
.timeline {
position: relative;
margin-top: 2rem;
background: #222;
height: 100px;
border: 1px solid #444;
overflow-x: scroll;
white-space: nowrap;
}
.bar {
position: relative;
display: inline-block;
width: 60px; /* each bar width */
height: 100px;
border-left: 1px solid #666;
box-sizing: border-box;
}
.bar-number {
position: absolute;
top: 2px;
left: 2px;
font-size: 0.75rem;
color: #888;
user-select: none;
}
.note {
position: absolute;
bottom: 10px;
width: 12px;
height: 12px;
background: #2a8;
border-radius: 50%;
cursor: pointer;
transform: translateX(-50%);
transition: background 0.3s;
}
.note:hover {
background: #6f9;
}
.controls {
margin-top: 1rem;
}
.info {
margin-top: 1rem;
font-size: 0.875rem;
color: #666;
}
a.share-link {
display: block;
margin-top: 1rem;
color: #2a8;
word-break: break-all;
}
</style>
</head>
<body>
<div class="container">
<h1>Rhythm Game Editor</h1>
<label>
Audio URL:
<input type="text" id="audioUrl" placeholder="Enter audio file URL here" />
</label>
<label>
BPM (beats per minute):
<input type="number" id="bpm" min="30" max="300" value="120" />
</label>
<label>
Bars Count:
<input type="number" id="barsCount" min="1" max="100" value="8" />
</label>
<div class="controls">
<button id="loadAudioBtn">Load Audio</button>
<button id="playPauseBtn" disabled>Play</button>
<button id="recordNoteBtn" disabled>Record Note (Space)</button>
</div>
<div class="info">
<p>Click bars or press (Space) to add notes at current time.<br />
Click notes to delete.<br />
Use the share link to save or share your rhythm.</p>
</div>
<div class="timeline" id="timeline"></div>
<a href="#" target="_blank" id="shareLink" class="share-link" style="display:none;"></a>
</div>
<script>
(() => {
// Constants
const BAR_WIDTH_PX = 60;
const NOTE_SIZE_PX = 12;
// Elements
const audioUrlInput = document.getElementById('audioUrl');
const bpmInput = document.getElementById('bpm');
const barsCountInput = document.getElementById('barsCount');
const loadAudioBtn = document.getElementById('loadAudioBtn');
const playPauseBtn = document.getElementById('playPauseBtn');
const recordNoteBtn = document.getElementById('recordNoteBtn');
const timeline = document.getElementById('timeline');
const shareLink = document.getElementById('shareLink');
// State
let audio = null;
let playing = false;
let bpm = 120;
let barsCount = 8;
let notes = []; // In seconds, sorted ascending
let lastTime = 0;
// Initialize from URL hash if present
function loadFromHash() {
if (!location.hash.startsWith('#/')) return false;
try {
const base64 = location.hash.substring(2);
const jsonStr = atob(base64);
const data = JSON.parse(jsonStr);
if (typeof data !== 'object') return false;
if (!Array.isArray(data.notes) || typeof data.bpm !== 'number' || typeof data.barsCount !== 'number' || typeof data.url !== 'string') return false;
notes = data.notes;
bpm = data.bpm;
barsCount = data.barsCount;
audioUrlInput.value = data.url;
bpmInput.value = bpm;
barsCountInput.value = barsCount;
return true;
} catch(e) {
console.warn('Failed to parse hash:', e);
return false;
}
}
// Save current state to URL hash
function updateHash() {
const data = {
notes,
bpm,
barsCount,
url: audioUrlInput.value.trim(),
};
const jsonStr = JSON.stringify(data);
const base64 = btoa(jsonStr);
location.hash = '#/' + base64;
shareLink.href = location.href;
shareLink.textContent = location.href;
shareLink.style.display = 'block';
}
// Load audio and set up audio element
function loadAudio() {
const url = audioUrlInput.value.trim();
if (!url) {
alert('Please enter a valid audio URL');
return;
}
if (audio) {
audio.pause();
audio.remove();
}
audio = new Audio(url);
audio.crossOrigin = "anonymous";
audio.load();
audio.addEventListener('canplay', () => {
playPauseBtn.disabled = false;
recordNoteBtn.disabled = false;
renderTimeline();
});
audio.addEventListener('ended', () => {
playing = false;
playPauseBtn.textContent = 'Play';
});
}
// Play or pause audio
function togglePlay() {
if (!audio) return;
if (playing) {
audio.pause();
} else {
audio.play();
}
playing = !playing;
playPauseBtn.textContent = playing ? 'Pause' : 'Play';
}
// Calculate duration per bar and total duration
// Each bar is 4 beats (common time)
function durationPerBar() {
return (60 / bpm) * 4;
}
function totalDuration() {
return durationPerBar() * barsCount;
}
// Add note at current time
function addNoteAtCurrentTime() {
if (!audio) return;
const t = audio.currentTime;
if (t > totalDuration()) return; // Ignore notes outside range
notes.push(t);
notes.sort((a,b) => a-b);
renderTimeline();
updateHash();
}
// Remove note (closest note within threshold)
function removeNoteAtTime(time) {
// Find note within 0.2s
const threshold = 0.2;
for(let i=0; i<notes.length; i++) {
if (Math.abs(notes[i] - time) < threshold) {
notes.splice(i,1);
renderTimeline();
updateHash();
return true;
}
}
return false;
}
// Convert time to pixel offset on timeline
function timeToPx(time) {
const total = totalDuration();
const width = BAR_WIDTH_PX * barsCount;
return (time / total) * width;
}
// Convert pixel to time
function pxToTime(px) {
const width = BAR_WIDTH_PX * barsCount;
const total = totalDuration();
return (px / width) * total;
}
// Render timeline bars and notes
function renderTimeline() {
timeline.innerHTML = '';
// Set width to fit all bars
timeline.style.width = `${BAR_WIDTH_PX * barsCount}px`;
// Bars
for(let i = 0; i < barsCount; i++) {
const bar = document.createElement('div');
bar.classList.add('bar');
bar.style.position = 'relative';
bar.dataset.barIndex = i + 1;
const barNumber = document.createElement('div');
barNumber.className = 'bar-number';
barNumber.textContent = i + 1;
bar.appendChild(barNumber);
// Clicking empty bar zone adds note at bar start time
bar.addEventListener('click', e => {
if(e.target.classList.contains('note')) return;
const barStartTime = durationPerBar() * i;
// Add note at bar start or close to clicked position
const rect = e.currentTarget.getBoundingClientRect();
const clickX = e.clientX - rect.left;
// Calculate precise time within bar
const clickedTimeInBar = (clickX / BAR_WIDTH_PX) * durationPerBar();
const noteTime = barStartTime + clickedTimeInBar;
// Add note
notes.push(noteTime);
notes.sort((a,b)=>a-b);
renderTimeline();
updateHash();
});
timeline.appendChild(bar);
}
// Notes
notes.forEach((noteTime, idx) => {
// Position relative to timeline
const px = timeToPx(noteTime);
const note = document.createElement('div');
note.className = 'note';
note.style.left = `${px}px`;
note.title = `Note at ${noteTime.toFixed(2)}s`;
note.style.bottom = '10px';
note.style.width = NOTE_SIZE_PX + 'px';
note.style.height = NOTE_SIZE_PX + 'px';
note.style.position = 'absolute';
note.style.cursor = 'pointer';
note.addEventListener('click', e => {
e.stopPropagation();
// Remove this note
notes.splice(idx,1);
renderTimeline();
updateHash();
});
timeline.appendChild(note);
});
}
// Keyboard shortcuts
function onKeyDown(e) {
if (e.target.tagName === 'INPUT') return; // ignore when typing input
if (e.code === 'Space') {
e.preventDefault();
if (recordNoteBtn.disabled) return;
addNoteAtCurrentTime();
}
else if (e.code === 'KeyP') {
// play/pause toggle
togglePlay();
}
}
// Event Listeners
loadAudioBtn.addEventListener('click', () => {
bpm = parseInt(bpmInput.value) || 120;
barsCount = parseInt(barsCountInput.value) || 8;
notes = []; // Clear notes on load
updateHash();
loadAudio();
});
playPauseBtn.addEventListener('click', () => {
togglePlay();
});
recordNoteBtn.addEventListener('click', () => {
addNoteAtCurrentTime();
});
bpmInput.addEventListener('change', () => {
bpm = parseInt(bpmInput.value) || 120;
renderTimeline();
updateHash();
});
barsCountInput.addEventListener('change', () => {
barsCount = parseInt(barsCountInput.value) || 8;
renderTimeline();
updateHash();
});
window.addEventListener('keydown', onKeyDown);
// On startup try load from url hash
if (loadFromHash()) {
bpm = parseInt(bpmInput.value) || 120;
barsCount = parseInt(barsCountInput.value) || 8;
updateHash();
loadAudio();
}
})();
</script>
</body>
</html>
Explanation
You enter an audio URL and set BPM and number of bars.
Click "Load Audio" to fetch the audio and prepare the timeline.
Press "Play" or "Pause" to control the audio.
When the audio plays, click the "Record Note" button or press Spacebar to record a note at current playback time.
The timeline shows horizontal bars, and notes as green dots positioned proportional to time.
Clicking a note deletes it.
Clicking inside a bar at a particular horizontal position adds a note at corresponding time in that bar.
The rhythm data (notes, bpm, bars count, audio url) is encoded into the URL hash so you can copy/share the URL.
Opening a URL with such a hash loads the rhythm data and audio automatically.
How to use:
Host this file on a web server or open it locally in a modern browser (Chrome/Firefox).
Paste a direct audio URL (mp3/ogg/wav) in "Audio URL", like: