summaryrefslogtreecommitdiff
path: root/opl/opl_timer.c
diff options
context:
space:
mode:
authorSimon Howard2014-05-11 00:14:04 -0400
committerSimon Howard2014-05-11 00:14:04 -0400
commit72ce4242077f23b7de876b977397856bd072a6b1 (patch)
tree1002b8eb4f15d16564a1acadfcd04a264cde558d /opl/opl_timer.c
parent495694da29ff736fba2fdc696553ee7197247174 (diff)
downloadchocolate-doom-72ce4242077f23b7de876b977397856bd072a6b1.tar.gz
chocolate-doom-72ce4242077f23b7de876b977397856bd072a6b1.tar.bz2
chocolate-doom-72ce4242077f23b7de876b977397856bd072a6b1.zip
opl: Change library to use us instead of ms.
Multi-track MIDI files are played back using separate callback chains for each track, and this introduces the possibility of one track becoming out of sync with the others. This was noticeable in WADs that use multi-track MIDIs, such as Alien Vendetta. Increase the timing resolution to microsecond precision to fix this.
Diffstat (limited to 'opl/opl_timer.c')
-rw-r--r--opl/opl_timer.c30
1 files changed, 15 insertions, 15 deletions
diff --git a/opl/opl_timer.c b/opl/opl_timer.c
index bab15687..312fe7fa 100644
--- a/opl/opl_timer.c
+++ b/opl/opl_timer.c
@@ -31,16 +31,16 @@ typedef enum
static SDL_Thread *timer_thread = NULL;
static thread_state_t timer_thread_state;
-static int current_time;
+static uint64_t current_time;
// If non-zero, callbacks are currently paused.
static int opl_timer_paused;
-// Offset in milliseconds to adjust time due to the fact that playback
+// Offset in microseconds to adjust time due to the fact that playback
// was paused.
-static unsigned int pause_offset = 0;
+static uint64_t pause_offset = 0;
// Queue of callbacks waiting to be invoked.
// The callback queue mutex is held while the callback queue structure
@@ -58,7 +58,7 @@ static SDL_mutex *timer_mutex;
// to be invoked. Otherwise, next_time is set to the time when the
// timer thread must wake up again to check.
-static int CallbackWaiting(unsigned int *next_time)
+static int CallbackWaiting(uint64_t *next_time)
{
// If paused, just wait in 50ms increments until unpaused.
// Update pause_offset so after we unpause, the callback
@@ -66,8 +66,8 @@ static int CallbackWaiting(unsigned int *next_time)
if (opl_timer_paused)
{
- *next_time = current_time + 50;
- pause_offset += 50;
+ *next_time = current_time + 50 * OPL_MS;
+ pause_offset += 50 * OPL_MS;
return 0;
}
@@ -76,7 +76,7 @@ static int CallbackWaiting(unsigned int *next_time)
if (OPL_Queue_IsEmpty(callback_queue))
{
- *next_time = current_time + 50;
+ *next_time = current_time + 50 * OPL_MS;
return 0;
}
@@ -89,11 +89,11 @@ static int CallbackWaiting(unsigned int *next_time)
return *next_time <= current_time;
}
-static unsigned int GetNextTime(void)
+static uint64_t GetNextTime(void)
{
opl_callback_t callback;
void *callback_data;
- unsigned int next_time;
+ uint64_t next_time;
int have_callback;
// Keep running through callbacks until there are none ready to
@@ -131,8 +131,8 @@ static unsigned int GetNextTime(void)
static int ThreadFunction(void *unused)
{
- unsigned int next_time;
- unsigned int now;
+ uint64_t next_time;
+ uint64_t now;
// Keep running until OPL_Timer_StopThread is called.
@@ -142,11 +142,11 @@ static int ThreadFunction(void *unused)
// wait until that time.
next_time = GetNextTime();
- now = SDL_GetTicks();
+ now = SDL_GetTicks() * OPL_MS;
if (next_time > now)
{
- SDL_Delay(next_time - now);
+ SDL_Delay((next_time - now) / OPL_MS);
}
// Update the current time.
@@ -209,11 +209,11 @@ void OPL_Timer_StopThread(void)
FreeResources();
}
-void OPL_Timer_SetCallback(unsigned int ms, opl_callback_t callback, void *data)
+void OPL_Timer_SetCallback(uint64_t us, opl_callback_t callback, void *data)
{
SDL_LockMutex(callback_queue_mutex);
OPL_Queue_Push(callback_queue, callback, data,
- current_time + ms - pause_offset);
+ current_time + us - pause_offset);
SDL_UnlockMutex(callback_queue_mutex);
}