Creating a Web Audio Amplifier

I am trying to build an amplifier using ScriptProcessorNode(yes, I know that it is outdated and yes, I am fully aware of createGainNode). The reason is that createGainNodeit is better suited to attenuate gain, rather than to increase it. I’m a sound engineer and I want to have a clip. I built a static version that processes a gain of at least 150 (as long as you limit the value of the floats after increasing). However, this new version, controlled by the range input signal, completely mutes the volume.

I do not get any errors in the console; why cut the volume?

<body>
<div id="button" style="background:#000;width:50px;height:50px;" onclick="connect()"></div>
<input id="range" type="range" min="1" max="128" value="1" oninput="myFunction(value)"/>
<script>
var context, 
    soundSource, 
    scriptNode, 
    buffer,
    value = 1;

    if(typeof AudioContext !== 'undefined') {
    context = new AudioContext();
}else if(typeof webkitAudioContext !== 'undefined') {
    context = new webkitAudioContext();
}else {
    throw new Error('AudioContext not supported. :(');
}

function xhr() {
    var request = new XMLHttpRequest();
    request.open("GET", 'bass.wav', true);
    request.responseType = 'arraybuffer';
    request.onload = function() {
        context.decodeAudioData(request.response, function onSuccess(decodedData) {
            buffer = decodedData;
        }, function onFailure() {
            alert("Decoding the audio buffer failed");
        });
    }
    request.send();
}
function connect() {
    scriptNode = context.createScriptProcessor(256, 2, 2);
    scriptNode.onaudioprocess = function(audioProcessingEvent) {
        var input = audioProcessingEvent.inputBuffer;
        var output = audioProcessingEvent.outputBuffer;

        for(var channel = 0; channel < datum.numberOfChannels; channel++) {
            var I = input.getChannelData(channel);
            var O = output.getChannelData(channel);

            for(var i = 0; i < input.length; i++) {
                O[i] = I[i];
                O[i] *= value;         
            }
        }
    }
    soundSource = context.createBufferSource();
    soundSource.buffer = buffer;
    soundSource.connect(scriptNode);
    scriptNode.connect(context.destination);
    soundSource.start();
}
connect();
function myFunction(val) {
    value = val;
    soundSource.disconnect();
    scriptNode.disconnect();
    soundSource.connect(scriptNode);
    scriptNode.connect(context.destination);
}
</script>

, , createGainNode, , .

2 createGainNode , . Chrome createGainNode , Firefox , . , , . - ? :

function connect() {
    soundSource = context.createBufferSource();
    soundSource.buffer = buffer;
    volume = context.createGain();
    volume.gain.value = 1500;
    soundSource.connect(volume);
    volume.connect(context.destination);
    soundSource.start();
}
function amplify() {
    soundSource.stop();

    var left = new Float32Array;
    var right = new Float32Array;

    left = buffer.getChannelData(0);
    right = buffer.getChannelData(1);

    for(var i = 0; i < left.length; i++) {
        left[i] = left[i] * 1500;
        if(left[i] > 0) {
            left[i] = 128;
        }
        if(left[i] < 0) {
            left[i] = -128;
        }
        right[i] = right[i] * 1500;
        if(right[i] > 0) {
            right[i] = 128;
        }
        if(right[i] < 0) {
            right[i] = -128;
        }
    }
    buffer.copyToChannel(left,0);
    buffer.copyToChannel(right,1);

    var amp = context.createBufferSource();
    amp.buffer = buffer;
    amp.connect(context.destination);
    amp.start();
}

3

bugzilla : https://bugzilla.mozilla.org/show_bug.cgi?id=1233821

+4

Source: https://habr.com/ru/post/1620679/


All Articles