Sounds Overview
An overview of how the MonoGame Framework provides audio playback through several core audio classes.
If your game is to use a few sound files, then the SoundEffect, SoundEffectInstance, and DynamicSoundEffectInstance classes will provide everything you need to play and stream audio during gameplay.
Simple Audio Playback
The simplest way to play sounds for background music or sound effects is to use SoundEffect and SoundEffectInstance. Source audio files are added like any other game asset to the project. For example code, see Playing a Sound, Looping a Sound, and Adjusting Pitch and Volume. For background music, see Playing a Song.
Accessing the Audio Buffer
Developers can use DynamicSoundEffectInstance for direct access to an audio buffer. By accessing the audio buffer, developers can manipulate sound, break up large sound files into smaller data chunks, and stream sound. For example code, see Streaming Data from a WAV File.
3D Audio
The SoundEffect class provides the ability to place audio in a 3D space. By creating AudioEmitter and AudioListener objects, the API can position a sound in 3D, and can change the 3D position of a sound during playback. Once you create and initialize AudioEmitter and AudioListener, call SoundEffectInstance.Apply3D.
Audio Constraints
When working with multiple platforms using MonoGame, there are a few constraints around audio that you will need to keep in mind and cater for, namely:
- Mobile platforms have a maximum of 32 sounds playing simultaneously.
- Desktop platforms have a maximum of 256 sounds playing simultaneously.
- Consoles and other platforms have their own constraints, please look at the console sdk documentation for more information,
Important
An InstancePlayLimitException exception is thrown if this limit is exceeded.
Audio Buffer Format
The byte[]
buffer format used as a parameter for the SoundEffect constructor, Microphone.GetData method, and DynamicSoundEffectInstance.SubmitBuffer method is PCM wave data. Additionally, the PCM format is interleaved and in little-endian.
The audio format has the following constraints:
- The audio channels can be mono (1) or stereo (2).
- The PCM wave file must have 16-bits per sample.
- The sample rate must be between 8,000 Hz and 48,000 Hz.
- The interleaving for stereo data is left channel to right channel.
Songs as Background Music
Access to the media library, combined with the ability to use playlists, allows games to create interesting background scores that can change with gameplay. Songs can be played directly from the media library, or can be imported by using the Content Pipeline. For more information, see Playing a Song.
Concepts
Important
How to articles to follow.
Reference
-
Provides a loaded sound resource.
-
Provides a single playing, paused, or stopped instance of a SoundEffect sound.
DynamicSoundEffectInstance Class
Provides properties, methods, and events for play back of the audio buffer.
-
Provides access to a song in the song library.