shithub: ft²

Download patch

ref: e8a29beb7a6ba80b8a6596010bee8cdfd8a24421
parent: b8597d3238c967105640024555278aec78a5eae1
author: Olav Sørensen <[email protected]>
date: Fri Jun 19 15:35:35 EDT 2020

Reset sample counter when pressing Play Song (reduces tiny delay)

Also some code cleanup (more sensible variable names).

--- a/src/ft2_audio.c
+++ b/src/ft2_audio.c
@@ -20,7 +20,7 @@
 
 static int8_t pmpCountDiv, pmpChannels = 2;
 static uint16_t smpBuffSize;
-static int32_t masterVol, oldAudioFreq, pmpLeft, randSeed = INITIAL_DITHER_SEED;
+static int32_t masterVol, oldAudioFreq, randSeed = INITIAL_DITHER_SEED;
 static int32_t prngStateL, prngStateR;
 static uint32_t tickTimeLen, tickTimeLenFrac;
 static float fAudioAmpMul;
@@ -161,7 +161,7 @@
 	if (bpm > MAX_BPM)
 		return;
 
-	audio.speedVal = audio.speedValTab[bpm];
+	audio.samplesPerTick = audio.speedValTab[bpm];
 
 	// get tick time length for audio/video sync timestamp
 	const uint64_t tickTimeLen64 = audio.tickTimeLengthTab[bpm];
@@ -261,7 +261,7 @@
 			}
 			else
 			{
-				v->SVolIPLen = audio.speedVal;
+				v->SVolIPLen = audio.samplesPerTick;
 				v->SLVolIP = ((int64_t)destVolL * audio.rampSpeedValMul) >> 32;
 				v->SRVolIP = ((int64_t)destVolR * audio.rampSpeedValMul) >> 32;
 			}
@@ -584,8 +584,8 @@
 	voice_t *v, *r;
 
 	assert(audio.speedVal <= MAX_WAV_RENDER_SAMPLES_PER_TICK);
-	memset(audio.mixBufferL, 0, audio.speedVal * sizeof (int32_t));
-	memset(audio.mixBufferR, 0, audio.speedVal * sizeof (int32_t));
+	memset(audio.mixBufferL, 0, audio.samplesPerTick * sizeof (int32_t));
+	memset(audio.mixBufferR, 0, audio.samplesPerTick * sizeof (int32_t));
 
 	// mix channels
 	v = voice; // normal voices
@@ -594,8 +594,8 @@
 	for (int32_t i = 0; i < song.antChn; i++, v++, r++)
 	{
 		// call the mixing routine currently set for the voice
-		if (v->mixRoutine != NULL) v->mixRoutine(v, audio.speedVal); // mix normal voice
-		if (r->mixRoutine != NULL) r->mixRoutine(r, audio.speedVal); // mix volume ramp voice
+		if (v->mixRoutine != NULL) v->mixRoutine(v, audio.samplesPerTick); // mix normal voice
+		if (r->mixRoutine != NULL) r->mixRoutine(r, audio.samplesPerTick); // mix volume ramp voice
 	}
 
 	// normalize mix buffer and send to audio stream
@@ -602,16 +602,16 @@
 	if (bitDepth == 16)
 	{
 		if (config.specialFlags2 & DITHERED_AUDIO)
-			sendSamples16BitDitherStereo(stream, audio.speedVal, 2);
+			sendSamples16BitDitherStereo(stream, audio.samplesPerTick, 2);
 		else
-			sendSamples16BitStereo(stream, audio.speedVal, 2);
+			sendSamples16BitStereo(stream, audio.samplesPerTick, 2);
 	}
 	else
 	{
-		sendSamples24BitStereo(stream, audio.speedVal, 2);
+		sendSamples24BitStereo(stream, audio.samplesPerTick, 2);
 	}
 
-	return audio.speedVal;
+	return audio.samplesPerTick;
 }
 
 int32_t pattQueueReadSize(void)
