shithub: pt2-clone

Download patch

ref: d6b1877bed615652629ec7e0409d844c0821b6e8
parent: a342c73ea4e2c18a6c7c3af73a48b8875d7c4056
author: Olav Sørensen <olav.sorensen@live.no>
date: Wed Nov 18 09:39:58 EST 2020

Fix BPM duration issue in MOD2WAV

--- a/src/pt2_mod2wav.c
+++ b/src/pt2_mod2wav.c
@@ -15,7 +15,7 @@
 #include "pt2_mod2wav.h"
 #include "pt2_structs.h"
 
-#define TICKS_PER_RENDER_CHUNK 32
+#define TICKS_PER_RENDER_CHUNK 64
 
 void storeTempVariables(void); // pt_modplayer.c
 bool intMusic(void); // pt_modplayer.c
@@ -25,14 +25,6 @@
 
 static void calcMod2WavTotalRows(void);
 
-static void renderSamples(int32_t samplesPerTick, int16_t *outStream)
-{
-	if (!intMusic())
-		wavRenderingDone = true;
-
-	outputAudio(outStream, samplesPerTick);
-}
-
 static int32_t SDLCALL mod2WavThreadFunc(void *ptr)
 {
 	wavHeader_t wavHeader;
@@ -45,11 +37,10 @@
 
 	wavRenderingDone = false;
 
-	uint8_t loopCounter = 8;
-	uint32_t totalSampleCounter = 0;
+	uint32_t sampleCounter = 0;
+	uint8_t tickCounter = 8;
+	double dTickSampleCounter = 0.0;
 
-	double dTickSamples = audio.dSamplesPerTick;
-
 	bool renderDone = false;
 	while (!renderDone)
 	{
@@ -65,20 +56,30 @@
 				break;
 			}
 
-			int32_t tickSamples = (int32_t)dTickSamples;
-			renderSamples(tickSamples, ptr16);
-			
-			dTickSamples -= tickSamples; // keep fractional part
-			dTickSamples += audio.dSamplesPerTick;
+			if (dTickSampleCounter <= 0.0)
+			{
+				// new replayer tick
 
-			tickSamples *= 2; // stereo
-			samplesInChunk += tickSamples;
-			totalSampleCounter += tickSamples;
-			ptr16 += tickSamples;
+				if (!intMusic())
+					wavRenderingDone = true;
 
-			if (++loopCounter >= 8)
+				dTickSampleCounter += audio.dSamplesPerTick;
+			}
+
+			int32_t remainingTick = (int32_t)ceil(dTickSampleCounter);
+
+			outputAudio(ptr16, remainingTick);
+			dTickSampleCounter -= remainingTick;
+
+			remainingTick *= 2; // stereo
+			samplesInChunk += remainingTick;
+			sampleCounter += remainingTick;
+
+			ptr16 += remainingTick;
+
+			if (++tickCounter >= 4)
 			{
-				loopCounter = 0;
+				tickCounter = 0;
 				ui.updateMod2WavDialog = true;
 			}
 		}
@@ -90,7 +91,7 @@
 
 	free(mod2WavBuffer);
 
-	if (totalSampleCounter & 1)
+	if (sampleCounter & 1)
 		fputc(0, f); // pad align byte
 
 	uint32_t totalRiffChunkLen = (uint32_t)ftell(f) - 8;
@@ -110,7 +111,7 @@
 	wavHeader.byteRate = (wavHeader.sampleRate * wavHeader.numChannels * wavHeader.bitsPerSample) / 8;
 	wavHeader.blockAlign = (wavHeader.numChannels * wavHeader.bitsPerSample) / 8;
 	wavHeader.subchunk2ID = 0x61746164; // "data"
-	wavHeader.subchunk2Size = totalSampleCounter * sizeof (int16_t);
+	wavHeader.subchunk2Size = sampleCounter * sizeof (int16_t);
 
 	// write main header
 	fwrite(&wavHeader, sizeof (wavHeader_t), 1, f);