Gamasutra: The Art & Business of Making Gamesspacer
Advanced Audio Streaming in Unity
View All     RSS
November 19, 2018
arrowPress Releases
November 19, 2018
Games Press
View All     RSS
  • Editor-In-Chief:
    Kris Graft
  • Editor:
    Alex Wawro
  • Contributors:
    Chris Kerr
    Alissa McAloon
    Emma Kidwell
    Bryant Francis
    Katherine Cross
  • Advertising:
    Libby Kruse






If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

Advanced Audio Streaming in Unity


June 6, 2013 Article Start Previous Page 3 of 3
 

FTP streaming has been working out very well, and for shorter audio tracks, it might be well worth to set the streaming percentage to 20-25 percent of the track length, or calculate 20 seconds of the total track, which equals 882,000 samples at 44.100.

The streaming of the SFXs was slightly more complex, and the SFXManager.js script is slightly more brain teasing, yet very simple to manage. For any Composer/Sound Designer it's a dream come true when sound design can be swapped and tested in real time. Which, for us, deploying and maintaining 6 Snake Games of various character yields for a strong pipeline. For a simple game like this, we organized the audio clips as enums. As they might change later, it will be useful to reference them by enum name, rather than a non-contextual number in other scripts:

When we had a list over all the audio clips, and their respective type, we wanted to stream them by order, so we could prioritize them based on usage in the game. Hopefully, the Game Over sound is the last sound you hear, but could also be among the first!

Click for larger version.

We kept all the audio volumes in the game constant at 1.0, so they could easily be mastered and adjusted in Pro Tools by Ben. It will most certainly result in a higher audio quality than adjusting the volume by the Unity built-in volume controller, thus ideal for any 2D game. The script creates an array of AudioClips, and stores them into memory once streamed, based on the prioritized order. This is the remainder of the script:

Click for larger version.

Similar to the audio streamer script, it also selects the SFX based on which game is selected. However, in this case, the streaming should be set to false:

www.GetAudioClip (false, false, AudioType.OGGVORBIS);

When loaded into memory, it'll avoid popping and other artifacts that could appear when streaming. The AUDIO_TYPES[_index] section can be expanded to include any types, though we decided to stick with .ogg and .wav for SFX. It's possible to add another else that points to AudioType.unknown as a fallback. I encourage playing around with these functions and implementing them in your own games. Call up your local sound designer and reskin those audioscapes!

The last section of this script is a Game Jam nugget. Right, because there's little time to set up a proper audio manager during a GJ, and a quick solution is a great timesaver. For the last Game Jam, I managed to implement more than 10 sound effect into our game in the last 15 min of the jam. The code looked more similar to this:

static function PlayAudio (_clip : String) {

ourAudio.PlayOneShot(Resources.Load("SFX/" + _clip), 1.0);

}

The great benefit about the function above, is that it can be called from any other script. Simply name the script above SFXManager and add it to a singleton object. You can call it from any other scripts:

SFXManager.PlayAudio("start_game");

This assumes that you have already created a folder named SFX under the Resources folder, and that you create a reference to a static AudioSource from the Awake() function.

All in all, the audio system works very well, and has great potential to be extended further. As a challenge, we would love to hear your suggestions for improvements. Some useful improvements that came to our minds:

  • Swapping between two or three audio sources to risk any audio cutoffs
  • Adding fallback sounds if the current audio is not currently streamed yet
  • A block of code that makes the script recognize the audio type on the server

We encourage you to explore, as there are endless possibilities here. If you have not dug into Unity audio, or audio steaming for web yet, this is the perfect time. We decided to share these scripts with you, so you can get both the stream_audio.js and SFXManager.js

Feel free download the scripts by clicking here.

This article first appeared on the Game Audio 101 here.


Article Start Previous Page 3 of 3

Related Jobs

Plarium Michigan Studio LP
Plarium Michigan Studio LP — Portage, Michigan, United States
[11.19.18]

Senior Game Developer
Monomi Park
Monomi Park — San Mateo, California, United States
[11.16.18]

Senior Game Engineer
Cold Iron Studios
Cold Iron Studios — San Jose, California, United States
[11.15.18]

Console Gameplay Engineer
Cold Iron Studios
Cold Iron Studios — San Jose, California, United States
[11.15.18]

Infrastructure Engineer





Loading Comments

loader image