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

    Add OPL library API function to set software emulation sample rate, and
    set from snd_samplerate in the configuration file.

    Subversion-branch: /branches/opl-branch
    Subversion-revision: 1723
---
 opl/opl.c          |  9 +++++++++
 opl/opl.h          |  4 ++++
 opl/opl_internal.h |  4 ++++
 opl/opl_sdl.c      | 19 +++++++++++++++----
 src/i_oplmusic.c   |  2 ++
 5 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/opl/opl.c b/opl/opl.c
index 8e57647e..2c8fd692 100644
--- a/opl/opl.c
+++ b/opl/opl.c
@@ -68,6 +68,8 @@ static opl_driver_t *drivers[] =
 static opl_driver_t *driver = NULL;
 static int init_stage_reg_writes = 1;

+unsigned int opl_sample_rate = 22050;
+
 //
 // Init/shutdown code.
 //
@@ -182,6 +184,13 @@ void OPL_Shutdown(void)
     }
 }

+// Set the sample rate used for software OPL emulation.
+
+void OPL_SetSampleRate(unsigned int rate)
+{
+    opl_sample_rate = rate;
+}
+
 void OPL_WritePort(opl_port_t port, unsigned int value)
 {
     if (driver != NULL)
diff --git a/opl/opl.h b/opl/opl.h
index 9f5d0a9f..04d3cf27 100644
--- a/opl/opl.h
+++ b/opl/opl.h
@@ -70,6 +70,10 @@ int OPL_Init(unsigned int port_base);

 void OPL_Shutdown(void);

+// Set the sample rate used for software emulation.
+
+void OPL_SetSampleRate(unsigned int rate);
+
 // Write to one of the OPL I/O ports:

 void OPL_WritePort(opl_port_t port, unsigned int value);
diff --git a/opl/opl_internal.h b/opl/opl_internal.h
index 78cbe7b2..4a46b060 100644
--- a/opl/opl_internal.h
+++ b/opl/opl_internal.h
@@ -56,5 +56,9 @@ typedef struct
     opl_set_paused_func set_paused_func;
 } opl_driver_t;

+// Sample rate to use when doing software emulation.
+
+extern unsigned int opl_sample_rate;
+
 #endif /* #ifndef OPL_INTERNAL_H */

diff --git a/opl/opl_sdl.c b/opl/opl_sdl.c
index 4a2e1ff8..2eb8288f 100644
--- a/opl/opl_sdl.c
+++ b/opl/opl_sdl.c
@@ -40,9 +40,6 @@

 #include "opl_queue.h"

-// TODO:
-#define opl_sample_rate 22050
-
 // When the callback mutex is locked using OPL_Lock, callback functions
 // are not invoked.

@@ -278,6 +275,20 @@ static void TimerHandler(int channel, double interval_seconds)
     SDL_UnlockMutex(callback_queue_mutex);
 }

+static unsigned int GetSliceSize(void)
+{
+    unsigned int slicesize;
+
+    slicesize = 1024 * (opl_sample_rate / 11025);
+
+    if (slicesize <= 1024)
+    {
+        slicesize = 1024;
+    }
+
+    return slicesize;
+}
+
 static int OPL_SDL_Init(unsigned int port_base)
 {
     // Check if SDL_mixer has been opened already
@@ -291,7 +302,7 @@ static int OPL_SDL_Init(unsigned int port_base)
             return 0;
         }

-        if (Mix_OpenAudio(opl_sample_rate, AUDIO_S16SYS, 2, 1024) < 0)
+        if (Mix_OpenAudio(opl_sample_rate, AUDIO_S16SYS, 2, GetSliceSize()) < 0)
         {
             fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());

diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c
index d32a163e..474877d4 100644
--- a/src/i_oplmusic.c
+++ b/src/i_oplmusic.c
@@ -1372,6 +1372,8 @@ static void I_OPL_ShutdownMusic(void)

 static boolean I_OPL_InitMusic(void)
 {
+    OPL_SetSampleRate(snd_samplerate);
+
     if (!OPL_Init(opl_io_port))
     {
         printf("Dude.  The Adlib isn't responding.\n");