-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Conversation
There was a problem hiding this 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.
Thanks! I'm going to refactor the controller instance management, it has been a bit messy for quite too long. |
@Jjagg I think I got things clean up. Let me know how it goes for you. EDIT: forgot Android, brb. |
Ready for review. |
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; } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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();
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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").
After giving some thoughts about static initializers, I believe that we are 100% fine with I am however not in favor of having @Jjagg @cra0zy What do you think? If it's ok to you, then it should be good to go. |
That's fine by me. |
@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. |
Looks fine to me, sorry for the delay. |
Thanks @mrhelmut! Merging this :D |
Hey there,
This PR should make
Microsoft.Xna.Framework.Media
andMicrosoft.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:
DllNotFoundException
orNoAudioHardwareException
)SoundEffect
Song
MediaPlayer
Microphone
orDynamicSoundEffectInstance
are used, OpenAL won't get loaded and the dll can be removed from release packagesSoundEffect.Initialize()
and developers can make it so it is not fatalAs suggested previously, sound engine initialization is now done whenever
SoundEffect
Song
MediaPlayer
Microphone
orDynamicSoundEffectInstance
are constructed.It is however possible to manually initialize the
SoundEffect
API by callingSoundEffect.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.