Skip to content

Commit

Permalink
fix(sound/kick): better sound
Browse files Browse the repository at this point in the history
  • Loading branch information
domi7777 committed Oct 5, 2024
1 parent 7da0fe5 commit 2038c60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/samples/kick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Create an Audio Context
const audioContext = new (window.AudioContext || (window as any).webkitAudioContext)();

// How It Works:
// Low-Frequency Oscillator: We start with a sine wave, which provides the smooth, deep bass characteristic of a kick drum.
Expand All @@ -9,7 +8,11 @@ const audioContext = new (window.AudioContext || (window as any).webkitAudioCont
// Pitch: Adjust the initial frequency (150 Hz) or the final frequency (60 Hz) to create different kinds of kicks (e.g., higher for more punch, lower for deeper bass).
// Decay: The duration of the gain envelope can be adjusted to make the kick longer or shorter.

import {createAudioContext} from './sample-utils.ts';

export function playKick() {
const audioContext = createAudioContext();

// Create an oscillator for the low "thump"
const osc = audioContext.createOscillator();
osc.type = 'sine'; // Sine wave for a smooth, deep bass sound
Expand All @@ -18,18 +21,18 @@ export function playKick() {
const gainNode = audioContext.createGain();

// Frequency (pitch) envelope: Start higher and quickly decay to a lower frequency
osc.frequency.setValueAtTime(150, audioContext.currentTime); // Initial higher frequency for punch
osc.frequency.exponentialRampToValueAtTime(60, audioContext.currentTime + 0.1); // Drop to 60Hz
osc.frequency.setValueAtTime(200, audioContext.currentTime); // Initial higher frequency for punch
osc.frequency.exponentialRampToValueAtTime(70, audioContext.currentTime + 0.05); // Drop to 60Hz

// Gain envelope: Start loud and quickly decay
gainNode.gain.setValueAtTime(1, audioContext.currentTime); // Start loud
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.5); // Decay to quiet over 0.5 seconds
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 1); // Decay to quiet over 1.5 seconds

// Connect oscillator to gain, then to the audio context destination
osc.connect(gainNode);
gainNode.connect(audioContext.destination);

// Start and stop the oscillator
osc.start();
osc.stop(audioContext.currentTime + 0.5); // Stop after 0.5 seconds (length of the kick)
osc.stop(audioContext.currentTime + 1.5); // Stop after 0.5 seconds (length of the kick)
}
4 changes: 4 additions & 0 deletions src/samples/sample-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const createAudioContext = () => {
const AudioContext = (window as any).AudioContext || (window as any).webkitAudioContext;
return new AudioContext();
}

0 comments on commit 2038c60

Please sign in to comment.