Creating a complete rhythm game editor with features such as BPM control, audio loading via URL, playing notes, recording beats across bars, rhythm counting, shortcuts, delete functionality, and sharing is a fairly large project. Below is a minimal but functional example to get you started. This includes:
- Setting BPM
- Loading audio from a URL
- Playing/pausing audio
- Displaying bars with beats (16th notes by default)
- Allowing clicking to toggle notes
- Recording new beats live (optional)
- Keyboard shortcuts for actions (space to play/pause, delete to remove notes)
- Sharing the rhythm as a JSON encoded in URL hash
You can extend this foundation with more sophisticated features as needed.
Features implemented in the example:
- BPM input to control tempo.
- Audio URL input and loading.
- Play/Pause audio sync with rhythm steps.
- Visual grid of bars and notes that can be toggled by clicking.
- Keyboard controls: space to start/pause, Delete to clear selected note.
- Basic sharing: saves rhythm pattern in URL hash (JSON encoded).
- Simple rhythm playback with note highlighter.
- Note recording by toggling notes live.
Here is the full HTML+CSS+JS 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: 0; padding: 0;
background: #222;
color: #eee;
display: flex; flex-direction: column; align-items: center;
min-height: 100vh;
}
h1 {
margin: 1rem;
}
#controls {
display: flex;
flex-wrap: wrap;
gap: 1rem;
margin-bottom: 1rem;
align-items: center;
}
#controls label {
font-size: 0.9rem;
}
input[type="number"], input[type="text"] {
padding: 0.3rem 0.5rem;
font-size: 1rem;
}
button {
padding: 0.5rem 1rem;
font-size: 1rem;
cursor: pointer;
background: #444;
border: none;
border-radius: 4px;
color: #eee;
transition: background 0.3s;
}
button:hover {
background: #666;
}
#grid {
display: grid;
grid-template-columns: repeat(16, 30px);
gap: 4px;
}
.note {
width: 30px;
height: 30px;
background: #444;
border-radius: 4px;
cursor: pointer;
user-select: none;
box-sizing: border-box;
border: 1px solid #555;
}
.note.active {
background: #0f0;
box-shadow: 0 0 8px #0f0;
}
.note.current {
border-color: #f00;
}
#bars-label {
margin-top: 0.5rem;
text-align: center;
}
#status {
margin-top: 1rem;
color: #aaa;
font-size: 0.9rem;
}
@media(max-width: 600px) {
#grid {
grid-template-columns: repeat(8, 30px);
}
}
</style>
</head>
<body>
<h1>Rhythm Game Editor</h1>
<div id="controls">
<label for="bpm">BPM:
<input id="bpm" type="number" min="10" max="300" value="120" />
</label>
<label for="audioURL">Audio URL:
<input id="audioURL" type="text" size="40" placeholder="Paste audio URL here" />
</label>
<button id="loadAudioBtn">Load Audio</button>
<button id="playPauseBtn" disabled>Play</button>
<button id="clearBtn">Clear Notes</button>
<button id="shareBtn">Share</button>
</div>
<div id="bars-label"></div>
<div id="grid"></div>
<div id="status">Use spacebar to play/pause, click notes to toggle, delete key to remove selected note.</div>
<script>
// Config and State
let bpm = 120;
let audioURL = '';
let audio = null;
let playing = false;
let timerId = null;
let currentStep = 0;
const barsCount = 4;
const stepsPerBar = 16; // 16th notes by default
const totalSteps = barsCount * stepsPerBar;
// Rhythm data: array of booleans indicating note presence at each step
let rhythmPattern = new Array(totalSteps).fill(false);
const gridEl = document.getElementById('grid');
const bpmInput = document.getElementById('bpm');
const audioURLInput = document.getElementById('audioURL');
const loadAudioBtn = document.getElementById('loadAudioBtn');
const playPauseBtn = document.getElementById('playPauseBtn');
const clearBtn = document.getElementById('clearBtn');
const barsLabel = document.getElementById('bars-label');
const shareBtn = document.getElementById('shareBtn');
// Render the grid notes
function renderGrid() {
gridEl.innerHTML = '';
for(let i=0; i<totalSteps; i++){
const note = document.createElement('div');
note.classList.add('note');
if(rhythmPattern[i]) note.classList.add('active');
note.dataset.index = i;
note.title = `Step ${i+1}`;
note.addEventListener('click', () => toggleNote(i));
gridEl.appendChild(note);
}
barsLabel.textContent = `Bars: ${barsCount} | Steps per Bar: ${stepsPerBar} | Total Steps: ${totalSteps}`;
}
function toggleNote(i){
rhythmPattern[i] = !rhythmPattern[i];
renderGrid();
}
// Playback logic
function playStep(){
// Highlight current step
const notes = gridEl.querySelectorAll('.note');
notes.forEach((note, i) => {
note.classList.toggle('current', i === currentStep);
});
// Play sound if active here (only visual for now)
if(rhythmPattern[currentStep]){
// Simple beep using Web Audio API for demo
playBeep();
}
currentStep = (currentStep +1) % totalSteps;
}
function playBeep() {
if (!audioCtx) {
audioCtx = new AudioContext();
}
const o = audioCtx.createOscillator();
const g = audioCtx.createGain();
o.type = 'square';
o.frequency.setValueAtTime(880, audioCtx.currentTime);
g.gain.setValueAtTime(0.2, audioCtx.currentTime);
o.connect(g);
g.connect(audioCtx.destination);
o.start();
o.stop(audioCtx.currentTime + 0.07);
}
let audioCtx = null;
function startPlaying(){
if(playing) return;
if(!audio) {
alert('Load an audio file first');
return;
}
playing = true;
playPauseBtn.textContent = 'Pause';
currentStep = 0;
const intervalMs = (60000 / bpm) / 4; // 16th note duration, because 1 beat = quarter note, 16th = quarter/4
playStep();
timerId = setInterval(playStep, intervalMs);
audio.currentTime = 0;
audio.play();
}
function stopPlaying(){
if(!playing) return;
playing = false;
playPauseBtn.textContent = 'Play';
clearInterval(timerId);
timerId = null;
currentStep = 0;
const notes = gridEl.querySelectorAll('.note');
notes.forEach(note => note.classList.remove('current'));
if(audio) audio.pause();
}
// Controls
bpmInput.addEventListener('change', e => {
const newBpm = parseInt(e.target.value);
if(isNaN(newBpm) || newBpm < 10 || newBpm > 300){
alert('BPM must be between 10 and 300');
e.target.value = bpm;
return;
}
bpm = newBpm;
if(playing){
stopPlaying();
startPlaying();
}
});
loadAudioBtn.addEventListener('click', () => {
const url = audioURLInput.value.trim();
if(!url) {
alert('Please enter an audio URL');
return;
}
loadAudio(url);
});
function loadAudio(url){
if(audio){
audio.pause();
audio = null;
}
audio = new Audio();
audio.crossOrigin = "anonymous"; // to avoid CORS if possible and allow sharing
audio.src = url;
audio.load();
audio.addEventListener('canplay', () => {
playPauseBtn.disabled = false;
statusMessage('Audio loaded. Ready to play.');
});
audio.addEventListener('error', () => {
alert('Failed to load audio. Check the URL or CORS.');
});
}
playPauseBtn.addEventListener('click', () => {
if(playing){
stopPlaying();
} else {
startPlaying();
}
});
clearBtn.addEventListener('click', () => {
if(confirm('Clear all notes?')){
rhythmPattern.fill(false);
renderGrid();
}
});
// Keyboard shortcuts
document.body.addEventListener('keydown', e => {
if(e.target.tagName === 'INPUT' || e.target.isContentEditable) return;
if(e.code === 'Space'){
e.preventDefault();
if(playing){
stopPlaying();
} else {
startPlaying();
}
} else if(e.code === 'Delete'){
// Delete current selected note (if highlighted)
if(currentStep >= 0 && currentStep < totalSteps){
rhythmPattern[currentStep] = false;
renderGrid();
}
}
});
// Sharing rhythm pattern by encoding JSON in URL hash
shareBtn.addEventListener('click', () => {
const data = {
bpm,
audioURL: audioURLInput.value.trim(),
rhythm: rhythmPattern
};
const jsonStr = JSON.stringify(data);
const encoded = encodeURIComponent(jsonStr);
const shareURL = `${location.origin}${location.pathname}#/data=${encoded}`;
prompt('Shareable URL:', shareURL);
});
// Load from URL hash if present
function loadFromHash() {
if(location.hash.startsWith('#/data=')){
try {
const encoded = location.hash.substring(7);
const jsonStr = decodeURIComponent(encoded);
const data = JSON.parse(jsonStr);
if(data.bpm) {
bpm = data.bpm;
bpmInput.value = bpm;
}
if(data.audioURL) {
audioURLInput.value = data.audioURL;
loadAudio(data.audioURL);
}
if(Array.isArray(data.rhythm) && data.rhythm.length === totalSteps) {
rhythmPattern = data.rhythm;
}
renderGrid();
} catch(ex) {
console.error('Failed to load data from URL hash', ex);
}
}
}
function statusMessage(msg){
document.getElementById('status').textContent = msg;
}
// Initialize
renderGrid();
loadFromHash();
</script>
</body>
</html>
How to use this editor:
- Enter the BPM (default 120).
- Paste a direct audio file URL (mp3, wav, etc.) into the input box, then click "Load Audio".
- Click Play to start the audio and run the rhythm grid playback.
- Click the grid squares to toggle notes on/off.
- Keyboard space to toggle play/pause.
- Keyboard Delete to clear the note at the current step.
- Clear Notes button to clear all notes.
- Share to generate a URL with your pattern encoded in the hash for sharing or bookmarking.
- Opening a shared URL will load and restore your pattern and audio URL.
Notes and Limitations:
- Audio loading depends on CORS; you must host or use URLs allowing cross-origin requests.
- The metronome beep is a simple short beep. Replace or extend this with your own sound samples.
- The note grid is one instrument channel; extend to multi-channel by adding rows.
- Recording live beats from keyboard or clicks is limited here, but you can toggle notes in the grid.
- For real game use, you’d want to sync note hits and visual effects to audio time precisely (this demo uses interval timer).
If you'd like me to help extend with more features like multi-track, advanced recording, MIDI, or dragging notes, just ask!