使用网络音频API的音调变送器?

人气:86 发布:2023-01-03 标签: javascript node.js web-audio-api pitch-shifting

问题描述

使用Node js的音调转换器

您好,我是Web开发的初学者!

我正在尝试建立一个在线音频播放器,为此我需要一个音调变送器。

我试着学习Web音频API,这对我来说不是很容易理解...

有没有人能用节点js帮助建立一个"音调移位器"……或建议学习Web音频API的资源...

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

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

推荐答案

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

更令人困惑的是,Node.js中还提供了一些浏览器API。全球可用的URL就是一个例子。在这一年,JSConf.eu张祖儿给了a talk explaining the strategy behind bringing more browser APIs to Node.js。但是,Web Audio API不在列表中。

在Node.js中提供Web Audio API是否有意义还有待商榷,但这当然是可能的。至少在一定程度上,如web-audio-api和web-audio-engine项目所示。

如果要在浏览器中实现PitchShifter,可以使用Tone.js附带的PitchShift Effect。下面是一个最小的例子:

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

21