i have a solution (i think) for songs with partial loops and other cool stuff

hi! like in the title, i found a way to play .ogg/.wav song files with some added stuff and i thought i’d share.

my implementation includes:

  • partial song loops
  • playing a song from any arbitrary position
  • changing volume
  • changing pitch

the built in MonoGame song implementation didn’t have partial song loops so that’s why i had to figure out something else.

i had a really hard time finding info about doing this, it was extra hard since i really don’t know anything about reading files, or whatever the heck bytes are. but with some intuition and some copying and pasting i was able to get things working some how.

my main resoures were this reddit post:

and this “hot examples” website:

checkout the gist below if you’re interested. it has the .ogg/.wav reading stuff and a basic use example.

i did notice that reading the .ogg files takes quit a bit longer than .wav files for some reason. so maybe i shouldn’t be loading all my songs at the start anymore, or maybe i did something wrong where it takes longer than it needs who knows.

feel free to let me know if there are any improvements that could be made!

so in response to the slowness i chanced the DynamicSong constructor to this

    public DynamicSong(string path)
        Name = path.Split("/").Last();
        Name = Name.Split(".")[0];
        Task.Factory.StartNew(() =>
            Loaded = true;

and then i just check if it’s loaded when i try to play it, if it isn’t i queue it until it is loaded. with this my game loads instantly as opposed to taking like 8 seconds.

this is another thing, where i’m not sure if i’m doing a no-no just willy-nilly opening up a bunch of threads when the game starts to load all these .ogg files (i don’t know much about threads, i just know it makes things go fast cause multiple things are happening at the same time). i have a pretty good computer so it’s hard to tell when i’m doing dumb performance stuff sometimes. on a weak computer if i open a bunch of threads like this will their computer explode? or is C# smart enough to not do that?

Experimenting with multithreaded code when you’re not sure what you’re doing can be risky. Thanks to race conditions and such, your code might appear to work fine one time and then seemingly randomly crash another time, without you having done anything noticeably different. It’s also really hard to debug multithreaded code, I think. I stay away from multithreading because I don’t know what I’m doing with it. As far as spinning up a bunch of threads on a weaker computer, no, it won’t explode. The worst that could happen to the computer would be 100% CPU usage, and that would just mean the computer would become sluggish.

Was that sarcasm? =P A cursory glance over your code makes it hard to believe you don’t know what bytes are.