Skip to content

SoundEffect Loading

John Baglio edited this page Oct 5, 2024 · 8 revisions

It is highly recommended to check the Basic Setup page if you haven't already.
The features mentioned in this page will not work if MonoSound has not been initialized.

Table of Contents
Preface
How to Load Sounds
XACT Support
Applying Sound Filters
Stream Sources

Preface

The MonoGame Content Builder tool isn't great. While it supports various audio formats when compiling, they are all converted into uncompressed WAVE data in the compiled .xnb file.

This can be especially problematic when you want to use formats like .ogg to reduce the size of your game on disk.

MonoSound provides an API via the MonoSound.EffectLoader class which allows you to load audio files directly instead of having to use an intermediate compiled format.

How to Load Sounds

To load a SoundEffect from a file, call EffectLoader.GetEffect():

SoundEffect sound = EffectLoader.GetEffect("Content/spooky.mp3");
sound.Play();

By default, MonoSound supports loading audio files from the following formats:

  • WAVE (.wav)
  • OGG Vorbis (.ogg)
  • MPEG Audio Layer III (.mp3)
  • Compiled SoundEffect (.xnb)
  • XACT Banks
  • Raw PCM Audio (.pcm) via class PcmFormat : CustomAudioFormat

Custom audio formats outside of these can also be supported, although they have to be registered if they want to be detected by this method.
See the Custom Audio Formats page for more information.

XACT Support

The legacy Cross-platform Audio Creation Tool (XACT) format is also supported by MonoSound, except it uses special methods.

To load a SoundEffect from XACT audio files, you first need the Wave Bank (.xwb) and corresponding Sound Bank (.xsb) files.
The Sound Engine (.xgs) file generated by XACT is not used by MonoSound and can be ignored.

Then, to load an effect from the banks, call EffectLoader.GetEffectFromBank():

SoundEffect xactSound = EffectLoader.GetEffectFromBank("Content/Sound Bank.xsb", "Content/Wave Bank.xwb", "mysound");
xactSound.Play();

NOTE: MonoSound only supports simple Cues for XACT audio.

Applying Sound Filters

MonoSound also has an API for applying sound filters to sounds.
To learn how to register sound filters, check the Sound Filtering page.

Once you have the IDs for the registered filters, you can load a SoundEffect that has been filtered by calling EffectLoader.GetFilteredEffect():

SoundEffect filteredEffect = EffectLoader.GetFilteredEffect("Content/spooky.mp3", id);
filteredEffect.Play();
// Or, if you have the filter instance
SoundEffect filteredEffect = EffectLoader.GetFilteredEffect("Content/spooky.mp3", /* SoLoudFilterInstance */instance);
filteredEffect.Play();

If you want to apply multiple filters to a SoundEffect, call EffectLoader.GetMultiFilteredEffect() instead:

SoundEffect filteredEffect = EffectLoader.GetMultiFilteredEffect("Content/spooky.mp3", id, id2);
filteredEffect.Play();
// Or, if you have the filter instances
SoundEffect filteredEffect = EffectLoader.GetMultiFilteredEffect("Content/spooky.mp3", /* SoLoudFilterInstance */instance, instance2);
filteredEffect.Play();

In a similar vein, XACT SoundEffect can have filters applied to them via EffectLoader.GetBankFilteredEffect():

SoundEffect filteredEffect = EffectLoader.GetBankFilteredEffect("Content/Sound Bank.xsb", "Content/Wave Bank.xwb", "mysound", id);
filteredEffect.Play();
// Or, if you have the filter instance
SoundEffect filteredEffect = EffectLoader.GetBankFilteredEffect("Content/Sound Bank.xsb", "Content/Wave Bank.xwb", "mysound", /* SoLoudFilterInstance */instance);
filteredEffect.Play();

... as well as EffectLoader.GetBankMultiFilteredEffect():

SoundEffect filteredEffect = EffectLoader.GetBankMultiFilteredEffect("Content/Sound Bank.xsb", "Content/Wave Bank.xwb", "mysound", id, id2);
filteredEffect.Play();
// Or, if you have the filter instances
SoundEffect filteredEffect = EffectLoader.GetBankMultiFilteredEffect("Content/Sound Bank.xsb", "Content/Wave Bank.xwb", "mysound", /* SoLoudFilterInstance */instance, instance2);
filteredEffect.Play();

Stream Sources

To load sound effects from a System.IO.Stream, you have to use the appropriate overload for GetEffect(), GetFilteredEffect(), etc.

If Controls.LogFilters is set to true and Controls.LogDirectory is set to a valid directory, all filtered SoundEffects will be logged.
Since System.IO.Stream doesn't have a name, a name for the logged file has to be specified in the method call.

// The logged file will be named something like "spookyLOWPASS_filtered_2024-10-15_17-40-53-101.wav"
SoundEffect filteredEffect = EffectLoader.GetFilteredEffect(soundStream, "spookyLOWPASS", /* BiquadResonantFilterInstance */bqrInstance);

Clone this wiki locally