godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.15k stars 21.19k forks source link

Cracking noise when closing the game by stopping the project from the editor #81237

Open starry-abyss opened 1 year ago

starry-abyss commented 1 year ago

Godot version

3.5.2

System information

macOS Ventura 13.5.1, Macbook Pro M1

Issue description

I was in the process of developing a jam game, and it had no sounds at all. I ran the game from the editor. Sometimes when I closed the game (by hitting stop in the editor), I heard a short cracking noise, similar to if an audio file had a final sample far from zero. But the problem is there were no audio files in the game at that moment.

Steps to reproduce

This issue happens only from time to time, I guess some garbage values get into the audiobuffer.

Minimal reproduction project

Sorry if it's too difficult to diagnose...

Calinou commented 1 year ago

Which kind of audio interface are you using (speakers/headphones, analog/wired USB/wireless USB/Bluetooth)?

Can you record the system audio output using a tool like Audacity or OBS while this occurs?

Also, can you reproduce this if you have another application playing audio at the same time? Some wireless headsets will "pop" after a silence as they automatically suspend to save battery.

I heard a short cracking noise, similar to if an audio file had a final sample far from zero. But the problem is there were no audio files in the game at that moment.

Godot starts an audio device even if no audio is playing, unless you set the audio driver to Dummy (case-sensitive) in the Project Settings. https://github.com/godotengine/godot/pull/63458 allows Godot to mute the AudioServer when a long enough silence occurs.

starry-abyss commented 1 year ago

The jam is over, I might try recording something anyway, but the issue occurs seldom. It's just the laptop speakers, nothing special.

About another application, I don't remember whether I had some music in the background, I think not, but this other application would make the issue not happen, right?

Calinou commented 1 year ago

but this other application would make the issue not happen, right?

Yes, because your audio device won't go to sleep just after Godot quits (since another application will still be playing audio).

starry-abyss commented 1 year ago

A little update:

I'm working on a project with Dummy audio driver and FMOD (replaces the audio system entirely). It uses Godot 4.2 beta4 currently. Still hearing the cracks from time to time when closing the game. However, there is music playing in the game via FMOD. Unlike the other linked issue, I don't use external editors for code. But it's probable that the internal GDScript editor is open when testing the game.

starry-abyss commented 1 year ago

I believe this is related to the fact I use the Stop Running Project button to close the game.

[FMOD] Shut down Runtime System

When I close by clicking the cross button in the window's corner, I see this message in the log (and I believe cracks don't happen then). But there is no such a message when closing the game from the editor.

Maybe Godot force-terminates the project and instead it could first try to send a close request?

UPDATE: actually this message in the log is seldom written :/ However, after running the project and closing it a few times using the two methods, I'm quite confident the Stop Running Project way is the cause of the cracks!

Calinou commented 1 year ago

I'm quite confident the Stop Running Project way is the cause of the cracks!

Stopping the running project makes the editor kill the project process, with no way for the process to react. Some audio cracking is expected when this happens as there's no way to smoothly fade out the audio before the project exits.

This is the same reason why Movie Maker mode creates invalid AVI files if you stop the running project via the editor instead of closing it by pressing the X button at the top of the window.

starry-abyss commented 1 year ago

Alright, here is one more detail: I also get this unpleasant crack when exiting by get_tree().quit()

To fix this I'm also now shutting down FMOD manually like this:

FMODStudioModule.shutdown()
get_tree().quit()

It's for this extension: https://github.com/alessandrofama/fmod-for-godot

After this change I only get a subtle click on exit from time to time, just like when quitting by the cross-click. UPDATE: there is no subtle click if there is no music, so I asked sound designers to add music fade out.

And this is from the code of the extension itself:

func _notification(what: int) -> void:
    if what == NOTIFICATION_WM_CLOSE_REQUEST:
        if get_child(0) && debug_scene:
            debug_scene.free()
        FMODStudioModule.shutdown()
passivestar commented 8 months ago

I have this too

Godot v4.2.1.stable - macOS 14.2.1

When no audio is playing I don't have this issue

When some audio is playing and I stop the game with get_tree().quit() I can hear a quiet pop around 20% of the times, and it's around the same volume as the sound that was playing. I would describe this as acceptable

However if I stop the game with Stop Running Project there's a very loud pop every time I stop the game. It's much louder than the sound that was playing, I'd say 2-3 times as loud and it's quite annoying

Using macbook speakers

No pop sounds in an OBS recording, only IRL 🙂

Calinou commented 8 months ago

However if I stop the game with Stop Running Project there's a very loud pop every time I stop the game. It's much louder than the sound that was playing, I'd say 2-3 times as loud and it's quite annoying

This is because Stop Running Project kills the process with OS.kill() instead of closing it gracefully, so there's no way for Godot to handle it. See also https://github.com/godotengine/godot/issues/85840.

passivestar commented 8 months ago

ok I'll stick to get_tree().quit() then

starry-abyss commented 8 months ago

There is no way for Godot game to handle it, but there should definitely be a way for Godot editor to request game quit properly and not instantly, I guess? Like sending this NOTIFICATION_WM_CLOSE_REQUEST, whatever it means in macOS case.

Also from what I remember from Linux command line, kill can send a signal of choice, with signal 9 meaning force-kill and other signals meaning other things.

passivestar commented 8 months ago

There is no way for Godot game to handle it, but there should definitely be a way for Godot editor to request game quit properly and not instantly, I guess? Like sending this NOTIFICATION_WM_CLOSE_REQUEST, whatever it means in macOS case.

but you probably don't want that? what if it's unresponsive? I guess you can use OS force quit in that case, but I'm not sure whether it's better or not 🤔

starry-abyss commented 8 months ago

If it's unresponsive for N seconds, then force-kill?

Or maybe send quit message to the debug build in the same way live reloaded assets are sent.