Creating a random sheet music generator with bars, seed, time signature, rhythm, keys, treble and bass clefs is an interesting challenge. Below, I'll provide a simplified example coded in HTML, CSS, and JS. It will:
Generate random notes within a key.
Support treble and bass clefs.
Support time signatures like 4/4, 3/4.
Use a seed for reproducible randomness.
Show bars and rhythms (simple durations).
Render notes with basic SVG or Canvas — for clarity and simplicity, SVG with Unicode music notes and basic staff lines.
For a more professional look, you'd typically use a library like VexFlow. But since you requested something self-contained, I'll make a simple custom implementation.
Features implemented:
Seeded random generator.
Time signatures: 4/4, 3/4.
Treble and bass clefs with appropriate note ranges.
Rhythm: quarter, half, eighth notes.
Bars with staff lines.
A simple key (e.g., C major) to keep accidentals out for now.
Full Example Code:
html
<!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: "Georgia", serif;
background: #f0f0f0;
padding: 20px;
}
#sheet {
background: white;
padding: 20px;
border: 2px solid #333;
display: inline-block;
}
svg {
display: block;
margin: 10px 0;
}
.staff-line {
stroke: black;
stroke-width: 1;
}
.bar-line {
stroke: black;
stroke-width: 2;
}
text.note {
font-size: 20px;
user-select: none;
}
#controls {
margin-bottom: 20px;
}
label {
margin-right: 10px;
}
</style>
</head>
<body>
<div id="controls">
<label>Seed: <input type="text" id="seedInput" value="1234" size="5"/></label>
<label>Time Signature:
<select id="timeSigInput">
<option value="4/4">4/4</option>
<option value="3/4">3/4</option>
<option value="6/8">6/8</option>
</select>
</label>
<label>Clef:
<select id="clefInput">
<option value="treble">Treble</option>
<option value="bass">Bass</option>
<option value="grand">Grand (Treble+Bass)</option>
</select>
</label>
<button id="generateBtn">Generate</button>
</div>
<div id="sheet"></div>
<script>
// Seeded random generator (Mulberry32)
function createRandom(seed) {
let s = seed;
return function() {
s += 0x6D2B79F5;
let t = s;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
// Note data
// We'll map notes to vertical positions on the staff for treble and bass clef.
// Simplify: staff lines 5 lines, each line and space corresponds to a note.
// vertical position is y in SVG
// Middle C is on ledger line between treble and bass
const NoteDurationValues = {
"whole": 4,
"half": 2,
"quarter":1,
"eighth":0.5
};
// Unicode Musical Symbols for quarter and eighth notes (more have half note, but no half note here)
// For simplicity, I'll just use quarter and eighth note symbols and add a tail or draw differently
const NoteUnicode = {
"whole": "\uD834\uDD5D", // 𝕝 (not standard, fallback)
"half": "\uD834\uDD5E", // 𝕞 (not standard, fallback)
"quarter": "\u2669", // ♩
"eighth": "\u266A" // ♪
};
// Notes in C major for treble clef (approximate, could be extended)
const TrebleNotes = [
{name:"E5", midi: 76}, // top space on treble staff (5th line)
{name:"D5", midi: 74},
{name:"C5", midi: 72},
{name:"B4", midi: 71},
{name:"A4", midi: 69},
{name:"G4", midi: 67},
{name:"F4", midi: 65},
{name:"E4", midi: 64},
{name:"D4", midi: 62},
{name:"C4", midi: 60}, // middle C
{name:"B3", midi: 59},
{name:"A3", midi: 57}
];
// Bass Notes approx
const BassNotes = [
{name:"G3", midi: 55},
{name:"F3", midi: 53},
{name:"E3", midi: 52},
{name:"D3", midi: 50},
{name:"C3", midi: 48},
{name:"B2", midi: 47},
{name:"A2", midi: 45},
{name:"G2", midi: 43},
{name:"F2", midi: 41},
{name:"E2", midi: 40},
{name:"D2", midi: 38},
{name:"C2", midi: 36}
];
// Map midi to vertical y pos on staff
// Staff lines are spaced 10 units apart vertically
// Treble staff: 5 lines, line 1 is bottom line
// We'll set line 1 y=70, then each line +10 up (line 5 at y=30)
// Spaces in between lines at y=80,60, etc.
// We'll use 'C4' (middle C, midi 60) as the reference note on y=90 (ledger line just below staff)
// Each note midi step difference is half-step (not line spacing), we'll approximate:
// Whole notes are on lines and spaces: every note step +5 units vertical.
// line/space increments correspond to a second interval (2 semitones), so 1 semitone = 2.5 units vertical approx.
// We'll map MIDI to vertical Y positions for treble and bass
function getTrebleY(midi) {
// middle C (60) = y 90 (ledger line below)
// each note steps up: 2 semitones => 10 units up
// One semitone = 5 units vertical (approx)
return 90 - (midi - 60)*5;
}
function getBassY(midi) {
// Bass staff lines y=150 to 110 (line 1 bottom=150, line 5 top=110)
// C3(48) ledger line above staff at y=105
// We'll set C3 (48) at y=105
// same scale
return 105 - (midi - 48)*5;
}
// Rhythms to use based on time signature
const RhythmOptions = {
"4/4": ["quarter", "quarter", "quarter", "quarter", "half", "half", "eighth", "eighth"],
"3/4": ["quarter", "quarter", "quarter", "half", "eighth", "eighth"],
"6/8": ["eighth", "eighth", "eighth", "eighth", "eighth", "eighth", "quarter"]
};
// Duration of measure in beats for given time signature
const TimeSignatureBeats = {
"4/4": 4,
"3/4": 3,
"6/8": 6
};
// Duration value of each beat in 6/8, (it's compound meter), 6/8 is 2 beats of dotted quarter (3 eighth notes each)
const BeatUnit = {
"4/4": 1,
"3/4": 1,
"6/8": 0.5 // eighth note = half beat
}
function generateNotes(random, clef, timeSig){
// generate notes to fill the bar(s)
// Goal: fill measures with notes accumulated durations
let notes = [];
const measureBeatCount = TimeSignatureBeats[timeSig];
const beatUnit = BeatUnit[timeSig];
// total beats in measure in quarter note units:
const measureTotal = measureBeatCount * beatUnit;
let remaining = measureTotal;
while(remaining > 0) {
// choose rhythm <= remaining
// rhythm options filtered for <= remaining
let possibleRhythms = RhythmOptions[timeSig].filter(dur => {
return NoteDurationValues[dur] <= remaining;
});
if(possibleRhythms.length === 0) break;
let rhythm = possibleRhythms[Math.floor(random()*possibleRhythms.length)];
let duration = NoteDurationValues[rhythm];
// pick note based on clef
let notePool;
if(clef === "treble") notePool = TrebleNotes;
else if(clef === "bass") notePool = BassNotes;
else if(clef === "grand") {
// Randomly assign treble or bass note
if(random() < 0.5) {
notePool = TrebleNotes;
} else {
notePool = BassNotes;
}
}
let note = notePool[Math.floor(random()*notePool.length)];
notes.push({note:note.name, midi:note.midi, duration: rhythm, y: clef === "bass" ? getBassY(note.midi) : getTrebleY(note.midi), clef});
remaining -= duration;
}
return notes;
}
function createStaff(svg, x, y, width){
for(let i=0; i<5; i++){
let lineY = y + i*10;
let line = document.createElementNS("http://www.w3.org/2000/svg","line");
line.setAttribute("x1", x);
line.setAttribute("y1", lineY);
line.setAttribute("x2", x+width);
line.setAttribute("y2", lineY);
line.setAttribute("class","staff-line");
svg.appendChild(line);
}
}
function createBarLines(svg, x, y, count, width){
// vertical lines between bars
for(let i=0; i<=count; i++){
let barX = x + i*width/count;
let line = document.createElementNS("http://www.w3.org/2000/svg","line");
line.setAttribute("x1", barX);
line.setAttribute("y1", y);
line.setAttribute("x2", barX);
line.setAttribute("y2", y+40);
line.setAttribute("class","bar-line");
svg.appendChild(line);
}
}
function createClef(svg, x, y, clef){
// Simplified clef symbols using text
let text = document.createElementNS("http://www.w3.org/2000/svg","text");
text.setAttribute("x", x);
text.setAttribute("y", y+30);
text.setAttribute("font-family","Arial");
text.setAttribute("font-size","50");
text.setAttribute("fill","black");
if(clef === "treble"){
text.textContent = "\uD834\uDD1E"; // G clef symbol 𝄞 (might not render everywhere)
} else if(clef === "bass"){
text.textContent = "\uD834\uDD22"; // F clef symbol 𝄢
}
svg.appendChild(text);
}
function createNote(svg, x, y, duration){
let note = document.createElementNS("http://www.w3.org/2000/svg","text");
note.setAttribute("x", x);
note.setAttribute("y", y);
note.setAttribute("class","note");
note.setAttribute("text-anchor","middle");
// Use quarter or eighth Unicode notes for display simplicity
if(duration === "quarter" || duration === "half") note.textContent = NoteUnicode["quarter"];
else if(duration === "eighth") note.textContent = NoteUnicode["eighth"];
else note.textContent = NoteUnicode["quarter"];
svg.appendChild(note);
}
function generateSheet(seed, timeSig, clef){
const random = createRandom(seed);
const measures = 4;
const barWidth = 200;
const barHeight = 50;
const sheetDiv = document.getElementById("sheet");
sheetDiv.innerHTML = "";
if(clef === "grand"){
// two staves: treble on top, bass bottom
let svgHeight = 130;
let svgWidth = barWidth*measures + 40;
let svg = document.createElementNS("http://www.w3.org/2000/svg","svg");
svg.setAttribute("width",svgWidth);
svg.setAttribute("height",svgHeight);
sheetDiv.appendChild(svg);
// Draw treble staff
for(let m=0; m<measures; m++){
createStaff(svg, 40 + barWidth*m, 10, barWidth);
createBarLines(svg, 40 + barWidth*m, 10, 1, barWidth);
}
createBarLines(svg, 40, 10, measures, barWidth)
createClef(svg, 10, 10, "treble");
// Draw bass staff
for(let m=0; m<measures; m++){
createStaff(svg, 40 + barWidth*m, 80, barWidth);
createBarLines(svg, 40 + barWidth*m, 80, 1, barWidth);
}
createBarLines(svg, 40, 80, measures, barWidth)
createClef(svg, 10, 80, "bass");
// Generate notes for treble and bass separately
// We'll generate 4 measures of 4/4 each clef (hmm but timeSig is one, for grand let's split randomly)
for(let m=0; m<measures; m++){
// Generate notes for treble staff for measure m:
let trebleNotes = generateNotes(random, "treble", timeSig);
// place notes on treble staff in m-th measure
let startX = 40 + m*barWidth;
let curX = startX + 20;
let spacePerBeat = barWidth / TimeSignatureBeats[timeSig];
// Place notes spaced evenly approximating rhythm duration proportionally
let totalDuration = trebleNotes.reduce((acc,n) => acc + NoteDurationValues[n.duration],0);
for(let n of trebleNotes){
createNote(svg, curX, n.y, n.duration);
curX += spacePerBeat * NoteDurationValues[n.duration];
}
// Bass notes
let bassNotes = generateNotes(random, "bass", timeSig);
startX = 40 + m*barWidth;
curX = startX + 20;
for(let n of bassNotes){
createNote(svg, curX, n.y, n.duration);
curX += spacePerBeat * NoteDurationValues[n.duration];
}
}
} else {
// single staff
let svgHeight = 70;
let svgWidth = barWidth*measures + 40;
let svg = document.createElementNS("http://www.w3.org/2000/svg","svg");
svg.setAttribute("width",svgWidth);
svg.setAttribute("height",svgHeight);
sheetDiv.appendChild(svg);
// staff lines
for(let m=0; m<measures; m++){
createStaff(svg, 40 + barWidth*m, 10, barWidth);
createBarLines(svg, 40 + barWidth*m, 10, 1, barWidth);
}
createBarLines(svg, 40, 10, measures, barWidth)
createClef(svg, 10, 10, clef);
for(let m=0; m<measures; m++){
let notes = generateNotes(random, clef, timeSig);
let startX = 40 + m*barWidth;
let curX = startX + 20;
let spacePerBeat = barWidth / TimeSignatureBeats[timeSig];
for(let n of notes){
createNote(svg, curX, n.y, n.duration);
curX += spacePerBeat * NoteDurationValues[n.duration];
}
}
}
}
// Initialize
document.getElementById("generateBtn").addEventListener("click",() => {
let seedStr = document.getElementById("seedInput").value.trim();
if(!seedStr) seedStr = "1234";
// convert seed str to int
let seed = 0;
for(let i=0; i<seedStr.length; i++){
seed += seedStr.charCodeAt(i) * (i+1);
}
let timeSig = document.getElementById("timeSigInput").value;
let clef = document.getElementById("clefInput").value;
generateSheet(seed, timeSig, clef);
});
// Generate on load
document.getElementById("generateBtn").click();
</script>
</body>
</html>
How it works:
You enter a seed (string), it gets converted to integer for seeded randomness.
Select time signature from dropdown.
Select clef (treble, bass, grand which shows treble and bass staffs).
Press Generate to display random notes filling 4 measures.
Notes' vertical position corresponds roughly to pitch.
Notes are displayed as quarter or eighth notes using Unicode symbols ♩ ♪ and basic staff lines are drawn as SVG lines.
This is a basic demonstration that can be expanded further.
If you want, I can help improve it to:
Add accidentals and key signatures.
Add rests.
Use professional music rendering libraries like VexFlow.