Sure! You want to create a simple rhythm game editor that can:
- Load an audio file by URL.
- Play the audio.
- Allow recording notes (beats) along a timeline.
- Display bars and count rhythm based on tempo.
- Support keyboard shortcuts for interaction.
- Allow deleting notes.
- Generate a shareable state (e.g., in the URL hash).
Below is a basic implementation in HTML/CSS/JS that you can expand upon.
Features:
- Load audio by URL input.
- Play/pause controls.
- Shows a visual timeline with bars (based on BPM and beats per bar).
- Click or press space to record a note at the current audio time.
- Notes shown as small blocks on the timeline.
- Delete notes by selecting and pressing Delete key.
- Keyboard shortcuts:
- Space: Add note
- Arrow left/right: Move playback
- Delete: Remove selected note
- Share the editor state (audio URL + notes + BPM) via URL hash for easy sharing and loading.
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: Arial, sans-serif;
margin: 10px;
}
#controls {
margin-bottom: 10px;
}
#timeline {
position: relative;
width: 100%;
height: 120px;
background: #222;
border: 2px solid #444;
user-select: none;
cursor: crosshair;
}
.bar {
position: absolute;
top: 0;
width: 2px;
height: 100%;
background: #555;
}
.bar.beat {
background: #888;
}
.note {
position: absolute;
top: 40px;
width: 8px;
height: 40px;
background: #08f;
border-radius: 2px;
pointer-events: auto;
cursor: pointer;
}
.note.selected {
background: #f90;
}
#status {
margin-top: 5px;
height: 18px;
}
#shareLink {
width: 100%;
font-family: monospace;
}
label {
font-weight: bold;
margin-right: 5px;
}
input[type="number"] {
width: 60px;
}
#instructions {
margin-top: 10px;
font-size: 0.9em;
color: #555;
}
</style>
</head>
<body>
<h2>Rhythm Game Editor</h2>
<div id="controls">
<label for="audioUrl">Audio URL:</label>
<input id="audioUrl" type="text" placeholder="Enter audio URL" size="50" />
<button id="loadAudioBtn">Load Audio</button>
<br/><br/>
<label for="bpmInput">BPM:</label>
<input id="bpmInput" type="number" min="40" max="300" value="120" />
<label for="beatsPerBarInput">Beats per Bar:</label>
<input id="beatsPerBarInput" type="number" min="1" max="16" value="4" />
<button id="playPauseBtn" disabled>Play</button>
<button id="stopBtn" disabled>Stop</button>
</div>
<div id="timeline" tabindex="0" title="Click or press Space to add note. Use arrow keys to seek. Del to delete selected note."></div>
<div id="status">Status: Idle</div>
<label for="shareLink">Shareable Link:</label>
<input id="shareLink" readonly />
<div id="instructions">
<strong>Shortcuts:</strong> Space: add note | Left/Right: seek 1s | Del: delete selected note | Click note: select
</div>
<script>
(() => {
// Elements
const audioUrlInput = document.getElementById('audioUrl');
const loadAudioBtn = document.getElementById('loadAudioBtn');
const playPauseBtn = document.getElementById('playPauseBtn');
const stopBtn = document.getElementById('stopBtn');
const timeline = document.getElementById('timeline');
const status = document.getElementById('status');
const bpmInput = document.getElementById('bpmInput');
const beatsPerBarInput = document.getElementById('beatsPerBarInput');
const shareLinkInput = document.getElementById('shareLink');
// Constants
const TIMELINE_WIDTH = timeline.clientWidth;
const TIMELINE_HEIGHT = timeline.clientHeight;
const NOTE_HEIGHT = 40;
const NOTE_TOP = 40;
const PIXELS_PER_SECOND = 100; // zoom level for timeline
// Variables
let audio = null;
let notes = []; // array of seconds
let bpm = 120;
let beatsPerBar = 4;
let duration = 0;
let selectedNoteIndex = -1;
let animationFrameId = null;
// Utility: Convert time (s) to position (px)
function timeToX(time) {
return time * PIXELS_PER_SECOND;
}
// Convert position (px) to time (s)
function xToTime(x) {
return x / PIXELS_PER_SECOND;
}
// Load audio from URL
function loadAudio(url) {
if (audio) {
audio.pause();
audio.src = '';
audio.remove();
audio = null;
}
audio = new Audio();
audio.crossOrigin = "anonymous";
audio.src = url;
audio.load();
audio.addEventListener('canplay', () => {
duration = audio.duration;
status.textContent = 'Status: Audio loaded, duration ' + duration.toFixed(2) + 's';
playPauseBtn.disabled = false;
stopBtn.disabled = false;
drawTimeline();
updateShareLink();
});
audio.addEventListener('ended', () => {
playPauseBtn.textContent = 'Play';
cancelAnimationFrame(animationFrameId);
});
audio.addEventListener('timeupdate', () => {
drawTimeline(); // update current time indicator
});
}
// Draw timeline bars and notes
function drawTimeline() {
timeline.innerHTML = '';
if (!duration) return;
// Draw bars
const secondsPerBeat = 60 / bpm;
const secondsPerBar = secondsPerBeat * beatsPerBar;
const totalBars = Math.ceil(duration / secondsPerBar);
const timelineWidthPx = duration * PIXELS_PER_SECOND;
timeline.style.width = timelineWidthPx + 'px';
for (let i = 0; i <= totalBars; i++) {
const barX = timeToX(i * secondsPerBar);
const bar = document.createElement('div');
bar.className = 'bar';
bar.style.left = barX + 'px';
bar.style.height = '100%';
timeline.appendChild(bar);
// Draw beats inside the bar
for (let b = 1; b < beatsPerBar; b++) {
const beatX = barX + timeToX(b * secondsPerBeat);
if (beatX > timelineWidthPx) break;
const beatLine = document.createElement('div');
beatLine.className = 'bar beat';
beatLine.style.left = beatX + 'px';
beatLine.style.height = '50%';
timeline.appendChild(beatLine);
}
}
// Draw notes
notes.forEach((noteTime, i) => {
if (noteTime > duration) return; // skip notes outside audio
const note = document.createElement('div');
note.className = 'note' + (i === selectedNoteIndex ? ' selected' : '');
note.style.left = (timeToX(noteTime) - 4) + 'px'; // center note at time pos
note.style.top = NOTE_TOP + 'px';
note.title = 'Note at ' + noteTime.toFixed(2) + 's';
note.dataset.index = i;
note.addEventListener('click', (e) => {
e.stopPropagation();
selectedNoteIndex = i;
drawTimeline();
});
timeline.appendChild(note);
});
// Draw playback cursor
if (audio && audio.currentTime && !audio.paused) {
const cursorX = timeToX(audio.currentTime);
const cursor = document.createElement('div');
cursor.style.position = 'absolute';
cursor.style.left = cursorX + 'px';
cursor.style.top = 0;
cursor.style.width = '2px';
cursor.style.height = '100%';
cursor.style.background = 'red';
timeline.appendChild(cursor);
}
}
// Play or pause audio
function togglePlayPause() {
if (!audio) return;
if (audio.paused) {
audio.play();
playPauseBtn.textContent = 'Pause';
updatePlaying();
} else {
audio.pause();
playPauseBtn.textContent = 'Play';
cancelAnimationFrame(animationFrameId);
}
}
// Update while playing - redraw timeline cursor
function updatePlaying() {
drawTimeline();
if (!audio.paused) {
animationFrameId = requestAnimationFrame(updatePlaying);
}
}
// Stop playback and reset time
function stopAudio() {
if (!audio) return;
audio.pause();
audio.currentTime = 0;
playPauseBtn.textContent = 'Play';
drawTimeline();
}
// Add note at current audio time or specified time
function addNoteAtTime(time) {
if (time < 0 || time > duration) return;
notes.push(time);
notes.sort((a,b) => a - b);
selectedNoteIndex = notes.indexOf(time);
drawTimeline();
updateShareLink();
}
// Delete selected note
function deleteSelectedNote() {
if (selectedNoteIndex >= 0 && selectedNoteIndex < notes.length) {
notes.splice(selectedNoteIndex, 1);
selectedNoteIndex = -1;
drawTimeline();
updateShareLink();
}
}
// Seek audio +/- seconds
function seekBy(seconds) {
if (!audio) return;
audio.currentTime = Math.min(Math.max(audio.currentTime + seconds, 0), duration);
drawTimeline();
}
// Click on timeline to seek there and add note if shift held
timeline.addEventListener('click', e => {
if (!duration) return;
const rect = timeline.getBoundingClientRect();
const x = e.clientX - rect.left;
const time = Math.min(xToTime(x), duration);
if(e.shiftKey){
addNoteAtTime(time);
} else {
if(audio) {
audio.currentTime = time;
drawTimeline();
}
}
});
// Keyboard shortcuts handling
document.addEventListener('keydown', e => {
if (!audio) return;
switch(e.code){
case 'Space':
e.preventDefault();
addNoteAtTime(audio.currentTime);
break;
case 'Delete':
e.preventDefault();
deleteSelectedNote();
break;
case 'ArrowLeft':
e.preventDefault();
seekBy(-1);
break;
case 'ArrowRight':
e.preventDefault();
seekBy(1);
break;
}
});
// Load audio button
loadAudioBtn.addEventListener('click', () => {
const url = audioUrlInput.value.trim();
if (!url) {
alert('Please enter an audio URL');
return;
}
loadAudio(url);
});
// Play/pause button
playPauseBtn.addEventListener('click', () => {
togglePlayPause();
});
// Stop button
stopBtn.addEventListener('click', () => {
stopAudio();
});
// Update BPM / BeatsPerBar
bpmInput.addEventListener('change', () => {
let val = parseInt(bpmInput.value);
if (val < 40) val = 40;
if (val > 300) val = 300;
bpm = val;
bpmInput.value = bpm;
drawTimeline();
updateShareLink();
});
beatsPerBarInput.addEventListener('change', () => {
let val = parseInt(beatsPerBarInput.value);
if (val < 1) val = 1;
if (val > 16) val = 16;
beatsPerBar = val;
beatsPerBarInput.value = beatsPerBar;
drawTimeline();
updateShareLink();
});
// Selecting note outside timeline click - deselect on background click
timeline.addEventListener('click', () => {
if(selectedNoteIndex!==-1){
selectedNoteIndex = -1;
drawTimeline();
}
});
// Share URL state saving and loading
function updateShareLink() {
if(!audio) return;
const state = {
audioUrl: audio.src,
bpm,
beatsPerBar,
notes,
};
const json = JSON.stringify(state);
const encoded = encodeURIComponent(btoa(json));
shareLinkInput.value = location.origin + location.pathname + '#/' + encoded;
}
// Load state from URL hash if present
function loadStateFromHash() {
if (!location.hash.startsWith('#/')) return;
try {
const base64 = location.hash.slice(2);
const json = atob(decodeURIComponent(base64));
const state = JSON.parse(json);
if(state.audioUrl){
audioUrlInput.value = state.audioUrl;
bpm = state.bpm || 120;
beatsPerBar = state.beatsPerBar || 4;
bpmInput.value = bpm;
beatsPerBarInput.value = beatsPerBar;
notes = state.notes || [];
selectedNoteIndex = -1;
loadAudio(state.audioUrl);
}
} catch (err) {
console.warn('Failed to load state from URL hash', err);
}
}
window.addEventListener('hashchange', loadStateFromHash);
// Init
loadStateFromHash();
})();
</script>
</body>
</html>
How to use:
- Paste the above code into an
.html file and open in a modern browser.
- Enter an audio URL (CORS-enabled is best, or try some simple mp3 webradios or your own files).
- Click Load Audio.
- Adjust BPM and beats per bar if needed.
- Press Play.
- Click on the timeline to seek or shift+click to add a note.
- Or press Space to add a note at current time.
- Select notes by clicking on them; press Delete to remove.
- Use left/right arrows to seek backward/forward.
- The Shareable Link updates automatically. You can copy/paste it to reload current editor state.
Notes
- The timeline width scales automatically with audio duration and zoom.
- The notes show up as blue blocks on the timeline under bars.
- Can be extended by adding different note types, quantization, different lanes, patterns, etc.
- The URL hash stores current editor state in base64 JSON for easy sharing.
- To avoid cross-origin issues, use audio URLs that allow CORS or run locally with proper permissions.
If you want me to add more features like multiple lanes, quantizing notes to beats, or export formats, just ask!