From 6ce25fe6c3e5f7e1f80aa109b5b4e870931244b2 Mon Sep 17 00:00:00 2001
From: Randy Heit <rheit@zdoom.fake>
Date: Fri, 25 Apr 2008 01:28:06 +0000
Subject: [PATCH] - Fixed: When an instrument's envelope runs out, it does not
 immediately ramp   to zero. Rather, it lets the remainder of the sample
 finish playing.

SVN r938 (trunk)
---
 docs/rh-log.txt                            |   2 +
 src/timidity/instrum.cpp                   |   4 +-
 src/timidity/mix.cpp                       |   5 +-
 tools/updaterevision/updaterevision.vcproj | 158 ++++++++++-----------
 4 files changed, 87 insertions(+), 82 deletions(-)

diff --git a/docs/rh-log.txt b/docs/rh-log.txt
index 360b5d7d5..3f0e66186 100644
--- a/docs/rh-log.txt
+++ b/docs/rh-log.txt
@@ -1,4 +1,6 @@
 April 24, 2008
+- Fixed: When an instrument's envelope runs out, it does not immediately ramp
+  to zero. Rather, it lets the remainder of the sample finish playing.
 - Fixed: When playing a MIDI file with EMIDI track designations to turn a
   track off, any ticks that had only events on the disabled track would cause
   the delay for that track to be thrown away, and the following notes on
diff --git a/src/timidity/instrum.cpp b/src/timidity/instrum.cpp
index 46f310083..338908623 100644
--- a/src/timidity/instrum.cpp
+++ b/src/timidity/instrum.cpp
@@ -31,6 +31,7 @@
 #include "timidity.h"
 #include "m_swap.h"
 #include "files.h"
+#include "templates.h"
 
 namespace Timidity
 {
@@ -417,7 +418,8 @@ fail:
 		for (j = 0; j < 6; j++)
 		{
 			sp->envelope_rate[j] = convert_envelope_rate(song, patch_data.EnvelopeRate[j]);
-			sp->envelope_offset[j] = convert_envelope_offset(patch_data.EnvelopeOffset[j]);
+			// GF1NEW clamps the offsets to the range [5,251], so we do too.
+			sp->envelope_offset[j] = convert_envelope_offset(clamp<BYTE>(patch_data.EnvelopeOffset[j], 5, 251));
 		}
 
 		/* Then read the sample data */
diff --git a/src/timidity/mix.cpp b/src/timidity/mix.cpp
index 8c004612f..cd0443a7a 100644
--- a/src/timidity/mix.cpp
+++ b/src/timidity/mix.cpp
@@ -43,8 +43,9 @@ int recompute_envelope(Voice *v)
 		/* Envelope ran out. */
 		/* play sampled release */
 		v->status &= ~(VOICE_SUSTAINING | VOICE_LPE);
-		v->status |= VOICE_RELEASING | VOICE_STOPPING;
-		return 1;
+		v->status |= VOICE_RELEASING;
+		v->envelope_increment = 0;
+		return 0;
 	}
 
 	if (stage == RELEASE && !(v->status & VOICE_RELEASING) && (v->sample->modes & PATCH_SUSTAIN))
diff --git a/tools/updaterevision/updaterevision.vcproj b/tools/updaterevision/updaterevision.vcproj
index c41849593..f3c1ebdf7 100644
--- a/tools/updaterevision/updaterevision.vcproj
+++ b/tools/updaterevision/updaterevision.vcproj
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
 	ProjectType="Visual C++"
-	Version="8,00"
+	Version="8.00"
 	Name="updaterevision"
 	ProjectGUID="{6077B7D6-349F-4077-B552-3BC302EF5859}"
 	RootNamespace="updaterevision"
@@ -95,6 +95,82 @@
 				Name="VCPostBuildEventTool"
 			/>
 		</Configuration>
+		<Configuration
+			Name="Debug|x64"
+			OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"
+			IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
+			ConfigurationType="1"
+			CharacterSet="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+				TargetEnvironment="3"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				Optimization="0"
+				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+				MinimalRebuild="true"
+				BasicRuntimeChecks="3"
+				RuntimeLibrary="3"
+				UsePrecompiledHeader="0"
+				WarningLevel="3"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="3"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="2"
+				GenerateDebugInformation="true"
+				SubSystem="1"
+				TargetMachine="17"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCWebDeploymentTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
 		<Configuration
 			Name="Release|Win32"
 			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
@@ -172,82 +248,6 @@
 				Name="VCPostBuildEventTool"
 			/>
 		</Configuration>
-		<Configuration
-			Name="Debug|x64"
-			OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"
-			IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
-			ConfigurationType="1"
-			CharacterSet="1"
-			>
-			<Tool
-				Name="VCPreBuildEventTool"
-			/>
-			<Tool
-				Name="VCCustomBuildTool"
-			/>
-			<Tool
-				Name="VCXMLDataGeneratorTool"
-			/>
-			<Tool
-				Name="VCWebServiceProxyGeneratorTool"
-			/>
-			<Tool
-				Name="VCMIDLTool"
-				TargetEnvironment="3"
-			/>
-			<Tool
-				Name="VCCLCompilerTool"
-				Optimization="0"
-				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
-				MinimalRebuild="true"
-				BasicRuntimeChecks="3"
-				RuntimeLibrary="3"
-				UsePrecompiledHeader="0"
-				WarningLevel="3"
-				Detect64BitPortabilityProblems="true"
-				DebugInformationFormat="3"
-			/>
-			<Tool
-				Name="VCManagedResourceCompilerTool"
-			/>
-			<Tool
-				Name="VCResourceCompilerTool"
-			/>
-			<Tool
-				Name="VCPreLinkEventTool"
-			/>
-			<Tool
-				Name="VCLinkerTool"
-				LinkIncremental="2"
-				GenerateDebugInformation="true"
-				SubSystem="1"
-				TargetMachine="17"
-			/>
-			<Tool
-				Name="VCALinkTool"
-			/>
-			<Tool
-				Name="VCManifestTool"
-			/>
-			<Tool
-				Name="VCXDCMakeTool"
-			/>
-			<Tool
-				Name="VCBscMakeTool"
-			/>
-			<Tool
-				Name="VCFxCopTool"
-			/>
-			<Tool
-				Name="VCAppVerifierTool"
-			/>
-			<Tool
-				Name="VCWebDeploymentTool"
-			/>
-			<Tool
-				Name="VCPostBuildEventTool"
-			/>
-		</Configuration>
 		<Configuration
 			Name="Release|x64"
 			OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"
@@ -348,7 +348,7 @@
 				/>
 			</FileConfiguration>
 			<FileConfiguration
-				Name="Release|Win32"
+				Name="Debug|x64"
 				>
 				<Tool
 					Name="VCCLCompilerTool"
@@ -356,7 +356,7 @@
 				/>
 			</FileConfiguration>
 			<FileConfiguration
-				Name="Debug|x64"
+				Name="Release|Win32"
 				>
 				<Tool
 					Name="VCCLCompilerTool"