81 #ifndef INCLUDE_JAR_MOD_H
82 #define INCLUDE_JAR_MOD_H
102 #define NUMMAXCHANNELS 32
103 #define MAXNOTES 12*12
104 #define DEFAULT_SAMPLE_RATE 48000
173 char* sampledata[31];
263 #ifdef JAR_MOD_IMPLEMENTATION
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
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
300 #define PERIOD_TABLE_LENGTH MAXNOTES
301 #define FULL_PERIOD_TABLE_LENGTH ( PERIOD_TABLE_LENGTH * 8 )
303 static const short periodtable[]=
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
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
326 typedef struct modtype_
328 unsigned char signature[5];
329 int numberofchannels;
358 static void memcopy(
void * dest,
void *source,
unsigned long size )
361 unsigned char * d,*
s;
363 d=(
unsigned char*)dest;
364 s=(
unsigned char*)source;
371 static void memclear(
void * dest,
unsigned char value,
unsigned long size )
376 d=(
unsigned char*)dest;
383 static int memcompare(
unsigned char * buf1,
unsigned char * buf2,
unsigned int size )
391 if(buf1[
i] != buf2[
i])
401 static int getnote(
jar_mod_context_t * mod,
unsigned short period,
int finetune )
405 for(
i = 0;
i < FULL_PERIOD_TABLE_LENGTH;
i++)
419 muint curnote, arpnote;
443 if(effect>>8!=4 && effect>>8!=6)
450 if( (
sample != 0) && ( (effect>>8) != EFFECT_VOLSLIDE_TONEPORTA ) )
456 if( ( (effect>>8) != EFFECT_TONE_PORTAMENTO && (effect>>8)!=EFFECT_VOLSLIDE_TONEPORTA) )
488 case EFFECT_ARPEGGIO:
501 cptr->
effect = EFFECT_ARPEGGIO;
510 arpnote = curnote + (((cptr->
parameffect>>4)&0xF)*8);
511 if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
512 arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
517 if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
518 arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
524 case EFFECT_PORTAMENTO_UP:
536 cptr->
effect = EFFECT_PORTAMENTO_UP;
540 case EFFECT_PORTAMENTO_DOWN:
549 cptr->
effect = EFFECT_PORTAMENTO_DOWN;
553 case EFFECT_TONE_PORTAMENTO:
565 cptr->
effect = EFFECT_TONE_PORTAMENTO;
566 if( (effect&0xff) != 0 )
589 cptr->
effect = EFFECT_VIBRATO;
590 if( ( effect & 0x0F ) != 0 )
592 if( ( effect & 0xF0 ) != 0 )
597 case EFFECT_VOLSLIDE_TONEPORTA:
614 cptr->
effect = EFFECT_VOLSLIDE_TONEPORTA;
615 if( ( effect & 0xFF ) != 0 )
620 case EFFECT_VOLSLIDE_VIBRATO:
629 cptr->
effect = EFFECT_VOLSLIDE_VIBRATO;
630 if( (effect & 0xFF) != 0 )
634 case EFFECT_SET_OFFSET:
643 cptr->
samppos = ((effect>>4) * 4096) + ((effect&0xF)*256);
647 case EFFECT_VOLUME_SLIDE:
656 cptr->
effect = EFFECT_VOLUME_SLIDE;
660 case EFFECT_JUMP_POSITION:
679 case EFFECT_SET_VOLUME:
686 cptr->
volume = (effect & 0xFF);
689 case EFFECT_PATTERN_BREAK:
708 case EFFECT_EXTENDED:
709 switch( (effect>>4) & 0xF )
711 case EFFECT_E_FINE_PORTA_UP:
720 cptr->
period -= (effect & 0xF);
725 case EFFECT_E_FINE_PORTA_DOWN:
733 cptr->
period += (effect & 0xF);
738 case EFFECT_E_FINE_VOLSLIDE_UP:
747 cptr->
volume += (effect & 0xF);
752 case EFFECT_E_FINE_VOLSLIDE_DOWN:
760 cptr->
volume -= (effect & 0xF);
765 case EFFECT_E_PATTERN_LOOP:
806 case EFFECT_E_PATTERN_DELAY:
819 case EFFECT_E_NOTE_CUT:
829 cptr->
effect = EFFECT_E_NOTE_CUT;
870 if( (effect&0xFF) < 0x21 )
879 if( (effect&0xFF) >= 0x21 )
882 mod->
bpm = effect&0xFF;
896 static void workeffect(
note * nptr,
channel * cptr )
900 case EFFECT_ARPEGGIO:
912 case EFFECT_PORTAMENTO_UP:
924 case EFFECT_PORTAMENTO_DOWN:
930 if( cptr->
period > 20000 )
936 case EFFECT_VOLSLIDE_TONEPORTA:
937 case EFFECT_TONE_PORTAMENTO:
971 if( cptr->
effect == EFFECT_VOLSLIDE_TONEPORTA )
990 case EFFECT_VOLSLIDE_VIBRATO:
1000 if( cptr->
effect == EFFECT_VOLSLIDE_VIBRATO )
1020 case EFFECT_VOLUME_SLIDE:
1038 case EFFECT_E_NOTE_CUT:
1067 for(
i=0;
i < PERIOD_TABLE_LENGTH - 1;
i++)
1069 for(
j=0;
j < 8;
j++)
1071 modctx->
fullperiod[(
i*8) +
j] = periodtable[
i] - ((( periodtable[
i] - periodtable[
i+1] ) / 8) *
j);
1092 if(stereo_separation < 4)
1097 if( bits == 8 || bits == 16 )
1098 modctx->
bits = bits;
1114 static bool jar_mod_load(
jar_mod_context_t * modctx,
void * mod_data,
int mod_data_size )
1119 unsigned char * modmemory,* endmodmemory;
1121 modmemory = (
unsigned char *)mod_data;
1122 endmodmemory = modmemory + mod_data_size;
1130 memcopy(&(modctx->
song.
title),modmemory,1084);
1134 while(modlist[
i].numberofchannels)
1159 if( modmemory >= endmodmemory )
1163 for (
i =
max = 0;
i < 128;
i++)
1165 while (max <= modctx->song.patterntable[
i])
1171 if( modmemory >= endmodmemory )
1176 for (
i = 0;
i < 31;
i++)
1192 if (sptr->
length == 0)
continue;
1195 modmemory += sptr->
length;
1200 if( modmemory > endmodmemory )
1237 unsigned int state_remaining_steps;
1245 if( modctx && outbuffer )
1249 state_remaining_steps = 0;
1266 for (
i = 0;
i < nbsample;
i++)
1282 worknote((
note*)(nptr+c), (
channel*)(cptr+c),(
char)(c+1),modctx);
1318 workeffect(nptr+c, cptr+c);
1326 if( trkbuf && !state_remaining_steps )
1337 for(
j =0, cptr = modctx->
channels; j < modctx->number_of_channels ;
j++, cptr++)
1372 if( cptr->
sampdata!=0 && ( ((
j&3)==1) || ((
j&3)==2) ) )
1377 if( cptr->
sampdata!=0 && ( ((
j&3)==0) || ((
j&3)==3) ) )
1382 if( trkbuf && !state_remaining_steps )
1403 if( trkbuf && !state_remaining_steps )
1412 state_remaining_steps--;
1433 if( l > 32767 ) l = 32767;
1434 if( l < -32768 ) l = -32768;
1435 if(
r > 32767 )
r = 32767;
1436 if(
r < -32768 )
r = -32768;
1439 outbuffer[(
i*2)] = l;
1440 outbuffer[(
i*2)+1] =
r;
1454 for (
i = 0;
i < nbsample;
i++)
1457 outbuffer[(
i*2)] = 0;
1458 outbuffer[(
i*2)+1] = 0;
1465 trkbuf->
name[0] = 0;
1478 memclear(&modctx->
song, 0,
sizeof(modctx->
song));
1513 jar_mod_reset(modctx);
1529 #if defined(_MSC_VER) && _MSC_VER >= 1500
1530 fopen_s(&f, filename,
"rb");
1532 f = fopen(filename,
"rb");
1536 fseek(f,0,SEEK_END);
1538 fseek(f,0,SEEK_SET);
1540 if(fsize && fsize < 32*1024*1024)
1542 modctx->
modfile = malloc(fsize);
1544 memset(modctx->
modfile, 0, fsize);
1545 fread(modctx->
modfile, fsize, 1, f);
1548 if(!jar_mod_load(modctx, (
void*)modctx->
modfile, fsize)) fsize = 0;
1587 if(jar_mod_reset(ctx)){
1588 jar_mod_load(ctx, ftmp, stmp);
1596 #endif // end of JAR_MOD_IMPLEMENTATION
1600 #endif //end of header file