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: 10px;
background: #111;
color: #eee;
}
h1 {
margin-bottom: 0.3em;
}
label {
margin-right: 0.5em;
}
input[type=text], input[type=number] {
width: 250px;
padding: 4px 6px;
background-color: #222;
border: 1px solid #555;
color: #eee;
border-radius: 4px;
}
input[type=number] {
width: 70px;
}
button {
padding: 5px 12px;
background-color: #3366cc;
border: none;
border-radius: 4px;
color: white;
font-weight: bold;
cursor: pointer;
margin-left: 10px;
}
button:hover {
background-color: #5588ff;
}
#timeline {
margin-top: 20px;
position: relative;
background: #222;
border: 2px solid #555;
width: 100%;
max-width: 960px;
height: 120px;
user-select: none;
font-size: 12px;
}
#bars {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
display: flex;
border-left: 2px solid #666;
}
.bar {
flex-grow: 1;
border-right: 2px solid #666;
position: relative;
box-sizing: border-box;
}
.beat-line {
position: absolute;
top: 0; bottom: 0;
width: 1px;
background: #444;
}
.beat-label {
position: absolute;
bottom: 2px;
color: #666;
font-size: 10px;
user-select: none;
pointer-events: none;
}
#notes {
position: absolute;
top: 0; left: 0;
height: 100%;
width: 100%;
pointer-events: none;
}
.note {
position: absolute;
width: 16px;
height: 16px;
background: #ff4444;
border-radius: 50%;
border: 2px solid white;
cursor: pointer;
pointer-events: auto;
transition: transform 0.2s ease;
}
.note.selected {
box-shadow: 0 0 8px 3px #ff8888;
transform: scale(1.3);
}
#status {
margin-top: 10px;
font-size: 13px;
color: #bbb;
}
#footer {
margin-top: 30px;
font-size: 11px;
color: #555;
max-width: 960px;
}
a {
color: #6699ff;
}
</style>
</head>
<body>
<h1>Rhythm Game Editor</h1>
<label for="mp3url">MP3 URL:</label>
<input id="mp3url" type="text" placeholder="https://..." />
<label for="bpm">BPM:</label>
<input id="bpm" type="number" min="10" max="400" value="120" />
<label for="bars">Bars:</label>
<input id="barscount" type="number" min="1" max="64" value="8" />
<button id="loadbtn">Load</button>
<div id="timeline" tabindex="0" aria-label="Rhythm Game timeline editor"></div>
<div id="status">Press Space to play/pause. Click timeline to add notes. Select notes to delete.</div>
<div id="footer">
<p>Keyboard Shortcuts: <b>Space</b> - Play/Pause, <b>Delete</b> - Delete selected note, <b>Arrow Left/Right</b> - Select previous/next note.</p>
<p>Share your editor state by copying the current URL. The state (MP3 URL, BPM, bars, notes) is saved in the URL hash.</p>
</div>
<script>
(() => {
const mp3urlInput = document.getElementById('mp3url');
const bpmInput = document.getElementById('bpm');
const barsCountInput = document.getElementById('barscount');
const loadBtn = document.getElementById('loadbtn');
const timeline = document.getElementById('timeline');
let audio = null;
let isPlaying = false;
let animationFrameId;
// Editor state
let state = {
mp3url: '',
bpm: 120,
bars: 8,
notes: [], // {bar: int, beat: float, id: int}
};
// Selection
let selectedNoteId = null;
// Constants
const beatsPerBar = 4;
// Helpers to serialize and deserialize state in URL hash
function encodeState(state) {
// Serialize notes as "b,b.b,b.b,..."
// bar and beat combined, e.g. "3,2.5"
// Will use JSON stringify and encodeURIComponent for simplicity
return encodeURIComponent(JSON.stringify(state));
}
function decodeState(s) {
try {
return JSON.parse(decodeURIComponent(s));
} catch {
return null;
}
}
// Load state from URL hash if available
function loadStateFromUrl() {
if(location.hash.startsWith('#')) {
const raw = location.hash.slice(1);
const loaded = decodeState(raw);
if(loaded && typeof loaded === 'object') {
if(typeof loaded.mp3url === 'string') state.mp3url = loaded.mp3url;
if(typeof loaded.bpm === 'number') state.bpm = loaded.bpm;
if(typeof loaded.bars === 'number') state.bars = loaded.bars;
if(Array.isArray(loaded.notes)) {
state.notes = loaded.notes.filter(n =>
Number.isInteger(n.bar) && typeof n.beat === 'number'
);
}
}
}
syncInputsToState();
}
// Update URL hash with current state
function updateUrlHash() {
location.hash = '#' + encodeState(state);
}
// Sync inputs from state
function syncInputsToState() {
mp3urlInput.value = state.mp3url || '';
bpmInput.value = state.bpm || 120;
barsCountInput.value = state.bars || 8;
}
// Update state from inputs
function updateStateFromInputs() {
state.mp3url = mp3urlInput.value.trim();
const bpmVal = Math.min(Math.max(parseInt(bpmInput.value), 10), 400);
if(!isNaN(bpmVal)) state.bpm = bpmVal;
const barsVal = Math.min(Math.max(parseInt(barsCountInput.value),1),64);
if(!isNaN(barsVal)) state.bars = barsVal;
}
// Create audio element and setup
function setupAudio() {
if(audio) {
audio.pause();
audio.remove();
audio = null;
}
if(state.mp3url) {
audio = new Audio(state.mp3url);
audio.crossOrigin = "anonymous";
audio.preload = 'auto';
}
}
// Play/pause toggle
function togglePlay() {
if(!audio) return;
if(isPlaying) {
audio.pause();
isPlaying = false;
cancelAnimationFrame(animationFrameId);
} else {
audio.play();
isPlaying = true;
animationFrameId = requestAnimationFrame(animationLoop);
}
}
// Animation loop to repaint timeline while playing
function animationLoop() {
renderTimeline();
if(isPlaying) {
animationFrameId = requestAnimationFrame(animationLoop);
}
}
// Render timeline: bars, beats, notes, playhead
function renderTimeline() {
timeline.innerHTML = ''; // clear
let w = timeline.clientWidth;
let h = timeline.clientHeight;
// Container divs
const barsDiv = document.createElement('div');
barsDiv.id = 'bars';
timeline.appendChild(barsDiv);
// Create bars
for(let b=0; b<state.bars; b++) {
const barDiv = document.createElement('div');
barDiv.className = 'bar';
barDiv.dataset.bar = b;
barsDiv.appendChild(barDiv);
// Add beat lines
for(let beat=1; beat<beatsPerBar; beat++) {
const beatLine = document.createElement('div');
beatLine.className = 'beat-line';
const pct = (beat / beatsPerBar) * 100;
beatLine.style.left = pct + '%';
barDiv.appendChild(beatLine);
// Beat number label
const label = document.createElement('div');
label.className = 'beat-label';
label.style.left = (pct - 2) + '%';
label.textContent = beat + 1;
barDiv.appendChild(label);
}
// Bar number label (top-left)
const barLabel = document.createElement('div');
barLabel.style.position = 'absolute';
barLabel.style.left = '4px';
barLabel.style.top = '4px';
barLabel.style.color = '#666';
barLabel.style.fontSize = '10px';
barLabel.style.userSelect = 'none';
barLabel.textContent = 'Bar ' + (b + 1);
barDiv.appendChild(barLabel);
}
// Notes container
const notesDiv = document.createElement('div');
notesDiv.id = 'notes';
timeline.appendChild(notesDiv);
// Render notes
state.notes.forEach(note => {
if(note.bar < 0 || note.bar >= state.bars) return;
// Compute left % position: bar start + (beat / beatsPerBar)
const barWidthPct = 100 / state.bars;
const leftPct = barWidthPct * note.bar + barWidthPct * (note.beat / beatsPerBar);
const noteDiv = document.createElement('div');
noteDiv.className = 'note';
noteDiv.style.left = `calc(${leftPct}% - 8px)`; // 8px offset half note width
noteDiv.style.top = `calc(50% - 8px)`;
noteDiv.title = `Bar ${note.bar+1}, Beat ${note.beat.toFixed(2)}`;
noteDiv.dataset.id = note.id;
if(note.id === selectedNoteId) {
noteDiv.classList.add('selected');
}
// Click selects note
noteDiv.addEventListener('click', e => {
e.stopPropagation();
selectedNoteId = note.id;
renderTimeline();
});
notesDiv.appendChild(noteDiv);
});
// Playhead
if(audio && !audio.paused) {
const secondsPerBeat = 60 / state.bpm;
const secondsPerBar = secondsPerBeat * beatsPerBar;
const currentTime = audio.currentTime;
const totalDurationSec = secondsPerBar * state.bars;
let playheadX = 0;
if(currentTime < totalDurationSec) {
playheadX = (currentTime / totalDurationSec) * 100;
} else {
playheadX = 100;
}
const playhead = document.createElement('div');
playhead.style.position = 'absolute';
playhead.style.left = playheadX + '%';
playhead.style.top = '0';
playhead.style.height = '100%';
playhead.style.width = '2px';
playhead.style.background = '#ffcc00';
playhead.style.zIndex = '10';
timeline.appendChild(playhead);
}
}
// Add note on click
function onTimelineClick(e) {
const rect = timeline.getBoundingClientRect();
const x = e.clientX - rect.left;
// Find bar and beat from x
const pxWidth = rect.width;
const pct = x / pxWidth;
const totalBeats = beatsPerBar * state.bars;
const exactBeat = pct * totalBeats;
let bar = Math.floor(exactBeat / beatsPerBar);
let beat = exactBeat % beatsPerBar;
// Snap beat to nearest 1/4 beats (optional: you can do finer snapping here)
const beatSnap = 0.25; // quarter beat snapping
beat = Math.round(beat / beatSnap) * beatSnap;
if(beat >= beatsPerBar) {
beat = 0;
bar++;
}
if(bar >= state.bars) bar = state.bars-1;
// Add new note with a unique ID
const newNote = {
bar,
beat,
id: Date.now() + Math.floor(Math.random()*1000),
};
state.notes.push(newNote);
selectedNoteId = newNote.id;
updateUrlHash();
renderTimeline();
}
// Delete selected note
function deleteSelectedNote() {
if(selectedNoteId === null) return;
const idx = state.notes.findIndex(n => n.id === selectedNoteId);
if(idx >= 0) {
state.notes.splice(idx, 1);
selectedNoteId = null;
updateUrlHash();
renderTimeline();
}
}
// Select next or previous note
function selectNextNote(forward = true) {
if(state.notes.length === 0) return;
if(selectedNoteId === null) {
selectedNoteId = state.notes[0].id;
renderTimeline();
return;
}
// Sort notes by bar then beat
const sortedNotes = state.notes.slice().sort((a,b) => {
if(a.bar !== b.bar) return a.bar - b.bar;
return a.beat - b.beat;
});
let idx = sortedNotes.findIndex(n => n.id === selectedNoteId);
if(idx === -1) {
selectedNoteId = sortedNotes[0].id;
renderTimeline();
return;
}
idx += forward ? 1 : -1;
if(idx < 0) idx = sortedNotes.length - 1;
if(idx >= sortedNotes.length) idx = 0;
selectedNoteId = sortedNotes[idx].id;
renderTimeline();
}
// Event listeners
loadBtn.addEventListener('click', () => {
updateStateFromInputs();
setupAudio();
selectedNoteId = null;
renderTimeline();
updateUrlHash();
});
timeline.addEventListener('click', e => {
onTimelineClick(e);
});
timeline.addEventListener('keydown', e => {
if(e.code === 'Space') {
e.preventDefault();
togglePlay();
} else if(e.code === 'Delete') {
e.preventDefault();
deleteSelectedNote();
} else if(e.code === 'ArrowRight') {
e.preventDefault();
selectNextNote(true);
} else if(e.code === 'ArrowLeft') {
e.preventDefault();
selectNextNote(false);
}
});
// Also keyboard shortcuts globally for convenience if timeline not focused
window.addEventListener('keydown', e => {
if(document.activeElement !== timeline) return;
if(e.code === 'Space') {
e.preventDefault();
togglePlay();
} else if(e.code === 'Delete') {
e.preventDefault();
deleteSelectedNote();
} else if(e.code === 'ArrowRight') {
e.preventDefault();
selectNextNote(true);
} else if(e.code === 'ArrowLeft') {
e.preventDefault();
selectNextNote(false);
}
});
// Update URL when inputs change
[mp3urlInput, bpmInput, barsCountInput].forEach(input => {
input.addEventListener('change', () => {
updateStateFromInputs();
setupAudio();
selectedNoteId = null;
renderTimeline();
updateUrlHash();
});
});
// Initialization
function init() {
loadStateFromUrl();
setupAudio();
renderTimeline();
// Focus timeline for keyboard shortcuts
timeline.focus();
}
init();
})();
</script>
</body>
</html>
Or any other direct MP3 URL with CORS enabled.
If you'd like me to add more advanced features (e.g. step snapping options, note types, loop playback, export/import JSON files, etc.), just ask!