gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Make me a html code for a beat sequencer
- User
Make me a html code for a beat sequencer
- Assistant
Make me a html code for a beat sequencer
Creating a basic beat sequencer involves HTML, CSS, and JavaScript for functionality. Below is a simple implementation of a beat sequencer:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Beat Sequencer</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f3f3f3;
font-family: Arial, sans-serif;
}
#sequencer {
display: grid;
grid-template-columns: repeat(8, 40px);
grid-gap: 5px;
}
.step {
width: 40px;
height: 40px;
background-color: #cccccc;
border: 1px solid #999;
cursor: pointer;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
}
.active {
background-color: #87ceeb;
}
#controls {
margin-top: 20px;
text-align: center;
}
button {
background-color: #87ceeb;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
cursor: pointer;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div id="sequencer"></div>
<div id="controls">
<button id="play">Play</button>
<button id="stop">Stop</button>
</div>
<script>
const sequencer = document.getElementById('sequencer');
const playButton = document.getElementById('play');
const stopButton = document.getElementById('stop');
const totalSteps = 8;
const totalTracks = 4; // Number of sound tracks
let interval;
let currentStep = 0;
const sounds = [
new Audio('https://s3-us-west-2.amazonaws.com/s.cdpn.io/127331/kick.mp3'),
new Audio('https://s3-us-west-2.amazonaws.com/s.cdpn.io/127331/snare.mp3'),
new Audio('https://s3-us-west-2.amazonaws.com/s.cdpn.io/127331/hihat.mp3'),
new Audio('https://s3-us-west-2.amazonaws.com/s.cdpn.io/127331/clap.mp3')
];
function createSequencer() {
for (let track = 0; track < totalTracks; track++) {
for (let step = 0; step < totalSteps; step++) {
const div = document.createElement('div');
div.classList.add('step');
div.dataset.track = track;
div.dataset.step = step;
div.addEventListener('click', toggleStep);
sequencer.appendChild(div);
}
}
}
function toggleStep(event) {
event.target.classList.toggle('active');
}
function playSequencer() {
interval = setInterval(() => {
for (let track = 0; track < totalTracks; track++) {
const steps = document.querySelectorAll(`[data-track="${track}"]`);
if (steps[currentStep].classList.contains('active')) {
sounds[track].currentTime = 0;
sounds[track].play();
}
steps.forEach(step => step.style.backgroundColor = '#cccccc');
steps[currentStep].style.backgroundColor = '#ffff99';
}
currentStep = (currentStep + 1) % totalSteps;
}, 300);
}
function stopSequencer() {
clearInterval(interval);
const steps = document.querySelectorAll('.step');
steps.forEach(step => step.style.backgroundColor = null);
currentStep = 0;
}
playButton.addEventListener('click', playSequencer);
stopButton.addEventListener('click', stopSequencer);
createSequencer();
</script>
</body>
</html>
createSequencer(): Generates the grid of steps.toggleStep(): Toggles the active/inactive state of a step (when it's clicked).playSequencer(): Starts playing the sequence at a fixed interval, highlighting the column currently playing and plays the associated sound if the step is active.stopSequencer(): Stops playback and resets the step indicator.You can run this code by creating an HTML file and viewing it in a browser. This basic sequencer can be expanded with additional features like adjustable tempos, more tracks, saving patterns, etc.
Not yet included in a published release.