react-native icon indicating copy to clipboard operation
react-native copied to clipboard

What about Web Audio!

Open zoomclub opened this issue 11 years ago • 11 comments

The W3C endorsed Web Audio API has been a long time coming to the web and is by all means a great success, by being integrated into modern browsers. Yet, technology is jumping forward again making great gains like Web Audio pretty well stranded.

What are the options for using the Web Audio API in React Native? Are there other alternatives? Is it better to tap into the iOS specific sound toolkit? Can it do what Web Audio does?

See more here: https://github.com/Izzimach/react-webaudio/issues/3#issuecomment-99261941

zoomclub avatar May 06 '15 13:05 zoomclub

The web audio API is just on the web, you can try to implement something similar on top of iOS tools like AVPlayer as I mentioned in our email discussion. As far as I know, there are not any plans to build this into the core at the moment but it would be awesome if you wanted to attempt to re-implement the web audio API on top of iOS components! Feel free to ping me on irc @notbrent if you want a hand with getting started.

brentvatne avatar May 07 '15 18:05 brentvatne

I've wrapped up AmazingAudioEngine as a react-native component. I'll push this up to a repo over the weekend. Right now it's just basic polyphonic playback, looping, volume and panning. It was actually fairly straight forward to put together.

I've avoided marshaling the audio data over the bridge. Audio data is loaded via a (local) URL only and once loaded, referenced using a proxy string from JavaScript. So there's no support for custom filters or any other JS side processing of the audio data. I'll add the stock filters supplied by The Amazing Audio Engine and possibly others so most of the WebAudio can be supported.

Note, also I'm using a native side function to fetch (remote) audio data and persist it locally in the app's temp dir. From this you can use the local file URL that can be used by the audio engine.

joewood avatar May 09 '15 02:05 joewood

This is great progress! Thanks, will watch for your repo...

zoomclub avatar May 09 '15 03:05 zoomclub

You can use WebAudio directly as long as you execute JS code by the UIWebView or WKWebView interpreter.

Unfortunatelly, for now RCTWebView/RCTWebViewManager doesn't have stringByEvaluatingJavaScriptFromString: exposed. There's also no WKWebView JS methods available. It might change soon - see https://github.com/facebook/react-native/pull/1191.

So the first step is to make a small component - either by subclassing RCTWebView/RCTWebViewManager or by wrapping WKWebView - that exposes JS execution.

Second step is to add such component to your app - it might be invisible if you don't want to present any HTML/CSS.

Third step is to send WebAudio code to it. You can write WebAudio code directly, wrap it in function object and pass it by simple:

function stringExecutableFunction(f) {
    return "(" + f.toString() + ")();"
}

Of course there are some limitations - you'll have to remember that you're executing WebAudio in a different context than the rest of the app, so for example the variables cannot be directly assigned from the outer scope. Either keep all the audio code in the WebAudio functions or do some kind of value interpolation. Not being a JS expert myself, I'm using placeholders for it right now. There's probably a better way - do you know it? :)

I do have a working proof of concept, can push it if you're interested.

siejkowski avatar May 09 '15 21:05 siejkowski

@siejkowski - awesome, I would love to see the proof of concept!

brentvatne avatar May 10 '15 00:05 brentvatne

Where is the proof? Hopefully this issue does not die out with just a whimper!

zoomclub avatar May 17 '15 01:05 zoomclub

There is also this https://github.com/sebpiq/node-web-audio-api

superandrew213 avatar Mar 22 '16 21:03 superandrew213

How would you use node-web-audio-api/ within react native (especally how to connect it to the audio output ?)

also @siejkowski any chance to see your proof of concept ?

tom-s avatar Apr 25 '16 22:04 tom-s

@tom-s Sorry, I haven't pushed it anywhere. Also, I haven't touched ReactNative for whole year, so there's probably a better solution than a hack.

PoC was based on creating a hidden WebView and passing all the web audio calls to it. UI component was used as a JS execution engine. All the bridging between WebView and ReactNative JS execution thread was done manually. It was a hack a year ago and it's even more of a hack right now. Please look for better options :)

siejkowski avatar May 04 '16 14:05 siejkowski

@joewood Just curious, do you ever intend to make your port of The Amazing Audio Engine public?

The repo is pretty sparse.

chrbradley avatar May 10 '16 17:05 chrbradley

Hey @chrbradley, yes - I actually stopped using it (so it's probably very out of date with React Native). I'll dig up the code and put it up. It may be a good starting point for somebody else. I'll update and push a link up later this week.

joewood avatar May 10 '16 17:05 joewood