Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made sound system optional #6629

Merged
merged 6 commits into from
Feb 10, 2019
Merged

Made sound system optional #6629

merged 6 commits into from
Feb 10, 2019

Conversation

ThomasFOG
Copy link
Contributor

@ThomasFOG ThomasFOG commented Jan 6, 2019

Hey there,

This PR should make Microsoft.Xna.Framework.Media and Microsoft.Xna.Framework.Audio optional so that no sound system is initialized if none of those namespace are used. This should make MonoGame friendly to third party sound engine like FMOD or Wwise.

It addresses:

As suggested previously, sound engine initialization is now done whenever SoundEffect Song MediaPlayer Microphone or DynamicSoundEffectInstance are constructed.
It is however possible to manually initialize the SoundEffect API by calling SoundEffect.Initialize(). This allows developers to get any initialization errors and act accordingly (e.g. disabling sound, or make it fatal).

Fixes #6515 #6107 #4717

@Jjagg @cra0zy
@tomspilman if this goes through, I'll try to get the console repos' compliant.

Copy link
Contributor

@Jjagg Jjagg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome PR! Thanks @mrhelmut :)
I made some comments, actually partly about existing code but made obvious in this PR.

MonoGame.Framework/Audio/OpenALSoundController.cs Outdated Show resolved Hide resolved
MonoGame.Framework/Audio/SoundEffect.OpenAL.cs Outdated Show resolved Hide resolved
MonoGame.Framework/Audio/SoundEffect.cs Show resolved Hide resolved
MonoGame.Framework/Media/Song.NVorbis.cs Outdated Show resolved Hide resolved
@ThomasFOG
Copy link
Contributor Author

Thanks! I'm going to refactor the controller instance management, it has been a bit messy for quite too long.

@ThomasFOG
Copy link
Contributor Author

ThomasFOG commented Jan 7, 2019

@Jjagg I think I got things clean up. Let me know how it goes for you.

EDIT: forgot Android, brb.

@ThomasFOG
Copy link
Contributor Author

Ready for review.

@Jjagg
Copy link
Contributor

Jjagg commented Jan 7, 2019

Looks good to me! Gonna hold off a little bit in case anyone else wants to go over this, but will merge if not.

@@ -128,7 +129,7 @@ public static ReadOnlyCollection<Microphone> All
/// </summary>
public static Microphone Default
{
get { return _default; }
get { SoundEffect.Initialize(); return _default; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write code like this in the static initializer instead of static property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so there is no confusion, ie:

static Microphone()
{
    SoundEffect.Initialize();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use static constructors in few other places.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to check if initialization was done properly on every call, rather than do it only once. It makes behavior more predictable. Static initializers complicate control flow because you can't be sure when they'll be called.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They get called during first access to their respective class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, Default and ```All`` are the microphone entry points, but you have to initialize the sound API to populate the microphones, and the Microphone class is static by design...
Unless we change the design of the Microphone class, I don't think we can do anything else than having this in a static field. I'm not a fan of static classes because this kind of design problems.

Alternative proposition: not initializing the sound API at all in the Microphone class, and throw if All/Default are accessed while not initialized. Forcing users to either call SoundEffect.Initialize() or to use the auto initialization when loading content.

I've seen static classes initializing without obvious calls to them, so I'd feel more comfortable with not having any library loading happening inside static ctor/fields.

For the same reason, I don't quite like OpenAL.NativeLibrary being initialized statically. I think I'm going to change that too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not change the API by forcing explicit initialization. If we could have a fresh API, I'd definitely want to make it explicit though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just checking if Audio systems can be initialized on game run?

For the same reason, I don't quite like OpenAL.NativeLibrary being initialized statically. I think I'm going to change that too.

Please don't, those are just wrappers. Think of them as DllImport statements :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but they do load the dll if the static ctor kicks in, which we want to avoid to not mess up with third party sound engine.

But if we can't get the sound api to initialize in a non static way for Microphone, there's no point in removing the static initialization from OpenAL anyway because it'll still be around within Microphone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am more suspicious of Microphone than I am of OpenAL. I'd prefer to not add a static constructor to Microphone.

If we'd like to be 100% safe, I'd suggest to move static initialization out of OpenAL func loading and manually load them (to have a perfect control on the "when" and "how").

@ThomasFOG
Copy link
Contributor Author

After giving some thoughts about static initializers, I believe that we are 100% fine with OpenAL statically loading the binary. I assume that people not willing to use OpenAL will remove the dll from their distribution, which doesn't trigger any error if OpenAL tries to load anyway (unless SoundEffect.Initialize() is called, but then it's the expected behavior).

I am however not in favor of having Microphone use a static initializer to initialize the sound API. As of now everything would be fine, but we have no control over what could reference Microphone and possibly trigger its initializer. For safety, I prefer to have the initialization upon accessing the microphones list.

@Jjagg @cra0zy What do you think? If it's ok to you, then it should be good to go.

@Jjagg
Copy link
Contributor

Jjagg commented Jan 14, 2019

That's fine by me.

@ThomasFOG
Copy link
Contributor Author

ThomasFOG commented Feb 7, 2019

@cra0zy Could you share your thoughts about the current status of this PR? It would be cool to move forward.

I've been running on this PR for quite some time now, using FMOD.

@harry-cpp
Copy link
Member

Looks fine to me, sorry for the delay.

@Jjagg
Copy link
Contributor

Jjagg commented Feb 10, 2019

Thanks @mrhelmut! Merging this :D

@Jjagg Jjagg merged commit 72919dc into MonoGame:develop Feb 10, 2019
@ThomasFOG ThomasFOG deleted the optionalSound branch February 20, 2019 07:59
@tomspilman tomspilman added this to the 3.8 Release milestone Aug 1, 2020
@tomspilman tomspilman changed the title Optional sound system Made sound system optional Aug 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[OpenAL] Wrong exception type when binaries are missing
4 participants