-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more pads and drums (before: 4, now: 8)
- Loading branch information
Showing
5 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Create an Audio Context | ||
const audioContext = new (window.AudioContext || (window as any).webkitAudioContext)(); | ||
|
||
export function playOpenHiHat() { | ||
// --- 1. White Noise for the hi-hat --- | ||
const bufferSize = audioContext.sampleRate * 0.5; // 0.5 second buffer for longer open hi-hat | ||
const noiseBuffer = audioContext.createBuffer(1, bufferSize, audioContext.sampleRate); | ||
const output = noiseBuffer.getChannelData(0); | ||
|
||
for (let i = 0; i < bufferSize; i++) { | ||
output[i] = Math.random() * 2 - 1; // Generate white noise | ||
} | ||
|
||
const noise = audioContext.createBufferSource(); | ||
noise.buffer = noiseBuffer; | ||
|
||
// --- 2. High-pass filter to remove lower frequencies --- | ||
const highPassFilter = audioContext.createBiquadFilter(); | ||
highPassFilter.type = 'highpass'; | ||
highPassFilter.frequency.setValueAtTime(5000, audioContext.currentTime); // High cutoff for brightness | ||
|
||
// --- 3. Low-pass filter to shape the high-end frequencies --- | ||
const lowPassFilter = audioContext.createBiquadFilter(); | ||
lowPassFilter.type = 'lowpass'; | ||
lowPassFilter.frequency.setValueAtTime(10000, audioContext.currentTime); // Smooth out the very high frequencies | ||
|
||
// --- 4. Gain envelope for the sound decay --- | ||
const gainNode = audioContext.createGain(); | ||
gainNode.gain.setValueAtTime(0.6, audioContext.currentTime); // Start at a moderate volume | ||
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.5); // Decay for a sustained open hi-hat sound | ||
|
||
// Connect the nodes together: noise -> highPass -> lowPass -> gain -> output | ||
noise.connect(highPassFilter); | ||
highPassFilter.connect(lowPassFilter); | ||
lowPassFilter.connect(gainNode); | ||
gainNode.connect(audioContext.destination); | ||
|
||
// Play the noise | ||
noise.start(); | ||
noise.stop(audioContext.currentTime + 0.5); // Stop after 0.5 seconds for an open hi-hat effect | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Create an Audio Context | ||
const audioContext = new (window.AudioContext || (window as any).webkitAudioContext)(); | ||
|
||
export function playRide() { | ||
// --- 1. White Noise for the metallic sound --- | ||
const bufferSize = audioContext.sampleRate * 1.2; // Slightly longer buffer for sustained ride sound | ||
const noiseBuffer = audioContext.createBuffer(1, bufferSize, audioContext.sampleRate); | ||
const output = noiseBuffer.getChannelData(0); | ||
|
||
for (let i = 0; i < bufferSize; i++) { | ||
output[i] = Math.random() * 2 - 1; // Generate white noise | ||
} | ||
|
||
const noise = audioContext.createBufferSource(); | ||
noise.buffer = noiseBuffer; | ||
|
||
// --- 2. High-pass filter to focus on high frequencies --- | ||
const highPassFilter = audioContext.createBiquadFilter(); | ||
highPassFilter.type = 'highpass'; | ||
highPassFilter.frequency.setValueAtTime(3000, audioContext.currentTime); // Lower frequency cutoff than hi-hat for more depth | ||
|
||
// --- 3. Low-pass filter to smooth out the harshness --- | ||
const lowPassFilter = audioContext.createBiquadFilter(); | ||
lowPassFilter.type = 'lowpass'; | ||
lowPassFilter.frequency.setValueAtTime(9000, audioContext.currentTime); // Tame the very high frequencies | ||
|
||
// --- 4. Gain envelope for the sound decay --- | ||
const gainNode = audioContext.createGain(); | ||
gainNode.gain.setValueAtTime(0.8, audioContext.currentTime); // Start at a higher volume for prominence | ||
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 1.2); // Longer decay for a sustained ride sound | ||
|
||
// Connect the nodes: noise -> highPass -> lowPass -> gain -> output | ||
noise.connect(highPassFilter); | ||
highPassFilter.connect(lowPassFilter); | ||
lowPassFilter.connect(gainNode); | ||
gainNode.connect(audioContext.destination); | ||
|
||
// Play the noise | ||
noise.start(); | ||
noise.stop(audioContext.currentTime + 1.2); // Stop after 1.2 seconds for a ringing ride cymbal effect | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {playTom1Low} from './tom-low.ts'; | ||
|
||
export function playTom2High() { | ||
playTom1Low(120, 400); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Create an Audio Context | ||
const audioContext = new (window.AudioContext || (window as any).webkitAudioContext)(); | ||
|
||
export function playTom1Low(hz = 99, durationInMs = 500) { | ||
// --- 1. Create an oscillator for the base tone --- | ||
const oscillator = audioContext.createOscillator(); | ||
oscillator.type = 'sine'; // Use a sine wave for the base | ||
oscillator.frequency.setValueAtTime(hz, audioContext.currentTime); // Lower frequency for a deep tom sound | ||
|
||
// --- 2. Create a gain node for controlling volume --- | ||
const gainNode = audioContext.createGain(); | ||
gainNode.gain.setValueAtTime(1, audioContext.currentTime); // Increase initial gain for louder sound | ||
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.5); // Quick decay | ||
|
||
// --- 3. Add a slight detuning effect to make the tom sound richer --- | ||
oscillator.detune.setValueAtTime(-5, audioContext.currentTime); // Small detuning for realism | ||
|
||
// --- 4. Connect nodes: oscillator -> gain -> output --- | ||
oscillator.connect(gainNode); | ||
gainNode.connect(audioContext.destination); | ||
|
||
// Start the oscillator | ||
oscillator.start(); | ||
|
||
// Stop after 0.5 seconds (length of the sound) | ||
oscillator.stop(audioContext.currentTime + (durationInMs / 1000)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters