0

使用 Node js 的音高变换器

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

所以我正在尝试构建一个在线音频播放器,我需要一个音高转换器。

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

任何人都可以帮助使用节点 js 构建“Pitch Shifter”...或者建议资源来学习网络音频 API ...

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

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

4

1 回答 1

1

不幸的是,Web Audio API 在 Node.js 上不可用。Node.js 只是一个 JavaScript 运行时,Web Audio API 不是 JavaScript 本身的一部分。它是由浏览器添加的 API。windowNode.js 中甚至没有。

更令人困惑的是,Node.js 中也提供了一些浏览器 API。一个例子是全局可用的URL。今年 JSConf.eu Joyee Cheung 发表了演讲,解释了将更多浏览器 API 引入 Node.js 背后的策略。但是,Web Audio API 不在列表中。

在 Node.js 中提供 Web 音频 API 是否有意义是有争议的,但它肯定是可能的。至少在一定程度上,如web-audio-apiweb-audio-engine项目所示。

如果你想在浏览器中实现 PitchShifter,你可以使用Tone.js附带的PitchShift效果。这是一个最小的例子:

<!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>
于 2019-10-27T12:29:18.030 回答