81#ifndef INCLUDE_JAR_MOD_H
82#define INCLUDE_JAR_MOD_H
86 #define JARMOD_MALLOC(sz) malloc(sz)
89 #define JARMOD_FREE(p) free(p)
99#define NUMMAXCHANNELS 32
100#define MAXNOTES 12*12
101#define DEFAULT_SAMPLE_RATE 48000
170 char* sampledata[31];
262#ifdef JAR_MOD_IMPLEMENTATION
269#define EFFECT_ARPEGGIO 0x0
270#define EFFECT_PORTAMENTO_UP 0x1
271#define EFFECT_PORTAMENTO_DOWN 0x2
272#define EFFECT_TONE_PORTAMENTO 0x3
273#define EFFECT_VIBRATO 0x4
274#define EFFECT_VOLSLIDE_TONEPORTA 0x5
275#define EFFECT_VOLSLIDE_VIBRATO 0x6
276#define EFFECT_VOLSLIDE_TREMOLO 0x7
277#define EFFECT_SET_PANNING 0x8
278#define EFFECT_SET_OFFSET 0x9
279#define EFFECT_VOLUME_SLIDE 0xA
280#define EFFECT_JUMP_POSITION 0xB
281#define EFFECT_SET_VOLUME 0xC
282#define EFFECT_PATTERN_BREAK 0xD
284#define EFFECT_EXTENDED 0xE
285#define EFFECT_E_FINE_PORTA_UP 0x1
286#define EFFECT_E_FINE_PORTA_DOWN 0x2
287#define EFFECT_E_GLISSANDO_CTRL 0x3
288#define EFFECT_E_VIBRATO_WAVEFORM 0x4
289#define EFFECT_E_SET_FINETUNE 0x5
290#define EFFECT_E_PATTERN_LOOP 0x6
291#define EFFECT_E_TREMOLO_WAVEFORM 0x7
292#define EFFECT_E_SET_PANNING_2 0x8
293#define EFFECT_E_RETRIGGER_NOTE 0x9
294#define EFFECT_E_FINE_VOLSLIDE_UP 0xA
295#define EFFECT_E_FINE_VOLSLIDE_DOWN 0xB
296#define EFFECT_E_NOTE_CUT 0xC
297#define EFFECT_E_NOTE_DELAY 0xD
298#define EFFECT_E_PATTERN_DELAY 0xE
299#define EFFECT_E_INVERT_LOOP 0xF
300#define EFFECT_SET_SPEED 0xF0
301#define EFFECT_SET_TEMPO 0xF2
303#define PERIOD_TABLE_LENGTH MAXNOTES
304#define FULL_PERIOD_TABLE_LENGTH ( PERIOD_TABLE_LENGTH * 8 )
306static const short periodtable[]=
308 27392, 25856, 24384, 23040, 21696, 20480, 19328, 18240, 17216, 16256, 15360, 14496,
309 13696, 12928, 12192, 11520, 10848, 10240, 9664, 9120, 8606, 8128, 7680, 7248,
310 6848, 6464, 6096, 5760, 5424, 5120, 4832, 4560, 4304, 4064, 3840, 3624,
311 3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1812,
312 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906,
313 856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
314 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
315 214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
316 107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
317 53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28,
318 27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14,
319 13, 13, 12, 11, 11, 10, 9, 9, 8, 8, 7, 7
322static const short sintable[]={
323 0, 24, 49, 74, 97, 120, 141,161,
324 180, 197, 212, 224, 235, 244, 250,253,
325 255, 253, 250, 244, 235, 224, 212,197,
326 180, 161, 141, 120, 97, 74, 49, 24
329typedef struct modtype_
331 unsigned char signature[5];
332 int numberofchannels;
361static void memcopy(
void * dest,
void *source,
unsigned long size )
364 unsigned char * d,*s;
366 d=(
unsigned char*)dest;
367 s=(
unsigned char*)source;
374static void memclear(
void * dest,
unsigned char value,
unsigned long size )
379 d=(
unsigned char*)dest;
386static int memcompare(
unsigned char * buf1,
unsigned char * buf2,
unsigned int size )
394 if(buf1[i] != buf2[i])
404static int getnote(
jar_mod_context_t * mod,
unsigned short period,
int finetune )
408 for(i = 0; i < FULL_PERIOD_TABLE_LENGTH; i++)
422 muint curnote, arpnote;
446 if(effect>>8!=4 && effect>>8!=6)
453 if( (
sample != 0) && ( (effect>>8) != EFFECT_VOLSLIDE_TONEPORTA ) )
459 if( ( (effect>>8) != EFFECT_TONE_PORTAMENTO && (effect>>8)!=EFFECT_VOLSLIDE_TONEPORTA) )
491 case EFFECT_ARPEGGIO:
504 cptr->
effect = EFFECT_ARPEGGIO;
513 arpnote = curnote + (((cptr->
parameffect>>4)&0xF)*8);
514 if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
515 arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
520 if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
521 arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
527 case EFFECT_PORTAMENTO_UP:
539 cptr->
effect = EFFECT_PORTAMENTO_UP;
543 case EFFECT_PORTAMENTO_DOWN:
552 cptr->
effect = EFFECT_PORTAMENTO_DOWN;
556 case EFFECT_TONE_PORTAMENTO:
568 cptr->
effect = EFFECT_TONE_PORTAMENTO;
569 if( (effect&0xff) != 0 )
592 cptr->
effect = EFFECT_VIBRATO;
593 if( ( effect & 0x0F ) != 0 )
595 if( ( effect & 0xF0 ) != 0 )
600 case EFFECT_VOLSLIDE_TONEPORTA:
617 cptr->
effect = EFFECT_VOLSLIDE_TONEPORTA;
618 if( ( effect & 0xFF ) != 0 )
623 case EFFECT_VOLSLIDE_VIBRATO:
632 cptr->
effect = EFFECT_VOLSLIDE_VIBRATO;
633 if( (effect & 0xFF) != 0 )
637 case EFFECT_SET_OFFSET:
646 cptr->
samppos = ((effect>>4) * 4096) + ((effect&0xF)*256);
650 case EFFECT_VOLUME_SLIDE:
659 cptr->
effect = EFFECT_VOLUME_SLIDE;
663 case EFFECT_JUMP_POSITION:
682 case EFFECT_SET_VOLUME:
689 cptr->
volume = (effect & 0xFF);
692 case EFFECT_PATTERN_BREAK:
711 case EFFECT_EXTENDED:
712 switch( (effect>>4) & 0xF )
714 case EFFECT_E_FINE_PORTA_UP:
723 cptr->
period -= (effect & 0xF);
728 case EFFECT_E_FINE_PORTA_DOWN:
736 cptr->
period += (effect & 0xF);
741 case EFFECT_E_FINE_VOLSLIDE_UP:
750 cptr->
volume += (effect & 0xF);
755 case EFFECT_E_FINE_VOLSLIDE_DOWN:
763 cptr->
volume -= (effect & 0xF);
768 case EFFECT_E_PATTERN_LOOP:
809 case EFFECT_E_PATTERN_DELAY:
822 case EFFECT_E_NOTE_CUT:
832 cptr->
effect = EFFECT_E_NOTE_CUT;
873 if( (effect&0xFF) < 0x21 )
882 if( (effect&0xFF) >= 0x21 )
885 mod->
bpm = effect&0xFF;
899static void workeffect(
note * nptr,
channel * cptr )
903 case EFFECT_ARPEGGIO:
915 case EFFECT_PORTAMENTO_UP:
927 case EFFECT_PORTAMENTO_DOWN:
933 if( cptr->
period > 20000 )
939 case EFFECT_VOLSLIDE_TONEPORTA:
940 case EFFECT_TONE_PORTAMENTO:
974 if( cptr->
effect == EFFECT_VOLSLIDE_TONEPORTA )
993 case EFFECT_VOLSLIDE_VIBRATO:
1003 if( cptr->
effect == EFFECT_VOLSLIDE_VIBRATO )
1023 case EFFECT_VOLUME_SLIDE:
1041 case EFFECT_E_NOTE_CUT:
1070 for(i=0; i < PERIOD_TABLE_LENGTH - 1; i++)
1072 for(j=0; j < 8; j++)
1074 modctx->
fullperiod[(i*8) + j] = periodtable[i] - ((( periodtable[i] - periodtable[i+1] ) / 8) * j);
1095 if(stereo_separation < 4)
1100 if( bits == 8 || bits == 16 )
1101 modctx->
bits = bits;
1117static bool jar_mod_load(
jar_mod_context_t * modctx,
void * mod_data,
int mod_data_size )
1122 unsigned char * modmemory,* endmodmemory;
1124 modmemory = (
unsigned char *)mod_data;
1125 endmodmemory = modmemory + mod_data_size;
1133 memcopy(&(modctx->
song.
title),modmemory,1084);
1137 while(modlist[i].numberofchannels)
1139 if(memcompare(modctx->
song.
signature,modlist[i].signature,4))
1162 if( modmemory >= endmodmemory )
1166 for (i = max = 0; i < 128; i++)
1168 while (max <= modctx->song.patterntable[i])
1174 if( modmemory >= endmodmemory )
1179 for (i = 0; i < 31; i++)
1183 for (i = 0, sptr = modctx->
song.
samples; i <31; i++, sptr++)
1185 t= (sptr->
length &0xFF00)>>8 | (sptr->
length &0xFF)<<8;
1188 t= (sptr->
reppnt &0xFF00)>>8 | (sptr->
reppnt &0xFF)<<8;
1191 t= (sptr->
replen &0xFF00)>>8 | (sptr->
replen &0xFF)<<8;
1195 if (sptr->
length == 0)
continue;
1198 modmemory += sptr->
length;
1203 if( modmemory > endmodmemory )
1240 unsigned int state_remaining_steps;
1248 if( modctx && outbuffer )
1252 state_remaining_steps = 0;
1269 for (i = 0; i < nbsample; i++)
1285 worknote((
note*)(nptr+c), (
channel*)(cptr+c),(
char)(c+1),modctx);
1321 workeffect(nptr+c, cptr+c);
1329 if( trkbuf && !state_remaining_steps )
1340 for(j =0, cptr = modctx->
channels; j < modctx->number_of_channels ; j++, cptr++)
1375 if( cptr->
sampdata!=0 && ( ((j&3)==1) || ((j&3)==2) ) )
1380 if( cptr->
sampdata!=0 && ( ((j&3)==0) || ((j&3)==3) ) )
1385 if( trkbuf && !state_remaining_steps )
1406 if( trkbuf && !state_remaining_steps )
1415 state_remaining_steps--;
1436 if( l > 32767 ) l = 32767;
1437 if( l < -32768 ) l = -32768;
1438 if( r > 32767 ) r = 32767;
1439 if( r < -32768 ) r = -32768;
1442 outbuffer[(i*2)] = l;
1443 outbuffer[(i*2)+1] = r;
1457 for (i = 0; i < nbsample; i++)
1460 outbuffer[(i*2)] = 0;
1461 outbuffer[(i*2)+1] = 0;
1468 trkbuf->
name[0] = 0;
1481 memclear(&modctx->
song, 0,
sizeof(modctx->
song));
1516 jar_mod_reset(modctx);
1529 FILE *f = fopen(filename,
"rb");
1532 fseek(f,0,SEEK_END);
1534 fseek(f,0,SEEK_SET);
1536 if(fsize && fsize < 32*1024*1024)
1540 memset(modctx->
modfile, 0, fsize);
1541 fread(modctx->
modfile, fsize, 1, f);
1544 if(!jar_mod_load(modctx, (
void*)modctx->
modfile, fsize)) fsize = 0;
1583 if(jar_mod_reset(ctx)){
1584 jar_mod_load(ctx, ftmp, stmp);
struct jar_mod_tracker_buffer_state_ jar_mod_tracker_buffer_state
void jar_mod_unload(jar_mod_context_t *modctx)
struct track_state_ track_state
bool jar_mod_setcfg(jar_mod_context_t *modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter)
mulong jar_mod_max_samples(jar_mod_context_t *modctx)
void jar_mod_fillbuffer(jar_mod_context_t *modctx, short *outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state *trkbuf)
mulong jar_mod_load_file(jar_mod_context_t *modctx, const char *filename)
#define DEFAULT_SAMPLE_RATE
mulong jar_mod_current_samples(jar_mod_context_t *modctx)
void jar_mod_seek_start(jar_mod_context_t *ctx)
struct tracker_state_ tracker_state
bool jar_mod_init(jar_mod_context_t *modctx)
#define JARMOD_MALLOC(sz)
struct tracker_state_instrument_ tracker_state_instrument
muint patternloopstartpoint
channel channels[NUMMAXCHANNELS]
muint fullperiod[MAXNOTES *8]
tracker_state_instrument instruments[31]
tracker_state * track_state_buf
unsigned short cur_effect
unsigned short cur_parameffect
unsigned char instrument_number
unsigned short cur_period
int cur_pattern_table_pos