Feature preview in Coherent UI 2.0: Custom Web Audio playback

by Nick June. 04, 14 0 Comment

The new Coherent UI 2.0 is right around the corner (coming on June 15th) and it brings some high-profile features such as support for popup windows, multiple custom protocols, custom Web Audio playback and much more. In this post I’ll shed some more (technical) light on the audio playback feature.

 

In Coherent UI 1.x, the sounds within a web page were played using a separate audio device object (created by Coherent UI), which was using some platform specific audio API. The specific APIs we used are Core Audio (Windows), Core Audio(Mac OSX) and ALSA(Linux). Everything worked like a charm if you just needed to play some 2D sound. If you needed to tune the audio, however, the situation was more complicated.

 

Here’s an obvious statement – most (all?) games have sound. On the technical side, they all use some audio API such as FMOD, WWise, OpenAL, etc, which creates some kind of audio device object that plays sounds. Do you see the connection here? Why do the work for setting up everything twice when you can to reuse your audio API for playing any sound that comes out of Coherent UI. That opens up a whole lot of new possibilities, too. For example, if you’re making a 3D game you can position the sound in 3D space! If that’s not good enough for you, you can also add DSP effects to the Web Audio from Coherent UI that’s consistent with the scene. Imagine that you’re underwater and you’re playing a sound in Coherent UI – you can now add environmental effects to that sound! You can also do any other processing you wish – turn up the volume, switch the left and right channels (I don’t know why anyone would do that, but you can :))… the possibilities are endless.

 

The new feature is actually very shy on API additions so it’s pretty straight forward. The idea is that you get a stream of PCM audio data and you keep playing it until you get a notification for the end of it. Here’s the complete list of the new public methods that the feature offers (in C++):

To get everything up and running, you start by setting the EnableClientAudioPlayback property to true.  Then, you need to implement the 4 ViewListener callbacks. Basically, you need to create a new audio source with the API of your choice when you receive the OnAudioStreamCreated callback, destroy it when you get OnAudioStreamClose, and play/pause it when you get OnAudioStreamPlay/OnAudioStreamPause, respectively.

The last method, View::GetAudioData, is what you should call when your source is (nearly) exhausted and you need more data. The call is synchronous so you might want to call that in a background thread. When it returns, you get the uncompressed PCM data in your buffer and the actual number of bytes read. Then you just feed the data to the audio source and proceed doing the same thing until the stream is closed. Pretty straightforward, right? We’ve also added samples using OpenAL and FMOD as backends to get you started.

 

The audio playback feature is just one of the many new exciting changes in Coherent UI 2.0. Stay tuned for more sneak previews and news for the product.

Don’t miss out on getting 50% off the price for the first month – sign up for our discount!

Social Shares

Leave a Comment