使用 web-audio-api 的变调器?

pitch shifter using web-audio-api?

使用 Node js 的变调器

嗨,我是网络开发的初学者!

soo 我正在尝试构建一个在线音频播放器,我需要一个变调器。

我尝试学习网络音频API,这对我来说不太容易理解...

任何人都可以使用 node js 帮助构建 "Pitch Shifter"...或者建议学习网络音频的资源 API...

为什么这段代码在 node js 中不起作用?

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

很遗憾,网络音频 API 在 Node.js 上不可用。 Node.js 只是一个 JavaScript 运行时,网络音频 API 不是 JavaScript 本身的一部分。它是由浏览器添加的 API。 Node.js 中甚至没有 window

有些浏览器 API 在 Node.js 中也可用,这让事情变得更加混乱。一个例子是全球可用 URL。今年JSConf.eu张心怡给了a talk explaining the strategy behind bringing more browser APIs to Node.js。但是网络音频 API 不在列表中。

在 Node.js 中提供 Web 音频 API 是否有意义是值得商榷的,但这肯定是可能的。至少在一定程度上如 web-audio-api and the web-audio-engine 项目所示。

如果您想在浏览器中实现 PitchShifter,您可以使用 PitchShift Effect that comes with Tone.js。这是一个最小的例子:

<!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>