Weird sound issues for some users

We have seen that SoundEffectInstance.PlatformStop() often throws an exception:

“System.AccessViolationException occurred in SharpDX.XAudio2.dll”

when exiting the game. Not sure if that is related.

 private void PlatformStop(bool immediate)
        {
            if (_voice != null)
            {
                if (immediate)
                {
                    _voice.Stop(0); <- throws exception on exit
                    _voice.FlushSourceBuffers();
                }
                else
                    _voice.Stop((int)PlayFlags.Tails);
            }

            _paused = false;
        }

Stack trace:

   ved SharpDX.XAudio2.SourceVoice.Stop(PlayFlags flags, Int32 operationSet)
   ved SharpDX.XAudio2.SourceVoice.Stop(Int32 operationSet)
   ved Microsoft.Xna.Framework.Audio.SoundEffectInstance.PlatformStop(Boolean immediate)
   ved Microsoft.Xna.Framework.Audio.SoundEffectInstance.Stop(Boolean immediate)
   ved Microsoft.Xna.Framework.Audio.SoundEffectInstancePool.StopPooledInstances(SoundEffect effect)
   ved Microsoft.Xna.Framework.Audio.SoundEffect.Dispose(Boolean disposing)
   ved Microsoft.Xna.Framework.Audio.SoundEffect.Finalize()

The destructor makes the call that crashes:

 /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="Microsoft.Xna.Framework.Audio.SoundEffect"/> is reclaimed by garbage collection.
        /// </summary>
        ~SoundEffect()
        {
            Dispose(false); <- crashes on exit
        }

Possibly the master voice is already disposed. Do you run audio management on a seperate thread? This can happen if you interact with SoundEffects after disposing your Game. This is a bug. Check out https://github.com/MonoGame/MonoGame/issues/5153 for details on this. It could be something else, but most likely somehow the master voice got destroyed.

No, the sound and music is controlled from the main thread.

Now one of the players who has sound problems is reporting a crash in MediaPlayer too:

Object reference not set to an instance of an object.
Line: 0
at Microsoft.Xna.Framework.Media.MediaPlayer.SetChannelVolumes()

This is the method that crashes:

 private static void SetChannelVolumes()
        {
            if (_volumeController == null)
                return;

            float volume = _isMuted ? 0f : _volume;
            for (int i = 0; i < _volumeController.ChannelCount; i++)
                _volumeController.SetChannelVolume(i, volume);
        }

Perhaps it crashes because the state isn’t Playing, not sure. I have a warning in comments that says:
“Assigning to it can be done just before calling MediaPlayer.Play, or when the local state variable described above is MediaState.Playing, and at no other state.”

EDIT: I found the GitHub issue for this error:

I had something like this that was down to culture settings… for example, if you load data from txt files, it is sensitive to whether you use comma or period as decimal points…

This will be a problem for users with different cultural settings on their machine, UNLESS you specify in your string reader to ignore that cultural sensitivity, and read the file as YOU intended…

In practice, it screws up the decimal point, so users can get volume levels outside the allowed range, which causes crashes… Also true for alpha values etc, any decimal value stored in a file you read from. -Though that just causes high alpha contrast, not crashes.

I’m actually reading the volume setting from an xml file. Wow, I never thought of that.

Yeah it freaked me out too… Luckily its a pretty easy fix, just an overload I think.

If it is the case that this is an issue for XML, but I imagine it would be, since users can edit these on client machines…?

Come to think of it, I think this happened to me while I was “transitioning” as well… I was converting my XNA code to monogame, and a few things had to be updated.
Or maybe it was the new editor version? Who knows…

Oh, and if that fixes it, dont forget to mark as solved. Fingers crossed.

Always use InvariantCulture for this stuff. You don’t want data you save for your game to be culture specific

1 Like

Well I don’t think it’s the cause. XmlSerializer is locale independent, and in addition, CurrentThread culture is Invariant. The user has decimal points ‘.’ in his file also.

Ok, but just as a reminder, about the last line of your comment, him having the same ‘.’ in his file would be the exact issue causing the error. The reader would be expecting a ‘,’ on HIS machine, WHILE expecting a ‘.’ on yours. And since the file only has one of those two, one of you guys would be having issues the other did not.

So, what happens if your xml files contain numbers with ‘,’ and ‘.’ ?
This serializer just interpenetrates either as the decimal marker when reading from the file?

I have added more information. The crash happens in the destructor.

It crashes when a comma is used on my machine. I don’t know about the user, I did ask the user to try changing it.
I believe the XmlSerializer really is culture independent, that’s what I found out when googling it.
Also, the XML standard specifies a dot as decimal symbol.

Now the user says it is not all the sounds that have the problem, for instance the sounds played by the main menu buttons are OK. But ingame the problem appears.

I’m sorry, I just have to ask, are you sure about the serializer and culture sensitivity? The problem just really sounds like a decimal error on some machines could be the cause. You would get all sorts of unpredictable errors and crashes, and nothing would caught by the debugger becasue the syntax and everything is correct. I have done a search for this, but I didn’t find any documentation one way or the other. It might have been implemented in a change or patch somewhere between the creation of XNA and current release of monogame.?

The MasterVolume property on sound effects validates that volume is within 0 and 1. So it will throw an exception regardless.

 public static float MasterVolume 
        { 
            get { return _masterVolume; }
            set
            {
                if (value < 0.0f || value > 1.0f)
                    throw new ArgumentOutOfRangeException();

                if (_masterVolume == value)
                    return;
                
                _masterVolume = value;
                SoundEffectInstancePool.UpdateMasterVolume();
            }
        }

Music volume doesn’t throw, but silently clamps the value within 0 and 1, so that should be safe.

Hm ok.
Well, since the problems seem to depend on the end-user machine, and seems related to audio, could it be a lack of available audio device on those machines? I think I remember my game not running depending on what audio cables I had plugged in.

I guess you could publish a version where you comment out all the audio, and see if that will run… That could help you zero in on the problem.

The game runs, but the sound effects are wrong, sometimes. According to the users.

Well that’s something. I’m out of specific ideas now, down to guess-work. Good luck from here on!

I’m kind of out of ideas to troubleshoot it. Here’s the issue I made:

1 Like

Did you ever get a resolution to this?

I have experienced something that seems similar to this that occurs after long plays of the game. No issues at first but after a half hour of so of gameplay, the sound becomes a garbled mess.

1 Like