General Audio

So I’ve been playing around for about 6 months in Monogame (when i’ve had spare time). I’ve created some nice 2d lighting that i’ve shared here and worked on a couple different projects / minigames. I’m currently in the process of playing with some audio.

There’s a few things in the audio portion that just dont seem complete. I got what I assume is trunk latest; and a few things (that I would give the opinion are needed) are missing. (Built my own libraries)

Song Duration
Song Looping

There’s nothing that’s stopping the engine from exposing these values, the work just isnt done (I think?)??? I give question marks cause I could totally be using this wrong.

Here’s what I did.

(Public / Private / Accessors / Modifiers changed throughout SoundEffect.cs Song.Default.cs)

In Song.Default.cs I made private void PlatformInitialize(string fileName)


 Duration = soundEffect.Duration;

which I set in SoundEffect FromStream

if (sfx.Format == OpenTK.Audio.OpenAL.ALFormat.Stereo16)
                sfx.Duration = GetSampleDuration(sfx._data.Length, (int)sfx.Rate, AudioChannels.Stereo);
            else if (sfx.Format == OpenTK.Audio.OpenAL.ALFormat.Mono16)
                sfx.Duration = GetSampleDuration(sfx._data.Length, (int)sfx.Rate, AudioChannels.Mono);

My song now has a valid duration.

Song now has a

public bool Looping
                return _sound.IsLooped;

                _sound.IsLooped = value;

Section (_sound is public)

As long as you call Looping=true before you call play (Which on windows (and I assume linux) invokes the AL_Looping value your music will now loop.

Now I can just call this, and have infinite music playing:

m_oLevelsSong = SpriteManager.Instance.m_poContentManager.Load<Song>("BKGMusic/Destro/BattleTheme.wav");
            m_oLevelsSong.Looping = true;

Is there another way around this, or is that pretty much the solution?

Song duration is already in the API…

It is only set when a Song is loaded via the content pipeline. If you are loading via Song.FromUri() then maybe we need this feature.

Song looping is not a property of Song itself. The repeat flag is on the player…

There is work there however as many of the platforms don’t implement repeating support. We can use help fixing those.

I would not try to improve Song.Default.cs.

That is the horrible fallback implementation of Song that internally uses SoundEffect. That path is a complete hack… it will load the entire music file into memory uncompressed. Every look at the size of an uncompressed MP3?

I would spend your effort fixing the platform you’re targeting to have a proper Song implementation.

To each their own. Without any tutorials its hard to say what the desired implementation should be.

I’m happy with my results; if others want to use it then there they go :).

Here’s what I would rather do:

m_oLevelsSong = SpriteManager.Instance.m_poContentManager.Load<Song>("BKGMusic/Destro/BattleTheme.wav");
MediaPlayer.IsRepeating = true;

However that doesn’t work. I’ve tried ogg files on windows, wma, nothing else loads through that song… Perhaps if you can identify the problem with the song file? I’m trying to NOT use mp3.

Trying it just like that above the music does not repeat.

Sure… that is why you can fork on GitHub. Make any changes you want. :smile:

alright? looping a song?

Song songname;

// load content
songname = Content.Load(“songfilename”);

// song looping algorithim for update method
if ((MediaPlayer.State == MediaState.Stopped) || (MediaPlayer.State == MediaState.Paused))