gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js rhythm game editor mp3 bpm share "#/"
- User
html css js rhythm game editor mp3 bpm share "#/"
- Assistant
html css js rhythm game editor mp3 bpm share "#/"
Certainly! Below is a simple web-based Rhythm Game Editor that lets you:
#/ followed by encoded data) so others can load your beats.The project uses HTML, CSS, and JavaScript only (no external dependencies). You can save your work and share a link.
Note: The actual audio file cannot be shared via URL hash because that would be huge. Instead, the user uploads the MP3 each time. The URL shares BPM and notes so others can reload if they use the same audio file.
<!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: #121212;
color: #eee;
}
h1 {
margin-bottom: 0.2em;
font-weight: 700;
color: #0af;
}
label {
font-weight: 600;
margin-right: 0.5em;
}
input[type="number"], input[type="file"] {
background: #222;
border: 1px solid #444;
border-radius: 3px;
color: #eee;
padding: 4px 6px;
}
button {
background: #0af;
border: none;
color: white;
padding: 8px 12px;
margin-left: 8px;
cursor: pointer;
border-radius: 3px;
font-weight: 600;
}
button:disabled {
background: #555;
cursor: default;
}
#timeline {
margin-top: 20px;
background: #1e1e1e;
border: 2px solid #0af;
height: 40px;
position: relative;
user-select: none;
cursor: pointer;
border-radius: 5px;
}
#timeline .notes {
position: absolute;
top: 0; bottom: 0;
height: 100%;
pointer-events: none;
}
#timeline .note {
position: absolute;
width: 8px;
height: 100%;
background: #0af;
opacity: 0.8;
transform: translateX(-50%);
}
#timeline .playhead {
position: absolute;
top: 0;
bottom: 0;
width: 2px;
background: #f00;
pointer-events: none;
transform: translateX(-50%);
}
#status {
margin-top: 10px;
}
#share-url {
width: 100%;
margin-top: 8px;
background: #222;
border: 1px solid #444;
color: #0af;
font-family: monospace;
font-size: 0.9em;
padding: 6px;
border-radius: 3px;
}
#instructions {
font-size: 0.9em;
color: #888;
margin-top: 15px;
max-width: 600px;
}
a {
color: #0af;
}
</style>
</head>
<body>
<h1>Rhythm Game Editor</h1>
<div>
<label for="audioFile">Upload MP3:</label>
<input type="file" id="audioFile" accept="audio/mpeg" />
</div>
<div style="margin-top:10px;">
<label for="bpmInput">BPM:</label>
<input type="number" id="bpmInput" min="20" max="400" step="1" value="120" />
<button id="loadBtn" disabled>Load</button>
</div>
<div id="status"></div>
<div id="timeline" title="Click to toggle note at this position (beat)"></div>
<div style="margin-top: 20px;">
<button id="playBtn" disabled>Play</button>
<button id="pauseBtn" disabled>Pause</button>
</div>
<textarea id="share-url" readonly placeholder="Load your song and notes, then click Load. The share URL appears here."></textarea>
<div id="instructions">
<p><strong>How to use:</strong></p>
<ol>
<li>Upload your MP3 file.</li>
<li>Set the BPM.</li>
<li>Click the timeline to place or remove notes at beats.</li>
<li>Use Play/Pause to preview.</li>
<li>Copy the link in the box to share your beat map (the same MP3 file required to play).</li>
<li>Others can load the same MP3, paste the URL, then click Load to import notes and BPM.</li>
</ol>
<p><em>Note:</em> The audio file is not embedded in the link. The same file must be uploaded to share the level properly.</p>
</div>
<script>
(() => {
const fileInput = document.getElementById('audioFile');
const bpmInput = document.getElementById('bpmInput');
const loadBtn = document.getElementById('loadBtn');
const playBtn = document.getElementById('playBtn');
const pauseBtn = document.getElementById('pauseBtn');
const timeline = document.getElementById('timeline');
const status = document.getElementById('status');
const shareURL = document.getElementById('share-url');
let audioCtx;
let audioBuffer;
let sourceNode;
let startTime = 0;
let pausedAt = 0;
let isPlaying = false;
let animationFrameId;
// Storing notes as array of floats representing beat positions
let notes = [];
// Cached audio file info for hash check
let audioFileName = '';
let audioFileSize = 0; // to check same file
// Timeline size and constants
const timelineWidth = 800; // px - fixed by CSS/JS calc?
// We'll use a relative width from the timeline element, sync this dynamically
function updateTimelineWidth() {
timelineWidthValue = timeline.clientWidth;
}
// Calculate seconds per beat from BPM
function secondsPerBeat() {
return 60 / bpmInput.value;
}
// Clear previous audio nodes and context
function stopAudio() {
if (sourceNode) {
try {
sourceNode.stop();
} catch {}
sourceNode.disconnect();
sourceNode = null;
}
if (audioCtx) {
// audioCtx.close(); audio context reuse causes delays sometimes, so not closing
}
isPlaying = false;
cancelAnimationFrame(animationFrameId);
updatePlayhead(0);
playBtn.disabled = false;
pauseBtn.disabled = true;
}
// Play from pause or start
function playAudio() {
if (!audioBuffer) return;
if (!audioCtx) {
audioCtx = new AudioContext();
}
sourceNode = audioCtx.createBufferSource();
sourceNode.buffer = audioBuffer;
sourceNode.connect(audioCtx.destination);
const offset = pausedAt;
startTime = audioCtx.currentTime - offset;
sourceNode.start(0, offset);
isPlaying = true;
playBtn.disabled = true;
pauseBtn.disabled = false;
sourceNode.onended = () => {
isPlaying = false;
pausedAt = 0;
updatePlayhead(0);
playBtn.disabled = false;
pauseBtn.disabled = true;
cancelAnimationFrame(animationFrameId);
};
animationFrameId = requestAnimationFrame(updateAnimation);
}
function pauseAudio() {
if (!audioCtx || !sourceNode) return;
pausedAt = audioCtx.currentTime - startTime;
stopAudio();
}
// Updates the red playhead indicator while playing
function updateAnimation() {
if (!isPlaying) return;
let elapsed = audioCtx.currentTime - startTime;
if (elapsed > audioBuffer.duration) {
elapsed = audioBuffer.duration;
}
const progress = elapsed / audioBuffer.duration;
updatePlayhead(progress);
animationFrameId = requestAnimationFrame(updateAnimation);
}
// Draw playhead position on timeline: 0..1 range
function updatePlayhead(progress) {
let playhead = timeline.querySelector('.playhead');
if (!playhead) {
playhead = document.createElement('div');
playhead.classList.add('playhead');
timeline.appendChild(playhead);
}
playhead.style.left = (progress * 100) + '%';
}
// Draw notes on timeline
function renderNotes() {
let notesContainer = timeline.querySelector('.notes');
if (!notesContainer) {
notesContainer = document.createElement('div');
notesContainer.classList.add('notes');
timeline.appendChild(notesContainer);
}
notesContainer.innerHTML = '';
if (!audioBuffer) return;
const totalBeats = (audioBuffer.duration / secondsPerBeat());
notes.forEach(noteBeat => {
const leftPct = (noteBeat / totalBeats) * 100;
const noteEl = document.createElement('div');
noteEl.classList.add('note');
noteEl.title = `Beat: ${noteBeat.toFixed(2)}`;
noteEl.style.left = leftPct + '%';
notesContainer.appendChild(noteEl);
});
}
// Add or remove note at beat
function toggleNoteAtBeat(beat) {
const tolerance = 0.2; // beats
// Check if note close to beat exists in notes
const existingIndex = notes.findIndex(n => Math.abs(n - beat) < tolerance);
if (existingIndex >= 0) {
// Remove note
notes.splice(existingIndex, 1);
} else {
// Add note
notes.push(beat);
notes.sort((a,b) => a-b);
}
renderNotes();
updateShareURL();
}
// Get beat from timeline click x-coordinate
function getBeatFromClick(clientX) {
const rect = timeline.getBoundingClientRect();
const x = clientX - rect.left;
let pct = x / rect.width;
if (pct < 0) pct = 0;
if (pct > 1) pct = 1;
if (!audioBuffer) return 0;
const duration = audioBuffer.duration;
const beatLength = secondsPerBeat();
const totalBeats = duration / beatLength;
const beat = pct * totalBeats;
return beat;
}
function updateStatus(msg, error = false) {
status.textContent = msg;
status.style.color = error ? 'crimson' : '#0af';
}
// Load state from URL hash like #/BPM|note1,note2,note3|fileHash
// fileHash used just for reference - must still upload same file manually
// Example: #/120|1.0,3.5,4.0|abc123
function loadFromHash() {
let hash = window.location.hash;
if (!hash.startsWith('#/')) return false;
hash = hash.slice(2); // remove "#/"
const parts = hash.split('|');
if (parts.length < 2) return false;
const bpm = parseInt(parts[0],10);
if (isNaN(bpm)) return false;
bpmInput.value = bpm;
if (parts[1]) {
notes = parts[1].split(',').map(s => parseFloat(s)).filter(n => !isNaN(n));
} else {
notes = [];
}
// fileHash can be parts[2], not used for logic but stored
updateShareURL();
return true;
}
// Save state to URL hash
function updateShareURL() {
if (!audioFileName) {
shareURL.value = 'Upload audio file to enable sharing.';
return;
}
const bpm = bpmInput.value;
const notesStr = notes.map(n => n.toFixed(2)).join(',');
const fileHash = simpleHash(audioFileName + audioFileSize);
const encoded = `${bpm}|${notesStr}|${fileHash}`;
const url = window.location.origin + window.location.pathname + '#/' + encoded;
shareURL.value = url;
}
// Simple hash function for file name + size
// used as a crude fingerprint for file identity
function simpleHash(str) {
let hash = 0;
for (let i=0; i<str.length; i++) {
const chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return Math.abs(hash).toString(36);
}
// On file selected, load audio buffer
function loadAudioFile(file) {
if (!file.type.startsWith('audio/')) {
updateStatus('Invalid audio file type.', true);
return;
}
if (audioCtx) {
audioCtx.close();
audioCtx = null;
}
const reader = new FileReader();
reader.onload = e => {
const arrayBuffer = e.target.result;
if (!audioCtx) audioCtx = new AudioContext();
audioCtx.decodeAudioData(arrayBuffer).then(buffer => {
audioBuffer = buffer;
audioFileName = file.name;
audioFileSize = file.size;
updateStatus(`Loaded audio: ${file.name} (${buffer.duration.toFixed(2)} sec)`);
playBtn.disabled = true;
pauseBtn.disabled = true;
loadBtn.disabled = false;
notes = [];
renderNotes();
updatePlayhead(0);
// Try loading hash notes if present
if (loadFromHash()) {
renderNotes();
updateStatus(`Loaded audio and applied notes from URL.`);
}
}).catch(err => {
updateStatus(`Error decoding audio: ${err.message}`, true);
});
};
reader.readAsArrayBuffer(file);
}
// On Load button click: just redraw notes and update share URL
// (Necessary when importing from URL)
function onLoadClick() {
if (!audioBuffer) {
updateStatus('Load an audio file first.', true);
return;
}
renderNotes();
updateShareURL();
playBtn.disabled = false;
pauseBtn.disabled = true;
}
// Timeline click to toggle note at beat
timeline.addEventListener('click', e => {
if (!audioBuffer) return;
const beat = getBeatFromClick(e.clientX);
toggleNoteAtBeat(beat);
});
// Event listeners
fileInput.addEventListener('change', e => {
if (e.target.files.length > 0) {
loadAudioFile(e.target.files[0]);
}
});
bpmInput.addEventListener('input', () => {
if (!audioBuffer) return;
renderNotes();
updateShareURL();
});
loadBtn.addEventListener('click', () => {
onLoadClick();
});
playBtn.addEventListener('click', () => {
if (!audioBuffer) {
updateStatus('Load audio first', true);
return;
}
if (!audioCtx) audioCtx = new AudioContext();
playAudio();
});
pauseBtn.addEventListener('click', () => {
pauseAudio();
});
// Resize observer to adjust timeline width for notes
function onResize() {
renderNotes();
}
window.addEventListener('resize', onResize);
// On page load, attempt to load notes from URL but no audio yet loaded
window.addEventListener('load', () => {
loadFromHash();
});
})();
</script>
</body>
</html>
If you want me to help you add features like exporting/importing JSON, or more complex editing, let me know!
Not yet included in a published release.