Sure! Below is a simple example of a random sheet music generator using HTML, CSS, and JavaScript. It generates random bars with a given seed, time signature, rhythm, and keys.
Features:
- Seeded randomness so the same seed produces the same music.
- Configurable time signature (e.g., 4/4, 3/4).
- Randomly chosen notes or rests based on rhythm subdivision.
- Randomly chosen key (C major, G major, F major, etc.) - for now choosing notes inside the key scale.
- Simple visual representation using div bars and note symbols.
How to use:
- Change the seed, time signature, rhythm subdivision, and key from the inputs.
- Click Generate to see the generated random bars.
Implementation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Random Sheet Music Generator</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 1rem;
background: #f0f0f0;
}
.controls {
margin-bottom: 1rem;
}
label {
margin-right: 1rem;
}
.sheet {
background: white;
border: 1px solid #333;
padding: 1rem;
max-width: 800px;
margin-top: 1rem;
}
.bar {
border-bottom: 1px solid black;
margin-bottom: 1rem;
padding-bottom: 0.5rem;
display: flex;
align-items: center;
}
.note {
margin-right: 0.5rem;
font-weight: bold;
font-size: 20px;
user-select: none;
}
.rest {
color: gray;
font-style: italic;
}
.time-signature {
font-weight: bold;
margin-bottom: 1rem;
}
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>Random Sheet Music Generator</h1>
<div class="controls">
<label>
Seed: <input id="seed-input" type="number" value="12345" />
</label>
<label>
Time Signature:
<select id="time-signature">
<option value="4/4">4/4</option>
<option value="3/4">3/4</option>
<option value="6/8">6/8</option>
<option value="2/4">2/4</option>
</select>
</label>
<label>
Rhythm Subdivision (notes per beat):
<select id="rhythm-subdivision">
<option value="1">Quarter notes</option>
<option value="2" selected>8th notes</option>
<option value="4">16th notes</option>
</select>
</label>
<label>
Key:
<select id="key-select">
<option value="C">C Major</option>
<option value="G">G Major</option>
<option value="F">F Major</option>
<option value="D">D Major</option>
<option value="A">A Major</option>
</select>
</label>
<button id="generate-btn">Generate</button>
</div>
<div id="sheet" class="sheet"></div>
<script>
// Simple PRNG with seed (Mulberry32)
function mulberry32(a) {
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
}
// Notes in keys (only natural notes for simplicity)
const keyScales = {
'C': ['C', 'D', 'E', 'F', 'G', 'A', 'B'],
'G': ['G', 'A', 'B', 'C', 'D', 'E', 'F#'],
'F': ['F', 'G', 'A', 'Bb', 'C', 'D', 'E'],
'D': ['D', 'E', 'F#', 'G', 'A', 'B', 'C#'],
'A': ['A', 'B', 'C#', 'D', 'E', 'F#', 'G#']
};
// Note symbols (for simplicity letters, could be Unicode musical symbols or images)
// Alternative: Use Unicode musical note symbols U+2669, U+266A, etc.
// Here just letters for clarity and rests
// We'll randomly pick notes or rests
// Rests represented as 'rest'
// For rhythm subdivision, generate that many notes per beat
// time signature tells #beats per bar and which note gets the beat (denominator)
// Convert time signature string to beatsPerBar and beatNote
function parseTimeSignature(ts) {
const parts = ts.split('/');
return {beatsPerBar: +parts[0], beatNote: +parts[1]};
}
// Generate one bar of notes/rests
// total notes = beatsPerBar * rhythmSubdivision
// each note duration = beat duration / rhythmSubdivision (for reference)
function generateBar(rng, beatsPerBar, rhythmSubdivision, scale) {
const totalNotes = beatsPerBar * rhythmSubdivision;
let barNotes = [];
for(let i = 0; i < totalNotes; i++) {
// 20% chance rest, 80% note
if(rng() < 0.2) {
barNotes.push({type:'rest', value:'rest'});
} else {
// randomly pick a scale note
const note = scale[Math.floor(rng() * scale.length)];
barNotes.push({type:'note', value: note});
}
}
return barNotes;
}
function renderBar(barNotes, rhythmSubdivision) {
const barDiv = document.createElement('div');
barDiv.classList.add('bar');
barNotes.forEach(n => {
const noteSpan = document.createElement('span');
noteSpan.classList.add('note');
if(n.type === 'note') {
noteSpan.textContent = n.value;
} else {
noteSpan.textContent = '𝄽'; // quarter rest Unicode U+1D13D but fallback shown here as 𝄽
noteSpan.classList.add('rest');
}
// Adjust size depending on rhythmSubdivision (smaller notes for more subdivision)
noteSpan.style.fontSize = `${20 - rhythmSubdivision * 2}px`;
barDiv.appendChild(noteSpan);
});
return barDiv;
}
function generateSheet(seed, timeSig, rhythmSubdivision, key) {
const {beatsPerBar, beatNote} = parseTimeSignature(timeSig);
// Use simple scale from keyScales (array of note names)
const scale = keyScales[key] || keyScales['C'];
// Setup PRNG
const rng = mulberry32(seed);
// Number of bars, for demo let's show 4 bars
const barsCount = 4;
const sheet = document.createElement('div');
// Time signature label
const tsLabel = document.createElement('div');
tsLabel.classList.add('time-signature');
tsLabel.textContent = `Time Signature: ${beatsPerBar}/${beatNote} | Key: ${key} Major | Rhythm: ${rhythmSubdivision} notes per beat`;
sheet.appendChild(tsLabel);
for(let i = 0; i < barsCount; i++) {
const barNotes = generateBar(rng, beatsPerBar, rhythmSubdivision, scale);
const barDiv = renderBar(barNotes, rhythmSubdivision);
sheet.appendChild(barDiv);
}
return sheet;
}
document.getElementById('generate-btn').addEventListener('click', () => {
const seed = parseInt(document.getElementById('seed-input').value) || Date.now();
const timeSig = document.getElementById('time-signature').value;
const rhythmSub = +document.getElementById('rhythm-subdivision').value;
const key = document.getElementById('key-select').value;
const sheetDiv = document.getElementById('sheet');
sheetDiv.innerHTML = '';
const musicSheet = generateSheet(seed, timeSig, rhythmSub, key);
sheetDiv.appendChild(musicSheet);
});
// Initial generate
document.getElementById('generate-btn').click();
</script>
</body>
</html>
Explanation:
- The mulberry32 function is a fast pseudo-random number generator initialized with your seed, making output deterministic.
- Time signature splits the rhythm into bars and beats.
- Each beat is subdivided by the rhythm subdivision (quarter notes, eighth notes, sixteenth notes).
- Notes are random picked from the chosen key scale.
- Rests are shown as Unicode quarter rest character
𝄽. (If your font doesn't support it, you might see a box — you can replace with "rest" text.)
- The notes are displayed as their letter names for simplicity.
- You can expand this with actual music notation libraries (like VexFlow) but that would be more complex.
Let me know if you'd like enhancements like real staff notation, duration markers, or exporting MIDI!