From 2038c6005e41b4d34d41e8f11dd261ed716d8bf1 Mon Sep 17 00:00:00 2001 From: Dominique Loiseau <5888729+domi7777@users.noreply.github.com> Date: Sat, 5 Oct 2024 14:57:43 +0200 Subject: [PATCH] fix(sound/kick): better sound --- src/samples/kick.ts | 13 ++++++++----- src/samples/sample-utils.ts | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 src/samples/sample-utils.ts diff --git a/src/samples/kick.ts b/src/samples/kick.ts index 064db45..89ef8c2 100644 --- a/src/samples/kick.ts +++ b/src/samples/kick.ts @@ -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. @@ -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 @@ -18,12 +21,12 @@ 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); @@ -31,5 +34,5 @@ export function playKick() { // 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) } diff --git a/src/samples/sample-utils.ts b/src/samples/sample-utils.ts new file mode 100644 index 0000000..66eceac --- /dev/null +++ b/src/samples/sample-utils.ts @@ -0,0 +1,4 @@ +export const createAudioContext = () => { + const AudioContext = (window as any).AudioContext || (window as any).webkitAudioContext; + return new AudioContext(); +}