| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | #ifndef INCLUDE_JAR_MOD_H |
| 82 | #define INCLUDE_JAR_MOD_H |
| 83 | |
| 84 | |
| 85 | #ifndef JARMOD_MALLOC |
| 86 | #define JARMOD_MALLOC(sz) malloc(sz) |
| 87 | #endif |
| 88 | #ifndef JARMOD_FREE |
| 89 | #define JARMOD_FREE(p) free(p) |
| 90 | #endif |
| 91 | |
| 92 | |
| 93 | |
| 94 | typedef unsigned char muchar; |
| 95 | typedef unsigned short muint; |
| 96 | typedef short mint; |
| 97 | typedef unsigned long mulong; |
| 98 | |
| 99 | #define NUMMAXCHANNELS 32 |
| 100 | #define MAXNOTES 12*12 |
| 101 | #define DEFAULT_SAMPLE_RATE 48000 |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | #pragma pack(1) |
| 107 | |
| 108 | typedef struct { |
| 109 | muchar name[22]; |
| 110 | muint length; |
| 111 | muchar finetune; |
| 112 | muchar volume; |
| 113 | muint reppnt; |
| 114 | muint replen; |
| 115 | } sample; |
| 116 | |
| 117 | typedef struct { |
| 118 | muchar sampperiod; |
| 119 | muchar period; |
| 120 | muchar sampeffect; |
| 121 | muchar effect; |
| 122 | } note; |
| 123 | |
| 124 | typedef struct { |
| 125 | muchar title[20]; |
| 126 | sample samples[31]; |
| 127 | muchar length; |
| 128 | muchar protracker; |
| 129 | muchar patterntable[128]; |
| 130 | muchar signature[4]; |
| 131 | muchar speed; |
| 132 | } module; |
| 133 | |
| 134 | #pragma pack() |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | typedef struct { |
| 140 | char* sampdata; |
| 141 | muint sampnum; |
| 142 | muint length; |
| 143 | muint reppnt; |
| 144 | muint replen; |
| 145 | mulong samppos; |
| 146 | muint period; |
| 147 | muchar volume; |
| 148 | mulong ticks; |
| 149 | muchar effect; |
| 150 | muchar parameffect; |
| 151 | muint effect_code; |
| 152 | mint decalperiod; |
| 153 | mint portaspeed; |
| 154 | mint portaperiod; |
| 155 | mint vibraperiod; |
| 156 | mint Arpperiods[3]; |
| 157 | muchar ArpIndex; |
| 158 | mint oldk; |
| 159 | muchar volumeslide; |
| 160 | muchar vibraparam; |
| 161 | muchar vibrapointeur; |
| 162 | muchar finetune; |
| 163 | muchar cut_param; |
| 164 | muint patternloopcnt; |
| 165 | muint patternloopstartpoint; |
| 166 | } channel; |
| 167 | |
| 168 | typedef struct { |
| 169 | module song; |
| 170 | char* sampledata[31]; |
| 171 | note* patterndata[128]; |
| 172 | |
| 173 | mulong playrate; |
| 174 | muint tablepos; |
| 175 | muint patternpos; |
| 176 | muint patterndelay; |
| 177 | muint jump_loop_effect; |
| 178 | muchar bpm; |
| 179 | mulong patternticks; |
| 180 | mulong patterntickse; |
| 181 | mulong patternticksaim; |
| 182 | mulong sampleticksconst; |
| 183 | mulong samplenb; |
| 184 | channel channels[NUMMAXCHANNELS]; |
| 185 | muint number_of_channels; |
| 186 | muint fullperiod[MAXNOTES * 8]; |
| 187 | muint mod_loaded; |
| 188 | mint last_r_sample; |
| 189 | mint last_l_sample; |
| 190 | mint stereo; |
| 191 | mint stereo_separation; |
| 192 | mint bits; |
| 193 | mint filter; |
| 194 | |
| 195 | muchar *modfile; |
| 196 | mulong modfilesize; |
| 197 | muint loopcount; |
| 198 | } jar_mod_context_t; |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | typedef struct track_state_ |
| 204 | { |
| 205 | unsigned char instrument_number; |
| 206 | unsigned short cur_period; |
| 207 | unsigned char cur_volume; |
| 208 | unsigned short cur_effect; |
| 209 | unsigned short cur_parameffect; |
| 210 | }track_state; |
| 211 | |
| 212 | typedef struct tracker_state_ |
| 213 | { |
| 214 | int number_of_tracks; |
| 215 | int bpm; |
| 216 | int speed; |
| 217 | int cur_pattern; |
| 218 | int cur_pattern_pos; |
| 219 | int cur_pattern_table_pos; |
| 220 | unsigned int buf_index; |
| 221 | track_state tracks[32]; |
| 222 | }tracker_state; |
| 223 | |
| 224 | typedef struct tracker_state_instrument_ |
| 225 | { |
| 226 | char name[22]; |
| 227 | int active; |
| 228 | }tracker_state_instrument; |
| 229 | |
| 230 | typedef struct jar_mod_tracker_buffer_state_ |
| 231 | { |
| 232 | int nb_max_of_state; |
| 233 | int nb_of_state; |
| 234 | int cur_rd_index; |
| 235 | int sample_step; |
| 236 | char name[64]; |
| 237 | tracker_state_instrument instruments[31]; |
| 238 | tracker_state * track_state_buf; |
| 239 | }jar_mod_tracker_buffer_state; |
| 240 | |
| 241 | #ifdef __cplusplus |
| 242 | extern "C" { |
| 243 | #endif |
| 244 | |
| 245 | bool jar_mod_init(jar_mod_context_t * modctx); |
| 246 | bool jar_mod_setcfg(jar_mod_context_t * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter); |
| 247 | void jar_mod_fillbuffer(jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf); |
| 248 | void jar_mod_unload(jar_mod_context_t * modctx); |
| 249 | mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename); |
| 250 | mulong jar_mod_current_samples(jar_mod_context_t * modctx); |
| 251 | mulong jar_mod_max_samples(jar_mod_context_t * modctx); |
| 252 | void jar_mod_seek_start(jar_mod_context_t * ctx); |
| 253 | |
| 254 | #ifdef __cplusplus |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | #ifdef JAR_MOD_IMPLEMENTATION |
| 263 | |
| 264 | #include <stdio.h> |
| 265 | #include <stdlib.h> |
| 266 | |
| 267 | |
| 268 | |
| 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 |
| 283 | |
| 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 |
| 302 | |
| 303 | #define PERIOD_TABLE_LENGTH MAXNOTES |
| 304 | #define FULL_PERIOD_TABLE_LENGTH ( PERIOD_TABLE_LENGTH * 8 ) |
| 305 | |
| 306 | static const short periodtable[]= |
| 307 | { |
| 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 |
| 320 | }; |
| 321 | |
| 322 | static 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 |
| 327 | }; |
| 328 | |
| 329 | typedef struct modtype_ |
| 330 | { |
| 331 | unsigned char signature[5]; |
| 332 | int numberofchannels; |
| 333 | }modtype; |
| 334 | |
| 335 | modtype modlist[]= |
| 336 | { |
| 337 | { "M!K!",4}, |
| 338 | { "M.K.",4}, |
| 339 | { "FLT4",4}, |
| 340 | { "FLT8",8}, |
| 341 | { "4CHN",4}, |
| 342 | { "6CHN",6}, |
| 343 | { "8CHN",8}, |
| 344 | { "10CH",10}, |
| 345 | { "12CH",12}, |
| 346 | { "14CH",14}, |
| 347 | { "16CH",16}, |
| 348 | { "18CH",18}, |
| 349 | { "20CH",20}, |
| 350 | { "22CH",22}, |
| 351 | { "24CH",24}, |
| 352 | { "26CH",26}, |
| 353 | { "28CH",28}, |
| 354 | { "30CH",30}, |
| 355 | { "32CH",32}, |
| 356 | { "",0} |
| 357 | }; |
| 358 | |
| 359 | |
| 360 | |
| 361 | static void memcopy( void * dest, void *source, unsigned long size ) |
| 362 | { |
| 363 | unsigned long i; |
| 364 | unsigned char * d,*s; |
| 365 | |
| 366 | d=(unsigned char*)dest; |
| 367 | s=(unsigned char*)source; |
| 368 | for(i=0;i<size;i++) |
| 369 | { |
| 370 | d[i]=s[i]; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void memclear( void * dest, unsigned char value, unsigned long size ) |
| 375 | { |
| 376 | unsigned long i; |
| 377 | unsigned char * d; |
| 378 | |
| 379 | d=(unsigned char*)dest; |
| 380 | for(i=0;i<size;i++) |
| 381 | { |
| 382 | d[i]=value; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static int memcompare( unsigned char * buf1, unsigned char * buf2, unsigned int size ) |
| 387 | { |
| 388 | unsigned int i; |
| 389 | |
| 390 | i = 0; |
| 391 | |
| 392 | while(i<size) |
| 393 | { |
| 394 | if(buf1[i] != buf2[i]) |
| 395 | { |
| 396 | return 0; |
| 397 | } |
| 398 | i++; |
| 399 | } |
| 400 | |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | static int getnote( jar_mod_context_t * mod, unsigned short period, int finetune ) |
| 405 | { |
| 406 | int i; |
| 407 | |
| 408 | for(i = 0; i < FULL_PERIOD_TABLE_LENGTH; i++) |
| 409 | { |
| 410 | if(period >= mod->fullperiod[i]) |
| 411 | { |
| 412 | return i; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return MAXNOTES; |
| 417 | } |
| 418 | |
| 419 | static void worknote( note * nptr, channel * cptr, char t, jar_mod_context_t * mod ) |
| 420 | { |
| 421 | muint sample, period, effect, operiod; |
| 422 | muint curnote, arpnote; |
| 423 | |
| 424 | sample = (nptr->sampperiod & 0xF0) | (nptr->sampeffect >> 4); |
| 425 | period = ((nptr->sampperiod & 0xF) << 8) | nptr->period; |
| 426 | effect = ((nptr->sampeffect & 0xF) << 8) | nptr->effect; |
| 427 | |
| 428 | operiod = cptr->period; |
| 429 | |
| 430 | if ( period || sample ) |
| 431 | { |
| 432 | if( sample && sample < 32 ) |
| 433 | { |
| 434 | cptr->sampnum = sample - 1; |
| 435 | } |
| 436 | |
| 437 | if( period || sample ) |
| 438 | { |
| 439 | cptr->sampdata = (char *) mod->sampledata[cptr->sampnum]; |
| 440 | cptr->length = mod->song.samples[cptr->sampnum].length; |
| 441 | cptr->reppnt = mod->song.samples[cptr->sampnum].reppnt; |
| 442 | cptr->replen = mod->song.samples[cptr->sampnum].replen; |
| 443 | |
| 444 | cptr->finetune = (mod->song.samples[cptr->sampnum].finetune)&0xF; |
| 445 | |
| 446 | if(effect>>8!=4 && effect>>8!=6) |
| 447 | { |
| 448 | cptr->vibraperiod=0; |
| 449 | cptr->vibrapointeur=0; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if( (sample != 0) && ( (effect>>8) != EFFECT_VOLSLIDE_TONEPORTA ) ) |
| 454 | { |
| 455 | cptr->volume = mod->song.samples[cptr->sampnum].volume; |
| 456 | cptr->volumeslide = 0; |
| 457 | } |
| 458 | |
| 459 | if( ( (effect>>8) != EFFECT_TONE_PORTAMENTO && (effect>>8)!=EFFECT_VOLSLIDE_TONEPORTA) ) |
| 460 | { |
| 461 | if (period!=0) |
| 462 | cptr->samppos = 0; |
| 463 | } |
| 464 | |
| 465 | cptr->decalperiod = 0; |
| 466 | if( period ) |
| 467 | { |
| 468 | if(cptr->finetune) |
| 469 | { |
| 470 | if( cptr->finetune <= 7 ) |
| 471 | { |
| 472 | period = mod->fullperiod[getnote(mod,period,0) + cptr->finetune]; |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | period = mod->fullperiod[getnote(mod,period,0) - (16 - (cptr->finetune)) ]; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | cptr->period = period; |
| 481 | } |
| 482 | |
| 483 | } |
| 484 | |
| 485 | cptr->effect = 0; |
| 486 | cptr->parameffect = 0; |
| 487 | cptr->effect_code = effect; |
| 488 | |
| 489 | switch (effect >> 8) |
| 490 | { |
| 491 | case EFFECT_ARPEGGIO: |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | if(effect&0xff) |
| 503 | { |
| 504 | cptr->effect = EFFECT_ARPEGGIO; |
| 505 | cptr->parameffect = effect&0xff; |
| 506 | |
| 507 | cptr->ArpIndex = 0; |
| 508 | |
| 509 | curnote = getnote(mod,cptr->period,cptr->finetune); |
| 510 | |
| 511 | cptr->Arpperiods[0] = cptr->period; |
| 512 | |
| 513 | arpnote = curnote + (((cptr->parameffect>>4)&0xF)*8); |
| 514 | if( arpnote >= FULL_PERIOD_TABLE_LENGTH ) |
| 515 | arpnote = FULL_PERIOD_TABLE_LENGTH - 1; |
| 516 | |
| 517 | cptr->Arpperiods[1] = mod->fullperiod[arpnote]; |
| 518 | |
| 519 | arpnote = curnote + (((cptr->parameffect)&0xF)*8); |
| 520 | if( arpnote >= FULL_PERIOD_TABLE_LENGTH ) |
| 521 | arpnote = FULL_PERIOD_TABLE_LENGTH - 1; |
| 522 | |
| 523 | cptr->Arpperiods[2] = mod->fullperiod[arpnote]; |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case EFFECT_PORTAMENTO_UP: |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | cptr->effect = EFFECT_PORTAMENTO_UP; |
| 540 | cptr->parameffect = effect&0xff; |
| 541 | break; |
| 542 | |
| 543 | case EFFECT_PORTAMENTO_DOWN: |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | cptr->effect = EFFECT_PORTAMENTO_DOWN; |
| 553 | cptr->parameffect = effect&0xff; |
| 554 | break; |
| 555 | |
| 556 | case EFFECT_TONE_PORTAMENTO: |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | cptr->effect = EFFECT_TONE_PORTAMENTO; |
| 569 | if( (effect&0xff) != 0 ) |
| 570 | { |
| 571 | cptr->portaspeed = (short)(effect&0xff); |
| 572 | } |
| 573 | |
| 574 | if(period!=0) |
| 575 | { |
| 576 | cptr->portaperiod = period; |
| 577 | cptr->period = operiod; |
| 578 | } |
| 579 | break; |
| 580 | |
| 581 | case EFFECT_VIBRATO: |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | cptr->effect = EFFECT_VIBRATO; |
| 593 | if( ( effect & 0x0F ) != 0 ) |
| 594 | cptr->vibraparam = (cptr->vibraparam & 0xF0) | ( effect & 0x0F ); |
| 595 | if( ( effect & 0xF0 ) != 0 ) |
| 596 | cptr->vibraparam = (cptr->vibraparam & 0x0F) | ( effect & 0xF0 ); |
| 597 | |
| 598 | break; |
| 599 | |
| 600 | case EFFECT_VOLSLIDE_TONEPORTA: |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | if( period != 0 ) |
| 612 | { |
| 613 | cptr->portaperiod = period; |
| 614 | cptr->period = operiod; |
| 615 | } |
| 616 | |
| 617 | cptr->effect = EFFECT_VOLSLIDE_TONEPORTA; |
| 618 | if( ( effect & 0xFF ) != 0 ) |
| 619 | cptr->volumeslide = ( effect & 0xFF ); |
| 620 | |
| 621 | break; |
| 622 | |
| 623 | case EFFECT_VOLSLIDE_VIBRATO: |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | cptr->effect = EFFECT_VOLSLIDE_VIBRATO; |
| 633 | if( (effect & 0xFF) != 0 ) |
| 634 | cptr->volumeslide = (effect & 0xFF); |
| 635 | break; |
| 636 | |
| 637 | case EFFECT_SET_OFFSET: |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | cptr->samppos = ((effect>>4) * 4096) + ((effect&0xF)*256); |
| 647 | |
| 648 | break; |
| 649 | |
| 650 | case EFFECT_VOLUME_SLIDE: |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | cptr->effect = EFFECT_VOLUME_SLIDE; |
| 660 | cptr->volumeslide = (effect & 0xFF); |
| 661 | break; |
| 662 | |
| 663 | case EFFECT_JUMP_POSITION: |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | mod->tablepos = (effect & 0xFF); |
| 673 | if(mod->tablepos >= mod->song.length) |
| 674 | { |
| 675 | mod->tablepos = 0; |
| 676 | } |
| 677 | mod->patternpos = 0; |
| 678 | mod->jump_loop_effect = 1; |
| 679 | |
| 680 | break; |
| 681 | |
| 682 | case EFFECT_SET_VOLUME: |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | cptr->volume = (effect & 0xFF); |
| 690 | break; |
| 691 | |
| 692 | case EFFECT_PATTERN_BREAK: |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | mod->patternpos = ( ((effect>>4)&0xF)*10 + (effect&0xF) ) * mod->number_of_channels; |
| 702 | mod->jump_loop_effect = 1; |
| 703 | mod->tablepos++; |
| 704 | if(mod->tablepos >= mod->song.length) |
| 705 | { |
| 706 | mod->tablepos = 0; |
| 707 | } |
| 708 | |
| 709 | break; |
| 710 | |
| 711 | case EFFECT_EXTENDED: |
| 712 | switch( (effect>>4) & 0xF ) |
| 713 | { |
| 714 | case EFFECT_E_FINE_PORTA_UP: |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | cptr->period -= (effect & 0xF); |
| 724 | if( cptr->period < 113 ) |
| 725 | cptr->period = 113; |
| 726 | break; |
| 727 | |
| 728 | case EFFECT_E_FINE_PORTA_DOWN: |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | cptr->period += (effect & 0xF); |
| 737 | if( cptr->period > 856 ) |
| 738 | cptr->period = 856; |
| 739 | break; |
| 740 | |
| 741 | case EFFECT_E_FINE_VOLSLIDE_UP: |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | cptr->volume += (effect & 0xF); |
| 751 | if( cptr->volume>64 ) |
| 752 | cptr->volume = 64; |
| 753 | break; |
| 754 | |
| 755 | case EFFECT_E_FINE_VOLSLIDE_DOWN: |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | cptr->volume -= (effect & 0xF); |
| 764 | if( cptr->volume > 200 ) |
| 765 | cptr->volume = 0; |
| 766 | break; |
| 767 | |
| 768 | case EFFECT_E_PATTERN_LOOP: |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | if( effect & 0xF ) |
| 781 | { |
| 782 | if( cptr->patternloopcnt ) |
| 783 | { |
| 784 | cptr->patternloopcnt--; |
| 785 | if( cptr->patternloopcnt ) |
| 786 | { |
| 787 | mod->patternpos = cptr->patternloopstartpoint; |
| 788 | mod->jump_loop_effect = 1; |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | cptr->patternloopstartpoint = mod->patternpos ; |
| 793 | } |
| 794 | } |
| 795 | else |
| 796 | { |
| 797 | cptr->patternloopcnt = (effect & 0xF); |
| 798 | mod->patternpos = cptr->patternloopstartpoint; |
| 799 | mod->jump_loop_effect = 1; |
| 800 | } |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | cptr->patternloopstartpoint = mod->patternpos; |
| 805 | } |
| 806 | |
| 807 | break; |
| 808 | |
| 809 | case EFFECT_E_PATTERN_DELAY: |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | mod->patterndelay = (effect & 0xF); |
| 820 | break; |
| 821 | |
| 822 | case EFFECT_E_NOTE_CUT: |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | cptr->effect = EFFECT_E_NOTE_CUT; |
| 833 | cptr->cut_param = (effect & 0xF); |
| 834 | if(!cptr->cut_param) |
| 835 | cptr->volume = 0; |
| 836 | break; |
| 837 | |
| 838 | default: |
| 839 | |
| 840 | break; |
| 841 | } |
| 842 | break; |
| 843 | |
| 844 | case 0xF: |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | if( (effect&0xFF) < 0x21 ) |
| 874 | { |
| 875 | if( effect&0xFF ) |
| 876 | { |
| 877 | mod->song.speed = effect&0xFF; |
| 878 | mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm))); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if( (effect&0xFF) >= 0x21 ) |
| 883 | { |
| 884 | |
| 885 | mod->bpm = effect&0xFF; |
| 886 | mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm))); |
| 887 | } |
| 888 | |
| 889 | break; |
| 890 | |
| 891 | default: |
| 892 | |
| 893 | break; |
| 894 | |
| 895 | } |
| 896 | |
| 897 | } |
| 898 | |
| 899 | static void workeffect( note * nptr, channel * cptr ) |
| 900 | { |
| 901 | switch(cptr->effect) |
| 902 | { |
| 903 | case EFFECT_ARPEGGIO: |
| 904 | |
| 905 | if( cptr->parameffect ) |
| 906 | { |
| 907 | cptr->decalperiod = cptr->period - cptr->Arpperiods[cptr->ArpIndex]; |
| 908 | |
| 909 | cptr->ArpIndex++; |
| 910 | if( cptr->ArpIndex>2 ) |
| 911 | cptr->ArpIndex = 0; |
| 912 | } |
| 913 | break; |
| 914 | |
| 915 | case EFFECT_PORTAMENTO_UP: |
| 916 | |
| 917 | if(cptr->period) |
| 918 | { |
| 919 | cptr->period -= cptr->parameffect; |
| 920 | |
| 921 | if( cptr->period < 113 || cptr->period > 20000 ) |
| 922 | cptr->period = 113; |
| 923 | } |
| 924 | |
| 925 | break; |
| 926 | |
| 927 | case EFFECT_PORTAMENTO_DOWN: |
| 928 | |
| 929 | if(cptr->period) |
| 930 | { |
| 931 | cptr->period += cptr->parameffect; |
| 932 | |
| 933 | if( cptr->period > 20000 ) |
| 934 | cptr->period = 20000; |
| 935 | } |
| 936 | |
| 937 | break; |
| 938 | |
| 939 | case EFFECT_VOLSLIDE_TONEPORTA: |
| 940 | case EFFECT_TONE_PORTAMENTO: |
| 941 | |
| 942 | if( cptr->period && ( cptr->period != cptr->portaperiod ) && cptr->portaperiod ) |
| 943 | { |
| 944 | if( cptr->period > cptr->portaperiod ) |
| 945 | { |
| 946 | if( cptr->period - cptr->portaperiod >= cptr->portaspeed ) |
| 947 | { |
| 948 | cptr->period -= cptr->portaspeed; |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | cptr->period = cptr->portaperiod; |
| 953 | } |
| 954 | } |
| 955 | else |
| 956 | { |
| 957 | if( cptr->portaperiod - cptr->period >= cptr->portaspeed ) |
| 958 | { |
| 959 | cptr->period += cptr->portaspeed; |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | cptr->period = cptr->portaperiod; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if( cptr->period == cptr->portaperiod ) |
| 968 | { |
| 969 | |
| 970 | cptr->portaperiod = 0; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | if( cptr->effect == EFFECT_VOLSLIDE_TONEPORTA ) |
| 975 | { |
| 976 | if( cptr->volumeslide > 0x0F ) |
| 977 | { |
| 978 | cptr->volume = cptr->volume + (cptr->volumeslide>>4); |
| 979 | |
| 980 | if(cptr->volume>63) |
| 981 | cptr->volume = 63; |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | cptr->volume = cptr->volume - (cptr->volumeslide); |
| 986 | |
| 987 | if(cptr->volume>63) |
| 988 | cptr->volume=0; |
| 989 | } |
| 990 | } |
| 991 | break; |
| 992 | |
| 993 | case EFFECT_VOLSLIDE_VIBRATO: |
| 994 | case EFFECT_VIBRATO: |
| 995 | |
| 996 | cptr->vibraperiod = ( (cptr->vibraparam&0xF) * sintable[cptr->vibrapointeur&0x1F] )>>7; |
| 997 | |
| 998 | if( cptr->vibrapointeur > 31 ) |
| 999 | cptr->vibraperiod = -cptr->vibraperiod; |
| 1000 | |
| 1001 | cptr->vibrapointeur = (cptr->vibrapointeur+(((cptr->vibraparam>>4))&0xf)) & 0x3F; |
| 1002 | |
| 1003 | if( cptr->effect == EFFECT_VOLSLIDE_VIBRATO ) |
| 1004 | { |
| 1005 | if( cptr->volumeslide > 0xF ) |
| 1006 | { |
| 1007 | cptr->volume = cptr->volume+(cptr->volumeslide>>4); |
| 1008 | |
| 1009 | if( cptr->volume > 64 ) |
| 1010 | cptr->volume = 64; |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | cptr->volume = cptr->volume - cptr->volumeslide; |
| 1015 | |
| 1016 | if( cptr->volume > 64 ) |
| 1017 | cptr->volume = 0; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | break; |
| 1022 | |
| 1023 | case EFFECT_VOLUME_SLIDE: |
| 1024 | |
| 1025 | if( cptr->volumeslide > 0xF ) |
| 1026 | { |
| 1027 | cptr->volume += (cptr->volumeslide>>4); |
| 1028 | |
| 1029 | if( cptr->volume > 64 ) |
| 1030 | cptr->volume = 64; |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | cptr->volume -= (cptr->volumeslide&0xf); |
| 1035 | |
| 1036 | if( cptr->volume > 64 ) |
| 1037 | cptr->volume = 0; |
| 1038 | } |
| 1039 | break; |
| 1040 | |
| 1041 | case EFFECT_E_NOTE_CUT: |
| 1042 | if(cptr->cut_param) |
| 1043 | cptr->cut_param--; |
| 1044 | |
| 1045 | if(!cptr->cut_param) |
| 1046 | cptr->volume = 0; |
| 1047 | break; |
| 1048 | |
| 1049 | default: |
| 1050 | break; |
| 1051 | |
| 1052 | } |
| 1053 | |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | bool jar_mod_init(jar_mod_context_t * modctx) |
| 1058 | { |
| 1059 | muint i,j; |
| 1060 | |
| 1061 | if( modctx ) |
| 1062 | { |
| 1063 | memclear(modctx, 0, sizeof(jar_mod_context_t)); |
| 1064 | modctx->playrate = DEFAULT_SAMPLE_RATE; |
| 1065 | modctx->stereo = 1; |
| 1066 | modctx->stereo_separation = 1; |
| 1067 | modctx->bits = 16; |
| 1068 | modctx->filter = 1; |
| 1069 | |
| 1070 | for(i=0; i < PERIOD_TABLE_LENGTH - 1; i++) |
| 1071 | { |
| 1072 | for(j=0; j < 8; j++) |
| 1073 | { |
| 1074 | modctx->fullperiod[(i*8) + j] = periodtable[i] - ((( periodtable[i] - periodtable[i+1] ) / 8) * j); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return 1; |
| 1079 | } |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | bool jar_mod_setcfg(jar_mod_context_t * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter) |
| 1085 | { |
| 1086 | if( modctx ) |
| 1087 | { |
| 1088 | modctx->playrate = samplerate; |
| 1089 | |
| 1090 | if( stereo ) |
| 1091 | modctx->stereo = 1; |
| 1092 | else |
| 1093 | modctx->stereo = 0; |
| 1094 | |
| 1095 | if(stereo_separation < 4) |
| 1096 | { |
| 1097 | modctx->stereo_separation = stereo_separation; |
| 1098 | } |
| 1099 | |
| 1100 | if( bits == 8 || bits == 16 ) |
| 1101 | modctx->bits = bits; |
| 1102 | else |
| 1103 | modctx->bits = 16; |
| 1104 | |
| 1105 | if( filter ) |
| 1106 | modctx->filter = 1; |
| 1107 | else |
| 1108 | modctx->filter = 0; |
| 1109 | |
| 1110 | return 1; |
| 1111 | } |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | static bool jar_mod_load( jar_mod_context_t * modctx, void * mod_data, int mod_data_size ) |
| 1118 | { |
| 1119 | muint i, max; |
| 1120 | unsigned short t; |
| 1121 | sample *sptr; |
| 1122 | unsigned char * modmemory,* endmodmemory; |
| 1123 | |
| 1124 | modmemory = (unsigned char *)mod_data; |
| 1125 | endmodmemory = modmemory + mod_data_size; |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | if(modmemory) |
| 1130 | { |
| 1131 | if( modctx ) |
| 1132 | { |
| 1133 | memcopy(&(modctx->song), modmemory, 1084); |
| 1134 | |
| 1135 | i = 0; |
| 1136 | modctx->number_of_channels = 0; |
| 1137 | while(modlist[i].numberofchannels) |
| 1138 | { |
| 1139 | if(memcompare(modctx->song.signature,modlist[i].signature,4)) |
| 1140 | { |
| 1141 | modctx->number_of_channels = modlist[i].numberofchannels; |
| 1142 | } |
| 1143 | |
| 1144 | i++; |
| 1145 | } |
| 1146 | |
| 1147 | if( !modctx->number_of_channels ) |
| 1148 | { |
| 1149 | |
| 1150 | |
| 1151 | memcopy(&(modctx->song.signature), (void *)"M.K.", 4); |
| 1152 | memcopy(&(modctx->song.length), &(modctx->song.samples[15]), 130); |
| 1153 | memclear(&(modctx->song.samples[15]), 0, 480); |
| 1154 | modmemory += 600; |
| 1155 | modctx->number_of_channels = 4; |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | modmemory += 1084; |
| 1160 | } |
| 1161 | |
| 1162 | if( modmemory >= endmodmemory ) |
| 1163 | return 0; |
| 1164 | |
| 1165 | |
| 1166 | for (i = max = 0; i < 128; i++) |
| 1167 | { |
| 1168 | while (max <= modctx->song.patterntable[i]) |
| 1169 | { |
| 1170 | modctx->patterndata[max] = (note*)modmemory; |
| 1171 | modmemory += (256*modctx->number_of_channels); |
| 1172 | max++; |
| 1173 | |
| 1174 | if( modmemory >= endmodmemory ) |
| 1175 | return 0; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | for (i = 0; i < 31; i++) |
| 1180 | modctx->sampledata[i]=0; |
| 1181 | |
| 1182 | |
| 1183 | for (i = 0, sptr = modctx->song.samples; i <31; i++, sptr++) |
| 1184 | { |
| 1185 | t= (sptr->length &0xFF00)>>8 | (sptr->length &0xFF)<<8; |
| 1186 | sptr->length = t*2; |
| 1187 | |
| 1188 | t= (sptr->reppnt &0xFF00)>>8 | (sptr->reppnt &0xFF)<<8; |
| 1189 | sptr->reppnt = t*2; |
| 1190 | |
| 1191 | t= (sptr->replen &0xFF00)>>8 | (sptr->replen &0xFF)<<8; |
| 1192 | sptr->replen = t*2; |
| 1193 | |
| 1194 | |
| 1195 | if (sptr->length == 0) continue; |
| 1196 | |
| 1197 | modctx->sampledata[i] = (char*)modmemory; |
| 1198 | modmemory += sptr->length; |
| 1199 | |
| 1200 | if (sptr->replen + sptr->reppnt > sptr->length) |
| 1201 | sptr->replen = sptr->length - sptr->reppnt; |
| 1202 | |
| 1203 | if( modmemory > endmodmemory ) |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | modctx->tablepos = 0; |
| 1210 | modctx->patternpos = 0; |
| 1211 | modctx->song.speed = 6; |
| 1212 | modctx->bpm = 125; |
| 1213 | modctx->samplenb = 0; |
| 1214 | |
| 1215 | modctx->patternticks = (((long)modctx->song.speed * modctx->playrate * 5)/ (2 * modctx->bpm)) + 1; |
| 1216 | modctx->patternticksaim = ((long)modctx->song.speed * modctx->playrate * 5) / (2 * modctx->bpm); |
| 1217 | |
| 1218 | modctx->sampleticksconst = 3546894UL / modctx->playrate; |
| 1219 | |
| 1220 | for(i=0; i < modctx->number_of_channels; i++) |
| 1221 | { |
| 1222 | modctx->channels[i].volume = 0; |
| 1223 | modctx->channels[i].period = 0; |
| 1224 | } |
| 1225 | |
| 1226 | modctx->mod_loaded = 1; |
| 1227 | |
| 1228 | return 1; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | void jar_mod_fillbuffer( jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf ) |
| 1236 | { |
| 1237 | unsigned long i, j; |
| 1238 | unsigned long k; |
| 1239 | unsigned char c; |
| 1240 | unsigned int state_remaining_steps; |
| 1241 | int l,r; |
| 1242 | int ll,lr; |
| 1243 | int tl,tr; |
| 1244 | short finalperiod; |
| 1245 | note *nptr; |
| 1246 | channel *cptr; |
| 1247 | |
| 1248 | if( modctx && outbuffer ) |
| 1249 | { |
| 1250 | if(modctx->mod_loaded) |
| 1251 | { |
| 1252 | state_remaining_steps = 0; |
| 1253 | |
| 1254 | if( trkbuf ) |
| 1255 | { |
| 1256 | trkbuf->cur_rd_index = 0; |
| 1257 | |
| 1258 | memcopy(trkbuf->name,modctx->song.title,sizeof(modctx->song.title)); |
| 1259 | |
| 1260 | for(i=0;i<31;i++) |
| 1261 | { |
| 1262 | memcopy(trkbuf->instruments[i].name,modctx->song.samples[i].name,sizeof(trkbuf->instruments[i].name)); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | ll = modctx->last_l_sample; |
| 1267 | lr = modctx->last_r_sample; |
| 1268 | |
| 1269 | for (i = 0; i < nbsample; i++) |
| 1270 | { |
| 1271 | |
| 1272 | if( modctx->patternticks++ > modctx->patternticksaim ) |
| 1273 | { |
| 1274 | if( !modctx->patterndelay ) |
| 1275 | { |
| 1276 | nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]]; |
| 1277 | nptr = nptr + modctx->patternpos; |
| 1278 | cptr = modctx->channels; |
| 1279 | |
| 1280 | modctx->patternticks = 0; |
| 1281 | modctx->patterntickse = 0; |
| 1282 | |
| 1283 | for(c=0;c<modctx->number_of_channels;c++) |
| 1284 | { |
| 1285 | worknote((note*)(nptr+c), (channel*)(cptr+c),(char)(c+1),modctx); |
| 1286 | } |
| 1287 | |
| 1288 | if( !modctx->jump_loop_effect ) |
| 1289 | modctx->patternpos += modctx->number_of_channels; |
| 1290 | else |
| 1291 | modctx->jump_loop_effect = 0; |
| 1292 | |
| 1293 | if( modctx->patternpos == 64*modctx->number_of_channels ) |
| 1294 | { |
| 1295 | modctx->tablepos++; |
| 1296 | modctx->patternpos = 0; |
| 1297 | if(modctx->tablepos >= modctx->song.length) |
| 1298 | { |
| 1299 | modctx->tablepos = 0; |
| 1300 | modctx->loopcount++; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | else |
| 1305 | { |
| 1306 | modctx->patterndelay--; |
| 1307 | modctx->patternticks = 0; |
| 1308 | modctx->patterntickse = 0; |
| 1309 | } |
| 1310 | |
| 1311 | } |
| 1312 | |
| 1313 | if( modctx->patterntickse++ > (modctx->patternticksaim/modctx->song.speed) ) |
| 1314 | { |
| 1315 | nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]]; |
| 1316 | nptr = nptr + modctx->patternpos; |
| 1317 | cptr = modctx->channels; |
| 1318 | |
| 1319 | for(c=0;c<modctx->number_of_channels;c++) |
| 1320 | { |
| 1321 | workeffect(nptr+c, cptr+c); |
| 1322 | } |
| 1323 | |
| 1324 | modctx->patterntickse = 0; |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | if( trkbuf && !state_remaining_steps ) |
| 1330 | { |
| 1331 | if( trkbuf->nb_of_state < trkbuf->nb_max_of_state ) |
| 1332 | { |
| 1333 | memclear(&trkbuf->track_state_buf[trkbuf->nb_of_state], 0, sizeof(tracker_state)); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | l=0; |
| 1338 | r=0; |
| 1339 | |
| 1340 | for(j =0, cptr = modctx->channels; j < modctx->number_of_channels ; j++, cptr++) |
| 1341 | { |
| 1342 | if( cptr->period != 0 ) |
| 1343 | { |
| 1344 | finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod; |
| 1345 | if( finalperiod ) |
| 1346 | { |
| 1347 | cptr->samppos += ( (modctx->sampleticksconst<<10) / finalperiod ); |
| 1348 | } |
| 1349 | |
| 1350 | cptr->ticks++; |
| 1351 | |
| 1352 | if( cptr->replen<=2 ) |
| 1353 | { |
| 1354 | if( (cptr->samppos>>10) >= (cptr->length) ) |
| 1355 | { |
| 1356 | cptr->length = 0; |
| 1357 | cptr->reppnt = 0; |
| 1358 | |
| 1359 | if( cptr->length ) |
| 1360 | cptr->samppos = cptr->samppos % (((unsigned long)cptr->length)<<10); |
| 1361 | else |
| 1362 | cptr->samppos = 0; |
| 1363 | } |
| 1364 | } |
| 1365 | else |
| 1366 | { |
| 1367 | if( (cptr->samppos>>10) >= (unsigned long)(cptr->replen+cptr->reppnt) ) |
| 1368 | { |
| 1369 | cptr->samppos = ((unsigned long)(cptr->reppnt)<<10) + (cptr->samppos % ((unsigned long)(cptr->replen+cptr->reppnt)<<10)); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | k = cptr->samppos >> 10; |
| 1374 | |
| 1375 | if( cptr->sampdata!=0 && ( ((j&3)==1) || ((j&3)==2) ) ) |
| 1376 | { |
| 1377 | r += ( cptr->sampdata[k] * cptr->volume ); |
| 1378 | } |
| 1379 | |
| 1380 | if( cptr->sampdata!=0 && ( ((j&3)==0) || ((j&3)==3) ) ) |
| 1381 | { |
| 1382 | l += ( cptr->sampdata[k] * cptr->volume ); |
| 1383 | } |
| 1384 | |
| 1385 | if( trkbuf && !state_remaining_steps ) |
| 1386 | { |
| 1387 | if( trkbuf->nb_of_state < trkbuf->nb_max_of_state ) |
| 1388 | { |
| 1389 | trkbuf->track_state_buf[trkbuf->nb_of_state].number_of_tracks = modctx->number_of_channels; |
| 1390 | trkbuf->track_state_buf[trkbuf->nb_of_state].buf_index = i; |
| 1391 | trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern = modctx->song.patterntable[modctx->tablepos]; |
| 1392 | trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_pos = modctx->patternpos / modctx->number_of_channels; |
| 1393 | trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_table_pos = modctx->tablepos; |
| 1394 | trkbuf->track_state_buf[trkbuf->nb_of_state].bpm = modctx->bpm; |
| 1395 | trkbuf->track_state_buf[trkbuf->nb_of_state].speed = modctx->song.speed; |
| 1396 | trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_effect = cptr->effect_code; |
| 1397 | trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_parameffect = cptr->parameffect; |
| 1398 | trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = finalperiod; |
| 1399 | trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_volume = cptr->volume; |
| 1400 | trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].instrument_number = (unsigned char)cptr->sampnum; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | if( trkbuf && !state_remaining_steps ) |
| 1407 | { |
| 1408 | state_remaining_steps = trkbuf->sample_step; |
| 1409 | |
| 1410 | if(trkbuf->nb_of_state < trkbuf->nb_max_of_state) |
| 1411 | trkbuf->nb_of_state++; |
| 1412 | } |
| 1413 | else |
| 1414 | { |
| 1415 | state_remaining_steps--; |
| 1416 | } |
| 1417 | |
| 1418 | tl = (short)l; |
| 1419 | tr = (short)r; |
| 1420 | |
| 1421 | if ( modctx->filter ) |
| 1422 | { |
| 1423 | |
| 1424 | l = (l+ll)>>1; |
| 1425 | r = (r+lr)>>1; |
| 1426 | } |
| 1427 | |
| 1428 | if ( modctx->stereo_separation == 1 ) |
| 1429 | { |
| 1430 | |
| 1431 | l = (l+(r>>1)); |
| 1432 | r = (r+(l>>1)); |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | if( l > 32767 ) l = 32767; |
| 1437 | if( l < -32768 ) l = -32768; |
| 1438 | if( r > 32767 ) r = 32767; |
| 1439 | if( r < -32768 ) r = -32768; |
| 1440 | |
| 1441 | |
| 1442 | outbuffer[(i*2)] = l; |
| 1443 | outbuffer[(i*2)+1] = r; |
| 1444 | |
| 1445 | ll = tl; |
| 1446 | lr = tr; |
| 1447 | |
| 1448 | } |
| 1449 | |
| 1450 | modctx->last_l_sample = ll; |
| 1451 | modctx->last_r_sample = lr; |
| 1452 | |
| 1453 | modctx->samplenb = modctx->samplenb+nbsample; |
| 1454 | } |
| 1455 | else |
| 1456 | { |
| 1457 | for (i = 0; i < nbsample; i++) |
| 1458 | { |
| 1459 | |
| 1460 | outbuffer[(i*2)] = 0; |
| 1461 | outbuffer[(i*2)+1] = 0; |
| 1462 | } |
| 1463 | |
| 1464 | if(trkbuf) |
| 1465 | { |
| 1466 | trkbuf->nb_of_state = 0; |
| 1467 | trkbuf->cur_rd_index = 0; |
| 1468 | trkbuf->name[0] = 0; |
| 1469 | memclear(trkbuf->track_state_buf, 0, sizeof(tracker_state) * trkbuf->nb_max_of_state); |
| 1470 | memclear(trkbuf->instruments, 0, sizeof(trkbuf->instruments)); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | static bool jar_mod_reset( jar_mod_context_t * modctx) |
| 1478 | { |
| 1479 | if(modctx) |
| 1480 | { |
| 1481 | memclear(&modctx->song, 0, sizeof(modctx->song)); |
| 1482 | memclear(&modctx->sampledata, 0, sizeof(modctx->sampledata)); |
| 1483 | memclear(&modctx->patterndata, 0, sizeof(modctx->patterndata)); |
| 1484 | modctx->tablepos = 0; |
| 1485 | modctx->patternpos = 0; |
| 1486 | modctx->patterndelay = 0; |
| 1487 | modctx->jump_loop_effect = 0; |
| 1488 | modctx->bpm = 0; |
| 1489 | modctx->patternticks = 0; |
| 1490 | modctx->patterntickse = 0; |
| 1491 | modctx->patternticksaim = 0; |
| 1492 | modctx->sampleticksconst = 0; |
| 1493 | modctx->samplenb = 0; |
| 1494 | memclear(modctx->channels, 0, sizeof(modctx->channels)); |
| 1495 | modctx->number_of_channels = 0; |
| 1496 | modctx->mod_loaded = 0; |
| 1497 | modctx->last_r_sample = 0; |
| 1498 | modctx->last_l_sample = 0; |
| 1499 | |
| 1500 | return jar_mod_init(modctx); |
| 1501 | } |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | void jar_mod_unload( jar_mod_context_t * modctx) |
| 1506 | { |
| 1507 | if(modctx) |
| 1508 | { |
| 1509 | if(modctx->modfile) |
| 1510 | { |
| 1511 | JARMOD_FREE(modctx->modfile); |
| 1512 | modctx->modfile = 0; |
| 1513 | modctx->modfilesize = 0; |
| 1514 | modctx->loopcount = 0; |
| 1515 | } |
| 1516 | jar_mod_reset(modctx); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename) |
| 1521 | { |
| 1522 | mulong fsize = 0; |
| 1523 | if(modctx->modfile) |
| 1524 | { |
| 1525 | JARMOD_FREE(modctx->modfile); |
| 1526 | modctx->modfile = 0; |
| 1527 | } |
| 1528 | |
| 1529 | FILE *f = fopen(filename, "rb"); |
| 1530 | if(f) |
| 1531 | { |
| 1532 | fseek(f,0,SEEK_END); |
| 1533 | fsize = ftell(f); |
| 1534 | fseek(f,0,SEEK_SET); |
| 1535 | |
| 1536 | if(fsize && fsize < 32*1024*1024) |
| 1537 | { |
| 1538 | modctx->modfile = (muchar *) JARMOD_MALLOC(fsize); |
| 1539 | modctx->modfilesize = fsize; |
| 1540 | memset(modctx->modfile, 0, fsize); |
| 1541 | fread(modctx->modfile, fsize, 1, f); |
| 1542 | fclose(f); |
| 1543 | |
| 1544 | if(!jar_mod_load(modctx, (void *)modctx->modfile, fsize)) fsize = 0; |
| 1545 | } else fsize = 0; |
| 1546 | } |
| 1547 | return fsize; |
| 1548 | } |
| 1549 | |
| 1550 | mulong jar_mod_current_samples(jar_mod_context_t * modctx) |
| 1551 | { |
| 1552 | if(modctx) |
| 1553 | return modctx->samplenb; |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | mulong jar_mod_max_samples(jar_mod_context_t * ctx) |
| 1560 | { |
| 1561 | mint buff[2]; |
| 1562 | mulong len; |
| 1563 | mulong lastcount = ctx->loopcount; |
| 1564 | |
| 1565 | while(ctx->loopcount <= lastcount) |
| 1566 | jar_mod_fillbuffer(ctx, buff, 1, 0); |
| 1567 | |
| 1568 | len = ctx->samplenb; |
| 1569 | jar_mod_seek_start(ctx); |
| 1570 | |
| 1571 | return len; |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | void jar_mod_seek_start(jar_mod_context_t * ctx) |
| 1576 | { |
| 1577 | if(ctx && ctx->modfile) |
| 1578 | { |
| 1579 | muchar* ftmp = ctx->modfile; |
| 1580 | mulong stmp = ctx->modfilesize; |
| 1581 | muint lcnt = ctx->loopcount; |
| 1582 | |
| 1583 | if(jar_mod_reset(ctx)){ |
| 1584 | jar_mod_load(ctx, ftmp, stmp); |
| 1585 | ctx->modfile = ftmp; |
| 1586 | ctx->modfilesize = stmp; |
| 1587 | ctx->loopcount = lcnt; |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | #endif |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | #endif |