gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js random music sequencer generator chord bars seed timesig rhythm keys
- User
html css js random music sequencer generator chord bars seed timesig rhythm keys
- Assistant
html css js random music sequencer generator chord bars seed timesig rhythm keys
Creating a random music sequencer generator with chord bars, seed-based randomness, time signatures, rhythm, and keys involves quite a few components. Below is a simple example in HTML/CSS/JS that:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Random Music Sequencer Generator</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background: #222;
color: #eee;
}
label, select, input, button {
margin: 5px 10px 15px 0;
font-size: 16px;
}
#bars {
display: flex;
margin-top: 20px;
gap: 10px;
}
.bar {
background: #444;
padding: 10px;
border-radius: 6px;
text-align: center;
min-width: 60px;
cursor: pointer;
user-select: none;
transition: background 0.2s ease;
}
.bar.playing {
background: #0f0;
color: #000;
font-weight: bold;
}
.controls {
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>Random Music Sequencer Generator</h1>
<div class="controls">
<label for="key">Key:</label>
<select id="key">
<option value="C">C (Major)</option>
<option value="G">G (Major)</option>
<option value="D">D (Major)</option>
<option value="A">A (Major)</option>
<option value="E">E (Major)</option>
<option value="F">F (Major)</option>
<option value="Bb">Bb (Major)</option>
<option value="Eb">Eb (Major)</option>
</select>
<label for="timesig">Time Signature:</label>
<select id="timesig">
<option value="4/4">4/4</option>
<option value="3/4">3/4</option>
<option value="6/8">6/8</option>
</select>
<label for="tempo">Tempo (BPM):</label>
<input id="tempo" type="number" value="90" min="40" max="200" />
<label for="seed">Seed:</label>
<input id="seed" type="text" placeholder="random" />
<button id="generateBtn">Generate</button>
<button id="playBtn">Play</button>
<button id="stopBtn" disabled>Stop</button>
</div>
<div id="bars"></div>
<script>
(() => {
const keyElement = document.getElementById("key");
const timesigElement = document.getElementById("timesig");
const tempoElement = document.getElementById("tempo");
const seedElement = document.getElementById("seed");
const generateBtn = document.getElementById("generateBtn");
const playBtn = document.getElementById("playBtn");
const stopBtn = document.getElementById("stopBtn");
const barsContainer = document.getElementById("bars");
// Helper: seeded RNG
function seededRandom(seed) {
// A simple Mulberry32 PRNG
let t = seed;
return function() {
t |= 0; t = t + 0x6D2B79F5 | 0;
let r = Math.imul(t ^ t >>> 15, 1 | t);
r = r + Math.imul(r ^ r >>> 7, 61 | r) ^ r;
return ((r ^ r >>> 14) >>> 0) / 4294967296;
}
}
// Notes order for reference
// for Major scale: W W H W W W H steps
const noteNames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
// Find note index from name
function noteIndex(note) {
// Handle flats by converting to sharps
if(note.length === 2 && note[1] === 'b') {
const baseNote = note[0];
const baseIndex = noteNames.indexOf(baseNote);
return (baseIndex + 11) % 12; // down a semitone
}
return noteNames.indexOf(note);
}
// Build major scale given root note (returns array of 7 note names)
function buildMajorScale(root) {
const intervals = [2,2,1,2,2,2,1];
let idx = noteIndex(root);
const scale = [noteNames[idx]];
intervals.forEach(step => {
idx = (idx + step) % 12;
scale.push(noteNames[idx]);
});
scale.pop(); // last is octave, remove
return scale;
}
// Simple chord formulas (triads)
// Input scale notes and chord degree to build chord
// Major triads on I, IV, V; minor on ii, iii, vi; diminished on vii°
// For demo, we'll pick from I, IV, V, vi chords only for simplicity
// Chord notes array of note names
function buildChord(scale, degree) {
// degree 1-based: 1-7
// triads: root, +2, +4
// Note: scale length = 7
const root = scale[(degree - 1) % 7];
// To get other chord tones:
const third = scale[(degree + 1) % 7];
const fifth = scale[(degree + 3) % 7];
// We can also determine chord quality:
// I, IV, V = major ; vi = minor
let quality = 'maj';
if ([2,3,6].includes(degree)) quality = 'min';
// Return chord object
return { root, third, fifth, degree, quality };
}
// Convert chord to string representation (e.g. "C", "Am", "F")
function chordToString(chord) {
if(chord.quality === 'maj') return chord.root;
if(chord.quality === 'min') return chord.root + 'm';
return chord.root;
}
// Chord degree options to pick from for progression
// Common pop progression degrees
const commonDegrees = [1,4,5,6];
// Generate random chord progression bars
function generateChordProgression(scale, length, random) {
const progression = [];
for(let i=0; i<length; i++) {
// pick degree from commonDegrees randomly
const deg = commonDegrees[Math.floor(random() * commonDegrees.length)];
progression.push(buildChord(scale, deg));
}
return progression;
}
// Audio context and sound generation
let audioCtx;
let isPlaying = false;
let scheduledEvents = [];
// Play chord: play 3 notes as sine waves for duration
function playChord(chord, startTime, duration) {
if(!audioCtx) return;
const osc1 = audioCtx.createOscillator();
const osc2 = audioCtx.createOscillator();
const osc3 = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
// Convert note name to freq
function noteToFreq(note, octave=4) {
// Equal temperament freq calculation
// A4 = 440Hz is note number 69
// Note number: C0 = 12
const semitoneMap = {
'C': 0, 'C#': 1, 'D': 2, 'D#': 3, 'E': 4,
'F': 5, 'F#': 6, 'G': 7, 'G#': 8, 'A': 9,
'A#': 10, 'B': 11,
};
let semitone = semitoneMap[note];
if(semitone === undefined) {
// fallback, could be flat
if(note.length === 2 && note[1] === 'b') {
const base = note[0];
const baseVal = semitoneMap[base];
if(baseVal !== undefined) semitone = (baseVal + 11) % 12;
}
}
const noteNumber = 12 + (octave * 12) + semitone;
return 440 * Math.pow(2, (noteNumber - 69) / 12);
}
const freq1 = noteToFreq(chord.root);
const freq2 = noteToFreq(chord.third);
const freq3 = noteToFreq(chord.fifth);
[osc1, osc2, osc3].forEach(osc => {
osc.type = 'sine';
});
osc1.frequency.setValueAtTime(freq1, startTime);
osc2.frequency.setValueAtTime(freq2, startTime);
osc3.frequency.setValueAtTime(freq3, startTime);
gainNode.gain.setValueAtTime(0, startTime);
gainNode.gain.linearRampToValueAtTime(0.15, startTime + 0.02);
gainNode.gain.linearRampToValueAtTime(0, startTime + duration);
osc1.connect(gainNode);
osc2.connect(gainNode);
osc3.connect(gainNode);
gainNode.connect(audioCtx.destination);
osc1.start(startTime);
osc2.start(startTime);
osc3.start(startTime);
osc1.stop(startTime + duration);
osc2.stop(startTime + duration);
osc3.stop(startTime + duration);
scheduledEvents.push(osc1, osc2, osc3, gainNode);
}
// Stop all scheduled sounds
function stopSound() {
if(scheduledEvents.length) {
scheduledEvents.forEach(node=>{
try {
node.disconnect();
if(node.stop) node.stop();
} catch {}
});
scheduledEvents = [];
}
}
// Variables for current progression and playback
let currentProgression = [];
let barsCount = 4; // default bars length for sequencer
let timeSignature = [4,4]; // default
let tempo = 90;
let randomGen;
let playTimeouts = [];
// Update bars UI
function updateBarsUI(progression) {
barsContainer.innerHTML = '';
progression.forEach((chord, i) => {
const barDiv = document.createElement('div');
barDiv.className = 'bar';
barDiv.textContent = chordToString(chord);
barDiv.dataset.index = i;
barsContainer.appendChild(barDiv);
});
}
// Parse time signature string like "4/4" to array
function parseTimeSignature(str) {
const parts = str.split('/');
if(parts.length === 2) {
return [parseInt(parts[0], 10), parseInt(parts[1], 10)];
}
return [4,4];
}
// Play progression with visual highlighting
function playProgression() {
if(isPlaying) return;
if(!currentProgression.length) return;
if(!audioCtx) audioCtx = new AudioContext();
isPlaying = true;
playBtn.disabled = true;
stopBtn.disabled = false;
tempo = parseInt(tempoElement.value,10);
const beatDuration = 60 / tempo; // seconds per beat
// Calculate bar duration from time signature beats
const beatsPerBar = timeSignature[0];
const beatUnit = timeSignature[1];
// For simplicity assume beatUnit = 4 or 8
// bar length in seconds:
// beatsPerBar * beatDuration * (4/beatUnit)
// Adjust for compound meters like 6/8
const barDuration = beatsPerBar * beatDuration * (4 / beatUnit);
let startTime = audioCtx.currentTime + 0.1;
currentProgression.forEach((chord, i) => {
playChord(chord, startTime + i * barDuration, barDuration * 0.9);
// Visual highlight
const timeoutId = setTimeout(() => {
// remove all playing highlights
document.querySelectorAll('.bar.playing').forEach(el=>{
el.classList.remove('playing');
});
const barDivs = barsContainer.children;
if(barDivs[i]) barDivs[i].classList.add('playing');
if(i === currentProgression.length - 1) {
// schedule stop highlight on last bar end
setTimeout(() => {
document.querySelectorAll('.bar.playing').forEach(el=>{
el.classList.remove('playing');
});
isPlaying = false;
playBtn.disabled = false;
stopBtn.disabled = true;
}, barDuration * 1000);
}
}, (barDuration * i * 1000));
playTimeouts.push(timeoutId);
});
}
function stopPlayback() {
if(!isPlaying) return;
stopSound();
playTimeouts.forEach(id => clearTimeout(id));
playTimeouts = [];
isPlaying = false;
playBtn.disabled = false;
stopBtn.disabled = true;
document.querySelectorAll('.bar.playing').forEach(el=>{
el.classList.remove('playing');
});
}
generateBtn.addEventListener('click', () => {
// Create a seed - if none given use random
let seed = seedElement.value;
if(!seed) seed = String(Math.floor(Math.random() * 1000000));
// Convert seed string to number for RNG
// simple hash function
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for(let i=0; i<str.length; i++) {
const 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;
}
const seedNum = cyrb128(seed);
randomGen = seededRandom(seedNum);
const selectedKey = keyElement.value;
const scale = buildMajorScale(selectedKey);
timeSignature = parseTimeSignature(timesigElement.value);
tempo = parseInt(tempoElement.value, 10) || 90;
barsCount = 4; // you can make this adjustable later
currentProgression = generateChordProgression(scale, barsCount, randomGen);
updateBarsUI(currentProgression);
});
playBtn.addEventListener('click', playProgression);
stopBtn.addEventListener('click', stopPlayback);
// Auto-generate on page load
generateBtn.click();
})();
</script>
</body>
</html>
This basic tool can be further expanded with:
Let me know if you'd want me to help extend or customize this!