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: 1rem;
background: #222;
color: #eee;
}
h1 {
margin-bottom: 0.25rem;
}
label {
display: inline-block;
margin: 0.5rem 1rem 0.5rem 0;
}
input[type="text"], input[type="number"] {
background: #333;
color: #eee;
border: 1px solid #444;
padding: 0.3rem;
border-radius: 3px;
}
button {
background: #444;
border: none;
color: #eee;
padding: 0.5rem 1rem;
margin: 0.3rem 0.5rem 0.3rem 0;
border-radius: 3px;
cursor: pointer;
}
button:hover {
background: #666;
}
#timeline {
position: relative;
margin-top: 1rem;
background: #111;
border: 1px solid #444;
height: 120px;
user-select: none;
overflow-x: auto;
}
#timeline canvas {
display: block;
background: #141414;
}
#notes {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
pointer-events: none;
}
.note {
position: absolute;
width: 12px;
height: 20px;
background: #e55;
border-radius: 2px;
cursor: pointer;
pointer-events: auto;
user-select: none;
}
#share-link {
display: block;
margin-top: 1rem;
background: #222;
padding: 0.5rem;
border: 1px solid #444;
color: #0f0;
font-family: monospace;
white-space: nowrap;
overflow-x: auto;
}
#info {
font-size: 0.85rem;
color: #999;
margin-top: 0.5rem;
}
</style>
</head>
<body>
<h1>Rhythm Game Editor</h1>
<label>
Audio URL:
<input type="text" id="audio-url" placeholder="https://example.com/song.mp3" size="50" />
</label>
<button id="load-audio">Load</button>
<button id="play-pause" disabled>Play</button>
<button id="record-note" disabled>Record Note (Space)</button>
<label>
Bars:
<input type="number" id="bars-count" value="4" min="1" max="16" />
</label>
<label>
Beats per Bar:
<input type="number" id="beats-per-bar" value="4" min="1" max="16" />
</label>
<div id="timeline" tabindex="0" aria-label="Timeline of rhythm notes">
<canvas id="grid-canvas" height="120"></canvas>
<div id="notes"></div>
</div>
<div id="info">
<strong>Shortcuts:</strong> <br/>
Spacebar = Record note at current position | Backspace/Delete = Delete last note | Click note = Delete note
</div>
<label>
Share:
<input type="text" id="share-link" readonly />
</label>
<script>
(() => {
const audioUrlInput = document.getElementById('audio-url');
const loadAudioBtn = document.getElementById('load-audio');
const playPauseBtn = document.getElementById('play-pause');
const recordNoteBtn = document.getElementById('record-note');
const barsInput = document.getElementById('bars-count');
const beatsInput = document.getElementById('beats-per-bar');
const timeline = document.getElementById('timeline');
const gridCanvas = document.getElementById('grid-canvas');
const notesContainer = document.getElementById('notes');
const shareInput = document.getElementById('share-link');
let audio = null;
let isPlaying = false;
let barsCount = parseInt(barsInput.value);
let beatsPerBar = parseInt(beatsInput.value);
const BPM = 120; // default BPM for snapping (can be advanced later)
// Data model for notes: array of times in seconds (snapped to beat)
let notes = [];
// Constants for drawing:
// We'll map the audio's length or bars count and BPM to width
// Let's define pixels per beat:
const pxPerBeat = 80;
// Audio duration (seconds), default 0 (updated on loaded metadata)
let audioDuration = 0;
// Play/pause handling
function createAudio(url) {
if (audio) {
audio.pause();
audio = null;
}
audio = new Audio(url);
audio.crossOrigin = "anonymous";
audio.preload = "auto";
audio.addEventListener('loadedmetadata', () => {
audioDuration = audio.duration;
if (audioDuration === Infinity || isNaN(audioDuration)) {
// For some streams, duration is unknown, fallback:
audioDuration = barsCount * beatsPerBar * 60 / BPM;
}
redrawGrid();
redrawNotes();
playPauseBtn.disabled = false;
recordNoteBtn.disabled = false;
updateShareLink();
});
audio.addEventListener('ended', () => {
isPlaying = false;
playPauseBtn.textContent = 'Play';
});
}
// Play/pause toggle
function togglePlay() {
if (!audio) return;
if (audio.paused) {
audio.play();
isPlaying = true;
playPauseBtn.textContent = 'Pause';
updateLoop();
} else {
audio.pause();
isPlaying = false;
playPauseBtn.textContent = 'Play';
}
}
// Record note at current audio time (snapped to nearest beat)
function recordNoteAtCurrentTime() {
if (!audio) return;
const curTime = audio.currentTime;
// snap time to nearest beat
// Beat duration in seconds = 60/BPM
const beatDuration = 60 / BPM;
let snappedTime = Math.round(curTime / beatDuration) * beatDuration;
// Clamp snappedTime to audio duration or bar length, whichever smaller
const maxTime = Math.min(audioDuration, barsCount * beatsPerBar * beatDuration);
if (snappedTime > maxTime) snappedTime = maxTime;
// Avoid duplicate note at same time (+/- small epsilon)
const epsilon = 0.02;
if (notes.some(t => Math.abs(t - snappedTime) < epsilon)) {
return; // do not add duplicate
}
notes.push(snappedTime);
notes.sort((a,b) => a-b);
redrawNotes();
updateShareLink();
}
// Delete the last note
function deleteLastNote() {
if (notes.length > 0) {
notes.pop();
redrawNotes();
updateShareLink();
}
}
// Delete specific note by time
function deleteNoteAtTime(time) {
notes = notes.filter(t => t !== time);
redrawNotes();
updateShareLink();
}
// Drawing the grid representing bars and beats
function redrawGrid() {
const ctx = gridCanvas.getContext('2d');
barsCount = parseInt(barsInput.value);
beatsPerBar = parseInt(beatsInput.value);
const totalBeats = barsCount * beatsPerBar;
const canvasWidth = pxPerBeat * totalBeats;
gridCanvas.width = canvasWidth;
gridCanvas.height = 120;
// Clear
ctx.clearRect(0, 0, canvasWidth, 120);
// Fill background
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, canvasWidth, 120);
ctx.strokeStyle = '#555';
ctx.lineWidth = 1;
ctx.font = "12px monospace";
ctx.fillStyle = '#777';
ctx.textAlign = 'center';
// Draw vertical lines for beats and bars
for (let i=0; i<= totalBeats; i++) {
const x = i * pxPerBeat + 0.5;
if(i % beatsPerBar === 0) {
// Bar lines thicker
ctx.strokeStyle = '#aaa';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x,0);
ctx.lineTo(x,120);
ctx.stroke();
// Label bar number (1-based)
let barNumber = i / beatsPerBar + 1;
if (i !== totalBeats) {
ctx.fillText(`Bar ${barNumber}`, x + pxPerBeat*beatsPerBar/2, 15);
}
} else {
// Beat lines thinner
ctx.strokeStyle = '#555';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x, 30);
ctx.lineTo(x, 120);
ctx.stroke();
}
}
// Draw horizontal lines to indicate note lanes (optional)
ctx.strokeStyle = '#333';
for (let y=40; y<= 100; y += 20) {
ctx.beginPath();
ctx.moveTo(0, y + 0.5);
ctx.lineTo(canvasWidth, y + 0.5);
ctx.stroke();
}
}
// Draw notes in #notes container positioned absolutely
function redrawNotes() {
notesContainer.innerHTML = '';
barsCount = parseInt(barsInput.value);
beatsPerBar = parseInt(beatsInput.value);
const totalBeats = barsCount * beatsPerBar;
const beatDuration = 60 / BPM;
const timelineWidth = pxPerBeat * totalBeats;
notesContainer.style.width = timelineWidth + 'px';
notesContainer.style.height = '120px';
notes.forEach(time => {
// convert time to position:
const x = (time / beatDuration) * pxPerBeat;
const note = document.createElement('div');
note.className = 'note';
note.style.left = (x - 6) + 'px'; // center horizontally (12px wide, so half is 6)
note.style.top = '60px';
note.title = `Time: ${time.toFixed(2)}s`;
note.tabIndex = 0;
// Delete on click
note.addEventListener('click', e => {
e.stopPropagation();
deleteNoteAtTime(time);
});
// Delete on keyboard (delete or backspace) when note focused
note.addEventListener('keydown', e => {
if (e.key === 'Delete' || e.key === 'Backspace') {
deleteNoteAtTime(time);
e.preventDefault();
}
});
notesContainer.appendChild(note);
});
}
// Update the share input with URL containing rhythm and audio data
function updateShareLink() {
const data = {
audioUrl: audioUrlInput.value.trim(),
barsCount: parseInt(barsInput.value),
beatsPerBar: parseInt(beatsInput.value),
notes
};
try {
const json = JSON.stringify(data);
const encoded = encodeURIComponent(json);
// URL hash uses #/
const baseUrl = location.href.split('#')[0];
const shareUrl = `${baseUrl}#/` + encoded;
shareInput.value = shareUrl;
// Update location hash silently without scrolling
history.replaceState(null, '', `#/` + encoded);
} catch (e) {
shareInput.value = 'Error encoding data';
}
}
// Load data from URL hash if available
function loadFromHash() {
if (location.hash.startsWith('#/')) {
try {
const dataStr = decodeURIComponent(location.hash.substring(2));
const data = JSON.parse(dataStr);
if(data.audioUrl) audioUrlInput.value = data.audioUrl;
if(data.barsCount) barsInput.value = data.barsCount;
if(data.beatsPerBar) beatsInput.value = data.beatsPerBar;
if(Array.isArray(data.notes)) notes = data.notes;
barsCount = parseInt(barsInput.value);
beatsPerBar = parseInt(beatsInput.value);
updateShareLink();
redrawGrid();
redrawNotes();
// Automatically load audio
if(data.audioUrl) {
createAudio(data.audioUrl);
playPauseBtn.disabled = false;
recordNoteBtn.disabled = false;
}
} catch(e) {
console.warn('Failed to load data from URL hash:', e);
}
}
}
// UI Event listeners
loadAudioBtn.addEventListener('click', () => {
const url = audioUrlInput.value.trim();
if (!url) {
alert('Please enter an audio URL');
return;
}
createAudio(url);
notes = [];
redrawNotes();
updateShareLink();
});
playPauseBtn.addEventListener('click', togglePlay);
recordNoteBtn.addEventListener('click', recordNoteAtCurrentTime);
barsInput.addEventListener('change', () => {
barsCount = parseInt(barsInput.value);
redrawGrid();
redrawNotes();
updateShareLink();
});
beatsInput.addEventListener('change', () => {
beatsPerBar = parseInt(beatsInput.value);
redrawGrid();
redrawNotes();
updateShareLink();
});
// Spacebar records note, Backspace/Delete deletes last note
window.addEventListener('keydown', e => {
if(e.target.tagName === 'INPUT') return; // ignore inputs
if(e.code === 'Space') {
e.preventDefault();
if(!playPauseBtn.disabled) recordNoteAtCurrentTime();
} else if(e.key === 'Backspace' || e.key === 'Delete') {
e.preventDefault();
deleteLastNote();
}
});
// Periodically update playhead on grid for visual feedback
function updateLoop() {
if(isPlaying && audio) {
drawPlayhead();
requestAnimationFrame(updateLoop);
} else {
drawPlayhead(false);
}
}
function drawPlayhead(draw = true) {
const ctx = gridCanvas.getContext('2d');
redrawGrid(); // redraw entire grid including bars
redrawNotes();
if(!draw || !audio) return;
const beatDuration = 60 / BPM;
const time = audio.currentTime;
const x = (time / beatDuration) * pxPerBeat;
ctx.strokeStyle = '#0f0';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x+0.5, 0);
ctx.lineTo(x+0.5, 120);
ctx.stroke();
}
// Initialize all on load
loadFromHash();
redrawGrid();
redrawNotes();
})();
</script>
</body>
</html>
But for a solid starting point that covers your requirements, this should work well!