@@ -930,18 +930,16 @@
 
 static void SDLCALL audioCallback(void *userdata, Uint8 *stream, int len)
 {
-	int32_t a, b;
-
 	assert(len < 65536); // limitation in mixer
 	assert(pmpCountDiv > 0);
 
-	a = len / pmpCountDiv;
-	if (a <= 0)
+	int32_t samplesLeft = len / pmpCountDiv;
+	if (samplesLeft <= 0)
 		return;
 
-	while (a > 0)
+	while (samplesLeft > 0)
 	{
-		if (pmpLeft == 0)
+		if (audio.tickSampleCounter == 0)
 		{
 			// new replayer tick
 
@@ -954,20 +952,20 @@
 			mix_UpdateChannelVolPanFrq();
 			fillVisualsSyncBuffer();
 
-			pmpLeft = audio.speedVal;
+			audio.tickSampleCounter = audio.samplesPerTick;
 
 			replayerBusy = false;
 		}
 
-		b = a;
-		if (b > pmpLeft)
-			b = pmpLeft;
+		int32_t samplesToMix = samplesLeft;
+		if (samplesToMix > audio.tickSampleCounter)
+			samplesToMix = audio.tickSampleCounter;
 
-		mixAudio(stream, b, pmpChannels);
-		stream += b * pmpCountDiv;
+		mixAudio(stream, samplesToMix, pmpChannels);
+		stream += samplesToMix * pmpCountDiv;
 
-		a -= b;
-		pmpLeft -= b;
+		samplesLeft -= samplesToMix;
+		audio.tickSampleCounter -= samplesToMix;
 	}
 
 	(void)userdata;
@@ -1225,7 +1223,7 @@
 
 	stopAllScopes();
 
-	pmpLeft = 0; // reset sample counter
+	audio.tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick
 
 	calcReplayRate(audio.freq);
 
--- a/src/ft2_audio.h
+++ b/src/ft2_audio.h
@@ -51,8 +51,9 @@
 	int32_t inputDeviceNum, outputDeviceNum, lastWorkingAudioFreq, lastWorkingAudioBits;
 	int32_t quickVolSizeVal, *mixBufferL, *mixBufferR, *mixBufferLUnaligned, *mixBufferRUnaligned;
 	int32_t rampQuickVolMul, rampSpeedValMul, speedValTab[MAX_BPM+1], rampSpeedValMulTab[MAX_BPM+1];
+	int32_t tickSampleCounter;
 	uint32_t freq;
-	uint32_t audLatencyPerfValInt, audLatencyPerfValFrac, speedVal, musicTimeSpeedVal;
+	uint32_t audLatencyPerfValInt, audLatencyPerfValFrac, samplesPerTick, musicTimeSpeedVal;
 	uint64_t tickTime64, tickTime64Frac, tickTimeLengthTab[MAX_BPM+1];
 	double dAudioLatencyMs;
 	SDL_AudioDeviceID dev;
--- a/src/ft2_replayer.c
+++ b/src/ft2_replayer.c
@@ -2780,6 +2780,8 @@
 	if (song.tempo == 0)
 		song.tempo = song.initialTempo;
 
+	audio.tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick
+
 	unlockMixerCallback();
 
 	ui.updatePosSections = true;
--- a/vs2019_project/ft2-clone/ft2-clone.vcxproj.filters
+++ b/vs2019_project/ft2-clone/ft2-clone.vcxproj.filters
@@ -1,42 +1,49 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup>
+    <ClCompile Include="..\..\src\ft2_about.c" />
     <ClCompile Include="..\..\src\ft2_audio.c" />
+    <ClCompile Include="..\..\src\ft2_audioselector.c" />
+    <ClCompile Include="..\..\src\ft2_bmp.c" />
+    <ClCompile Include="..\..\src\ft2_checkboxes.c" />
     <ClCompile Include="..\..\src\ft2_config.c" />
+    <ClCompile Include="..\..\src\ft2_diskop.c" />
     <ClCompile Include="..\..\src\ft2_edit.c" />
+    <ClCompile Include="..\..\src\ft2_events.c" />
     <ClCompile Include="..\..\src\ft2_gui.c" />
     <ClCompile Include="..\..\src\ft2_help.c" />
     <ClCompile Include="..\..\src\ft2_inst_ed.c" />
+    <ClCompile Include="..\..\src\ft2_intrp_table.c" />
     <ClCompile Include="..\..\src\ft2_keyboard.c" />
     <ClCompile Include="..\..\src\ft2_main.c" />
+    <ClCompile Include="..\..\src\ft2_midi.c" />
+    <ClCompile Include="..\..\src\ft2_mix.c" />
+    <ClCompile Include="..\..\src\ft2_module_loader.c" />
+    <ClCompile Include="..\..\src\ft2_module_saver.c" />
     <ClCompile Include="..\..\src\ft2_mouse.c" />
     <ClCompile Include="..\..\src\ft2_nibbles.c" />
+    <ClCompile Include="..\..\src\ft2_palette.c" />
+    <ClCompile Include="..\..\src\ft2_pattern_draw.c" />
+    <ClCompile Include="..\..\src\ft2_pattern_ed.c" />
+    <ClCompile Include="..\..\src\ft2_pushbuttons.c" />
+    <ClCompile Include="..\..\src\ft2_radiobuttons.c" />
     <ClCompile Include="..\..\src\ft2_replayer.c" />
-    <ClCompile Include="..\..\src\ft2_scopes.c" />
-    <ClCompile Include="..\..\src\ft2_video.c" />
     <ClCompile Include="..\..\src\ft2_sample_ed.c" />
-    <ClCompile Include="..\..\src\ft2_pattern_ed.c" />
+    <ClCompile Include="..\..\src\ft2_sample_ed_features.c" />
     <ClCompile Include="..\..\src\ft2_sample_loader.c" />
     <ClCompile Include="..\..\src\ft2_sample_saver.c" />
-    <ClCompile Include="..\..\src\ft2_diskop.c" />
-    <ClCompile Include="..\..\src\ft2_unicode.c" />
-    <ClCompile Include="..\..\src\ft2_midi.c" />
-    <ClCompile Include="..\..\src\ft2_wav_renderer.c" />
-    <ClCompile Include="..\..\src\ft2_trim.c" />
-    <ClCompile Include="..\..\src\ft2_checkboxes.c" />
-    <ClCompile Include="..\..\src\ft2_pushbuttons.c" />
-    <ClCompile Include="..\..\src\ft2_radiobuttons.c" />
+    <ClCompile Include="..\..\src\ft2_sampling.c" />
+    <ClCompile Include="..\..\src\ft2_scopedraw.c" />
+    <ClCompile Include="..\..\src\ft2_scopes.c" />
     <ClCompile Include="..\..\src\ft2_scrollbars.c" />
-    <ClCompile Include="..\..\src\ft2_textboxes.c" />
+    <ClCompile Include="..\..\src\ft2_structs.c" />
     <ClCompile Include="..\..\src\ft2_sysreqs.c" />
-    <ClCompile Include="..\..\src\ft2_module_loader.c" />
-    <ClCompile Include="..\..\src\ft2_module_saver.c" />
-    <ClCompile Include="..\..\src\ft2_mix.c" />
-    <ClCompile Include="..\..\src\ft2_sampling.c" />
-    <ClCompile Include="..\..\src\ft2_audioselector.c" />
-    <ClCompile Include="..\..\src\ft2_events.c" />
-    <ClCompile Include="..\..\src\ft2_pattern_draw.c" />
-    <ClCompile Include="..\..\src\ft2_sample_ed_features.c" />
+    <ClCompile Include="..\..\src\ft2_tables.c" />
+    <ClCompile Include="..\..\src\ft2_textboxes.c" />
+    <ClCompile Include="..\..\src\ft2_trim.c" />
+    <ClCompile Include="..\..\src\ft2_unicode.c" />
+    <ClCompile Include="..\..\src\ft2_video.c" />
+    <ClCompile Include="..\..\src\ft2_wav_renderer.c" />
     <ClCompile Include="..\..\src\rtmidi\RtMidi.cpp">
       <Filter>rtmidi</Filter>
     </ClCompile>
@@ -43,17 +50,15 @@
     <ClCompile Include="..\..\src\rtmidi\rtmidi_c.cpp">
       <Filter>rtmidi</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\ft2_palette.c" />
-    <ClCompile Include="..\..\src\ft2_scopedraw.c" />
-    <ClCompile Include="..\..\src\ft2_tables.c" />
-    <ClCompile Include="..\..\src\ft2_intrp_table.c" />
-    <ClCompile Include="..\..\src\ft2_bmp.c" />
-    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_nibbles.c">
+    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_fonts.c">
       <Filter>graphics</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_midi.c">
+    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_gui.c">
       <Filter>graphics</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_instr.c">
+      <Filter>graphics</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\src\gfxdata\ft2_bmp_logo.c">
       <Filter>graphics</Filter>
     </ClCompile>
@@ -60,164 +65,159 @@
     <ClCompile Include="..\..\src\gfxdata\ft2_bmp_looppins.c">
       <Filter>graphics</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_mouse.c">
+    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_midi.c">
       <Filter>graphics</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_fonts.c">
+    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_mouse.c">
       <Filter>graphics</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_instr.c">
+    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_nibbles.c">
       <Filter>graphics</Filter>
     </ClCompile>
     <ClCompile Include="..\..\src\gfxdata\ft2_bmp_scopes.c">
       <Filter>graphics</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\gfxdata\ft2_bmp_gui.c">
-      <Filter>graphics</Filter>
-    </ClCompile>
-    <ClCompile Include="..\..\src\ft2_structs.c" />
-    <ClCompile Include="..\..\src\ft2_about.c" />
   </ItemGroup>
   <ItemGroup>
-    <ClInclude Include="..\..\src\ft2_audio.h">
+    <ClInclude Include="..\..\src\rtmidi\RtMidi.h">
+      <Filter>rtmidi</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\src\rtmidi\rtmidi_c.h">
+      <Filter>rtmidi</Filter>
+    </ClInclude>
+    <ClInclude Include="..\..\src\ft2_about.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_config.h">
+    <ClInclude Include="..\..\src\ft2_audio.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_edit.h">
+    <ClInclude Include="..\..\src\ft2_audioselector.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_gui.h">
+    <ClInclude Include="..\..\src\ft2_bmp.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_header.h">
+    <ClInclude Include="..\..\src\ft2_checkboxes.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_help.h">
+    <ClInclude Include="..\..\src\ft2_config.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_inst_ed.h">
+    <ClInclude Include="..\..\src\ft2_diskop.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_keyboard.h">
+    <ClInclude Include="..\..\src\ft2_edit.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_mouse.h">
+    <ClInclude Include="..\..\src\ft2_events.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_nibbles.h">
+    <ClInclude Include="..\..\src\ft2_gfxdata.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_replayer.h">
+    <ClInclude Include="..\..\src\ft2_gui.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_scopes.h">
+    <ClInclude Include="..\..\src\ft2_header.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_sample_ed.h">
+    <ClInclude Include="..\..\src\ft2_help.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_pattern_ed.h">
+    <ClInclude Include="..\..\src\ft2_inst_ed.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_sample_loader.h">
+    <ClInclude Include="..\..\src\ft2_intrp_table.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_sample_saver.h">
+    <ClInclude Include="..\..\src\ft2_keyboard.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_video.h">
+    <ClInclude Include="..\..\src\ft2_midi.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_diskop.h">
+    <ClInclude Include="..\..\src\ft2_mix.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_unicode.h">
+    <ClInclude Include="..\..\src\ft2_mix_macros.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_midi.h">
+    <ClInclude Include="..\..\src\ft2_module_loader.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_wav_renderer.h">
+    <ClInclude Include="..\..\src\ft2_module_saver.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_about.h">
+    <ClInclude Include="..\..\src\ft2_mouse.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_trim.h">
+    <ClInclude Include="..\..\src\ft2_nibbles.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_checkboxes.h">
+    <ClInclude Include="..\..\src\ft2_palette.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_pushbuttons.h">
+    <ClInclude Include="..\..\src\ft2_pattern_draw.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_radiobuttons.h">
+    <ClInclude Include="..\..\src\ft2_pattern_ed.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_scrollbars.h">
+    <ClInclude Include="..\..\src\ft2_pushbuttons.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_textboxes.h">
+    <ClInclude Include="..\..\src\ft2_radiobuttons.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_sysreqs.h">
+    <ClInclude Include="..\..\src\ft2_replayer.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_module_loader.h">
+    <ClInclude Include="..\..\src\ft2_sample_ed.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_module_saver.h">
+    <ClInclude Include="..\..\src\ft2_sample_ed_features.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_gfxdata.h">
+    <ClInclude Include="..\..\src\ft2_sample_loader.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_mix.h">
+    <ClInclude Include="..\..\src\ft2_sample_saver.h">
       <Filter>headers</Filter>
     </ClInclude>
     <ClInclude Include="..\..\src\ft2_sampling.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_audioselector.h">
+    <ClInclude Include="..\..\src\ft2_scopedraw.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_mix_macros.h">
+    <ClInclude Include="..\..\src\ft2_scopes.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_events.h">
+    <ClInclude Include="..\..\src\ft2_scrollbars.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_pattern_draw.h">
+    <ClInclude Include="..\..\src\ft2_structs.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_palette.h">
+    <ClInclude Include="..\..\src\ft2_sysreqs.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_sample_ed_features.h">
+    <ClInclude Include="..\..\src\ft2_tables.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\rtmidi\RtMidi.h">
-      <Filter>rtmidi</Filter>
-    </ClInclude>
-    <ClInclude Include="..\..\src\rtmidi\rtmidi_c.h">
-      <Filter>rtmidi</Filter>
-    </ClInclude>
-    <ClInclude Include="..\..\src\ft2_scopedraw.h">
+    <ClInclude Include="..\..\src\ft2_textboxes.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_tables.h">
+    <ClInclude Include="..\..\src\ft2_trim.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_intrp_table.h">
+    <ClInclude Include="..\..\src\ft2_unicode.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_bmp.h">
+    <ClInclude Include="..\..\src\ft2_video.h">
       <Filter>headers</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\ft2_structs.h">
+    <ClInclude Include="..\..\src\ft2_wav_renderer.h">
       <Filter>headers</Filter>
     </ClInclude>
   </ItemGroup>