gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js random sheet music generator bars seed timesig rhythm
- User
html css js random sheet music generator bars seed timesig rhythm
- Assistant
html css js random sheet music generator bars seed timesig rhythm
Creating a random sheet music generator with configurable bars, seed-based randomness, time signature, and rhythm involves some complexity, but we can create a simplified example using HTML, CSS, and JavaScript. This example will:
♩, ♪, ♫) to represent rhythms visually.1), eighth notes (0.5), and sixteenth notes (0.25).<!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;
margin: 2rem;
background: #f8f9fa;
}
label, input, select, button {
margin: 0.25rem 0.5rem 1rem 0;
font-size: 1rem;
}
#sheet-music {
font-size: 2rem;
margin-top: 1rem;
white-space: pre-wrap;
background: white;
padding: 1rem;
border: 1px solid #ccc;
}
.bar {
display: inline-block;
margin-right: 1rem;
border-right: 2px solid black;
padding-right: 0.5rem;
}
.bar:last-child {
border-right: none;
}
.note {
cursor: default;
margin-right: 0.2rem;
}
</style>
</head>
<body>
<h1>Random Sheet Music Generator</h1>
<label for="bars">Number of Bars:</label>
<input type="number" id="bars" value="4" min="1" max="32" />
<label for="seed">Seed:</label>
<input type="text" id="seed" value="1234" />
<label for="beatsPerBar">Beats per bar:</label>
<select id="beatsPerBar">
<option value="4" selected>4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="6">6</option>
</select>
<label for="noteValue">Note Value (beat unit):</label>
<select id="noteValue">
<option value="4" selected>4 (quarter note)</option>
<option value="8">8 (eighth note)</option>
<option value="16">16 (sixteenth note)</option>
</select>
<button id="generateBtn">Generate</button>
<div id="sheet-music"></div>
<script>
// Simple string hash for seed conversion
function cyrb128(str) {
let h1=1779033703, h2=3144134277, h3=1013904242, h4=2773480762;
for(let i=0; i<str.length; i++) {
let k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1>>>18), 597399067);
h2 = Math.imul(h4 ^ (h2>>>22), 2869860233);
h3 = Math.imul(h1 ^ (h3>>>17), 951274213);
h4 = Math.imul(h2 ^ (h4>>>19), 2716044179);
return [(h1^h2^h3^h4)>>>0];
}
// Simple seeded pseudo-random generator (Mulberry32)
function mulberry32(seed) {
return function() {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t>>>15), t | 1);
t ^= t + Math.imul(t ^ (t>>>7), t | 61);
return ((t ^ (t>>>14)) >>> 0) / 4294967296;
}
}
// Helper to pick a random element from array
function choice(arr, randomFn) {
return arr[Math.floor(randomFn() * arr.length)];
}
// Note definitions
const NOTE_TYPES = [
{ duration: 1, symbol: '♩', name: 'Quarter Note' }, // quarter note = 1 beat
{ duration: 0.5, symbol: '♪', name: 'Eighth Note' }, // eighth note = 0.5 beat
{ duration: 0.25, symbol: '♫', name: 'Sixteenth Note' } // sixteenth note = 0.25 beat
];
// Generate notes to fill the bar exactly
function generateBar(beatsPerBar, noteUnitFactor, randomFn) {
// noteUnitFactor converts the note value to beats
// For example if beat unit is 8 (eighth note), then each 'beat' is 0.5 of quarter. That means quarter = 2 beats in eighth note unit
// To adjust, we scale durations accordingly
// total beats in duration scale = beatsPerBar * (4 / noteUnitFactor)
// We'll generate durations as fractions of the beat unit (e.g. quarter note = 1 beat in 4/4, but in 8 it's 2 beats)
let totalBeats = beatsPerBar * (4 / noteUnitFactor);
let barNotes = [];
let sum = 0;
// Filter note types that fit into the noteUnitFactor scale without remainder:
// E.g., in 8 note unit, quarter note duration is 2 beats.
let filteredNotes = NOTE_TYPES.filter(n => Number.isInteger(n.duration * (4 / noteUnitFactor)));
while (sum < totalBeats) {
// Pick random note that fits in remaining space
let remaining = totalBeats - sum;
let candidates = filteredNotes.filter(n => n.duration * (4 / noteUnitFactor) <= remaining + 0.001);
if (candidates.length === 0) break; // no suitable note fits, break to avoid infinite loop
let n = choice(candidates, randomFn);
barNotes.push(n);
sum += n.duration * (4 / noteUnitFactor);
}
return barNotes;
}
function renderSheetMusic(bars, beatsPerBar, noteValue, seedStr) {
let seed = cyrb128(seedStr)[0];
let randomFn = mulberry32(seed);
const container = document.getElementById('sheet-music');
container.innerHTML = '';
for (let i=0; i<bars; i++) {
let barNotes = generateBar(beatsPerBar, parseInt(noteValue), randomFn);
let barDiv = document.createElement('div');
barDiv.className = 'bar';
barNotes.forEach(n => {
let noteSpan = document.createElement('span');
noteSpan.className = 'note';
noteSpan.textContent = n.symbol;
noteSpan.title = `${n.name} (${n.duration} beats)`;
barDiv.appendChild(noteSpan);
});
container.appendChild(barDiv);
}
}
// Event listener for button
document.getElementById('generateBtn').addEventListener('click', () => {
let bars = parseInt(document.getElementById('bars').value);
let seed = document.getElementById('seed').value;
let beatsPerBar = parseInt(document.getElementById('beatsPerBar').value);
let noteValue = document.getElementById('noteValue').value;
renderSheetMusic(bars, beatsPerBar, noteValue, seed);
});
// Initial render
renderSheetMusic(4, 4, '4', '1234');
</script>
</body>
</html>
This can be extended by integrating with libraries like VexFlow for real music notation rendering, or by adding pitch and note placement.
If you want me to prepare a more advanced example with graphical staff, note placement, or pitches — let me know!
Not yet included in a published release.