How to change sound volume using webAudio api gain property

A quick function to adjust html5 web audio gain value or "audio volume"
sound gain value is controlled by creating a gain node and setting it's gain value 0-1

where 0 is the lowest value 1 is the maximum gain value 

let sound = function sound(url, params) {
    // params
    let loop = params.loop;
    // make a context
    let audioContext = new (window.AudioContext || window.webkitAudioContext)();
    let source = audioContext.createBufferSource();

    let gainNode = audioContext.createGain()
        gainNode.gain.value = 0.05 // sound percentage

    let request = new XMLHttpRequest();'GET', url, true);

    request.responseType = 'arraybuffer';
    request.onload = function() {
            audioContext.decodeAudioData(request.response, function(buffer) {
            source.buffer = buffer;
            source.loop = loop;

        }, (e) => {
            throw new Error('Audio error! ', e);



Popular posts from this blog

How to set content disposition header for nginx server to force content download

Animated daylight on HTML5 Canvas

How to make a static http server in nodejs using express

How to make a Snake Game using JavaScript and html5

JS13K Preparation