Creating a simple oscillator sequencer in JavaScript can be done using the Web Audio API. Below, I'll provide an example of how you can create a basic sequencer that plays different sine wave frequencies. This is a simple implementation, and you can expand and modify it based on your requirements.
Basic Oscillator Sequencer Example
This example will create an audio context, set up an oscillator, and use a sequence of notes to play them at regular intervals.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Oscillator Sequencer</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Basic Oscillator Sequencer</h1>
<button id="start-sequencer">Start Sequencer</button>
<button id="stop-sequencer">Stop Sequencer</button>
<script>
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
let intervalId;
const sequence = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25]; // C4 to C5 frequencies
let currentIndex = 0;
function playOscillator(frequency) {
const oscillator = audioCtx.createOscillator();
oscillator.type = 'sine'; // You can change this to 'square', 'sawtooth', 'triangle' etc.
oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime);
oscillator.connect(audioCtx.destination);
oscillator.start();
oscillator.stop(audioCtx.currentTime + 0.5); // Play each note for 0.5 seconds
}
function startSequencer() {
if (!intervalId) {
intervalId = setInterval(() => {
playOscillator(sequence[currentIndex]);
currentIndex = (currentIndex + 1) % sequence.length; // Loop through the sequence
}, 600); // Play next note every 600 ms
}
}
function stopSequencer() {
clearInterval(intervalId);
intervalId = null;
}
document.getElementById('start-sequencer').addEventListener('click', () => {
startSequencer();
});
document.getElementById('stop-sequencer').addEventListener('click', () => {
stopSequencer();
});
</script>
</body>
</html>
Explanation:
HTML Structure: Simple buttons to start and stop the sequencer.
Audio Context: We create an AudioContext to manage the audio operations.
Oscillator Function: The playOscillator function creates an oscillator, sets its frequency, connects it to the audio context’s destination (speakers), starts it, and schedules it to stop after 0.5 seconds.
Sequencing Logic: The startSequencer function uses setInterval to play each note from the sequence array in regular intervals (600 ms), looping back to the beginning when the end of the sequence is reached.
Stop Control: The stopSequencer function clears the interval, stopping the sequencer.
Usage:
Open the HTML file in a browser that supports the Web Audio API.
Click "Start Sequencer" to hear the sequence of notes.
Click "Stop Sequencer" to stop the playback.
You can modify the sequence array or change the interval value to create different musical patterns and tempos. Enjoy experimenting!