]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_xmp.c
common: Move infostring functions to new com_infostring.c
[xonotic/darkplaces.git] / snd_xmp.c
index f19f78b5bb167d82ea24dacaa8fb229ef27d5421..9876803d571fbcfc8c0922ba1d513b5100d1f2d5 100644 (file)
--- a/snd_xmp.c
+++ b/snd_xmp.c
  */
 
 
-#include "quakedef.h"
+#include "darkplaces.h"
 #include "snd_main.h"
 #include "snd_xmp.h"
+#include "sound.h"
 
 #ifdef LINK_TO_LIBXMP
 #include <xmp.h>
@@ -69,7 +70,7 @@
 
 #define xmp_dll 1
 
-qboolean XMP_OpenLibrary (void) {return true;}
+qbool XMP_OpenLibrary (void) {return true;}
 void XMP_CloseLibrary (void) {}
 #else
 
@@ -121,8 +122,8 @@ void XMP_CloseLibrary (void) {}
 //#define XMP_MAX_ENV_POINTS   32      /* Max number of envelope points */
 #define XMP_MAX_MOD_LENGTH     256     /* Max number of patterns in module */
 //#define XMP_MAX_CHANNELS     64      /* Max number of channels in module */
-//#define XMP_MAX_SRATE                49170   /* max sampling rate (Hz) */
-//#define XMP_MIN_SRATE                4000    /* min sampling rate (Hz) */
+#define XMP_MAX_SRATE          49170   /* max sampling rate (Hz) */
+#define XMP_MIN_SRATE          4000    /* min sampling rate (Hz) */
 //#define XMP_MIN_BPM          20      /* min BPM */
 #define XMP_MAX_FRAMESIZE      (5 * XMP_MAX_SRATE * 2 / XMP_MIN_BPM)
 
@@ -143,11 +144,11 @@ static const char **qxmp_version;
 static const unsigned int *qxmp_vercode;
 
 struct xmp_channel {
-//     int pan;                        /* Channel pan (0x80 is center) */
-//     int vol;                        /* Channel volume */
-//#define XMP_CHANNEL_SYNTH    (1 << 0)  /* Channel is synthesized */
-//#define XMP_CHANNEL_MUTE     (1 << 1)  /* Channel is muted */
-//     int flg;                        /* Channel flags */
+       int pan;                        /* Channel pan (0x80 is center) */
+       int vol;                        /* Channel volume */
+#define XMP_CHANNEL_SYNTH      (1 << 0)  /* Channel is synthesized */
+#define XMP_CHANNEL_MUTE       (1 << 1)  /* Channel is muted */
+       int flg;                        /* Channel flags */
 };
 
 //struct xmp_sequence {
@@ -191,7 +192,8 @@ struct xmp_module_info {
        struct xmp_sequence *seq_data;  /* Pointer to sequence data */
 };
 
-struct xmp_frame_info {                        /* Current frame information */
+struct xmp_frame_info
+// {                   /* Current frame information */
 //     int pos;                        /* Current position */
 //     int pattern;                    /* Current pattern */
 //     int row;                        /* Current row in pattern */
@@ -223,7 +225,8 @@ struct xmp_frame_info {                     /* Current frame information */
 //             unsigned char reserved; /* Reserved */
 //             struct xmp_event event; /* Current track event */
 //     } channel_info[XMP_MAX_CHANNELS];
-};
+//}
+;
 
 // Functions exported from libxmp
 static xmp_context (*qxmp_create_context)  (void);
@@ -343,7 +346,7 @@ XMP_OpenLibrary
 Try to load the libxmp DLL
 ====================
 */
-qboolean XMP_OpenLibrary (void)
+qbool XMP_OpenLibrary (void)
 {
        const char* dllnames_xmp [] =
        {
@@ -364,16 +367,16 @@ qboolean XMP_OpenLibrary (void)
                return true;
 
 // COMMANDLINEOPTION: Sound: -noxmp disables xmp module sound support
-       if (COM_CheckParm("-noxmp"))
+       if (Sys_CheckParm("-noxmp"))
                return false;
 
        // Load the DLL
-       if (Sys_LoadLibrary (dllnames_xmp, &xmp_dll, xmpfuncs))
+       if (Sys_LoadDependency (dllnames_xmp, &xmp_dll, xmpfuncs))
        {
                if (*qxmp_vercode < 0x040200)
                {
                        Con_Printf("Found incompatible XMP library version %s, not loading. (4.2.0 or higher required)\n", *qxmp_version);
-                       Sys_UnloadLibrary (&xmp_dll);
+                       Sys_FreeLibrary (&xmp_dll);
                        return false;
                }
                if (developer_loading.integer >= 1)
@@ -394,7 +397,7 @@ Unload the libxmp DLL
 */
 void XMP_CloseLibrary (void)
 {
-       Sys_UnloadLibrary (&xmp_dll);
+       Sys_FreeLibrary (&xmp_dll);
 }
 
 #endif
@@ -462,9 +465,22 @@ static void XMP_GetSamplesFloat(channel_t *ch, sfx_t *sfx, int firstsampleframe,
                }
 
                // start playing the loaded file
-               if (sfx->format.width == 1)    { format |= XMP_FORMAT_8BIT; } // else 16bit
+               if (sfx->format.width == 1)    { format |= XMP_FORMAT_8BIT | XMP_FORMAT_UNSIGNED; } // else 16bit
                if (sfx->format.channels == 1) { format |= XMP_FORMAT_MONO; } // else stereo
-               if (qxmp_start_player(per_ch->playercontext, sfx->format.speed, format) < 0) // FIXME: only if speed is in XMP acceptable range, else default to 48khz and let DP mix
+
+               if (qxmp_start_player(per_ch->playercontext, sfx->format.speed, format) < 0)
+               {
+                       Mem_Free(per_ch);
+                       return;
+               }
+               /* percentual left/right channel separation, default is 70. */
+               if (sfx->format.channels == 2 && (qxmp_set_player(per_ch->playercontext, XMP_PLAYER_MIX, 50) != 0))
+               {
+                       Mem_Free(per_ch);
+                       return;
+               }
+               /* interpolation type, default is XMP_INTERP_LINEAR */
+               if (qxmp_set_player(per_ch->playercontext, XMP_PLAYER_INTERP, XMP_INTERP_SPLINE) != 0)
                {
                        Mem_Free(per_ch);
                        return;
@@ -577,7 +593,7 @@ XMP_LoadModFile
 Load an XMP module file into memory
 ===============
 */
-qboolean XMP_LoadModFile(const char *filename, sfx_t *sfx)
+qbool XMP_LoadModFile(const char *filename, sfx_t *sfx)
 {
        fs_offset_t filesize;
        unsigned char *data;
@@ -591,7 +607,7 @@ qboolean XMP_LoadModFile(const char *filename, sfx_t *sfx)
 #endif
 
 // COMMANDLINEOPTION: Sound: -noxmp disables xmp module sound support
-       if (COM_CheckParm("-noxmp"))
+       if (Sys_CheckParm("-noxmp"))
                return false;
 
        // Return if already loaded
@@ -632,20 +648,20 @@ qboolean XMP_LoadModFile(const char *filename, sfx_t *sfx)
        // set dp sfx
        sfx->memsize += sizeof(*per_sfx);
        sfx->memsize += filesize; // total memory used (including sfx_t and fetcher data)
-//     sfx->format // format describing the audio data that fetcher->getsamplesfloat shall return
-       sfx->format.speed = 48000; // default to this sample rate
-       sfx->format.width = 2;  // default to 16 bit samples
-//     sfx->format.width = 1; // 8-bit
-       sfx->format.channels = 2; // default to stereo
-//     sfx->format.channels = 1; // mono
+       if (S_GetSoundRate() > XMP_MAX_SRATE)
+               sfx->format.speed = 48000;
+       else if (S_GetSoundRate() < XMP_MIN_SRATE)
+               sfx->format.speed = 8000;
+       else
+               sfx->format.speed = S_GetSoundRate();
+       sfx->format.width = S_GetSoundWidth();  // 2 = 16 bit samples
+       sfx->format.channels = S_GetSoundChannels();
        sfx->flags |= SFXFLAG_STREAMED; // cf SFXFLAG_* defines
-//     sfx->total_length // in (pcm) sample frames
-       sfx->total_length = 1<<30; // 2147384647; // they always loop (FIXME this breaks after 6 hours, we need support for a real "infinite" value!)
+       sfx->total_length = 1<<30; // 2147384647; // in (pcm) sample frames - they always loop (FIXME this breaks after 6 hours, we need support for a real "infinite" value!)
        sfx->loopstart = sfx->total_length; // (modplug does it) in sample frames. equals total_length if not looped
        sfx->fetcher_data = per_sfx;
        sfx->fetcher = &xmp_fetcher;
-//     sfx->volume_mult // for replay gain (multiplier to apply)
-//     sfx->volume_peak // for replay gain (highest peak); if set to 0, ReplayGain isn't supported
+       sfx->volume_peak = 0;
 
        qxmp_get_module_info(xc, &mi);
        if (developer_loading.integer >= 2)