0

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)();

Aadarsh velu
  • 33
  • 1
  • 6

1 Answers1

1

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>
chrisguttandin
  • 7,025
  • 15
  • 21
  • Thanks for your Response, Chris! I'm happy with your answers. Do you know to create a 3d effect in tone js? – Aadarsh velu Oct 28 '19 at 09:03
  • There is a spatialization example (https://tonejs.github.io/examples/spatialPanner.html) in the official documentation. – chrisguttandin Oct 28 '19 at 09:31
  • Hi, I'm interested in doing an audio app on the web, so I need to connect pitch shifter to web audio API! so your above code works but I need to connect this to web audio API's audio context, Is there any way? if there anyways just please help me, dude! – Aadarsh velu Nov 01 '19 at 15:19
  • Thanks for turning this into a separate question. https://stackoverflow.com/questions/58676929/how-to-connect-web-audio-api-to-tone-js I think this is the better approach as it doesn't have much in common with the original question. And it allows others to find it more easily. – chrisguttandin Nov 03 '19 at 12:32