简体   繁体   中英

Node.js - Having Node.js/Express work with Web Audio API

I am programming a Node.js/Express that requires the use of the Web Audio API. I've noticed that this is in-fact 'not possible' due to reasons explained in this stackoverflow answer . However, I've found two projects online that seem to me to use Node.js/Express as well as the Web Audio API. They can be found here and here . When I try to do something similar in my application (running using npm start I get the following error:

/Users/user/Documents/STAGE4/PRCO304/RTMC/controllers/workstation/workstation.js:1
(function (exports, require, module, __filename, __dirname) { var context = new AudioContext();
                                                                                  ^

ReferenceError: AudioContext is not defined

When I change the line var context = new AudioContext(); to something like window.AudioContext = window.AudioContext I get the error that the window isn't defined, which I understand as how Node.js works.

Could someone please explain to me how both those projects manage to use the Web Audio API with Node.js/Express where when I try to implement something similar in mine it fails?

Thanks in advance.

Web Audio API is on the browser side, baked into modern browsers no library is explicitly pulled in. Of course nodejs is server side so you would need to write your own or discover a node package where someone has ported the Web Audio API, using its published specs , for use directly on the nodejs platform, that is if you want a stand alone nodejs server based audio rendering project, independent of any browser. The SO answer you refer to mentions a npm module using this approach

You could of course design your system to just interconnect both the server and browser which is very doable today and the route most people take when combining node with audio.

You have not made clear which architecture you prefer ???

nodejs certainly can be used with Web Audio API in either architecture and I have written projects using each approach

Your error above I would guess is trying to execute server side code which is not talking to a browser ? In this case make sure your are pulling in a nodejs ported Web Audio API library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM