]> git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_xmp.c
Prevent players moving too far when stepping up
[xonotic/darkplaces.git] / snd_xmp.c
1 /*
2         Copyright (C) 2014 nyov <nyov@nexnode.net>
3
4         This program is free software; you can redistribute it and/or
5         modify it under the terms of the GNU General Public License
6         as published by the Free Software Foundation; either version 2
7         of the License, or (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13         See the GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /*
19  * libxmp is licensed under the terms of the Lesser General Public License 2.1
20  */
21
22
23 #include "darkplaces.h"
24 #include "snd_main.h"
25 #include "snd_xmp.h"
26 #include "sound.h"
27
28 #ifdef LINK_TO_LIBXMP
29 #include <xmp.h>
30 #if ((XMP_VERCODE+0) < 0x040200)
31 #error libxmp version 4.2 or newer is required when linking to libxmp
32 #endif
33
34 /* libxmp API */
35 // Version and player information
36 #define qxmp_version xmp_version // const char *xmp_version
37 #define qxmp_vercode xmp_vercode // const unsigned int xmp_vercode
38 //#define qxmp_get_format_list xmp_get_format_list // char **xmp_get_format_list()
39 // Context creation
40 #define qxmp_create_context xmp_create_context // xmp_context xmp_create_context()
41 #define qxmp_free_context xmp_free_context // void xmp_free_context(xmp_context c)
42 // Module loading
43 //#define qxmp_test_module xmp_test_module // int xmp_test_module(char *path, struct xmp_test_info *test_info)
44 //#define qxmp_load_module xmp_load_module // int xmp_load_module(xmp_context c, char *path)
45 #define qxmp_load_module_from_memory xmp_load_module_from_memory // int xmp_load_module_from_memory(xmp_context c, void *mem, long size)
46 //#define qxmp_load_module_from_file xmp_load_module_from_file // int xmp_load_module_from_file(xmp_context c, FILE *f, long size)
47 #define qxmp_release_module xmp_release_module // void xmp_release_module(xmp_context c)
48 //#define qxmp_scan_module xmp_scan_module // void xmp_scan_module(xmp_context c)
49 #define qxmp_get_module_info xmp_get_module_info // void xmp_get_module_info(xmp_context c, struct xmp_module_info *info)
50 // Module playing
51 #define qxmp_start_player xmp_start_player // int xmp_start_player(xmp_context c, int rate, int format)
52 #define qxmp_play_frame xmp_play_frame // int xmp_play_frame(xmp_context c)
53 #define qxmp_play_buffer xmp_play_buffer // int xmp_play_buffer(xmp_context c, void *buffer, int size, int loop)
54 #define qxmp_get_frame_info xmp_get_frame_info // void xmp_get_frame_info(xmp_context c, struct xmp_frame_info *info)
55 #define qxmp_end_player xmp_end_player // void xmp_end_player(xmp_context c)
56 // Player control
57 //#define qxmp_next_position xmp_next_position // int xmp_next_position(xmp_context c)
58 //#define qxmp_prev_position xmp_prev_position// int xmp_prev_position(xmp_context c)
59 //#define qxmp_set_position xmp_set_position // int xmp_set_position(xmp_context c, int pos)
60 //#define qxmp_stop_module xmp_stop_module // void xmp_stop_module(xmp_context c)
61 //#define qxmp_restart_module xmp_restart_module // void xmp_restart_module(xmp_context c)
62 //#define qxmp_seek_time xmp_seek_time // int xmp_seek_time(xmp_context c, int time)
63 //#define qxmp_channel_mute xmp_channel_mute // int xmp_channel_mute(xmp_context c, int channel, int status)
64 //#define qxmp_channel_vol xmp_channel_vol // int xmp_channel_vol(xmp_context c, int channel, int vol)
65 //#define qxmp_inject_event xmp_inject_event // void xmp_inject_event(xmp_context c, int channel, struct xmp_event *event)
66 // Player parameter setting
67 //#define qxmp_set_instrument_path xmp_set_instrument_path // int xmp_set_instrument_path(xmp_context c, char *path)
68 #define qxmp_get_player xmp_get_player // int xmp_get_player(xmp_context c, int param)
69 #define qxmp_set_player xmp_set_player // int xmp_set_player(xmp_context c, int param, int val)
70
71 #define xmp_dll 1
72
73 qbool XMP_OpenLibrary (void) {return true;}
74 void XMP_CloseLibrary (void) {}
75 #else
76
77 /* libxmp ABI */
78 /*
79 =================================================================
80
81   definitions from xmp.h
82
83 =================================================================
84 */
85
86 // constants from libxmp
87 #define XMP_NAME_SIZE           64      /* Size of module name and type */
88
89 /* sample format flags */
90 #define XMP_FORMAT_8BIT         (1 << 0) /* Mix to 8-bit instead of 16 */
91 #define XMP_FORMAT_UNSIGNED     (1 << 1) /* Mix to unsigned samples */
92 #define XMP_FORMAT_MONO         (1 << 2) /* Mix to mono instead of stereo */
93
94 /* player parameters */
95 #define XMP_PLAYER_AMP          0       /* Amplification factor */
96 #define XMP_PLAYER_MIX          1       /* Stereo mixing */
97 #define XMP_PLAYER_INTERP       2       /* Interpolation type */
98 #define XMP_PLAYER_DSP          3       /* DSP effect flags */
99 #define XMP_PLAYER_FLAGS        4       /* Player flags */
100 #define XMP_PLAYER_CFLAGS       5       /* Player flags for current module */
101 #define XMP_PLAYER_SMPCTL       6       /* Sample control flags */
102 #define XMP_PLAYER_VOLUME       7       /* Player module volume */
103 #define XMP_PLAYER_STATE        8       /* Internal player state */
104 #define XMP_PLAYER_SMIX_VOLUME  9       /* SMIX volume */
105 #define XMP_PLAYER_DEFPAN       10      /* Default pan setting */
106
107 /* interpolation types */
108 #define XMP_INTERP_NEAREST      0       /* Nearest neighbor */
109 #define XMP_INTERP_LINEAR       1       /* Linear (default) */
110 #define XMP_INTERP_SPLINE       2       /* Cubic spline */
111
112 /* player state */
113 #define XMP_STATE_UNLOADED      0       /* Context created */
114 #define XMP_STATE_LOADED        1       /* Module loaded */
115 #define XMP_STATE_PLAYING       2       /* Module playing */
116
117 /* sample flags */
118 #define XMP_SMPCTL_SKIP         (1 << 0) /* Don't load samples */
119
120 /* limits */
121 //#define XMP_MAX_KEYS          121     /* Number of valid keys */
122 //#define XMP_MAX_ENV_POINTS    32      /* Max number of envelope points */
123 #define XMP_MAX_MOD_LENGTH      256     /* Max number of patterns in module */
124 //#define XMP_MAX_CHANNELS      64      /* Max number of channels in module */
125 #define XMP_MAX_SRATE           49170   /* max sampling rate (Hz) */
126 #define XMP_MIN_SRATE           4000    /* min sampling rate (Hz) */
127 //#define XMP_MIN_BPM           20      /* min BPM */
128 #define XMP_MAX_FRAMESIZE       (5 * XMP_MAX_SRATE * 2 / XMP_MIN_BPM)
129
130 /* error codes */
131 #define XMP_END                 1
132 #define XMP_ERROR_INTERNAL      2       /* Internal error */
133 #define XMP_ERROR_FORMAT        3       /* Unsupported module format */
134 #define XMP_ERROR_LOAD          4       /* Error loading file */
135 #define XMP_ERROR_DEPACK        5       /* Error depacking file */
136 #define XMP_ERROR_SYSTEM        6       /* System error */
137 #define XMP_ERROR_INVALID       7       /* Invalid parameter */
138 #define XMP_ERROR_STATE         8       /* Invalid player state */
139
140 // types from libxmp
141 typedef char *xmp_context;
142
143 static const char **qxmp_version;
144 static const unsigned int *qxmp_vercode;
145
146 struct xmp_channel {
147         int pan;                        /* Channel pan (0x80 is center) */
148         int vol;                        /* Channel volume */
149 #define XMP_CHANNEL_SYNTH       (1 << 0)  /* Channel is synthesized */
150 #define XMP_CHANNEL_MUTE        (1 << 1)  /* Channel is muted */
151         int flg;                        /* Channel flags */
152 };
153
154 //struct xmp_sequence {
155 //      int entry_point;
156 //      int duration;
157 //};
158
159 struct xmp_module {
160         char name[XMP_NAME_SIZE];       /* Module title */
161         char type[XMP_NAME_SIZE];       /* Module format */
162         int pat;                        /* Number of patterns */
163         int trk;                        /* Number of tracks */
164         int chn;                        /* Tracks per pattern */
165         int ins;                        /* Number of instruments */
166         int smp;                        /* Number of samples */
167         int spd;                        /* Initial speed */
168         int bpm;                        /* Initial BPM */
169         int len;                        /* Module length in patterns */
170         int rst;                        /* Restart position */
171         int gvl;                        /* Global volume */
172
173         struct xmp_pattern **xxp;       /* Patterns */
174         struct xmp_track **xxt;         /* Tracks */
175         struct xmp_instrument *xxi;     /* Instruments */
176         struct xmp_sample *xxs;         /* Samples */
177         struct xmp_channel xxc[64];     /* Channel info */
178         unsigned char xxo[XMP_MAX_MOD_LENGTH];  /* Orders */
179 };
180
181 //struct xmp_test_info {
182 //      char name[XMP_NAME_SIZE];       /* Module title */
183 //      char type[XMP_NAME_SIZE];       /* Module format */
184 //};
185
186 struct xmp_module_info {
187         unsigned char md5[16];          /* MD5 message digest */
188         int vol_base;                   /* Volume scale */
189         struct xmp_module *mod;         /* Pointer to module data */
190         char *comment;                  /* Comment text, if any */
191         int num_sequences;              /* Number of valid sequences */
192         struct xmp_sequence *seq_data;  /* Pointer to sequence data */
193 };
194
195 struct xmp_frame_info
196 // {                    /* Current frame information */
197 //      int pos;                        /* Current position */
198 //      int pattern;                    /* Current pattern */
199 //      int row;                        /* Current row in pattern */
200 //      int num_rows;                   /* Number of rows in current pattern */
201 //      int frame;                      /* Current frame */
202 //      int speed;                      /* Current replay speed */
203 //      int bpm;                        /* Current bpm */
204 //      int time;                       /* Current module time in ms */
205 //      int total_time;                 /* Estimated replay time in ms*/
206 //      int frame_time;                 /* Frame replay time in us */
207 //      void *buffer;                   /* Pointer to sound buffer */
208 //      int buffer_size;                /* Used buffer size */
209 //      int total_size;                 /* Total buffer size */
210 //      int volume;                     /* Current master volume */
211 //      int loop_count;                 /* Loop counter */
212 //      int virt_channels;              /* Number of virtual channels */
213 //      int virt_used;                  /* Used virtual channels */
214 //      int sequence;                   /* Current sequence */
215 //
216 //      struct xmp_channel_info {       /* Current channel information */
217 //              unsigned int period;    /* Sample period */
218 //              unsigned int position;  /* Sample position */
219 //              short pitchbend;        /* Linear bend from base note*/
220 //              unsigned char note;     /* Current base note number */
221 //              unsigned char instrument; /* Current instrument number */
222 //              unsigned char sample;   /* Current sample number */
223 //              unsigned char volume;   /* Current volume */
224 //              unsigned char pan;      /* Current stereo pan */
225 //              unsigned char reserved; /* Reserved */
226 //              struct xmp_event event; /* Current track event */
227 //      } channel_info[XMP_MAX_CHANNELS];
228 //}
229 ;
230
231 // Functions exported from libxmp
232 static xmp_context (*qxmp_create_context)  (void);
233 static void        (*qxmp_free_context)    (xmp_context);
234 //static int         (*qxmp_test_module)     (char *, struct xmp_test_info *);
235 //static int         (*qxmp_load_module)     (xmp_context, char *);
236 //static void        (*qxmp_scan_module)     (xmp_context);
237 static void        (*qxmp_release_module)  (xmp_context);
238 static int         (*qxmp_start_player)    (xmp_context, int, int);
239 static int         (*qxmp_play_frame)      (xmp_context);
240 static int         (*qxmp_play_buffer)     (xmp_context, void *, int, int);
241 static void        (*qxmp_get_frame_info)  (xmp_context, struct xmp_frame_info *);
242 static void        (*qxmp_end_player)      (xmp_context);
243 //static void        (*qxmp_inject_event)    (xmp_context, int, struct xmp_event *);
244 static void        (*qxmp_get_module_info) (xmp_context, struct xmp_module_info *);
245 //static char      **(*qxmp_get_format_list) (void); // FIXME: did I do this right?
246 //static int         (*qxmp_next_position)   (xmp_context);
247 //static int         (*qxmp_prev_position)   (xmp_context);
248 //static int         (*qxmp_set_position)    (xmp_context, int);
249 //static void        (*qxmp_stop_module)     (xmp_context);
250 //static void        (*qxmp_restart_module)  (xmp_context);
251 //static int         (*qxmp_seek_time)       (xmp_context, int);
252 //static int         (*qxmp_channel_mute)    (xmp_context, int, int);
253 //static int         (*qxmp_channel_vol)     (xmp_context, int, int);
254 static int         (*qxmp_set_player)      (xmp_context, int, int);
255 static int         (*qxmp_get_player)      (xmp_context, int);
256 //static int         (*qxmp_set_instrument_path) (xmp_context, char *);
257 static int         (*qxmp_load_module_from_memory) (xmp_context, void *, long);
258 //static int         (*qxmp_load_module_from_file) (xmp_context, void *, long);
259 //static int        (XMP_EXPORT *qxmp_load_module_from_file) (xmp_context, void *, long);
260
261 /* External sample mixer API */
262 /*
263 static int         (*qxmp_start_smix)       (xmp_context, int, int);
264 static void        (*qxmp_end_smix)         (xmp_context);
265 static int         (*qxmp_smix_play_instrument)(xmp_context, int, int, int, int);
266 static int         (*qxmp_smix_play_sample) (xmp_context, int, int, int, int);
267 static int         (*qxmp_smix_channel_pan) (xmp_context, int, int);
268 static int         (*qxmp_smix_load_sample) (xmp_context, int, char *);
269 static int         (*qxmp_smix_release_sample) (xmp_context, int);
270 // end Functions exported from libxmp
271 */
272
273 /*
274 =================================================================
275
276   DarkPlaces definitions
277
278 =================================================================
279 */
280
281 static dllfunction_t xmpfuncs[] =
282 {
283         /* libxmp ABI */
284         // Version and player information
285         {"xmp_version",                 (void **) &qxmp_version},
286         {"xmp_vercode",                 (void **) &qxmp_vercode},
287 //      {"xmp_get_format_list",         (void **) &qxmp_get_format_list},
288         // Context creation
289         {"xmp_create_context",          (void **) &qxmp_create_context},
290         {"xmp_free_context",            (void **) &qxmp_free_context},
291         // Module loading
292 //      {"xmp_test_module",             (void **) &qxmp_test_module},
293 //      {"xmp_load_module",             (void **) &qxmp_load_module},
294         {"xmp_load_module_from_memory", (void **) &qxmp_load_module_from_memory}, // since libxmp 4.2.0
295 //      {"xmp_load_module_from_file",   (void **) &qxmp_load_module_from_file},   // since libxmp 4.3.0
296         {"xmp_release_module",          (void **) &qxmp_release_module},
297 //      {"xmp_scan_module",             (void **) &qxmp_scan_module},
298         {"xmp_get_module_info",         (void **) &qxmp_get_module_info},
299         // Module playing
300         {"xmp_start_player",            (void **) &qxmp_start_player},
301         {"xmp_play_frame",              (void **) &qxmp_play_frame},
302         {"xmp_play_buffer",             (void **) &qxmp_play_buffer},
303         {"xmp_get_frame_info",          (void **) &qxmp_get_frame_info},
304         {"xmp_end_player",              (void **) &qxmp_end_player},
305         // Player control
306 //      {"xmp_next_position",           (void **) &qxmp_next_position},
307 //      {"xmp_prev_position",           (void **) &qxmp_prev_position},
308 //      {"xmp_set_position",            (void **) &qxmp_set_position},
309 //      {"xmp_stop_module",             (void **) &qxmp_stop_module},
310 //      {"xmp_restart_module",          (void **) &qxmp_restart_module},
311 //      {"xmp_seek_time",               (void **) &qxmp_seek_time},
312 //      {"xmp_channel_mute",            (void **) &qxmp_channel_mute},
313 //      {"xmp_channel_vol",             (void **) &qxmp_channel_vol},
314 //      {"xmp_inject_event",            (void **) &qxmp_inject_event},
315         // Player parameter setting
316 //      {"xmp_set_instrument_path",     (void **) &qxmp_set_instrument_path},
317         {"xmp_get_player",              (void **) &qxmp_get_player},
318         {"xmp_set_player",              (void **) &qxmp_set_player},
319         /* smix */ // for completeness sake only, right now
320 //      {"xmp_start_smix",              (void **) &qxmp_start_smix},
321 //      {"xmp_end_smix",                (void **) &qxmp_end_smix},
322 //      {"xmp_smix_play_instrument",    (void **) &qxmp_smix_play_instrument},
323 //      {"xmp_smix_play_sample",        (void **) &qxmp_smix_play_sample},
324 //      {"xmp_smix_channel_pan",        (void **) &qxmp_smix_channel_pan},
325 //      {"xmp_smix_load_sample",        (void **) &qxmp_smix_load_sample},
326 //      {"xmp_smix_release_sample",     (void **) &qxmp_smix_release_sample},
327         {NULL, NULL}
328 };
329
330 // libxmp DLL handle
331 static dllhandle_t xmp_dll = NULL;
332
333
334 /*
335 =================================================================
336
337   DLL load & unload
338
339 =================================================================
340 */
341
342 /*
343 ====================
344 XMP_OpenLibrary
345
346 Try to load the libxmp DLL
347 ====================
348 */
349 qbool XMP_OpenLibrary (void)
350 {
351         const char* dllnames_xmp [] =
352         {
353 #if defined(WIN32)
354                 "libxmp-4.dll",
355                 "libxmp.dll",
356 #elif defined(MACOSX) // FIXME: untested, please test a mac os build
357                 "libxmp.4.dylib",
358                 "libxmp.dylib",
359 #else
360                 "libxmp.so.4",
361                 "libxmp.so",
362 #endif
363                 NULL
364         };
365
366         if (xmp_dll) // Already loaded?
367                 return true;
368
369 // COMMANDLINEOPTION: Sound: -noxmp disables xmp module sound support
370         if (Sys_CheckParm("-noxmp"))
371                 return false;
372
373         // Load the DLL
374         if (Sys_LoadDependency (dllnames_xmp, &xmp_dll, xmpfuncs))
375         {
376                 if (*qxmp_vercode < 0x040200)
377                 {
378                         Con_Printf("Found incompatible XMP library version %s, not loading. (4.2.0 or higher required)\n", *qxmp_version);
379                         Sys_FreeLibrary (&xmp_dll);
380                         return false;
381                 }
382                 if (developer_loading.integer >= 1)
383                         Con_Printf("XMP library loaded, version %s (0x0%x)\n", *qxmp_version, *qxmp_vercode);
384                 return true;
385         }
386         else
387                 return false;
388 }
389
390
391 /*
392 ====================
393 XMP_CloseLibrary
394
395 Unload the libxmp DLL
396 ====================
397 */
398 void XMP_CloseLibrary (void)
399 {
400         Sys_FreeLibrary (&xmp_dll);
401 }
402
403 #endif
404
405 /*
406 =================================================================
407
408         Module file decoding
409
410 =================================================================
411 */
412
413 // Per-sfx data structure
414 typedef struct
415 {
416         unsigned char   *file;
417         size_t          filesize;
418 } xmp_stream_persfx_t;
419
420 // Per-channel data structure
421 typedef struct
422 {
423         xmp_context     playercontext;
424         int             bs;
425         int             buffer_firstframe;
426         int             buffer_numframes;
427         unsigned char   buffer[STREAM_BUFFERSIZE*4];
428 } xmp_stream_perchannel_t;
429
430
431 /*
432 ====================
433 XMP_GetSamplesFloat
434 ====================
435 */
436 static void XMP_GetSamplesFloat(channel_t *ch, sfx_t *sfx, int firstsampleframe, int numsampleframes, float *outsamplesfloat)
437 {
438         int i, len = numsampleframes * sfx->format.channels;
439         int f = sfx->format.width * sfx->format.channels; // bytes per frame in the buffer
440         xmp_stream_perchannel_t* per_ch = (xmp_stream_perchannel_t *)ch->fetcher_data;
441         xmp_stream_persfx_t* per_sfx = (xmp_stream_persfx_t *)sfx->fetcher_data;
442         const short *buf;
443         int newlength, done;
444         unsigned int format = 0;
445
446         // if this channel does not yet have a channel fetcher, make one
447         if (per_ch == NULL)
448         {
449                 // allocate a struct to keep track of our file position and buffer
450                 per_ch = (xmp_stream_perchannel_t *)Mem_Alloc(snd_mempool, sizeof(*per_ch));
451
452                 // create an xmp file context
453                 if ((per_ch->playercontext = qxmp_create_context()) == NULL)
454                 {
455                         //Con_Printf("error getting a libxmp file context; while trying to load file \"%s\"\n", filename);
456                         Mem_Free(per_ch);
457                         return;
458                 }
459                 // copy file to xmp
460                 if (qxmp_load_module_from_memory(per_ch->playercontext, (void *)per_sfx->file, (long)per_sfx->filesize) < 0)
461                 {
462                         qxmp_free_context(per_ch->playercontext);
463                         Mem_Free(per_ch);
464                         return;
465                 }
466
467                 // start playing the loaded file
468                 if (sfx->format.width == 1)    { format |= XMP_FORMAT_8BIT | XMP_FORMAT_UNSIGNED; } // else 16bit
469                 if (sfx->format.channels == 1) { format |= XMP_FORMAT_MONO; } // else stereo
470
471                 if (qxmp_start_player(per_ch->playercontext, sfx->format.speed, format) < 0)
472                 {
473                         Mem_Free(per_ch);
474                         return;
475                 }
476                 /* percentual left/right channel separation, default is 70. */
477                 if (sfx->format.channels == 2 && (qxmp_set_player(per_ch->playercontext, XMP_PLAYER_MIX, 50) != 0))
478                 {
479                         Mem_Free(per_ch);
480                         return;
481                 }
482                 /* interpolation type, default is XMP_INTERP_LINEAR */
483                 if (qxmp_set_player(per_ch->playercontext, XMP_PLAYER_INTERP, XMP_INTERP_SPLINE) != 0)
484                 {
485                         Mem_Free(per_ch);
486                         return;
487                 }
488
489                 per_ch->bs = 0;
490                 per_ch->buffer_firstframe = 0;
491                 per_ch->buffer_numframes = 0;
492                 // attach the struct to our channel
493                 ch->fetcher_data = (void *)per_ch;
494
495                 // reset internal xmp state / syncs buffer start with frame start
496                 qxmp_play_buffer(per_ch->playercontext, NULL, 0, 0);
497         }
498
499         // if the request is too large for our buffer, loop...
500         while (numsampleframes * f > (int)sizeof(per_ch->buffer))
501         {
502                 done = sizeof(per_ch->buffer) / f;
503                 XMP_GetSamplesFloat(ch, sfx, firstsampleframe, done, outsamplesfloat);
504                 firstsampleframe += done;
505                 numsampleframes -= done;
506                 outsamplesfloat += done * sfx->format.channels;
507         }
508
509         // seek if the request is before the current buffer (loop back)
510         // seek if the request starts beyond the current buffer by at least one frame (channel was zero volume for a while)
511         // do not seek if the request overlaps the buffer end at all (expected behavior)
512         if (per_ch->buffer_firstframe > firstsampleframe || per_ch->buffer_firstframe + per_ch->buffer_numframes < firstsampleframe)
513         {
514                 // we expect to decode forward from here so this will be our new buffer start
515                 per_ch->buffer_firstframe = firstsampleframe;
516                 per_ch->buffer_numframes = 0;
517                 // no seeking at this time
518         }
519
520         // render the file to pcm as needed
521         if (firstsampleframe + numsampleframes > per_ch->buffer_firstframe + per_ch->buffer_numframes)
522         {
523                 // first slide the buffer back, discarding any data preceding the range we care about
524                 int offset = firstsampleframe - per_ch->buffer_firstframe;
525                 int keeplength = per_ch->buffer_numframes - offset;
526                 if (keeplength > 0)
527                         memmove(per_ch->buffer, per_ch->buffer + offset * sfx->format.width * sfx->format.channels, keeplength * sfx->format.width * sfx->format.channels);
528                 per_ch->buffer_firstframe = firstsampleframe;
529                 per_ch->buffer_numframes -= offset;
530                 // render as much as we can fit in the buffer
531                 newlength = sizeof(per_ch->buffer) - per_ch->buffer_numframes * f;
532                 done = 0;
533 //              while (newlength > done && qxmp_play_buffer(per_ch->playercontext, (void *)((char *)per_ch->buffer + done), (int)(newlength - done), 1) == 0) // don't loop by default (TODO: fix pcm duration calculation first)
534                 while (newlength > done && qxmp_play_buffer(per_ch->playercontext, (void *)((char *)per_ch->buffer + done), (int)(newlength - done), 0) == 0) // loop forever
535                 {
536                         done += (int)(newlength - done);
537                 }
538                 // clear the missing space if any
539                 if (done < newlength)
540                 {
541                         memset(per_ch->buffer + done, 0, newlength - done);
542                 }
543                 // we now have more data in the buffer
544                 per_ch->buffer_numframes += done / f;
545         }
546
547         // convert the sample format for the caller
548         buf = (short *)((char *)per_ch->buffer + (firstsampleframe - per_ch->buffer_firstframe) * f);
549         for (i = 0;i < len;i++)
550                 outsamplesfloat[i] = buf[i] * (1.0f / 32768.0f);
551 }
552
553 /*
554 ====================
555 XMP_StopChannel
556 ====================
557 */
558 static void XMP_StopChannel(channel_t *ch)
559 {
560         xmp_stream_perchannel_t *per_ch = (xmp_stream_perchannel_t *)ch->fetcher_data;
561         if (per_ch != NULL)
562         {
563                 // stop the player
564                 qxmp_end_player(per_ch->playercontext);
565                 // free the module
566                 qxmp_release_module(per_ch->playercontext);
567                 // free the xmp playercontext
568                 qxmp_free_context(per_ch->playercontext);
569                 Mem_Free(per_ch);
570         }
571 }
572
573 /*
574 ====================
575 XMP_FreeSfx
576 ====================
577 */
578 static void XMP_FreeSfx(sfx_t *sfx)
579 {
580         xmp_stream_persfx_t* per_sfx = (xmp_stream_persfx_t *)sfx->fetcher_data;
581         // free the complete file we were keeping around
582         Mem_Free(per_sfx->file);
583         // free the file information structure
584         Mem_Free(per_sfx);
585 }
586
587 static const snd_fetcher_t xmp_fetcher = { XMP_GetSamplesFloat, XMP_StopChannel, XMP_FreeSfx };
588
589 /*
590 ===============
591 XMP_LoadModFile
592
593 Load an XMP module file into memory
594 ===============
595 */
596 qbool XMP_LoadModFile(const char *filename, sfx_t *sfx)
597 {
598         fs_offset_t filesize;
599         unsigned char *data;
600         xmp_context xc;
601         xmp_stream_persfx_t* per_sfx;
602         struct xmp_module_info mi;
603
604 #ifndef LINK_TO_LIBXMP
605         if (!xmp_dll)
606                 return false;
607 #endif
608
609 // COMMANDLINEOPTION: Sound: -noxmp disables xmp module sound support
610         if (Sys_CheckParm("-noxmp"))
611                 return false;
612
613         // Return if already loaded
614         if (sfx->fetcher != NULL)
615                 return true;
616
617         // Load the file
618         data = FS_LoadFile(filename, snd_mempool, false, &filesize);
619         if (data == NULL)
620                 return false;
621
622         // Create an xmp file context
623         if ((xc = qxmp_create_context()) == NULL)
624         {
625                 Con_Printf("error creating a libxmp file context; while trying to load file \"%s\"\n", filename);
626                 Mem_Free(data);
627                 return false;
628         }
629
630         if (developer_loading.integer >= 2)
631                 Con_Printf("Loading Module file (libxmp) \"%s\"\n", filename);
632
633         if (qxmp_load_module_from_memory(xc, (void *)data, (long)filesize) < 0) // Added in libxmp 4.2
634         {
635                 Con_Printf("error while trying to load xmp module \"%s\"\n", filename);
636                 qxmp_free_context(xc);
637                 Mem_Free(data);
638                 return false;
639         }
640
641         if (developer_loading.integer >= 2)
642                 Con_Printf ("\"%s\" will be streamed\n", filename);
643
644         // keep the file around
645         per_sfx = (xmp_stream_persfx_t *)Mem_Alloc (snd_mempool, sizeof (*per_sfx));
646         per_sfx->file = data;
647         per_sfx->filesize = filesize;
648         // set dp sfx
649         sfx->memsize += sizeof(*per_sfx);
650         sfx->memsize += filesize; // total memory used (including sfx_t and fetcher data)
651         if (S_GetSoundRate() > XMP_MAX_SRATE)
652                 sfx->format.speed = 48000;
653         else if (S_GetSoundRate() < XMP_MIN_SRATE)
654                 sfx->format.speed = 8000;
655         else
656                 sfx->format.speed = S_GetSoundRate();
657         sfx->format.width = S_GetSoundWidth();  // 2 = 16 bit samples
658         sfx->format.channels = S_GetSoundChannels();
659         sfx->flags |= SFXFLAG_STREAMED; // cf SFXFLAG_* defines
660         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!)
661         sfx->loopstart = sfx->total_length; // (modplug does it) in sample frames. equals total_length if not looped
662         sfx->fetcher_data = per_sfx;
663         sfx->fetcher = &xmp_fetcher;
664         sfx->volume_peak = 0;
665
666         qxmp_get_module_info(xc, &mi);
667         if (developer_loading.integer >= 2)
668         {
669                 Con_Printf("Decoding module (libxmp):\n"
670                         "    Module name  : %s\n"
671                         "    Module type  : %s\n"
672                         "    Module length: %i patterns\n"
673                         "    Patterns     : %i\n"
674                         "    Instruments  : %i\n"
675                         "    Samples      : %i\n"
676                         "    Channels     : %i\n"
677                         "    Initial Speed: %i\n"
678                         "    Initial BPM  : %i\n"
679                         "    Restart Pos. : %i\n"
680                         "    Global Volume: %i\n",
681                         mi.mod->name, mi.mod->type,
682                         mi.mod->len, mi.mod->pat, mi.mod->ins, mi.mod->smp, mi.mod->chn,
683                         mi.mod->spd, mi.mod->bpm, mi.mod->rst, mi.mod->gvl
684                 );
685         }
686         else if (developer_loading.integer == 1)
687                 Con_Printf("Decoding module (libxmp) \"%s\" (%s)\n", mi.mod->name, mi.mod->type);
688
689         qxmp_free_context(xc);
690         return true;
691 }