shithub: cstory

Download patch

ref: 788697dc5d7b4188fd6cf78b339b2165b6fec592
parent: e599d02fb144395e2fabde32270c55fa3c7a2600
author: Clownacy <[email protected]>
date: Wed Jan 29 18:18:47 EST 2020

Fix a possible hang in the audio mixer

It was likely a race condition between the game thread writing to
'organya_timer', and the audio thread reading it.

...I really need to rethink the API for this Organya-synchronisation
thing.

--- a/src/Backends/Audio.h
+++ b/src/Backends/Audio.h
@@ -22,3 +22,5 @@
 void AudioBackend_SetSoundFrequency(AudioBackend_Sound *sound, unsigned int frequency);
 void AudioBackend_SetSoundVolume(AudioBackend_Sound *sound, long volume);
 void AudioBackend_SetSoundPan(AudioBackend_Sound *sound, long pan);
+
+void AudioBackend_SetOrganyaTimer(unsigned short timer);
--- a/src/Backends/Audio/SDL2.cpp
+++ b/src/Backends/Audio/SDL2.cpp
@@ -44,6 +44,8 @@
 
 static unsigned long output_frequency;
 
+static unsigned short organya_timer;
+
 static double MillibelToScale(long volume)
 {
 	// Volume is in hundredths of decibels, from 0 to -10000
@@ -348,6 +350,15 @@
 	SDL_LockAudioDevice(device_id);
 
 	SetSoundPan(sound, pan);
+
+	SDL_UnlockAudioDevice(device_id);
+}
+
+void AudioBackend_SetOrganyaTimer(unsigned short timer)
+{
+	SDL_LockAudioDevice(device_id);
+
+	organya_timer = timer;
 
 	SDL_UnlockAudioDevice(device_id);
 }
--- a/src/Organya.cpp
+++ b/src/Organya.cpp
@@ -95,8 +95,6 @@
 	BOOL InitMusicData(const char *path);
 } ORGDATA;
 
-unsigned short organya_timer;
-
 AudioBackend_Sound *lpORGANBUFFER[8][8][2] = {NULL};
 
 /////////////////////////////////////////////
@@ -827,7 +825,7 @@
 	if (!audio_backend_initialised)
 		return;
 
-	organya_timer = org_data.info.wait;
+	AudioBackend_SetOrganyaTimer(org_data.info.wait);
 }
 
 BOOL ChangeOrganyaVolume(signed int volume)
@@ -847,7 +845,7 @@
 	if (!audio_backend_initialised)
 		return;
 
-	organya_timer = 0;
+	AudioBackend_SetOrganyaTimer(0);
 
 	// Stop notes
 	for (int i = 0; i < MAXMELODY; i++)
@@ -870,7 +868,7 @@
 	if (!audio_backend_initialised)
 		return;
 
-	organya_timer = 0;
+	AudioBackend_SetOrganyaTimer(0);
 
 	// Release everything related to org
 	org_data.ReleaseNote();
--- a/src/Organya.h
+++ b/src/Organya.h
@@ -6,8 +6,6 @@
 #define MAXMELODY 8
 #define MAXDRAM 8
 
-extern unsigned short organya_timer;
-
 extern BOOL g_mute[MAXTRACK];	// Used by the debug Mute menu
 
 BOOL MakeOrganyaWave(signed char track, signed char wave_no, signed char pipi);