shithub: pt2-clone

Download patch

ref: 6c56bc97933ef5a50092cff22b0f98033bbf2d30
parent: 94ae360adff69fcc0ad25b169c0fa1342ba21cbb
author: Olav Sørensen <olav.sorensen@live.no>
date: Thu Oct 28 11:36:50 EDT 2021

Fix potential thread issues

Fix potential thread issues between user input thread and audio thread, like when jamming a sample, toggling tuning tone, playing sample in the sample edtior, adjusting sample loop, etc.

--- a/src/pt2_audio.c
+++ b/src/pt2_audio.c
@@ -169,6 +169,10 @@
 
 void mixerUpdateLoops(void) // updates Paula loop (+ scopes)
 {
+	const bool audioWasntLocked = !audio.locked;
+	if (audioWasntLocked)
+		lockAudio();
+
 	for (int32_t i = 0; i < AMIGA_VOICES; i++)
 	{
 		const moduleChannel_t *ch = &song->channels[i];
@@ -180,6 +184,9 @@
 			paulaSetLength(i, s->loopLength >> 1);
 		}
 	}
+
+	if (audioWasntLocked)
+		unlockAudio();
 }
 
 void mixerKillVoice(int32_t ch)
--- a/src/pt2_edit.c
+++ b/src/pt2_edit.c
@@ -917,6 +917,8 @@
 	if (s->length <= 1)
 		return;
 
+	lockAudio();
+
 	song->channels[ch].n_samplenum = editor.currSample; // needed for sample playback/sampling line
 
 	const int8_t *n_start = &song->sampleData[s->offset];
@@ -937,6 +939,8 @@
 	// these take effect after the current DMA cycle is done
 	paulaSetData(ch, NULL);
 	paulaSetLength(ch, 1);
+
+	unlockAudio();
 }
 
 void jamAndPlaceSample(SDL_Scancode scancode, bool normalMode)
@@ -970,6 +974,8 @@
 		// don't play sample if we quantized to another row (will be played in modplayer instead)
 		if (editor.currMode != MODE_RECORD || !editor.didQuantize)
 		{
+			lockAudio();
+
 			chn->n_samplenum = editor.currSample;
 			chn->n_volume = s->volume;
 			chn->n_period = tempPeriod;
@@ -994,6 +1000,8 @@
 			// these take effect after the current DMA cycle is done
 			paulaSetData(ch, chn->n_loopstart);
 			paulaSetLength(ch, chn->n_replen);
+
+			unlockAudio();
 		}
 
 		// normalMode = normal keys, or else keypad keys (in jam mode)
--- a/src/pt2_header.h
+++ b/src/pt2_header.h
@@ -14,7 +14,7 @@
 #include "pt2_unicode.h"
 #include "pt2_palette.h"
 
-#define PROG_VER_STR "1.36"
+#define PROG_VER_STR "1.37"
 
 #ifdef _WIN32
 #define DIR_DELIMITER '\\'
--- a/src/pt2_sampler.c
+++ b/src/pt2_sampler.c
@@ -1262,6 +1262,8 @@
 		if (editor.tuningNote > 35)
 			editor.tuningNote = 35;
 
+		lockAudio();
+
 		song->channels[ch].n_volume = 64; // we need this for the scopes
 
 		paulaSetPeriod(ch, periodTable[editor.tuningNote]);
@@ -1269,6 +1271,8 @@
 		paulaSetData(ch, tuneToneData);
 		paulaSetLength(ch, sizeof (tuneToneData) / 2);
 		paulaStartDMA(ch);
+
+		unlockAudio();
 	}
 	else
 	{
@@ -1770,6 +1774,8 @@
 	assert(chn < AMIGA_VOICES);
 	assert(editor.currPlayNote <= 35);
 
+	lockAudio();
+
 	s = &song->samples[editor.currSample];
 	ch = &song->channels[chn];
 
@@ -1818,6 +1824,8 @@
 	}
 
 	updateSpectrumAnalyzer(ch->n_volume, ch->n_period);
+
+	unlockAudio();
 }
 
 void samplerPlayWaveform(void)