From ad50fa4d9e9278aaa45df8515d50e02f014d73eb Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Fri, 15 Mar 2019 10:02:50 -0400
Subject: [PATCH] Fix MIDI crash bug when changing music between levels

This happened when the old music was ERZ2 native midi, then warping to any other level.
---
 src/p_setup.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/p_setup.c b/src/p_setup.c
index 3d375e215c..dc93fd2fee 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -2725,19 +2725,20 @@ boolean P_SetupLevel(boolean skipprecip)
 	S_StopSounds();
 	S_ClearSfx();
 
+	// Fade out music here. Deduct 2 tics so the fade volume actually reaches 0.
+	// But don't halt the music! S_Start will take care of that. This dodges a MIDI crash bug.
+	if (cv_resetmusic.value ||
+		strnicmp(S_MusicName(),
+			(mapmusflags & MUSIC_RELOADRESET) ? mapheaderinfo[gamemap-1]->musname : mapmusname, 7))
+		S_FadeMusic(0, FixedMul(
+			FixedDiv((F_GetWipeLength(wipedefs[wipe_level_toblack])-2)*NEWTICRATERATIO, NEWTICRATE), MUSICRATE));
+
 	if (!titlemapinaction)
 	{
 		// Let's fade to black here
 		// But only if we didn't do the special stage wipe
 		if (rendermode != render_none && !ranspecialwipe)
 		{
-			// Fade out music here. Deduct 2 tics so the fade volume actually reaches 0
-			if (cv_resetmusic.value ||
-				strnicmp(S_MusicName(),
-					(mapmusflags & MUSIC_RELOADRESET) ? mapheaderinfo[gamemap-1]->musname : mapmusname, 7))
-				S_FadeOutStopMusic(FixedMul(
-					FixedDiv((F_GetWipeLength(wipedefs[wipe_level_toblack])-2)*NEWTICRATERATIO, NEWTICRATE), MUSICRATE));
-
 			F_WipeStartScreen();
 			V_DrawFill(0, 0, BASEVIDWIDTH, BASEVIDHEIGHT, 31);
 
@@ -2745,10 +2746,6 @@ boolean P_SetupLevel(boolean skipprecip)
 			F_RunWipe(wipedefs[wipe_level_toblack], false);
 		}
 
-		// As oddly named as this is, this handles music only.
-		// We should be fine starting it here.
-		S_Start();
-
 		if (ranspecialwipe == 2)
 		{
 			pausedelay = -3; // preticker plus one
@@ -2770,6 +2767,10 @@ boolean P_SetupLevel(boolean skipprecip)
 		}
 	}
 
+	// As oddly named as this is, this handles music only.
+	// We should be fine starting it here.
+	S_Start();
+
 	levelfadecol = (ranspecialwipe) ? 0 : 31;
 
 	// Close text prompt before freeing the old level
-- 
GitLab