webapi – Using the web audio API to create a mouse click sound

I am trying to make a mouse click sound using the web audio API but it’s so hard to understand how the different waves actually produces sounds.

Here’s my attempt at it which failed.

const audioContext = new (window.AudioContext || window.webkitAudioContext)();

const oscillator = audioContext.createOscillator();
oscillator.type="triangle"; // Use a sawtooth wave
oscillator.frequency.setValueAtTime(140, audioContext.currentTime); 
oscillator.frequency.setValueAtTime(1040, audioContext.currentTime + 0.025); 



    const gainNode = audioContext.createGain();
    gainNode.gain.setValueAtTime(1, audioContext.currentTime); 

    oscillator.connect(gainNode);


    gainNode.connect(audioContext.destination);


    const now = audioContext.currentTime;

    oscillator.start(now);

    // Stop the noise source after the envelope is complete
    oscillator.stop(now + 0.05);

Read more here: Source link