vkQuake2 doxygen  1.0 dev
jar_mod.h
Go to the documentation of this file.
1 // jar_mod.h - v0.01 - public domain C0 - Joshua Reisenauer
2 //
3 // HISTORY:
4 //
5 // v0.01 2016-03-12 Setup
6 //
7 //
8 // USAGE:
9 //
10 // In ONE source file, put:
11 //
12 // #define JAR_MOD_IMPLEMENTATION
13 // #include "jar_mod.h"
14 //
15 // Other source files should just include jar_mod.h
16 //
17 // SAMPLE CODE:
18 // jar_mod_context_t modctx;
19 // short samplebuff[4096];
20 // bool bufferFull = false;
21 // int intro_load(void)
22 // {
23 // jar_mod_init(&modctx);
24 // jar_mod_load_file(&modctx, "file.mod");
25 // return 1;
26 // }
27 // int intro_unload(void)
28 // {
29 // jar_mod_unload(&modctx);
30 // return 1;
31 // }
32 // int intro_tick(long counter)
33 // {
34 // if(!bufferFull)
35 // {
36 // jar_mod_fillbuffer(&modctx, samplebuff, 4096, 0);
37 // bufferFull=true;
38 // }
39 // if(IsKeyDown(KEY_ENTER))
40 // return 1;
41 // return 0;
42 // }
43 //
44 //
45 // LISCENSE:
46 //
47 // Written by: Jean-François DEL NERO (http://hxc2001.com/) <Email : jeanfrancoisdelnero <> free.fr>
48 // Adapted to jar_mod by: Joshua Adam Reisenauer <kd7tck@gmail.com>
49 // This program is free software. It comes without any warranty, to the
50 // extent permitted by applicable law. You can redistribute it and/or
51 // modify it under the terms of the Do What The Fuck You Want To Public
52 // License, Version 2, as published by Sam Hocevar. See
53 // http://sam.zoy.org/wtfpl/COPYING for more details.
55 // HxCMOD Core API:
56 // -------------------------------------------
57 // int jar_mod_init(jar_mod_context_t * modctx)
58 //
59 // - Initialize the jar_mod_context_t buffer. Must be called before doing anything else.
60 // Return 1 if success. 0 in case of error.
61 // -------------------------------------------
62 // mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename)
63 //
64 // - "Load" a MOD from file, context must already be initialized.
65 // Return size of file in bytes.
66 // -------------------------------------------
67 // void jar_mod_fillbuffer( jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf )
68 //
69 // - Generate and return the next samples chunk to outbuffer.
70 // nbsample specify the number of stereo 16bits samples you want.
71 // The output format is by default signed 48000Hz 16-bit Stereo PCM samples, otherwise it is changed with jar_mod_setcfg().
72 // The output buffer size in bytes must be equal to ( nbsample * 2 * channels ).
73 // The optional trkbuf parameter can be used to get detailed status of the player. Put NULL/0 is unused.
74 // -------------------------------------------
75 // void jar_mod_unload( jar_mod_context_t * modctx )
76 // - "Unload" / clear the player status.
77 // -------------------------------------------
79 
80 
81 #ifndef INCLUDE_JAR_MOD_H
82 #define INCLUDE_JAR_MOD_H
83 
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <stdbool.h> // comment this line out if you have bool defined somewhere else
87 #include <string.h> // For memset()
88 
89 
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93 
94 
95 
96 // Basic type
97 typedef unsigned char muchar;
98 typedef unsigned short muint;
99 typedef short mint;
100 typedef unsigned long mulong;
101 
102 #define NUMMAXCHANNELS 32
103 #define MAXNOTES 12*12
104 #define DEFAULT_SAMPLE_RATE 48000
105 //
106 // MOD file structures
107 //
108 
109 #pragma pack(1)
110 
111 typedef struct {
118 } sample;
119 
120 typedef struct {
125 } note;
126 
127 typedef struct {
128  muchar title[20];
129  sample samples[31];
130  muchar length; // length of tablepos
132  muchar patterntable[128];
133  muchar signature[4];
135 } module;
136 
137 #pragma pack()
138 
139 //
140 // HxCMod Internal structures
141 //
142 typedef struct {
143  char* sampdata;
159  mint Arpperiods[3];
169 } channel;
170 
171 typedef struct {
173  char* sampledata[31];
174  note* patterndata[128];
175 
189  muint fullperiod[MAXNOTES * 8];
197 
198  muchar *modfile; // the raw mod file
202 
203 //
204 // Player states structures
205 //
206 typedef struct track_state_
207 {
208  unsigned char instrument_number;
209  unsigned short cur_period;
210  unsigned char cur_volume;
211  unsigned short cur_effect;
212  unsigned short cur_parameffect;
213 }track_state;
214 
215 typedef struct tracker_state_
216 {
218  int bpm;
219  int speed;
223  unsigned int buf_index;
226 
228 {
229  char name[22];
230  int active;
232 
234 {
239  char name[64];
243 
244 
245 
246 bool jar_mod_init(jar_mod_context_t * modctx);
247 bool jar_mod_setcfg(jar_mod_context_t * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter);
248 void jar_mod_fillbuffer(jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf);
249 void jar_mod_unload(jar_mod_context_t * modctx);
250 mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename);
254 
255 #ifdef __cplusplus
256 }
257 #endif
258 //--------------------------------------------------------------------
259 
260 
261 
262 //-------------------------------------------------------------------------------
263 #ifdef JAR_MOD_IMPLEMENTATION
264 
265 // Effects list
266 #define EFFECT_ARPEGGIO 0x0 // Supported
267 #define EFFECT_PORTAMENTO_UP 0x1 // Supported
268 #define EFFECT_PORTAMENTO_DOWN 0x2 // Supported
269 #define EFFECT_TONE_PORTAMENTO 0x3 // Supported
270 #define EFFECT_VIBRATO 0x4 // Supported
271 #define EFFECT_VOLSLIDE_TONEPORTA 0x5 // Supported
272 #define EFFECT_VOLSLIDE_VIBRATO 0x6 // Supported
273 #define EFFECT_VOLSLIDE_TREMOLO 0x7 // - TO BE DONE -
274 #define EFFECT_SET_PANNING 0x8 // - TO BE DONE -
275 #define EFFECT_SET_OFFSET 0x9 // Supported
276 #define EFFECT_VOLUME_SLIDE 0xA // Supported
277 #define EFFECT_JUMP_POSITION 0xB // Supported
278 #define EFFECT_SET_VOLUME 0xC // Supported
279 #define EFFECT_PATTERN_BREAK 0xD // Supported
280 
281 #define EFFECT_EXTENDED 0xE
282 #define EFFECT_E_FINE_PORTA_UP 0x1 // Supported
283 #define EFFECT_E_FINE_PORTA_DOWN 0x2 // Supported
284 #define EFFECT_E_GLISSANDO_CTRL 0x3 // - TO BE DONE -
285 #define EFFECT_E_VIBRATO_WAVEFORM 0x4 // - TO BE DONE -
286 #define EFFECT_E_SET_FINETUNE 0x5 // - TO BE DONE -
287 #define EFFECT_E_PATTERN_LOOP 0x6 // Supported
288 #define EFFECT_E_TREMOLO_WAVEFORM 0x7 // - TO BE DONE -
289 #define EFFECT_E_SET_PANNING_2 0x8 // - TO BE DONE -
290 #define EFFECT_E_RETRIGGER_NOTE 0x9 // - TO BE DONE -
291 #define EFFECT_E_FINE_VOLSLIDE_UP 0xA // Supported
292 #define EFFECT_E_FINE_VOLSLIDE_DOWN 0xB // Supported
293 #define EFFECT_E_NOTE_CUT 0xC // Supported
294 #define EFFECT_E_NOTE_DELAY 0xD // - TO BE DONE -
295 #define EFFECT_E_PATTERN_DELAY 0xE // Supported
296 #define EFFECT_E_INVERT_LOOP 0xF // - TO BE DONE -
297 #define EFFECT_SET_SPEED 0xF0 // Supported
298 #define EFFECT_SET_TEMPO 0xF2 // Supported
299 
300 #define PERIOD_TABLE_LENGTH MAXNOTES
301 #define FULL_PERIOD_TABLE_LENGTH ( PERIOD_TABLE_LENGTH * 8 )
302 
303 static const short periodtable[]=
304 {
305  27392, 25856, 24384, 23040, 21696, 20480, 19328, 18240, 17216, 16256, 15360, 14496,
306  13696, 12928, 12192, 11520, 10848, 10240, 9664, 9120, 8606, 8128, 7680, 7248,
307  6848, 6464, 6096, 5760, 5424, 5120, 4832, 4560, 4304, 4064, 3840, 3624,
308  3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1812,
309  1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906,
310  856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
311  428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
312  214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
313  107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
314  53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28,
315  27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14,
316  13, 13, 12, 11, 11, 10, 9, 9, 8, 8, 7, 7
317 };
318 
319 static const short sintable[]={
320  0, 24, 49, 74, 97, 120, 141,161,
321  180, 197, 212, 224, 235, 244, 250,253,
322  255, 253, 250, 244, 235, 224, 212,197,
323  180, 161, 141, 120, 97, 74, 49, 24
324 };
325 
326 typedef struct modtype_
327 {
328  unsigned char signature[5];
329  int numberofchannels;
330 }modtype;
331 
332 modtype modlist[]=
333 {
334  { "M!K!",4},
335  { "M.K.",4},
336  { "FLT4",4},
337  { "FLT8",8},
338  { "4CHN",4},
339  { "6CHN",6},
340  { "8CHN",8},
341  { "10CH",10},
342  { "12CH",12},
343  { "14CH",14},
344  { "16CH",16},
345  { "18CH",18},
346  { "20CH",20},
347  { "22CH",22},
348  { "24CH",24},
349  { "26CH",26},
350  { "28CH",28},
351  { "30CH",30},
352  { "32CH",32},
353  { "",0}
354 };
355 
357 
358 static void memcopy( void * dest, void *source, unsigned long size )
359 {
360  unsigned long i;
361  unsigned char * d,*s;
362 
363  d=(unsigned char*)dest;
364  s=(unsigned char*)source;
365  for(i=0;i<size;i++)
366  {
367  d[i]=s[i];
368  }
369 }
370 
371 static void memclear( void * dest, unsigned char value, unsigned long size )
372 {
373  unsigned long i;
374  unsigned char * d;
375 
376  d=(unsigned char*)dest;
377  for(i=0;i<size;i++)
378  {
379  d[i]=value;
380  }
381 }
382 
383 static int memcompare( unsigned char * buf1, unsigned char * buf2, unsigned int size )
384 {
385  unsigned int i;
386 
387  i = 0;
388 
389  while(i<size)
390  {
391  if(buf1[i] != buf2[i])
392  {
393  return 0;
394  }
395  i++;
396  }
397 
398  return 1;
399 }
400 
401 static int getnote( jar_mod_context_t * mod, unsigned short period, int finetune )
402 {
403  int i;
404 
405  for(i = 0; i < FULL_PERIOD_TABLE_LENGTH; i++)
406  {
407  if(period >= mod->fullperiod[i])
408  {
409  return i;
410  }
411  }
412 
413  return MAXNOTES;
414 }
415 
416 static void worknote( note * nptr, channel * cptr, char t, jar_mod_context_t * mod )
417 {
418  muint sample, period, effect, operiod;
419  muint curnote, arpnote;
420 
421  sample = (nptr->sampperiod & 0xF0) | (nptr->sampeffect >> 4);
422  period = ((nptr->sampperiod & 0xF) << 8) | nptr->period;
423  effect = ((nptr->sampeffect & 0xF) << 8) | nptr->effect;
424 
425  operiod = cptr->period;
426 
427  if ( period || sample )
428  {
429  if( sample && sample < 32 )
430  {
431  cptr->sampnum = sample - 1;
432  }
433 
434  if( period || sample )
435  {
436  cptr->sampdata = (char *) mod->sampledata[cptr->sampnum];
437  cptr->length = mod->song.samples[cptr->sampnum].length;
438  cptr->reppnt = mod->song.samples[cptr->sampnum].reppnt;
439  cptr->replen = mod->song.samples[cptr->sampnum].replen;
440 
441  cptr->finetune = (mod->song.samples[cptr->sampnum].finetune)&0xF;
442 
443  if(effect>>8!=4 && effect>>8!=6)
444  {
445  cptr->vibraperiod=0;
446  cptr->vibrapointeur=0;
447  }
448  }
449 
450  if( (sample != 0) && ( (effect>>8) != EFFECT_VOLSLIDE_TONEPORTA ) )
451  {
452  cptr->volume = mod->song.samples[cptr->sampnum].volume;
453  cptr->volumeslide = 0;
454  }
455 
456  if( ( (effect>>8) != EFFECT_TONE_PORTAMENTO && (effect>>8)!=EFFECT_VOLSLIDE_TONEPORTA) )
457  {
458  if (period!=0)
459  cptr->samppos = 0;
460  }
461 
462  cptr->decalperiod = 0;
463  if( period )
464  {
465  if(cptr->finetune)
466  {
467  if( cptr->finetune <= 7 )
468  {
469  period = mod->fullperiod[getnote(mod,period,0) + cptr->finetune];
470  }
471  else
472  {
473  period = mod->fullperiod[getnote(mod,period,0) - (16 - (cptr->finetune)) ];
474  }
475  }
476 
477  cptr->period = period;
478  }
479 
480  }
481 
482  cptr->effect = 0;
483  cptr->parameffect = 0;
484  cptr->effect_code = effect;
485 
486  switch (effect >> 8)
487  {
488  case EFFECT_ARPEGGIO:
489  /*
490  [0]: Arpeggio
491  Where [0][x][y] means "play note, note+x semitones, note+y
492  semitones, then return to original note". The fluctuations are
493  carried out evenly spaced in one pattern division. They are usually
494  used to simulate chords, but this doesn't work too well. They are
495  also used to produce heavy vibrato. A major chord is when x=4, y=7.
496  A minor chord is when x=3, y=7.
497  */
498 
499  if(effect&0xff)
500  {
501  cptr->effect = EFFECT_ARPEGGIO;
502  cptr->parameffect = effect&0xff;
503 
504  cptr->ArpIndex = 0;
505 
506  curnote = getnote(mod,cptr->period,cptr->finetune);
507 
508  cptr->Arpperiods[0] = cptr->period;
509 
510  arpnote = curnote + (((cptr->parameffect>>4)&0xF)*8);
511  if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
512  arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
513 
514  cptr->Arpperiods[1] = mod->fullperiod[arpnote];
515 
516  arpnote = curnote + (((cptr->parameffect)&0xF)*8);
517  if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
518  arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
519 
520  cptr->Arpperiods[2] = mod->fullperiod[arpnote];
521  }
522  break;
523 
524  case EFFECT_PORTAMENTO_UP:
525  /*
526  [1]: Slide up
527  Where [1][x][y] means "smoothly decrease the period of current
528  sample by x*16+y after each tick in the division". The
529  ticks/division are set with the 'set speed' effect (see below). If
530  the period of the note being played is z, then the final period
531  will be z - (x*16 + y)*(ticks - 1). As the slide rate depends on
532  the speed, changing the speed will change the slide. You cannot
533  slide beyond the note B3 (period 113).
534  */
535 
536  cptr->effect = EFFECT_PORTAMENTO_UP;
537  cptr->parameffect = effect&0xff;
538  break;
539 
540  case EFFECT_PORTAMENTO_DOWN:
541  /*
542  [2]: Slide down
543  Where [2][x][y] means "smoothly increase the period of current
544  sample by x*16+y after each tick in the division". Similar to [1],
545  but lowers the pitch. You cannot slide beyond the note C1 (period
546  856).
547  */
548 
549  cptr->effect = EFFECT_PORTAMENTO_DOWN;
550  cptr->parameffect = effect&0xff;
551  break;
552 
553  case EFFECT_TONE_PORTAMENTO:
554  /*
555  [3]: Slide to note
556  Where [3][x][y] means "smoothly change the period of current sample
557  by x*16+y after each tick in the division, never sliding beyond
558  current period". The period-length in this channel's division is a
559  parameter to this effect, and hence is not played. Sliding to a
560  note is similar to effects [1] and [2], but the slide will not go
561  beyond the given period, and the direction is implied by that
562  period. If x and y are both 0, then the old slide will continue.
563  */
564 
565  cptr->effect = EFFECT_TONE_PORTAMENTO;
566  if( (effect&0xff) != 0 )
567  {
568  cptr->portaspeed = (short)(effect&0xff);
569  }
570 
571  if(period!=0)
572  {
573  cptr->portaperiod = period;
574  cptr->period = operiod;
575  }
576  break;
577 
578  case EFFECT_VIBRATO:
579  /*
580  [4]: Vibrato
581  Where [4][x][y] means "oscillate the sample pitch using a
582  particular waveform with amplitude y/16 semitones, such that (x *
583  ticks)/64 cycles occur in the division". The waveform is set using
584  effect [14][4]. By placing vibrato effects on consecutive
585  divisions, the vibrato effect can be maintained. If either x or y
586  are 0, then the old vibrato values will be used.
587  */
588 
589  cptr->effect = EFFECT_VIBRATO;
590  if( ( effect & 0x0F ) != 0 ) // Depth continue or change ?
591  cptr->vibraparam = (cptr->vibraparam & 0xF0) | ( effect & 0x0F );
592  if( ( effect & 0xF0 ) != 0 ) // Speed continue or change ?
593  cptr->vibraparam = (cptr->vibraparam & 0x0F) | ( effect & 0xF0 );
594 
595  break;
596 
597  case EFFECT_VOLSLIDE_TONEPORTA:
598  /*
599  [5]: Continue 'Slide to note', but also do Volume slide
600  Where [5][x][y] means "either slide the volume up x*(ticks - 1) or
601  slide the volume down y*(ticks - 1), at the same time as continuing
602  the last 'Slide to note'". It is illegal for both x and y to be
603  non-zero. You cannot slide outside the volume range 0..64. The
604  period-length in this channel's division is a parameter to this
605  effect, and hence is not played.
606  */
607 
608  if( period != 0 )
609  {
610  cptr->portaperiod = period;
611  cptr->period = operiod;
612  }
613 
614  cptr->effect = EFFECT_VOLSLIDE_TONEPORTA;
615  if( ( effect & 0xFF ) != 0 )
616  cptr->volumeslide = ( effect & 0xFF );
617 
618  break;
619 
620  case EFFECT_VOLSLIDE_VIBRATO:
621  /*
622  [6]: Continue 'Vibrato', but also do Volume slide
623  Where [6][x][y] means "either slide the volume up x*(ticks - 1) or
624  slide the volume down y*(ticks - 1), at the same time as continuing
625  the last 'Vibrato'". It is illegal for both x and y to be non-zero.
626  You cannot slide outside the volume range 0..64.
627  */
628 
629  cptr->effect = EFFECT_VOLSLIDE_VIBRATO;
630  if( (effect & 0xFF) != 0 )
631  cptr->volumeslide = (effect & 0xFF);
632  break;
633 
634  case EFFECT_SET_OFFSET:
635  /*
636  [9]: Set sample offset
637  Where [9][x][y] means "play the sample from offset x*4096 + y*256".
638  The offset is measured in words. If no sample is given, yet one is
639  still playing on this channel, it should be retriggered to the new
640  offset using the current volume.
641  */
642 
643  cptr->samppos = ((effect>>4) * 4096) + ((effect&0xF)*256);
644 
645  break;
646 
647  case EFFECT_VOLUME_SLIDE:
648  /*
649  [10]: Volume slide
650  Where [10][x][y] means "either slide the volume up x*(ticks - 1) or
651  slide the volume down y*(ticks - 1)". If both x and y are non-zero,
652  then the y value is ignored (assumed to be 0). You cannot slide
653  outside the volume range 0..64.
654  */
655 
656  cptr->effect = EFFECT_VOLUME_SLIDE;
657  cptr->volumeslide = (effect & 0xFF);
658  break;
659 
660  case EFFECT_JUMP_POSITION:
661  /*
662  [11]: Position Jump
663  Where [11][x][y] means "stop the pattern after this division, and
664  continue the song at song-position x*16+y". This shifts the
665  'pattern-cursor' in the pattern table (see above). Legal values for
666  x*16+y are from 0 to 127.
667  */
668 
669  mod->tablepos = (effect & 0xFF);
670  if(mod->tablepos >= mod->song.length)
671  {
672  mod->tablepos = 0;
673  }
674  mod->patternpos = 0;
675  mod->jump_loop_effect = 1;
676 
677  break;
678 
679  case EFFECT_SET_VOLUME:
680  /*
681  [12]: Set volume
682  Where [12][x][y] means "set current sample's volume to x*16+y".
683  Legal volumes are 0..64.
684  */
685 
686  cptr->volume = (effect & 0xFF);
687  break;
688 
689  case EFFECT_PATTERN_BREAK:
690  /*
691  [13]: Pattern Break
692  Where [13][x][y] means "stop the pattern after this division, and
693  continue the song at the next pattern at division x*10+y" (the 10
694  is not a typo). Legal divisions are from 0 to 63 (note Protracker
695  exception above).
696  */
697 
698  mod->patternpos = ( ((effect>>4)&0xF)*10 + (effect&0xF) ) * mod->number_of_channels;
699  mod->jump_loop_effect = 1;
700  mod->tablepos++;
701  if(mod->tablepos >= mod->song.length)
702  {
703  mod->tablepos = 0;
704  }
705 
706  break;
707 
708  case EFFECT_EXTENDED:
709  switch( (effect>>4) & 0xF )
710  {
711  case EFFECT_E_FINE_PORTA_UP:
712  /*
713  [14][1]: Fineslide up
714  Where [14][1][x] means "decrement the period of the current sample
715  by x". The incrementing takes place at the beginning of the
716  division, and hence there is no actual sliding. You cannot slide
717  beyond the note B3 (period 113).
718  */
719 
720  cptr->period -= (effect & 0xF);
721  if( cptr->period < 113 )
722  cptr->period = 113;
723  break;
724 
725  case EFFECT_E_FINE_PORTA_DOWN:
726  /*
727  [14][2]: Fineslide down
728  Where [14][2][x] means "increment the period of the current sample
729  by x". Similar to [14][1] but shifts the pitch down. You cannot
730  slide beyond the note C1 (period 856).
731  */
732 
733  cptr->period += (effect & 0xF);
734  if( cptr->period > 856 )
735  cptr->period = 856;
736  break;
737 
738  case EFFECT_E_FINE_VOLSLIDE_UP:
739  /*
740  [14][10]: Fine volume slide up
741  Where [14][10][x] means "increment the volume of the current sample
742  by x". The incrementing takes place at the beginning of the
743  division, and hence there is no sliding. You cannot slide beyond
744  volume 64.
745  */
746 
747  cptr->volume += (effect & 0xF);
748  if( cptr->volume>64 )
749  cptr->volume = 64;
750  break;
751 
752  case EFFECT_E_FINE_VOLSLIDE_DOWN:
753  /*
754  [14][11]: Fine volume slide down
755  Where [14][11][x] means "decrement the volume of the current sample
756  by x". Similar to [14][10] but lowers volume. You cannot slide
757  beyond volume 0.
758  */
759 
760  cptr->volume -= (effect & 0xF);
761  if( cptr->volume > 200 )
762  cptr->volume = 0;
763  break;
764 
765  case EFFECT_E_PATTERN_LOOP:
766  /*
767  [14][6]: Loop pattern
768  Where [14][6][x] means "set the start of a loop to this division if
769  x is 0, otherwise after this division, jump back to the start of a
770  loop and play it another x times before continuing". If the start
771  of the loop was not set, it will default to the start of the
772  current pattern. Hence 'loop pattern' cannot be performed across
773  multiple patterns. Note that loops do not support nesting, and you
774  may generate an infinite loop if you try to nest 'loop pattern's.
775  */
776 
777  if( effect & 0xF )
778  {
779  if( cptr->patternloopcnt )
780  {
781  cptr->patternloopcnt--;
782  if( cptr->patternloopcnt )
783  {
784  mod->patternpos = cptr->patternloopstartpoint;
785  mod->jump_loop_effect = 1;
786  }
787  else
788  {
789  cptr->patternloopstartpoint = mod->patternpos ;
790  }
791  }
792  else
793  {
794  cptr->patternloopcnt = (effect & 0xF);
795  mod->patternpos = cptr->patternloopstartpoint;
796  mod->jump_loop_effect = 1;
797  }
798  }
799  else // Start point
800  {
801  cptr->patternloopstartpoint = mod->patternpos;
802  }
803 
804  break;
805 
806  case EFFECT_E_PATTERN_DELAY:
807  /*
808  [14][14]: Delay pattern
809  Where [14][14][x] means "after this division there will be a delay
810  equivalent to the time taken to play x divisions after which the
811  pattern will be resumed". The delay only relates to the
812  interpreting of new divisions, and all effects and previous notes
813  continue during delay.
814  */
815 
816  mod->patterndelay = (effect & 0xF);
817  break;
818 
819  case EFFECT_E_NOTE_CUT:
820  /*
821  [14][12]: Cut sample
822  Where [14][12][x] means "after the current sample has been played
823  for x ticks in this division, its volume will be set to 0". This
824  implies that if x is 0, then you will not hear any of the sample.
825  If you wish to insert "silence" in a pattern, it is better to use a
826  "silence"-sample (see above) due to the lack of proper support for
827  this effect.
828  */
829  cptr->effect = EFFECT_E_NOTE_CUT;
830  cptr->cut_param = (effect & 0xF);
831  if(!cptr->cut_param)
832  cptr->volume = 0;
833  break;
834 
835  default:
836 
837  break;
838  }
839  break;
840 
841  case 0xF:
842  /*
843  [15]: Set speed
844  Where [15][x][y] means "set speed to x*16+y". Though it is nowhere
845  near that simple. Let z = x*16+y. Depending on what values z takes,
846  different units of speed are set, there being two: ticks/division
847  and beats/minute (though this one is only a label and not strictly
848  true). If z=0, then what should technically happen is that the
849  module stops, but in practice it is treated as if z=1, because
850  there is already a method for stopping the module (running out of
851  patterns). If z<=32, then it means "set ticks/division to z"
852  otherwise it means "set beats/minute to z" (convention says that
853  this should read "If z<32.." but there are some composers out there
854  that defy conventions). Default values are 6 ticks/division, and
855  125 beats/minute (4 divisions = 1 beat). The beats/minute tag is
856  only meaningful for 6 ticks/division. To get a more accurate view
857  of how things work, use the following formula:
858  24 * beats/minute
859  divisions/minute = -----------------
860  ticks/division
861  Hence divisions/minute range from 24.75 to 6120, eg. to get a value
862  of 2000 divisions/minute use 3 ticks/division and 250 beats/minute.
863  If multiple "set speed" effects are performed in a single division,
864  the ones on higher-numbered channels take precedence over the ones
865  on lower-numbered channels. This effect has a large number of
866  different implementations, but the one described here has the
867  widest usage.
868  */
869 
870  if( (effect&0xFF) < 0x21 )
871  {
872  if( effect&0xFF )
873  {
874  mod->song.speed = effect&0xFF;
875  mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm)));
876  }
877  }
878 
879  if( (effect&0xFF) >= 0x21 )
880  {
882  mod->bpm = effect&0xFF;
883  mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm)));
884  }
885 
886  break;
887 
888  default:
889  // Unsupported effect
890  break;
891 
892  }
893 
894 }
895 
896 static void workeffect( note * nptr, channel * cptr )
897 {
898  switch(cptr->effect)
899  {
900  case EFFECT_ARPEGGIO:
901 
902  if( cptr->parameffect )
903  {
904  cptr->decalperiod = cptr->period - cptr->Arpperiods[cptr->ArpIndex];
905 
906  cptr->ArpIndex++;
907  if( cptr->ArpIndex>2 )
908  cptr->ArpIndex = 0;
909  }
910  break;
911 
912  case EFFECT_PORTAMENTO_UP:
913 
914  if(cptr->period)
915  {
916  cptr->period -= cptr->parameffect;
917 
918  if( cptr->period < 113 || cptr->period > 20000 )
919  cptr->period = 113;
920  }
921 
922  break;
923 
924  case EFFECT_PORTAMENTO_DOWN:
925 
926  if(cptr->period)
927  {
928  cptr->period += cptr->parameffect;
929 
930  if( cptr->period > 20000 )
931  cptr->period = 20000;
932  }
933 
934  break;
935 
936  case EFFECT_VOLSLIDE_TONEPORTA:
937  case EFFECT_TONE_PORTAMENTO:
938 
939  if( cptr->period && ( cptr->period != cptr->portaperiod ) && cptr->portaperiod )
940  {
941  if( cptr->period > cptr->portaperiod )
942  {
943  if( cptr->period - cptr->portaperiod >= cptr->portaspeed )
944  {
945  cptr->period -= cptr->portaspeed;
946  }
947  else
948  {
949  cptr->period = cptr->portaperiod;
950  }
951  }
952  else
953  {
954  if( cptr->portaperiod - cptr->period >= cptr->portaspeed )
955  {
956  cptr->period += cptr->portaspeed;
957  }
958  else
959  {
960  cptr->period = cptr->portaperiod;
961  }
962  }
963 
964  if( cptr->period == cptr->portaperiod )
965  {
966  // If the slide is over, don't let it to be retriggered.
967  cptr->portaperiod = 0;
968  }
969  }
970 
971  if( cptr->effect == EFFECT_VOLSLIDE_TONEPORTA )
972  {
973  if( cptr->volumeslide > 0x0F )
974  {
975  cptr->volume = cptr->volume + (cptr->volumeslide>>4);
976 
977  if(cptr->volume>63)
978  cptr->volume = 63;
979  }
980  else
981  {
982  cptr->volume = cptr->volume - (cptr->volumeslide);
983 
984  if(cptr->volume>63)
985  cptr->volume=0;
986  }
987  }
988  break;
989 
990  case EFFECT_VOLSLIDE_VIBRATO:
991  case EFFECT_VIBRATO:
992 
993  cptr->vibraperiod = ( (cptr->vibraparam&0xF) * sintable[cptr->vibrapointeur&0x1F] )>>7;
994 
995  if( cptr->vibrapointeur > 31 )
996  cptr->vibraperiod = -cptr->vibraperiod;
997 
998  cptr->vibrapointeur = (cptr->vibrapointeur+(((cptr->vibraparam>>4))&0xf)) & 0x3F;
999 
1000  if( cptr->effect == EFFECT_VOLSLIDE_VIBRATO )
1001  {
1002  if( cptr->volumeslide > 0xF )
1003  {
1004  cptr->volume = cptr->volume+(cptr->volumeslide>>4);
1005 
1006  if( cptr->volume > 64 )
1007  cptr->volume = 64;
1008  }
1009  else
1010  {
1011  cptr->volume = cptr->volume - cptr->volumeslide;
1012 
1013  if( cptr->volume > 64 )
1014  cptr->volume = 0;
1015  }
1016  }
1017 
1018  break;
1019 
1020  case EFFECT_VOLUME_SLIDE:
1021 
1022  if( cptr->volumeslide > 0xF )
1023  {
1024  cptr->volume += (cptr->volumeslide>>4);
1025 
1026  if( cptr->volume > 64 )
1027  cptr->volume = 64;
1028  }
1029  else
1030  {
1031  cptr->volume -= (cptr->volumeslide&0xf);
1032 
1033  if( cptr->volume > 64 )
1034  cptr->volume = 0;
1035  }
1036  break;
1037 
1038  case EFFECT_E_NOTE_CUT:
1039  if(cptr->cut_param)
1040  cptr->cut_param--;
1041 
1042  if(!cptr->cut_param)
1043  cptr->volume = 0;
1044  break;
1045 
1046  default:
1047  break;
1048 
1049  }
1050 
1051 }
1052 
1054 bool jar_mod_init(jar_mod_context_t * modctx)
1055 {
1056  muint i,j;
1057 
1058  if( modctx )
1059  {
1060  memclear(modctx, 0, sizeof(jar_mod_context_t));
1061  modctx->playrate = DEFAULT_SAMPLE_RATE;
1062  modctx->stereo = 1;
1063  modctx->stereo_separation = 1;
1064  modctx->bits = 16;
1065  modctx->filter = 1;
1066 
1067  for(i=0; i < PERIOD_TABLE_LENGTH - 1; i++)
1068  {
1069  for(j=0; j < 8; j++)
1070  {
1071  modctx->fullperiod[(i*8) + j] = periodtable[i] - ((( periodtable[i] - periodtable[i+1] ) / 8) * j);
1072  }
1073  }
1074 
1075  return 1;
1076  }
1077 
1078  return 0;
1079 }
1080 
1081 bool jar_mod_setcfg(jar_mod_context_t * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter)
1082 {
1083  if( modctx )
1084  {
1085  modctx->playrate = samplerate;
1086 
1087  if( stereo )
1088  modctx->stereo = 1;
1089  else
1090  modctx->stereo = 0;
1091 
1092  if(stereo_separation < 4)
1093  {
1094  modctx->stereo_separation = stereo_separation;
1095  }
1096 
1097  if( bits == 8 || bits == 16 )
1098  modctx->bits = bits;
1099  else
1100  modctx->bits = 16;
1101 
1102  if( filter )
1103  modctx->filter = 1;
1104  else
1105  modctx->filter = 0;
1106 
1107  return 1;
1108  }
1109 
1110  return 0;
1111 }
1112 
1113 // make certain that mod_data stays in memory while playing
1114 static bool jar_mod_load( jar_mod_context_t * modctx, void * mod_data, int mod_data_size )
1115 {
1116  muint i, max;
1117  unsigned short t;
1118  sample *sptr;
1119  unsigned char * modmemory,* endmodmemory;
1120 
1121  modmemory = (unsigned char *)mod_data;
1122  endmodmemory = modmemory + mod_data_size;
1123 
1124 
1125 
1126  if(modmemory)
1127  {
1128  if( modctx )
1129  {
1130  memcopy(&(modctx->song.title),modmemory,1084);
1131 
1132  i = 0;
1133  modctx->number_of_channels = 0;
1134  while(modlist[i].numberofchannels)
1135  {
1136  if(memcompare(modctx->song.signature,modlist[i].signature,4))
1137  {
1138  modctx->number_of_channels = modlist[i].numberofchannels;
1139  }
1140 
1141  i++;
1142  }
1143 
1144  if( !modctx->number_of_channels )
1145  {
1146  // 15 Samples modules support
1147  // Shift the whole datas to make it look likes a standard 4 channels mod.
1148  memcopy(&(modctx->song.signature), "M.K.", 4);
1149  memcopy(&(modctx->song.length), &(modctx->song.samples[15]), 130);
1150  memclear(&(modctx->song.samples[15]), 0, 480);
1151  modmemory += 600;
1152  modctx->number_of_channels = 4;
1153  }
1154  else
1155  {
1156  modmemory += 1084;
1157  }
1158 
1159  if( modmemory >= endmodmemory )
1160  return 0; // End passed ? - Probably a bad file !
1161 
1162  // Patterns loading
1163  for (i = max = 0; i < 128; i++)
1164  {
1165  while (max <= modctx->song.patterntable[i])
1166  {
1167  modctx->patterndata[max] = (note*)modmemory;
1168  modmemory += (256*modctx->number_of_channels);
1169  max++;
1170 
1171  if( modmemory >= endmodmemory )
1172  return 0; // End passed ? - Probably a bad file !
1173  }
1174  }
1175 
1176  for (i = 0; i < 31; i++)
1177  modctx->sampledata[i]=0;
1178 
1179  // Samples loading
1180  for (i = 0, sptr = modctx->song.samples; i <31; i++, sptr++)
1181  {
1182  t= (sptr->length &0xFF00)>>8 | (sptr->length &0xFF)<<8;
1183  sptr->length = t*2;
1184 
1185  t= (sptr->reppnt &0xFF00)>>8 | (sptr->reppnt &0xFF)<<8;
1186  sptr->reppnt = t*2;
1187 
1188  t= (sptr->replen &0xFF00)>>8 | (sptr->replen &0xFF)<<8;
1189  sptr->replen = t*2;
1190 
1191 
1192  if (sptr->length == 0) continue;
1193 
1194  modctx->sampledata[i] = (char*)modmemory;
1195  modmemory += sptr->length;
1196 
1197  if (sptr->replen + sptr->reppnt > sptr->length)
1198  sptr->replen = sptr->length - sptr->reppnt;
1199 
1200  if( modmemory > endmodmemory )
1201  return 0; // End passed ? - Probably a bad file !
1202  }
1203 
1204  // States init
1205 
1206  modctx->tablepos = 0;
1207  modctx->patternpos = 0;
1208  modctx->song.speed = 6;
1209  modctx->bpm = 125;
1210  modctx->samplenb = 0;
1211 
1212  modctx->patternticks = (((long)modctx->song.speed * modctx->playrate * 5)/ (2 * modctx->bpm)) + 1;
1213  modctx->patternticksaim = ((long)modctx->song.speed * modctx->playrate * 5) / (2 * modctx->bpm);
1214 
1215  modctx->sampleticksconst = 3546894UL / modctx->playrate; //8448*428/playrate;
1216 
1217  for(i=0; i < modctx->number_of_channels; i++)
1218  {
1219  modctx->channels[i].volume = 0;
1220  modctx->channels[i].period = 0;
1221  }
1222 
1223  modctx->mod_loaded = 1;
1224 
1225  return 1;
1226  }
1227  }
1228 
1229  return 0;
1230 }
1231 
1232 void jar_mod_fillbuffer( jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf )
1233 {
1234  unsigned long i, j;
1235  unsigned long k;
1236  unsigned char c;
1237  unsigned int state_remaining_steps;
1238  int l,r;
1239  int ll,lr;
1240  int tl,tr;
1241  short finalperiod;
1242  note *nptr;
1243  channel *cptr;
1244 
1245  if( modctx && outbuffer )
1246  {
1247  if(modctx->mod_loaded)
1248  {
1249  state_remaining_steps = 0;
1250 
1251  if( trkbuf )
1252  {
1253  trkbuf->cur_rd_index = 0;
1254 
1255  memcopy(trkbuf->name,modctx->song.title,sizeof(modctx->song.title));
1256 
1257  for(i=0;i<31;i++)
1258  {
1259  memcopy(trkbuf->instruments[i].name,modctx->song.samples[i].name,sizeof(trkbuf->instruments[i].name));
1260  }
1261  }
1262 
1263  ll = modctx->last_l_sample;
1264  lr = modctx->last_r_sample;
1265 
1266  for (i = 0; i < nbsample; i++)
1267  {
1268  //---------------------------------------
1269  if( modctx->patternticks++ > modctx->patternticksaim )
1270  {
1271  if( !modctx->patterndelay )
1272  {
1273  nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]];
1274  nptr = nptr + modctx->patternpos;
1275  cptr = modctx->channels;
1276 
1277  modctx->patternticks = 0;
1278  modctx->patterntickse = 0;
1279 
1280  for(c=0;c<modctx->number_of_channels;c++)
1281  {
1282  worknote((note*)(nptr+c), (channel*)(cptr+c),(char)(c+1),modctx);
1283  }
1284 
1285  if( !modctx->jump_loop_effect )
1286  modctx->patternpos += modctx->number_of_channels;
1287  else
1288  modctx->jump_loop_effect = 0;
1289 
1290  if( modctx->patternpos == 64*modctx->number_of_channels )
1291  {
1292  modctx->tablepos++;
1293  modctx->patternpos = 0;
1294  if(modctx->tablepos >= modctx->song.length)
1295  {
1296  modctx->tablepos = 0;
1297  modctx->loopcount++; // count next loop
1298  }
1299  }
1300  }
1301  else
1302  {
1303  modctx->patterndelay--;
1304  modctx->patternticks = 0;
1305  modctx->patterntickse = 0;
1306  }
1307 
1308  }
1309 
1310  if( modctx->patterntickse++ > (modctx->patternticksaim/modctx->song.speed) )
1311  {
1312  nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]];
1313  nptr = nptr + modctx->patternpos;
1314  cptr = modctx->channels;
1315 
1316  for(c=0;c<modctx->number_of_channels;c++)
1317  {
1318  workeffect(nptr+c, cptr+c);
1319  }
1320 
1321  modctx->patterntickse = 0;
1322  }
1323 
1324  //---------------------------------------
1325 
1326  if( trkbuf && !state_remaining_steps )
1327  {
1328  if( trkbuf->nb_of_state < trkbuf->nb_max_of_state )
1329  {
1330  memclear(&trkbuf->track_state_buf[trkbuf->nb_of_state], 0, sizeof(tracker_state));
1331  }
1332  }
1333 
1334  l=0;
1335  r=0;
1336 
1337  for(j =0, cptr = modctx->channels; j < modctx->number_of_channels ; j++, cptr++)
1338  {
1339  if( cptr->period != 0 )
1340  {
1341  finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod;
1342  if( finalperiod )
1343  {
1344  cptr->samppos += ( (modctx->sampleticksconst<<10) / finalperiod );
1345  }
1346 
1347  cptr->ticks++;
1348 
1349  if( cptr->replen<=2 )
1350  {
1351  if( (cptr->samppos>>10) >= (cptr->length) )
1352  {
1353  cptr->length = 0;
1354  cptr->reppnt = 0;
1355 
1356  if( cptr->length )
1357  cptr->samppos = cptr->samppos % (((unsigned long)cptr->length)<<10);
1358  else
1359  cptr->samppos = 0;
1360  }
1361  }
1362  else
1363  {
1364  if( (cptr->samppos>>10) >= (unsigned long)(cptr->replen+cptr->reppnt) )
1365  {
1366  cptr->samppos = ((unsigned long)(cptr->reppnt)<<10) + (cptr->samppos % ((unsigned long)(cptr->replen+cptr->reppnt)<<10));
1367  }
1368  }
1369 
1370  k = cptr->samppos >> 10;
1371 
1372  if( cptr->sampdata!=0 && ( ((j&3)==1) || ((j&3)==2) ) )
1373  {
1374  r += ( cptr->sampdata[k] * cptr->volume );
1375  }
1376 
1377  if( cptr->sampdata!=0 && ( ((j&3)==0) || ((j&3)==3) ) )
1378  {
1379  l += ( cptr->sampdata[k] * cptr->volume );
1380  }
1381 
1382  if( trkbuf && !state_remaining_steps )
1383  {
1384  if( trkbuf->nb_of_state < trkbuf->nb_max_of_state )
1385  {
1387  trkbuf->track_state_buf[trkbuf->nb_of_state].buf_index = i;
1388  trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern = modctx->song.patterntable[modctx->tablepos];
1389  trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_pos = modctx->patternpos / modctx->number_of_channels;
1390  trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_table_pos = modctx->tablepos;
1391  trkbuf->track_state_buf[trkbuf->nb_of_state].bpm = modctx->bpm;
1392  trkbuf->track_state_buf[trkbuf->nb_of_state].speed = modctx->song.speed;
1393  trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_effect = cptr->effect_code;
1394  trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_parameffect = cptr->parameffect;
1395  trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = finalperiod;
1396  trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_volume = cptr->volume;
1397  trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].instrument_number = (unsigned char)cptr->sampnum;
1398  }
1399  }
1400  }
1401  }
1402 
1403  if( trkbuf && !state_remaining_steps )
1404  {
1405  state_remaining_steps = trkbuf->sample_step;
1406 
1407  if(trkbuf->nb_of_state < trkbuf->nb_max_of_state)
1408  trkbuf->nb_of_state++;
1409  }
1410  else
1411  {
1412  state_remaining_steps--;
1413  }
1414 
1415  tl = (short)l;
1416  tr = (short)r;
1417 
1418  if ( modctx->filter )
1419  {
1420  // Filter
1421  l = (l+ll)>>1;
1422  r = (r+lr)>>1;
1423  }
1424 
1425  if ( modctx->stereo_separation == 1 )
1426  {
1427  // Left & Right Stereo panning
1428  l = (l+(r>>1));
1429  r = (r+(l>>1));
1430  }
1431 
1432  // Level limitation
1433  if( l > 32767 ) l = 32767;
1434  if( l < -32768 ) l = -32768;
1435  if( r > 32767 ) r = 32767;
1436  if( r < -32768 ) r = -32768;
1437 
1438  // Store the final sample.
1439  outbuffer[(i*2)] = l;
1440  outbuffer[(i*2)+1] = r;
1441 
1442  ll = tl;
1443  lr = tr;
1444 
1445  }
1446 
1447  modctx->last_l_sample = ll;
1448  modctx->last_r_sample = lr;
1449 
1450  modctx->samplenb = modctx->samplenb+nbsample;
1451  }
1452  else
1453  {
1454  for (i = 0; i < nbsample; i++)
1455  {
1456  // Mod not loaded. Return blank buffer.
1457  outbuffer[(i*2)] = 0;
1458  outbuffer[(i*2)+1] = 0;
1459  }
1460 
1461  if(trkbuf)
1462  {
1463  trkbuf->nb_of_state = 0;
1464  trkbuf->cur_rd_index = 0;
1465  trkbuf->name[0] = 0;
1466  memclear(trkbuf->track_state_buf, 0, sizeof(tracker_state) * trkbuf->nb_max_of_state);
1467  memclear(trkbuf->instruments, 0, sizeof(trkbuf->instruments));
1468  }
1469  }
1470  }
1471 }
1472 
1473 //resets internals for mod context
1474 static bool jar_mod_reset( jar_mod_context_t * modctx)
1475 {
1476  if(modctx)
1477  {
1478  memclear(&modctx->song, 0, sizeof(modctx->song));
1479  memclear(&modctx->sampledata, 0, sizeof(modctx->sampledata));
1480  memclear(&modctx->patterndata, 0, sizeof(modctx->patterndata));
1481  modctx->tablepos = 0;
1482  modctx->patternpos = 0;
1483  modctx->patterndelay = 0;
1484  modctx->jump_loop_effect = 0;
1485  modctx->bpm = 0;
1486  modctx->patternticks = 0;
1487  modctx->patterntickse = 0;
1488  modctx->patternticksaim = 0;
1489  modctx->sampleticksconst = 0;
1490  modctx->samplenb = 0;
1491  memclear(modctx->channels, 0, sizeof(modctx->channels));
1492  modctx->number_of_channels = 0;
1493  modctx->mod_loaded = 0;
1494  modctx->last_r_sample = 0;
1495  modctx->last_l_sample = 0;
1496 
1497  return jar_mod_init(modctx);
1498  }
1499  return 0;
1500 }
1501 
1502 void jar_mod_unload( jar_mod_context_t * modctx)
1503 {
1504  if(modctx)
1505  {
1506  if(modctx->modfile)
1507  {
1508  free(modctx->modfile);
1509  modctx->modfile = 0;
1510  modctx->modfilesize = 0;
1511  modctx->loopcount = 0;
1512  }
1513  jar_mod_reset(modctx);
1514  }
1515 }
1516 
1517 
1518 
1519 mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename)
1520 {
1521  mulong fsize = 0;
1522  if(modctx->modfile)
1523  {
1524  free(modctx->modfile);
1525  modctx->modfile = 0;
1526  }
1527 
1528  FILE *f = NULL;
1529 #if defined(_MSC_VER) && _MSC_VER >= 1500
1530  fopen_s(&f, filename, "rb");
1531 #else
1532  f = fopen(filename, "rb");
1533 #endif
1534  if(f)
1535  {
1536  fseek(f,0,SEEK_END);
1537  fsize = ftell(f);
1538  fseek(f,0,SEEK_SET);
1539 
1540  if(fsize && fsize < 32*1024*1024)
1541  {
1542  modctx->modfile = malloc(fsize);
1543  modctx->modfilesize = fsize;
1544  memset(modctx->modfile, 0, fsize);
1545  fread(modctx->modfile, fsize, 1, f);
1546  fclose(f);
1547 
1548  if(!jar_mod_load(modctx, (void*)modctx->modfile, fsize)) fsize = 0;
1549  } else fsize = 0;
1550  }
1551  return fsize;
1552 }
1553 
1555 {
1556  if(modctx)
1557  return modctx->samplenb;
1558 
1559  return 0;
1560 }
1561 
1562 // Works, however it is very slow, this data should be cached to ensure it is run only once per file
1564 {
1565  mint buff[2];
1566  mulong len;
1567  mulong lastcount = ctx->loopcount;
1568 
1569  while(ctx->loopcount <= lastcount)
1570  jar_mod_fillbuffer(ctx, buff, 1, 0);
1571 
1572  len = ctx->samplenb;
1573  jar_mod_seek_start(ctx);
1574 
1575  return len;
1576 }
1577 
1578 // move seek_val to sample index, 0 -> jar_mod_max_samples is the range
1580 {
1581  if(ctx && ctx->modfile)
1582  {
1583  muchar* ftmp = ctx->modfile;
1584  mulong stmp = ctx->modfilesize;
1585  muint lcnt = ctx->loopcount;
1586 
1587  if(jar_mod_reset(ctx)){
1588  jar_mod_load(ctx, ftmp, stmp);
1589  ctx->modfile = ftmp;
1590  ctx->modfilesize = stmp;
1591  ctx->loopcount = lcnt;
1592  }
1593  }
1594 }
1595 
1596 #endif // end of JAR_MOD_IMPLEMENTATION
1597 //-------------------------------------------------------------------------------
1598 
1599 
1600 #endif //end of header file
jar_mod_tracker_buffer_state_::name
char name[64]
Definition: jar_mod.h:239
jar_mod_context_t::patterntickse
mulong patterntickse
Definition: jar_mod.h:183
channel::volumeslide
muchar volumeslide
Definition: jar_mod.h:162
channel::oldk
mint oldk
Definition: jar_mod.h:161
channel::decalperiod
mint decalperiod
Definition: jar_mod.h:155
jar_mod_context_t::patternpos
muint patternpos
Definition: jar_mod.h:178
module::length
muchar length
Definition: jar_mod.h:130
value
GLfloat value
Definition: qgl_win.c:63
channel::cut_param
muchar cut_param
Definition: jar_mod.h:166
jar_mod_tracker_buffer_state_::instruments
tracker_state_instrument instruments[31]
Definition: jar_mod.h:240
channel::vibraparam
muchar vibraparam
Definition: jar_mod.h:163
jar_mod_context_t::jump_loop_effect
muint jump_loop_effect
Definition: jar_mod.h:180
jar_mod_max_samples
mulong jar_mod_max_samples(jar_mod_context_t *modctx)
jar_mod_context_t::sampledata
char * sampledata[31]
Definition: jar_mod.h:173
channel::sampdata
char * sampdata
Definition: jar_mod.h:143
MAXNOTES
#define MAXNOTES
Definition: jar_mod.h:103
channel::reppnt
muint reppnt
Definition: jar_mod.h:146
tracker_state_::bpm
int bpm
Definition: jar_mod.h:218
note::period
muchar period
Definition: jar_mod.h:122
tracker_state
struct tracker_state_ tracker_state
channel::portaspeed
mint portaspeed
Definition: jar_mod.h:156
jar_mod_load_file
mulong jar_mod_load_file(jar_mod_context_t *modctx, const char *filename)
i
int i
Definition: q_shared.c:305
note::sampperiod
muchar sampperiod
Definition: jar_mod.h:121
module::speed
muchar speed
Definition: jar_mod.h:134
channel::effect_code
muint effect_code
Definition: jar_mod.h:154
channel::length
muint length
Definition: jar_mod.h:145
module::samples
sample samples[31]
Definition: jar_mod.h:129
channel::Arpperiods
mint Arpperiods[3]
Definition: jar_mod.h:159
note
Definition: jar_mod.h:120
track_state
struct track_state_ track_state
channel::ArpIndex
muchar ArpIndex
Definition: jar_mod.h:160
sintable
int sintable[4200]
Definition: r_rast.c:47
channel::vibrapointeur
muchar vibrapointeur
Definition: jar_mod.h:164
tracker_state_instrument_
Definition: jar_mod.h:227
muchar
unsigned char muchar
Definition: jar_mod.h:97
jar_mod_seek_start
void jar_mod_seek_start(jar_mod_context_t *ctx)
jar_mod_context_t::samplenb
mulong samplenb
Definition: jar_mod.h:186
tracker_state_instrument_::active
int active
Definition: jar_mod.h:230
sample::length
muint length
Definition: jar_mod.h:113
channel::portaperiod
mint portaperiod
Definition: jar_mod.h:157
jar_mod_tracker_buffer_state_
Definition: jar_mod.h:233
jar_mod_context_t::song
module song
Definition: jar_mod.h:172
track_state_::cur_effect
unsigned short cur_effect
Definition: jar_mod.h:211
jar_mod_context_t
Definition: jar_mod.h:171
jar_mod_tracker_buffer_state_::track_state_buf
tracker_state * track_state_buf
Definition: jar_mod.h:241
jar_mod_context_t::sampleticksconst
mulong sampleticksconst
Definition: jar_mod.h:185
j
GLint j
Definition: qgl_win.c:150
DEFAULT_SAMPLE_RATE
#define DEFAULT_SAMPLE_RATE
Definition: jar_mod.h:104
track_state_::cur_period
unsigned short cur_period
Definition: jar_mod.h:209
tracker_state_instrument_::name
char name[22]
Definition: jar_mod.h:229
jar_mod_unload
void jar_mod_unload(jar_mod_context_t *modctx)
module
Definition: jar_mod.h:127
jar_mod_context_t::bits
mint bits
Definition: jar_mod.h:195
r
GLdouble GLdouble r
Definition: qgl_win.c:336
jar_mod_context_t::filter
mint filter
Definition: jar_mod.h:196
jar_mod_context_t::tablepos
muint tablepos
Definition: jar_mod.h:177
channel::period
muint period
Definition: jar_mod.h:149
jar_mod_context_t::patternticks
mulong patternticks
Definition: jar_mod.h:182
channel::ticks
mulong ticks
Definition: jar_mod.h:151
NUMMAXCHANNELS
#define NUMMAXCHANNELS
Definition: jar_mod.h:102
jar_mod_tracker_buffer_state_::sample_step
int sample_step
Definition: jar_mod.h:238
note::sampeffect
muchar sampeffect
Definition: jar_mod.h:123
t
GLdouble t
Definition: qgl_win.c:328
tracker_state_instrument
struct tracker_state_instrument_ tracker_state_instrument
tracker_state_::buf_index
unsigned int buf_index
Definition: jar_mod.h:223
jar_mod_context_t::last_l_sample
mint last_l_sample
Definition: jar_mod.h:192
note::effect
muchar effect
Definition: jar_mod.h:124
channel::parameffect
muchar parameffect
Definition: jar_mod.h:153
jar_mod_context_t::stereo_separation
mint stereo_separation
Definition: jar_mod.h:194
tracker_state_::cur_pattern_table_pos
int cur_pattern_table_pos
Definition: jar_mod.h:222
module::title
muchar title[20]
Definition: jar_mod.h:128
jar_mod_context_t::number_of_channels
muint number_of_channels
Definition: jar_mod.h:188
module::protracker
muchar protracker
Definition: jar_mod.h:131
NULL
#define NULL
Definition: q_shared.h:67
muint
unsigned short muint
Definition: jar_mod.h:98
channels
channel_t channels[MAX_CHANNELS]
Definition: snd_dma.c:42
channel::patternloopstartpoint
muint patternloopstartpoint
Definition: jar_mod.h:168
track_state_::cur_parameffect
unsigned short cur_parameffect
Definition: jar_mod.h:212
jar_mod_current_samples
mulong jar_mod_current_samples(jar_mod_context_t *modctx)
sample::finetune
muchar finetune
Definition: jar_mod.h:114
name
cvar_t * name
Definition: cl_main.c:79
jar_mod_context_t::bpm
muchar bpm
Definition: jar_mod.h:181
s
static fixed16_t s
Definition: r_scan.c:30
sample::replen
muint replen
Definition: jar_mod.h:117
jar_mod_init
bool jar_mod_init(jar_mod_context_t *modctx)
track_state_::cur_volume
unsigned char cur_volume
Definition: jar_mod.h:210
channel::replen
muint replen
Definition: jar_mod.h:147
jar_mod_context_t::patterndelay
muint patterndelay
Definition: jar_mod.h:179
channel::patternloopcnt
muint patternloopcnt
Definition: jar_mod.h:167
channel::finetune
muchar finetune
Definition: jar_mod.h:165
tracker_state_
Definition: jar_mod.h:215
tracker_state_::cur_pattern
int cur_pattern
Definition: jar_mod.h:220
track_state_
Definition: jar_mod.h:206
jar_mod_context_t::loopcount
muint loopcount
Definition: jar_mod.h:200
jar_mod_context_t::modfile
muchar * modfile
Definition: jar_mod.h:198
sample::name
muchar name[22]
Definition: jar_mod.h:112
mint
short mint
Definition: jar_mod.h:99
jar_mod_fillbuffer
void jar_mod_fillbuffer(jar_mod_context_t *modctx, short *outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state *trkbuf)
jar_mod_context_t::patterndata
note * patterndata[128]
Definition: jar_mod.h:174
jar_mod_context_t::playrate
mulong playrate
Definition: jar_mod.h:176
jar_mod_tracker_buffer_state_::nb_of_state
int nb_of_state
Definition: jar_mod.h:236
tracker_state_::number_of_tracks
int number_of_tracks
Definition: jar_mod.h:217
tracker_state_::cur_pattern_pos
int cur_pattern_pos
Definition: jar_mod.h:221
channel::samppos
mulong samppos
Definition: jar_mod.h:148
channel::volume
muchar volume
Definition: jar_mod.h:150
sample::reppnt
muint reppnt
Definition: jar_mod.h:116
jar_mod_context_t::channels
channel channels[NUMMAXCHANNELS]
Definition: jar_mod.h:187
tracker_state_::speed
int speed
Definition: jar_mod.h:219
mulong
unsigned long mulong
Definition: jar_mod.h:100
track_state_::instrument_number
unsigned char instrument_number
Definition: jar_mod.h:208
jar_mod_context_t::stereo
mint stereo
Definition: jar_mod.h:193
sample
Definition: jar_mod.h:111
module::signature
muchar signature[4]
Definition: jar_mod.h:133
jar_mod_context_t::patternticksaim
mulong patternticksaim
Definition: jar_mod.h:184
jar_mod_tracker_buffer_state_::cur_rd_index
int cur_rd_index
Definition: jar_mod.h:237
sample::volume
muchar volume
Definition: jar_mod.h:115
channel
Definition: jar_mod.h:142
jar_mod_context_t::modfilesize
mulong modfilesize
Definition: jar_mod.h:199
tracker_state_::tracks
track_state tracks[32]
Definition: jar_mod.h:224
max
#define max(a, b)
Definition: vk_local.h:75
jar_mod_context_t::mod_loaded
muint mod_loaded
Definition: jar_mod.h:190
jar_mod_tracker_buffer_state_::nb_max_of_state
int nb_max_of_state
Definition: jar_mod.h:235
module::patterntable
muchar patterntable[128]
Definition: jar_mod.h:132
channel::vibraperiod
mint vibraperiod
Definition: jar_mod.h:158
jar_mod_context_t::last_r_sample
mint last_r_sample
Definition: jar_mod.h:191
jar_mod_tracker_buffer_state
struct jar_mod_tracker_buffer_state_ jar_mod_tracker_buffer_state
channel::effect
muchar effect
Definition: jar_mod.h:152
channel::sampnum
muint sampnum
Definition: jar_mod.h:144
jar_mod_context_t::fullperiod
muint fullperiod[MAXNOTES *8]
Definition: jar_mod.h:189
jar_mod_setcfg
bool jar_mod_setcfg(jar_mod_context_t *modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter)