Sound

Unity Manual > User Guide > Creating Gameplay > Sound

Audio Listener

The Audio Listener acts as a microphone-like device. It receives input from any given Audio Source in the scene and plays sounds through the computer speakers. It is traditionally attached to the Main Camera.


The Audio Listener, attached to the Main Camera

Properties

The Audio Listener has no properties. It simply must be added to work. It is always added to the Main Camera by default.

Details

The Audio Listener works in conjunction with Audio Sources, allowing you to create the aural experience for your games. When the Audio Listener is attached to a GameObject in your scene, any Sources that are close enough to the Listener will be picked up and output to the computer's speakers. Each scene can only have 1 Audio Listener to work properly.

As long as the Sources are in mono format, the Listener will automatically position the sound correctly in the stereo field, at the correct volume. Stereo Sources will automatically play in both speakers. For example, if your character walks off a street into a night club, the night club's music should probably be stereo, while the individual voices of characters in the club should be mono with their realistic positioning being handled by Unity.

You should attach the Audio Listener to either the Main Camera or to the GameObject that represents the player. Try both to find what suits your game best.

Hints

  • Each scene can only have one Audio Listener.
  • You access the project-wide audio settings using the Audio Manager, found in the Edit->Project Settings->Audio menu.
  • View the Audio Clip Component page for more information about Mono vs Stereo sounds.

Audio Source

The Audio Source takes an Audio Clip and plays it from a position in the world.


The Audio Source in the Scene View and Inspector

Properties

Audio ClipReference to the sound clip file that will be played
Play On AwakeIf enabled, the sound will start playing the moment the scene launches. If disabled, you need to start it using the Play() command from scripting.
VolumeHow loud the sound is at a distance of 1 world unit (1 meter) from the Audio Listener.
PitchAmount of change in pitch due to slowdown/speed up of the Audio Clip. Value 1 is normal playback speed.
Min VolumeThe minimum value of the sound. No matter how far away you get, the sound will not get softer than this value.
Max VolumeHow loud the sound gets at the loudest. No matter how close you get, the sound will never get louder than this value.
Rolloff FactorHow fast the sound fades. The higher the value, the closer the Listener has to be before hearing the sound.
LoopEnable this to make the Audio Clip loop when it reaches the end.

Creating Audio Sources

Audio Sources don't do anything without an assigned Audio Clip. The Clip is the actual sound file that will be played back. The Source is like a controller for starting and stopping playback of that clip, and modifying other audio properties.

To create a new Audio Source:

  1. Import your audio files into your Unity Project. These are now Audio Clips.
  2. Go to GameObject->Create Empty from the menubar.
  3. With the new GameObject selected, select Component->Audio->Audio Source.
  4. Assign the Audio Clip property of the Audio Source Component in the Inspector.

Hints

  • The key to a nice sound environment is tweaking the Rolloff Factor.
  • 3D audio effects will only work for mono Audio Clips. Stereo Audio Clips will be mixed as-is into the sound output.

Audio Clip

Audio Clips are used by Audio Sources to represent the audio asset imported into Unity. Unity supports both Mono and Stereo audio assets.


The Audio Clip Inspector

Audio Clips just work. The only thing you should have to do with them is reference them from within Audio Sources.

Properties

Sound assets only have read-only properties. Some of these properties can be changed in the Import Settings

FormatThe format the sound is stored in. Unity supports 4 raw formats and one compressed:
Mono 8 bit8 bit uncompressed mono PCM audio
Mono 16 bit16 bit uncompressed mono PCM audio
Stereo 8 bit8 bit uncompressed stereo PCM audio
Stereo 16 bit16 bit uncompressed stereo PCM audio
Ogg VorbisOgg Vorbis encoded stereo or mono audio
LengthThe duration of the sound file in seconds.
FrequencyThe sampling frequency of the file.

Importing Audio Assets

Unity features support for two types of audio: Uncompressed Audio or Ogg Vorbis. Any type of audio file you import into your project will be converted to one of these formats.

File Type Conversion

.AIFFConverted to uncompressed audio on import, best for short sound effects.
.WAVConverted to uncompressed audio on import, best for short sound effects.
.MP3Converted to Ogg Vorbis on import, best for longer music tracks.
.OGGCompressed audio format, best for longer music tracks.

Import Settings

If you are importing a file that is not already compressed as Ogg Vorbis, you have a number of options in the Import Settings of the Audio Clip. To access the Import Settings, select the Audio Clip in the Project View and choose Assets->Import Settings... from the menu bar, or click the Settings button in the Project View header. Here, you can compress the Clip into Ogg Vorbis format, force it into Mono or Stereo playback, and tweak other options. There are positives and negatives for both Ogg Vorbis and uncompressed audio. Each has its own ideal usage scenarios, and you generally should not use either one exclusively.


The Audio Clip Import Settings
Audio FormatOnly available if the file was initially saved in an uncompressed format.
UncompressedBest for short sound effects. For more info, jump to the using uncompressed audio section.
Ogg VorbisBest for longer audio files like music. For more info, jump to the using Ogg Vorbis section.
ChannelsYou can force the sound to play back in Mono or Stereo. This is important for using Positional Audio.
BitrateSet the bitrate to optimize file size of compressed Ogg Vorbis Clip
Decompress on loadIf enabled, this Clip will be decompressed into memory when the containing level is loaded. This is important for optimizing player experience.

Using Positional Audio

Positional audio is the placement and volume of sounds across the computer speakers. It simulates distance and location of sounds by fading volume and panning across speakers. Unity supports Positional audio, but only mono Audio Clips are played positionally. Stereo sounds are always at the volume and panning determined by the asset file. For this reason, mono Clips are best for sound effects, and stereo Clips are best for music.

There are special cases, like if you wanted music playing from two unique in-game speaker environment objects. The closer you move your character to one of the speakers, the louder the music will get. This setup would require the speaker music in mono format.

If you have an Audio Clip that is in stereo, but you want to use it positionally, change the Channels property to Force Mono in the Import Settings. This allows you to be very creative with how you use audio in your game.

When to use Uncompressed Audio

If you are creating a web player game, do not use uncompressed audio. Only use Ogg Vorbis.

Uncompressed has the benefit of being quick to play without increasing CPU taxation. This is because it does not need to be decompressed before it can be played. However, uncompressed audio is quite large so it will increase the size of your player. Usually short Clips like sound effects are good for uncompressed audio, unless you are publishing a web player. If you are publishing a web player, you don't want to increase the file size any more than is absolutely necessary so you basically won't use uncompressed audio, unless you have a few crucial sounds that are shorter than 0.5 seconds. Clips that Uncompressed audio should usually be either 22khz or 11khz. Ideally you should not use 44khz since that takes up too much disk space and the quality difference is not very audible.

When to use Ogg Vorbis

If you are creating a web player game, always use Ogg Vorbis.

Ogg Vorbis files have the benefit of being compressed, which minimizes the amount of disk space they consume. This means that you can include long audio files without making a huge increase in the file size of your player. However, before they can be played back they must be decompressed. This takes time and processor cycles. Frequency of Ogg Vorbis files should be 44khz, because playback will be faster than files with a 22khz frequency.

Loading Ogg Vorbis files

If you're using Ogg Vorbis, you have an option to choose when the Clip will be decompressed. The option is set in the Import Settings:

DecompressOnLoad enabledDecompress and load the Clip into RAM when the player is loading.
DecompressOnLoad disabled (Live decompression)Decompress and play the Clip real-time when the Clip is triggered to start.

Decompressing on load is the most common. It means that the levels containing the Clip will take longer to load initially, and it will use up more of the end-user's RAM. Live decompression does not load the Clip data into RAM at level load. Instead, it requires the CPU to decompress the sound real-time. This means it is more CPU intensive rather than RAM intensive.

To summarize, enable decompression on load as much as possible, and definitely for all short Clips. Pick live decompression for longer Clips like music tracks. All Ogg Vorbis Clips using live decompression will increase CPU usage. It can use a substantial amount of CPU to live decompress multiple Clips at once, so you should avoid playing more than 2 live decompression Clips at once.

Hints

  • There is not much of a reason to import your own .ogg files. Create your sounds in AIFF or WAV format, then you can compress them if you want to right inside the Import Settings.
  • Stereo sounds are always played as-is. If you want to use attenuation and other 3D audio effects, force your Clip to be mono in the Import Settings.