From a8e79308562fbcea7a39ed1846329959cbf343b3 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 17 Oct 2009 22:45:22 +0000 Subject: Change GetSliceSize() to always return a power of two. Subversion-branch: /branches/opl-branch Subversion-revision: 1724 --- opl/opl_sdl.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/opl/opl_sdl.c b/opl/opl_sdl.c index 2eb8288f..1963d5cd 100644 --- a/opl/opl_sdl.c +++ b/opl/opl_sdl.c @@ -40,6 +40,8 @@ #include "opl_queue.h" +#define MAX_SOUND_SLICE_TIME 100 /* ms */ + // When the callback mutex is locked using OPL_Lock, callback functions // are not invoked. @@ -277,16 +279,26 @@ static void TimerHandler(int channel, double interval_seconds) static unsigned int GetSliceSize(void) { - unsigned int slicesize; + int limit; + int n; + + limit = (opl_sample_rate * MAX_SOUND_SLICE_TIME) / 1000; - slicesize = 1024 * (opl_sample_rate / 11025); + // Try all powers of two, not exceeding the limit. - if (slicesize <= 1024) + for (n=0;; ++n) { - slicesize = 1024; + // 2^n <= limit < 2^n+1 ? + + if ((1 << (n + 1)) > limit) + { + return (1 << n); + } } - return slicesize; + // Should never happen? + + return 1024; } static int OPL_SDL_Init(unsigned int port_base) -- cgit v1.2.3