foxygit / doom Log in
commit a8e79308562fbcea7a39ed1846329959cbf343b3
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat Oct 17 22:45:22 2009 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat Oct 17 22:45:22 2009 +0000

    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)