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: 0;
background: #222;
color: #eee;
display: flex;
flex-direction: column;
height: 100vh;
}
header, footer {
background: #111;
padding: 10px 20px;
}
header {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
}
header label {
font-size: 0.9em;
}
input[type="text"], input[type="number"] {
padding: 5px;
font-size: 1em;
background: #333;
border: 1px solid #555;
border-radius: 4px;
color: #eee;
width: 250px;
}
input[type="number"] {
width: 80px;
}
#combo-count {
margin-left: auto;
font-weight: bold;
font-size: 1.2em;
}
#timeline {
flex: 1;
overflow-x: auto;
background: #111;
border-top: 1px solid #444;
border-bottom: 1px solid #444;
padding: 20px;
user-select: none;
}
.bar {
display: inline-block;
vertical-align: top;
width: 160px;
border-right: 1px solid #444;
margin-right: 5px;
}
.bar-label {
text-align: center;
margin-bottom: 6px;
font-size: 0.8em;
color: #888;
}
.counts {
display: flex;
justify-content: space-between;
}
.count {
flex: 1;
height: 50px;
margin: 0 1px;
background: #333;
border-radius: 4px;
border: 1px solid #555;
position: relative;
cursor: pointer;
transition: background-color 0.2s;
}
.count:hover {
background: #555;
}
.count.active {
background: #0a84ff;
box-shadow: 0 0 8px #0af;
}
.count.delete-mode {
background: #880000;
}
footer {
font-size: 0.9em;
color: #666;
}
@media (max-width: 600px) {
#timeline {
padding: 10px;
}
.bar {
width: 100px;
margin-right: 3px;
}
}
</style>
</head>
<body>
<header>
<label>MP3 URL: <input type="text" id="mp3-url" placeholder="Paste MP3 URL here" /></label>
<label>BPM: <input type="number" id="bpm" min="30" max="300" value="120" /></label>
<label>Beats / Bar:
<select id="beats-per-bar">
<option value="4" selected>4/4</option>
<option value="3">3/4</option>
<option value="5">5/4</option>
</select>
</label>
<button id="load-mp3">Load</button>
<button id="play-pause">Play</button>
<button id="clear">Clear</button>
<button id="save">Save To URL</button>
<button id="load-pattern">Load From URL</button>
<label><input type="checkbox" id="delete-mode" /> Delete Mode (D)</label>
<div id="combo-count">Combo: 0</div>
</header>
<div id="timeline" tabindex="0" aria-label="Timeline editor"></div>
<footer>
Shortcuts: Space = Play/Pause | D = Toggle Delete | ←/→ = Navigate Bars | S = Save | L = Load
</footer>
<script>
(() => {
const mp3UrlInput = document.getElementById('mp3-url');
const bpmInput = document.getElementById('bpm');
const beatsPerBarSel = document.getElementById('beats-per-bar');
const loadMp3Btn = document.getElementById('load-mp3');
const playPauseBtn = document.getElementById('play-pause');
const timeline = document.getElementById('timeline');
const comboCount = document.getElementById('combo-count');
const clearBtn = document.getElementById('clear');
const saveBtn = document.getElementById('save');
const loadPatternBtn = document.getElementById('load-pattern');
const deleteModeCheckbox = document.getElementById('delete-mode');
// Audio element
let audio = null;
// Settings/state variables
let bpm = +bpmInput.value || 120;
let beatsPerBar = +beatsPerBarSel.value || 4;
let barsCount = 16; // how many bars visible / editable
let notes = {}; // { barIndex: { countIndex: true } }
let currentBarStart = 0;
let deleteMode = false;
// Helpers
function updateComboCount() {
let count = 0;
for (const b in notes) count += Object.keys(notes[b]).length;
comboCount.textContent = 'Combo: ' + count;
}
function renderTimeline() {
timeline.innerHTML = '';
for (let bar = currentBarStart; bar < currentBarStart + barsCount; bar++) {
const barDiv = document.createElement('div');
barDiv.className = 'bar';
barDiv.dataset.bar = bar;
const barLabel = document.createElement('div');
barLabel.className = 'bar-label';
barLabel.textContent = `Bar ${bar + 1}`;
barDiv.appendChild(barLabel);
const countsDiv = document.createElement('div');
countsDiv.className = 'counts';
for (let i = 0; i < beatsPerBar; i++) {
const countDiv = document.createElement('div');
countDiv.className = 'count';
countDiv.dataset.bar = bar;
countDiv.dataset.count = i;
countDiv.title = `Bar ${bar+1} Count ${i+1}`;
if (notes[bar] && notes[bar][i]) {
countDiv.classList.add('active');
}
if (deleteMode) {
countDiv.classList.add('delete-mode');
}
countsDiv.appendChild(countDiv);
}
barDiv.appendChild(countsDiv);
timeline.appendChild(barDiv);
}
updateComboCount();
}
function toggleNote(bar, count) {
if (!notes[bar]) notes[bar] = {};
if (deleteMode) {
// Delete mode only deletes notes
if (notes[bar][count]) {
delete notes[bar][count];
if (Object.keys(notes[bar]).length === 0) {
delete notes[bar];
}
}
} else {
// Toggle note
if (notes[bar][count]) {
delete notes[bar][count];
if (Object.keys(notes[bar]).length === 0) {
delete notes[bar];
}
} else {
notes[bar][count] = true;
}
}
renderTimeline();
}
function clearNotes() {
notes = {};
renderTimeline();
}
function loadAudio(url) {
if (audio) {
audio.pause();
audio.src = '';
}
audio = new Audio(url);
audio.preload = 'auto';
audio.onended = () => {
playPauseBtn.textContent = 'Play';
};
}
function playPauseAudio() {
if (!audio) return;
if (audio.paused) {
audio.play();
playPauseBtn.textContent = 'Pause';
} else {
audio.pause();
playPauseBtn.textContent = 'Play';
}
}
// Encode "notes" state as compact string for sharing (simple)
// Format: bar,count;bar,count;...
function encodeNotes(data) {
let parts = [];
for (let bar in data) {
for (let count in data[bar]) {
parts.push(bar + ',' + count);
}
}
return parts.join(';');
}
// Decode string back to notes object
function decodeNotes(str) {
let obj = {};
if (!str) return obj;
const pairs = str.split(';');
for (const p of pairs) {
if (!p) continue;
let [bar, count] = p.split(',');
bar = +bar;
count = +count;
if (!obj[bar]) obj[bar] = {};
obj[bar][count] = true;
}
return obj;
}
// Save pattern to URL hash: #/bpm=120&beats=4&pattern=0,0;0,2;1,1...
function saveToUrl() {
const pattern = encodeNotes(notes);
const urlData = `bpm=${bpm}&beats=${beatsPerBar}&pattern=${encodeURIComponent(pattern)}`;
history.replaceState(null, '', '#/' + urlData);
alert('Pattern saved in URL hash! Share the URL to share your beat.');
}
// Load pattern from URL hash
function loadFromUrl() {
const hash = location.hash;
if (!hash.startsWith('#/')) return;
try {
const query = hash.slice(2);
const params = new URLSearchParams(query);
const loadedBpm = params.get('bpm');
const loadedBeats = params.get('beats');
const pattern = params.get('pattern');
if (loadedBpm) {
bpm = +loadedBpm;
bpmInput.value = bpm;
}
if (loadedBeats) {
beatsPerBar = +loadedBeats;
beatsPerBarSel.value = beatsPerBar;
}
if (pattern) {
notes = decodeNotes(decodeURIComponent(pattern));
}
renderTimeline();
alert('Pattern loaded from URL hash!');
} catch(e) {
alert('Failed to load pattern: ' + e.message);
}
}
// -- Event Listeners --
loadMp3Btn.onclick = () => {
const url = mp3UrlInput.value.trim();
if (!url) {
alert('Please enter an MP3 URL');
return;
}
loadAudio(url);
};
playPauseBtn.onclick = () => {
playPauseAudio();
};
bpmInput.onchange = () => {
bpm = +bpmInput.value || 120;
renderTimeline();
};
beatsPerBarSel.onchange = () => {
beatsPerBar = +beatsPerBarSel.value || 4;
renderTimeline();
};
clearBtn.onclick = () => {
if (confirm('Clear all notes?')) {
clearNotes();
}
};
saveBtn.onclick = () => {
saveToUrl();
};
loadPatternBtn.onclick = () => {
loadFromUrl();
};
deleteModeCheckbox.onclick = () => {
deleteMode = deleteModeCheckbox.checked;
renderTimeline();
};
timeline.addEventListener('click', e => {
let el = e.target;
if (!el.classList.contains('count')) return;
const bar = +el.dataset.bar;
const count = +el.dataset.count;
toggleNote(bar, count);
});
// Keyboard shortcuts
document.body.addEventListener('keydown', e => {
if (e.target.tagName === 'INPUT' || e.target === timeline) return; // ignore if typing or when timeline focused
if (e.code === 'Space') {
e.preventDefault();
playPauseAudio();
} else if (e.key.toLowerCase() === 'd') {
deleteMode = !deleteMode;
deleteModeCheckbox.checked = deleteMode;
renderTimeline();
} else if (e.key === 'ArrowLeft') {
currentBarStart = Math.max(0, currentBarStart - 1);
renderTimeline();
} else if (e.key === 'ArrowRight') {
currentBarStart++;
renderTimeline();
} else if (e.key.toLowerCase() === 's') {
saveToUrl();
} else if (e.key.toLowerCase() === 'l') {
loadFromUrl();
}
});
// Initial render
renderTimeline();
// Optionally try loading pattern if hash exists on page load
if (location.hash.startsWith('#/')) {
loadFromUrl();
}
})();
</script>
</body>
</html>
If you want, I can help you extend this editor or create a player using this rhythm data!