javascriptnode.jsweb-audio-apipitch-shifting

pitch shifter using web-audio-api?


Pitch Shifter using Node js

Hi, I'm a beginner in web Development!

soo I'm trying to build an online audio player for that I need a pitch shifter.

I try to learn web audio API which isn't soo easy to understand for me...

Can anyone help to build a "Pitch Shifter" using node js... Or suggest resources to learn web audio API...

why is this code not working in node js?

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();


Solution

  • Unfortunately the Web Audio API is not available on Node.js. Node.js is just a JavaScript runtime and the Web Audio API is not part of JavaScript itself. It is an API which is added by the browser. There isn't even a window in Node.js.

    To make things even more confusing there are some browser APIs which are available in Node.js as well. An example for that is the globally available URL. At this years JSConf.eu Joyee Cheung gave a talk explaining the strategy behind bringing more browser APIs to Node.js. However the Web Audio API is not on the list.

    It is debatable wether it makes sense to have the Web Audio API available in Node.js or not but it is certainly possible. At least to a certain extend as shown by the web-audio-api and the web-audio-engine projects.

    If you want to implement a PitchShifter in the browser you could use the PitchShift Effect that comes with Tone.js. Here is a minimal example:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
            <button id="start-stop">start</button>
            <button id="up">up</button>
            <button id="down">down</button>
            <script src="https://unpkg.com/tone@13.8.25/build/Tone.js"></script>
            <script>
                const $startStopButton = document.getElementById('start-stop');
                const $upButton = document.getElementById('up');
                const $downButton = document.getElementById('down');
    
                const oscillator = new Tone.Oscillator();
                const pitchShift = new Tone.PitchShift();
    
                oscillator.connect(pitchShift);
                pitchShift.toMaster();
    
                $startStopButton.onclick = () => {
                    if ($startStopButton.textContent === 'start') {
                        $startStopButton.textContent = 'stop';
                        oscillator.start();
                    } else {
                        $startStopButton.textContent = 'start';
                        oscillator.stop();
                    }
                };
    
                $upButton.onclick = () => pitchShift.pitch += 1;
                $downButton.onclick = () => pitchShift.pitch -= 1;
            </script>
        </body>
    </html>