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

Open-graph meta data tags introdution

What is 'this.' keyword in JavaScript

How to create zip archives in nodejs

what is 'array.includes' function in JavaScript

How to build a twitter retweet bot using JavaScript