1 /////////////////////////////////////////////////////////////////////////////////// 2 //-------------------------------------------------------------------------------// 3 //-------------------------------------------------------------------------------// 4 //-----------H----H--X----X-----CCCCC----22222----0000-----0000------11----------// 5 //----------H----H----X-X-----C--------------2---0----0---0----0--1--1-----------// 6 //---------HHHHHH-----X------C----------22222---0----0---0----0-----1------------// 7 //--------H----H----X--X----C----------2-------0----0---0----0-----1-------------// 8 //-------H----H---X-----X---CCCCC-----222222----0000-----0000----1111------------// 9 //-------------------------------------------------------------------------------// 10 //----------------------------------------------------- http://hxc2001.free.fr --// 11 /////////////////////////////////////////////////////////////////////////////////// 12 // File : hxcmod.c 13 // Contains: a tiny mod player 14 // 15 // Written by: Jean Fran�ois DEL NERO 16 // 17 // You are free to do what you want with this code. 18 // A credit is always appreciated if you include it into your prod :) 19 // 20 // This file include some parts of the Noisetracker/Soundtracker/Protracker 21 // Module Format documentation written by Andrew Scott (Adrenalin Software) 22 // (modformat.txt) 23 // 24 // The core (hxcmod.c/hxcmod.h) is designed to have the least external dependency. 25 // So it should be usable on almost all OS and systems. 26 // Please also note that no dynamic allocation is done into the HxCMOD core. 27 // 28 // Change History (most recent first): 29 /////////////////////////////////////////////////////////////////////////////////// 30 // HxCMOD Core API: 31 // ------------------------------------------- 32 // int hxcmod_init(modcontext * modctx) 33 // 34 // - Initialize the modcontext buffer. Must be called before doing anything else. 35 // Return 1 if success. 0 in case of error. 36 // ------------------------------------------- 37 // int hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size ) 38 // 39 // - "Load" a MOD from memory (from "mod_data" with size "mod_data_size"). 40 // Return 1 if success. 0 in case of error. 41 // ------------------------------------------- 42 // void hxcmod_fillbuffer( modcontext * modctx, unsigned short * outbuffer, unsigned long nbsample, tracker_buffer_state * trkbuf ) 43 // 44 // - Generate and return the next samples chunk to outbuffer. 45 // nbsample specify the number of stereo 16bits samples you want. 46 // The output format is signed 44100Hz 16-bit Stereo PCM samples. 47 // The output buffer size in byte must be equal to ( nbsample * 2 * 2 ). 48 // The optional trkbuf parameter can be used to get detailed status of the player. Put NULL/0 is unused. 49 // ------------------------------------------- 50 // void hxcmod_unload( modcontext * modctx ) 51 // - "Unload" / clear the player status. 52 // ------------------------------------------- 53 /////////////////////////////////////////////////////////////////////////////////// 54 55 #include "hxcmod.h" 56 57 /////////////////////////////////////////////////////////////////////////////////// 58 59 // Effects list 60 #define EFFECT_ARPEGGIO 0x0 // Supported 61 #define EFFECT_PORTAMENTO_UP 0x1 // Supported 62 #define EFFECT_PORTAMENTO_DOWN 0x2 // Supported 63 #define EFFECT_TONE_PORTAMENTO 0x3 // Supported 64 #define EFFECT_VIBRATO 0x4 // Supported 65 #define EFFECT_VOLSLIDE_TONEPORTA 0x5 // Supported 66 #define EFFECT_VOLSLIDE_VIBRATO 0x6 // Supported 67 #define EFFECT_VOLSLIDE_TREMOLO 0x7 // - TO BE DONE - 68 #define EFFECT_SET_PANNING 0x8 // - TO BE DONE - 69 #define EFFECT_SET_OFFSET 0x9 // Supported 70 #define EFFECT_VOLUME_SLIDE 0xA // Supported 71 #define EFFECT_JUMP_POSITION 0xB // Supported 72 #define EFFECT_SET_VOLUME 0xC // Supported 73 #define EFFECT_PATTERN_BREAK 0xD // Supported 74 75 #define EFFECT_EXTENDED 0xE 76 #define EFFECT_E_FINE_PORTA_UP 0x1 // Supported 77 #define EFFECT_E_FINE_PORTA_DOWN 0x2 // Supported 78 #define EFFECT_E_GLISSANDO_CTRL 0x3 // - TO BE DONE - 79 #define EFFECT_E_VIBRATO_WAVEFORM 0x4 // - TO BE DONE - 80 #define EFFECT_E_SET_FINETUNE 0x5 // - TO BE DONE - 81 #define EFFECT_E_PATTERN_LOOP 0x6 // Supported 82 #define EFFECT_E_TREMOLO_WAVEFORM 0x7 // - TO BE DONE - 83 #define EFFECT_E_SET_PANNING_2 0x8 // - TO BE DONE - 84 #define EFFECT_E_RETRIGGER_NOTE 0x9 // - TO BE DONE - 85 #define EFFECT_E_FINE_VOLSLIDE_UP 0xA // Supported 86 #define EFFECT_E_FINE_VOLSLIDE_DOWN 0xB // Supported 87 #define EFFECT_E_NOTE_CUT 0xC // Supported 88 #define EFFECT_E_NOTE_DELAY 0xD // - TO BE DONE - 89 #define EFFECT_E_PATTERN_DELAY 0xE // Supported 90 #define EFFECT_E_INVERT_LOOP 0xF // - TO BE DONE - 91 #define EFFECT_SET_SPEED 0xF0 // Supported 92 #define EFFECT_SET_TEMPO 0xF2 // Supported 93 94 #define PERIOD_TABLE_LENGTH MAXNOTES 95 #define FULL_PERIOD_TABLE_LENGTH ( PERIOD_TABLE_LENGTH * 8 ) 96 97 static const short periodtable[]= 98 { 99 27392, 25856, 24384, 23040, 21696, 20480, 19328, 18240, 17216, 16256, 15360, 14496, 100 13696, 12928, 12192, 11520, 10848, 10240, 9664, 9120, 8606, 8128, 7680, 7248, 101 6848, 6464, 6096, 5760, 5424, 5120, 4832, 4560, 4304, 4064, 3840, 3624, 102 3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1812, 103 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906, 104 856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453, 105 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226, 106 214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113, 107 107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56, 108 53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28, 109 27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 110 13, 13, 12, 11, 11, 10, 9, 9, 8, 8, 7, 7 111 }; 112 113 static const short sintable[]={ 114 0, 24, 49, 74, 97, 120, 141,161, 115 180, 197, 212, 224, 235, 244, 250,253, 116 255, 253, 250, 244, 235, 224, 212,197, 117 180, 161, 141, 120, 97, 74, 49, 24 118 }; 119 120 typedef struct modtype_ 121 { 122 unsigned char signature[5]; 123 int numberofchannels; 124 }modtype; 125 126 modtype modlist[]= 127 { 128 { "M!K!",4}, 129 { "M.K.",4}, 130 { "FLT4",4}, 131 { "FLT8",8}, 132 { "4CHN",4}, 133 { "6CHN",6}, 134 { "8CHN",8}, 135 { "10CH",10}, 136 { "12CH",12}, 137 { "14CH",14}, 138 { "16CH",16}, 139 { "18CH",18}, 140 { "20CH",20}, 141 { "22CH",22}, 142 { "24CH",24}, 143 { "26CH",26}, 144 { "28CH",28}, 145 { "30CH",30}, 146 { "32CH",32}, 147 { "",0} 148 }; 149 150 /////////////////////////////////////////////////////////////////////////////////// 151 152 static void memcopy( void * dest, void *source, unsigned long size ) 153 { 154 unsigned long i; 155 unsigned char * d,*s; 156 157 d=(unsigned char*)dest; 158 s=(unsigned char*)source; 159 for(i=0;i<size;i++) 160 { 161 d[i]=s[i]; 162 } 163 } 164 165 static void memclear( void * dest, unsigned char value, unsigned long size ) 166 { 167 unsigned long i; 168 unsigned char * d; 169 170 d=(unsigned char*)dest; 171 for(i=0;i<size;i++) 172 { 173 d[i]=value; 174 } 175 } 176 177 static int memcompare( unsigned char * buf1, unsigned char * buf2, unsigned int size ) 178 { 179 unsigned int i; 180 181 i = 0; 182 183 while(i<size) 184 { 185 if(buf1[i] != buf2[i]) 186 { 187 return 0; 188 } 189 i++; 190 } 191 192 return 1; 193 } 194 195 static int getnote( modcontext * mod, unsigned short period, int finetune ) 196 { 197 int i; 198 199 for(i = 0; i < FULL_PERIOD_TABLE_LENGTH; i++) 200 { 201 if(period >= mod->fullperiod[i]) 202 { 203 return i; 204 } 205 } 206 207 return MAXNOTES; 208 } 209 210 static void worknote( note * nptr, channel * cptr,char t,modcontext * mod ) 211 { 212 muint sample, period, effect, operiod; 213 muint curnote, arpnote; 214 215 sample = (nptr->sampperiod & 0xF0) | (nptr->sampeffect >> 4); 216 period = ((nptr->sampperiod & 0xF) << 8) | nptr->period; 217 effect = ((nptr->sampeffect & 0xF) << 8) | nptr->effect; 218 219 operiod = cptr->period; 220 221 if ( period || sample ) 222 { 223 if( sample && sample<32 ) 224 { 225 cptr->sampnum = sample - 1; 226 } 227 228 if( period || sample ) 229 { 230 cptr->sampdata =(char *) mod->sampledata[cptr->sampnum]; 231 cptr->length = mod->song.samples[cptr->sampnum].length; 232 cptr->reppnt = mod->song.samples[cptr->sampnum].reppnt; 233 cptr->replen = mod->song.samples[cptr->sampnum].replen; 234 235 cptr->finetune = (mod->song.samples[cptr->sampnum].finetune)&0xF; 236 237 if(effect>>8!=4 && effect>>8!=6) 238 { 239 cptr->vibraperiod=0; 240 cptr->vibrapointeur=0; 241 } 242 } 243 244 if( (sample != 0) && ( (effect>>8) != EFFECT_VOLSLIDE_TONEPORTA ) ) 245 { 246 cptr->volume = mod->song.samples[cptr->sampnum].volume; 247 cptr->volumeslide = 0; 248 } 249 250 if( ( (effect>>8) != EFFECT_TONE_PORTAMENTO && (effect>>8)!=EFFECT_VOLSLIDE_TONEPORTA) ) 251 { 252 if (period!=0) 253 cptr->samppos = 0; 254 } 255 256 cptr->decalperiod=0; 257 if( period ) 258 { 259 if(cptr->finetune) 260 { 261 if( cptr->finetune <= 7 ) 262 { 263 period = mod->fullperiod[getnote(mod,period,0) + cptr->finetune]; 264 } 265 else 266 { 267 period = mod->fullperiod[getnote(mod,period,0) - (16 - (cptr->finetune)) ]; 268 } 269 } 270 271 cptr->period = period; 272 } 273 274 } 275 276 cptr->effect = 0; 277 cptr->parameffect = 0; 278 cptr->effect_code = effect; 279 280 switch (effect >> 8) 281 { 282 case EFFECT_ARPEGGIO: 283 /* 284 [0]: Arpeggio 285 Where [0][x][y] means "play note, note+x semitones, note+y 286 semitones, then return to original note". The fluctuations are 287 carried out evenly spaced in one pattern division. They are usually 288 used to simulate chords, but this doesn't work too well. They are 289 also used to produce heavy vibrato. A major chord is when x=4, y=7. 290 A minor chord is when x=3, y=7. 291 */ 292 293 if(effect&0xff) 294 { 295 cptr->effect = EFFECT_ARPEGGIO; 296 cptr->parameffect = effect&0xff; 297 298 cptr->ArpIndex = 0; 299 300 curnote = getnote(mod,cptr->period,cptr->finetune); 301 302 cptr->Arpperiods[0] = cptr->period; 303 304 arpnote = curnote + (((cptr->parameffect>>4)&0xF)*8); 305 if( arpnote >= FULL_PERIOD_TABLE_LENGTH ) 306 arpnote = FULL_PERIOD_TABLE_LENGTH - 1; 307 308 cptr->Arpperiods[1] = mod->fullperiod[arpnote]; 309 310 arpnote = curnote + (((cptr->parameffect)&0xF)*8); 311 if( arpnote >= FULL_PERIOD_TABLE_LENGTH ) 312 arpnote = FULL_PERIOD_TABLE_LENGTH - 1; 313 314 cptr->Arpperiods[2] = mod->fullperiod[arpnote]; 315 } 316 break; 317 318 case EFFECT_PORTAMENTO_UP: 319 /* 320 [1]: Slide up 321 Where [1][x][y] means "smoothly decrease the period of current 322 sample by x*16+y after each tick in the division". The 323 ticks/division are set with the 'set speed' effect (see below). If 324 the period of the note being played is z, then the final period 325 will be z - (x*16 + y)*(ticks - 1). As the slide rate depends on 326 the speed, changing the speed will change the slide. You cannot 327 slide beyond the note B3 (period 113). 328 */ 329 330 cptr->effect = EFFECT_PORTAMENTO_UP; 331 cptr->parameffect = effect&0xff; 332 break; 333 334 case EFFECT_PORTAMENTO_DOWN: 335 /* 336 [2]: Slide down 337 Where [2][x][y] means "smoothly increase the period of current 338 sample by x*16+y after each tick in the division". Similar to [1], 339 but lowers the pitch. You cannot slide beyond the note C1 (period 340 856). 341 */ 342 343 cptr->effect = EFFECT_PORTAMENTO_DOWN; 344 cptr->parameffect = effect&0xff; 345 break; 346 347 case EFFECT_TONE_PORTAMENTO: 348 /* 349 [3]: Slide to note 350 Where [3][x][y] means "smoothly change the period of current sample 351 by x*16+y after each tick in the division, never sliding beyond 352 current period". The period-length in this channel's division is a 353 parameter to this effect, and hence is not played. Sliding to a 354 note is similar to effects [1] and [2], but the slide will not go 355 beyond the given period, and the direction is implied by that 356 period. If x and y are both 0, then the old slide will continue. 357 */ 358 359 cptr->effect = EFFECT_TONE_PORTAMENTO; 360 if( (effect&0xff) != 0 ) 361 { 362 cptr->portaspeed = (short)(effect&0xff); 363 } 364 365 if(period!=0) 366 { 367 cptr->portaperiod = period; 368 cptr->period = operiod; 369 } 370 break; 371 372 case EFFECT_VIBRATO: 373 /* 374 [4]: Vibrato 375 Where [4][x][y] means "oscillate the sample pitch using a 376 particular waveform with amplitude y/16 semitones, such that (x * 377 ticks)/64 cycles occur in the division". The waveform is set using 378 effect [14][4]. By placing vibrato effects on consecutive 379 divisions, the vibrato effect can be maintained. If either x or y 380 are 0, then the old vibrato values will be used. 381 */ 382 383 cptr->effect = EFFECT_VIBRATO; 384 if( ( effect & 0x0F ) != 0 ) // Depth continue or change ? 385 cptr->vibraparam = (cptr->vibraparam & 0xF0) | ( effect & 0x0F ); 386 if( ( effect & 0xF0 ) != 0 ) // Speed continue or change ? 387 cptr->vibraparam = (cptr->vibraparam & 0x0F) | ( effect & 0xF0 ); 388 389 break; 390 391 case EFFECT_VOLSLIDE_TONEPORTA: 392 /* 393 [5]: Continue 'Slide to note', but also do Volume slide 394 Where [5][x][y] means "either slide the volume up x*(ticks - 1) or 395 slide the volume down y*(ticks - 1), at the same time as continuing 396 the last 'Slide to note'". It is illegal for both x and y to be 397 non-zero. You cannot slide outside the volume range 0..64. The 398 period-length in this channel's division is a parameter to this 399 effect, and hence is not played. 400 */ 401 402 if( period != 0 ) 403 { 404 cptr->portaperiod = period; 405 cptr->period = operiod; 406 } 407 408 cptr->effect = EFFECT_VOLSLIDE_TONEPORTA; 409 if( ( effect & 0xFF ) != 0 ) 410 cptr->volumeslide = ( effect & 0xFF ); 411 412 break; 413 414 case EFFECT_VOLSLIDE_VIBRATO: 415 /* 416 [6]: Continue 'Vibrato', but also do Volume slide 417 Where [6][x][y] means "either slide the volume up x*(ticks - 1) or 418 slide the volume down y*(ticks - 1), at the same time as continuing 419 the last 'Vibrato'". It is illegal for both x and y to be non-zero. 420 You cannot slide outside the volume range 0..64. 421 */ 422 423 cptr->effect = EFFECT_VOLSLIDE_VIBRATO; 424 if( (effect & 0xFF) != 0 ) 425 cptr->volumeslide = (effect & 0xFF); 426 break; 427 428 case EFFECT_SET_OFFSET: 429 /* 430 [9]: Set sample offset 431 Where [9][x][y] means "play the sample from offset x*4096 + y*256". 432 The offset is measured in words. If no sample is given, yet one is 433 still playing on this channel, it should be retriggered to the new 434 offset using the current volume. 435 */ 436 437 cptr->samppos = ((effect>>4) * 4096) + ((effect&0xF)*256); 438 439 break; 440 441 case EFFECT_VOLUME_SLIDE: 442 /* 443 [10]: Volume slide 444 Where [10][x][y] means "either slide the volume up x*(ticks - 1) or 445 slide the volume down y*(ticks - 1)". If both x and y are non-zero, 446 then the y value is ignored (assumed to be 0). You cannot slide 447 outside the volume range 0..64. 448 */ 449 450 cptr->effect = EFFECT_VOLUME_SLIDE; 451 cptr->volumeslide = (effect & 0xFF); 452 break; 453 454 case EFFECT_JUMP_POSITION: 455 /* 456 [11]: Position Jump 457 Where [11][x][y] means "stop the pattern after this division, and 458 continue the song at song-position x*16+y". This shifts the 459 'pattern-cursor' in the pattern table (see above). Legal values for 460 x*16+y are from 0 to 127. 461 */ 462 463 mod->tablepos = (effect & 0xFF); 464 if(mod->tablepos >= mod->song.length) 465 mod->tablepos = 0; 466 mod->patternpos = 0; 467 mod->jump_loop_effect = 1; 468 469 break; 470 471 case EFFECT_SET_VOLUME: 472 /* 473 [12]: Set volume 474 Where [12][x][y] means "set current sample's volume to x*16+y". 475 Legal volumes are 0..64. 476 */ 477 478 cptr->volume = (effect & 0xFF); 479 break; 480 481 case EFFECT_PATTERN_BREAK: 482 /* 483 [13]: Pattern Break 484 Where [13][x][y] means "stop the pattern after this division, and 485 continue the song at the next pattern at division x*10+y" (the 10 486 is not a typo). Legal divisions are from 0 to 63 (note Protracker 487 exception above). 488 */ 489 490 mod->patternpos = ( ((effect>>4)&0xF)*10 + (effect&0xF) ) * mod->number_of_channels; 491 mod->jump_loop_effect = 1; 492 mod->tablepos++; 493 if(mod->tablepos >= mod->song.length) 494 mod->tablepos = 0; 495 496 break; 497 498 case EFFECT_EXTENDED: 499 switch( (effect>>4) & 0xF ) 500 { 501 case EFFECT_E_FINE_PORTA_UP: 502 /* 503 [14][1]: Fineslide up 504 Where [14][1][x] means "decrement the period of the current sample 505 by x". The incrementing takes place at the beginning of the 506 division, and hence there is no actual sliding. You cannot slide 507 beyond the note B3 (period 113). 508 */ 509 510 cptr->period -= (effect & 0xF); 511 if( cptr->period < 113 ) 512 cptr->period = 113; 513 break; 514 515 case EFFECT_E_FINE_PORTA_DOWN: 516 /* 517 [14][2]: Fineslide down 518 Where [14][2][x] means "increment the period of the current sample 519 by x". Similar to [14][1] but shifts the pitch down. You cannot 520 slide beyond the note C1 (period 856). 521 */ 522 523 cptr->period += (effect & 0xF); 524 if( cptr->period > 856 ) 525 cptr->period = 856; 526 break; 527 528 case EFFECT_E_FINE_VOLSLIDE_UP: 529 /* 530 [14][10]: Fine volume slide up 531 Where [14][10][x] means "increment the volume of the current sample 532 by x". The incrementing takes place at the beginning of the 533 division, and hence there is no sliding. You cannot slide beyond 534 volume 64. 535 */ 536 537 cptr->volume += (effect & 0xF); 538 if( cptr->volume>64 ) 539 cptr->volume = 64; 540 break; 541 542 case EFFECT_E_FINE_VOLSLIDE_DOWN: 543 /* 544 [14][11]: Fine volume slide down 545 Where [14][11][x] means "decrement the volume of the current sample 546 by x". Similar to [14][10] but lowers volume. You cannot slide 547 beyond volume 0. 548 */ 549 550 cptr->volume -= (effect & 0xF); 551 if( cptr->volume > 200 ) 552 cptr->volume = 0; 553 break; 554 555 case EFFECT_E_PATTERN_LOOP: 556 /* 557 [14][6]: Loop pattern 558 Where [14][6][x] means "set the start of a loop to this division if 559 x is 0, otherwise after this division, jump back to the start of a 560 loop and play it another x times before continuing". If the start 561 of the loop was not set, it will default to the start of the 562 current pattern. Hence 'loop pattern' cannot be performed across 563 multiple patterns. Note that loops do not support nesting, and you 564 may generate an infinite loop if you try to nest 'loop pattern's. 565 */ 566 567 if( effect & 0xF ) 568 { 569 if( cptr->patternloopcnt ) 570 { 571 cptr->patternloopcnt--; 572 if( cptr->patternloopcnt ) 573 { 574 mod->patternpos = cptr->patternloopstartpoint; 575 mod->jump_loop_effect = 1; 576 } 577 else 578 { 579 cptr->patternloopstartpoint = mod->patternpos ; 580 } 581 } 582 else 583 { 584 cptr->patternloopcnt = (effect & 0xF); 585 mod->patternpos = cptr->patternloopstartpoint; 586 mod->jump_loop_effect = 1; 587 } 588 } 589 else // Start point 590 { 591 cptr->patternloopstartpoint = mod->patternpos; 592 } 593 594 break; 595 596 case EFFECT_E_PATTERN_DELAY: 597 /* 598 [14][14]: Delay pattern 599 Where [14][14][x] means "after this division there will be a delay 600 equivalent to the time taken to play x divisions after which the 601 pattern will be resumed". The delay only relates to the 602 interpreting of new divisions, and all effects and previous notes 603 continue during delay. 604 */ 605 606 mod->patterndelay = (effect & 0xF); 607 break; 608 609 case EFFECT_E_NOTE_CUT: 610 /* 611 [14][12]: Cut sample 612 Where [14][12][x] means "after the current sample has been played 613 for x ticks in this division, its volume will be set to 0". This 614 implies that if x is 0, then you will not hear any of the sample. 615 If you wish to insert "silence" in a pattern, it is better to use a 616 "silence"-sample (see above) due to the lack of proper support for 617 this effect. 618 */ 619 cptr->effect = EFFECT_E_NOTE_CUT; 620 cptr->cut_param = (effect & 0xF); 621 if(!cptr->cut_param) 622 cptr->volume = 0; 623 break; 624 625 default: 626 627 break; 628 } 629 break; 630 631 case 0xF: 632 /* 633 [15]: Set speed 634 Where [15][x][y] means "set speed to x*16+y". Though it is nowhere 635 near that simple. Let z = x*16+y. Depending on what values z takes, 636 different units of speed are set, there being two: ticks/division 637 and beats/minute (though this one is only a label and not strictly 638 true). If z=0, then what should technically happen is that the 639 module stops, but in practice it is treated as if z=1, because 640 there is already a method for stopping the module (running out of 641 patterns). If z<=32, then it means "set ticks/division to z" 642 otherwise it means "set beats/minute to z" (convention says that 643 this should read "If z<32.." but there are some composers out there 644 that defy conventions). Default values are 6 ticks/division, and 645 125 beats/minute (4 divisions = 1 beat). The beats/minute tag is 646 only meaningful for 6 ticks/division. To get a more accurate view 647 of how things work, use the following formula: 648 24 * beats/minute 649 divisions/minute = ----------------- 650 ticks/division 651 Hence divisions/minute range from 24.75 to 6120, eg. to get a value 652 of 2000 divisions/minute use 3 ticks/division and 250 beats/minute. 653 If multiple "set speed" effects are performed in a single division, 654 the ones on higher-numbered channels take precedence over the ones 655 on lower-numbered channels. This effect has a large number of 656 different implementations, but the one described here has the 657 widest usage. 658 */ 659 660 if( (effect&0xFF) < 0x21 ) 661 { 662 if( effect&0xFF ) 663 { 664 mod->song.speed = effect&0xFF; 665 mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm))); 666 } 667 } 668 669 if( (effect&0xFF) >= 0x21 ) 670 { 671 /// HZ = 2 * BPM / 5 672 mod->bpm = effect&0xFF; 673 mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm))); 674 } 675 676 break; 677 678 default: 679 // Unsupported effect 680 break; 681 682 } 683 684 } 685 686 static void workeffect( note * nptr, channel * cptr ) 687 { 688 switch(cptr->effect) 689 { 690 case EFFECT_ARPEGGIO: 691 692 if( cptr->parameffect ) 693 { 694 cptr->decalperiod = cptr->period - cptr->Arpperiods[cptr->ArpIndex]; 695 696 cptr->ArpIndex++; 697 if( cptr->ArpIndex>2 ) 698 cptr->ArpIndex = 0; 699 } 700 break; 701 702 case EFFECT_PORTAMENTO_UP: 703 704 if(cptr->period) 705 { 706 cptr->period -= cptr->parameffect; 707 708 if( cptr->period < 113 || cptr->period > 20000 ) 709 cptr->period = 113; 710 } 711 712 break; 713 714 case EFFECT_PORTAMENTO_DOWN: 715 716 if(cptr->period) 717 { 718 cptr->period += cptr->parameffect; 719 720 if( cptr->period > 20000 ) 721 cptr->period = 20000; 722 } 723 724 break; 725 726 case EFFECT_VOLSLIDE_TONEPORTA: 727 case EFFECT_TONE_PORTAMENTO: 728 729 if( cptr->period && ( cptr->period != cptr->portaperiod ) && cptr->portaperiod ) 730 { 731 if( cptr->period > cptr->portaperiod ) 732 { 733 if( cptr->period - cptr->portaperiod >= cptr->portaspeed ) 734 { 735 cptr->period -= cptr->portaspeed; 736 } 737 else 738 { 739 cptr->period = cptr->portaperiod; 740 } 741 } 742 else 743 { 744 if( cptr->portaperiod - cptr->period >= cptr->portaspeed ) 745 { 746 cptr->period += cptr->portaspeed; 747 } 748 else 749 { 750 cptr->period = cptr->portaperiod; 751 } 752 } 753 754 if( cptr->period == cptr->portaperiod ) 755 { 756 // If the slide is over, don't let it to be retriggered. 757 cptr->portaperiod = 0; 758 } 759 } 760 761 if( cptr->effect == EFFECT_VOLSLIDE_TONEPORTA ) 762 { 763 if( cptr->volumeslide > 0x0F ) 764 { 765 cptr->volume = cptr->volume + (cptr->volumeslide>>4); 766 767 if(cptr->volume>63) 768 cptr->volume = 63; 769 } 770 else 771 { 772 cptr->volume = cptr->volume - (cptr->volumeslide); 773 774 if(cptr->volume>63) 775 cptr->volume=0; 776 } 777 } 778 break; 779 780 case EFFECT_VOLSLIDE_VIBRATO: 781 case EFFECT_VIBRATO: 782 783 cptr->vibraperiod = ( (cptr->vibraparam&0xF) * sintable[cptr->vibrapointeur&0x1F] )>>7; 784 785 if( cptr->vibrapointeur > 31 ) 786 cptr->vibraperiod = -cptr->vibraperiod; 787 788 cptr->vibrapointeur = (cptr->vibrapointeur+(((cptr->vibraparam>>4))&0xf)) & 0x3F; 789 790 if( cptr->effect == EFFECT_VOLSLIDE_VIBRATO ) 791 { 792 if( cptr->volumeslide > 0xF ) 793 { 794 cptr->volume = cptr->volume+(cptr->volumeslide>>4); 795 796 if( cptr->volume > 64 ) 797 cptr->volume = 64; 798 } 799 else 800 { 801 cptr->volume = cptr->volume - cptr->volumeslide; 802 803 if( cptr->volume > 64 ) 804 cptr->volume = 0; 805 } 806 } 807 808 break; 809 810 case EFFECT_VOLUME_SLIDE: 811 812 if( cptr->volumeslide > 0xF ) 813 { 814 cptr->volume += (cptr->volumeslide>>4); 815 816 if( cptr->volume > 64 ) 817 cptr->volume = 64; 818 } 819 else 820 { 821 cptr->volume -= (cptr->volumeslide&0xf); 822 823 if( cptr->volume > 64 ) 824 cptr->volume = 0; 825 } 826 break; 827 828 case EFFECT_E_NOTE_CUT: 829 if(cptr->cut_param) 830 cptr->cut_param--; 831 832 if(!cptr->cut_param) 833 cptr->volume = 0; 834 break; 835 836 default: 837 break; 838 839 } 840 841 } 842 843 /////////////////////////////////////////////////////////////////////////////////// 844 int hxcmod_init(modcontext * modctx) 845 { 846 muint i,j; 847 848 if( modctx ) 849 { 850 memclear(modctx,0,sizeof(modcontext)); 851 modctx->playrate = 44100; 852 modctx->stereo = 1; 853 modctx->stereo_separation = 1; 854 modctx->bits = 16; 855 modctx->filter = 1; 856 857 for(i=0;i<PERIOD_TABLE_LENGTH - 1;i++) 858 { 859 for(j=0;j<8;j++) 860 { 861 modctx->fullperiod[(i*8) + j] = periodtable[i] - ((( periodtable[i] - periodtable[i+1] ) / 8) * j); 862 } 863 } 864 865 return 1; 866 } 867 868 return 0; 869 } 870 871 int hxcmod_setcfg(modcontext * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter) 872 { 873 if( modctx ) 874 { 875 modctx->playrate = samplerate; 876 877 if( stereo ) 878 modctx->stereo = 1; 879 else 880 modctx->stereo = 0; 881 882 if(stereo_separation < 4) 883 { 884 modctx->stereo_separation = stereo_separation; 885 } 886 887 if( bits == 8 || bits == 16 ) 888 { 889 modctx->bits = bits; 890 } 891 892 if( filter ) 893 modctx->filter = 1; 894 else 895 modctx->filter = 0; 896 897 return 1; 898 } 899 900 return 0; 901 } 902 903 int hxcmod_load( modcontext * modctx, void * mod_data, int mod_data_size ) 904 { 905 muint i, max; 906 unsigned short t; 907 sample *sptr; 908 unsigned char * modmemory,* endmodmemory; 909 910 modmemory = (unsigned char *)mod_data; 911 endmodmemory = modmemory + mod_data_size; 912 913 if(modmemory) 914 { 915 if( modctx ) 916 { 917 memcopy(&(modctx->song.title),modmemory,1084); 918 919 i = 0; 920 modctx->number_of_channels = 0; 921 while(modlist[i].numberofchannels) 922 { 923 if(memcompare(modctx->song.signature,modlist[i].signature,4)) 924 { 925 modctx->number_of_channels = modlist[i].numberofchannels; 926 } 927 928 i++; 929 } 930 931 if( !modctx->number_of_channels ) 932 { 933 // 15 Samples modules support 934 // Shift the whole datas to make it look likes a standard 4 channels mod. 935 memcopy(&(modctx->song.signature), "M.K.", 4); 936 memcopy(&(modctx->song.length), &(modctx->song.samples[15]), 130); 937 memclear(&(modctx->song.samples[15]), 0, 480); 938 modmemory += 600; 939 modctx->number_of_channels = 4; 940 } 941 else 942 { 943 modmemory += 1084; 944 } 945 946 if( modmemory >= endmodmemory ) 947 return 0; // End passed ? - Probably a bad file ! 948 949 // Patterns loading 950 for (i = max = 0; i < 128; i++) 951 { 952 while (max <= modctx->song.patterntable[i]) 953 { 954 modctx->patterndata[max] = (note*)modmemory; 955 modmemory += (256*modctx->number_of_channels); 956 max++; 957 958 if( modmemory >= endmodmemory ) 959 return 0; // End passed ? - Probably a bad file ! 960 } 961 } 962 963 for (i = 0; i < 31; i++) 964 modctx->sampledata[i]=0; 965 966 // Samples loading 967 for (i = 0, sptr = modctx->song.samples; i <31; i++, sptr++) 968 { 969 t= (sptr->length &0xFF00)>>8 | (sptr->length &0xFF)<<8; 970 sptr->length = t*2; 971 972 t= (sptr->reppnt &0xFF00)>>8 | (sptr->reppnt &0xFF)<<8; 973 sptr->reppnt = t*2; 974 975 t= (sptr->replen &0xFF00)>>8 | (sptr->replen &0xFF)<<8; 976 sptr->replen = t*2; 977 978 979 if (sptr->length == 0) continue; 980 981 modctx->sampledata[i] = (char*)modmemory; 982 modmemory += sptr->length; 983 984 if (sptr->replen + sptr->reppnt > sptr->length) 985 sptr->replen = sptr->length - sptr->reppnt; 986 987 if( modmemory > endmodmemory ) 988 return 0; // End passed ? - Probably a bad file ! 989 } 990 991 // States init 992 993 modctx->tablepos = 0; 994 modctx->patternpos = 0; 995 modctx->song.speed = 6; 996 modctx->bpm = 125; 997 modctx->samplenb = 0; 998 999 modctx->patternticks = (((long)modctx->song.speed * modctx->playrate * 5)/ (2 * modctx->bpm)) + 1; 1000 modctx->patternticksaim = ((long)modctx->song.speed * modctx->playrate * 5) / (2 * modctx->bpm); 1001 1002 modctx->sampleticksconst = 3546894UL / modctx->playrate; //8448*428/playrate; 1003 1004 for(i=0; i < modctx->number_of_channels; i++) 1005 { 1006 modctx->channels[i].volume = 0; 1007 modctx->channels[i].period = 0; 1008 } 1009 1010 modctx->mod_loaded = 1; 1011 1012 return 1; 1013 } 1014 } 1015 1016 return 0; 1017 } 1018 1019 void hxcmod_fillbuffer( modcontext * modctx, unsigned short * outbuffer, unsigned long nbsample, tracker_buffer_state * trkbuf ) 1020 { 1021 unsigned long i, j; 1022 unsigned long k; 1023 unsigned char c; 1024 unsigned int state_remaining_steps; 1025 int l,r; 1026 int ll,lr; 1027 int tl,tr; 1028 short finalperiod; 1029 note *nptr; 1030 channel *cptr; 1031 1032 if( modctx && outbuffer ) 1033 { 1034 if(modctx->mod_loaded) 1035 { 1036 state_remaining_steps = 0; 1037 1038 if( trkbuf ) 1039 { 1040 trkbuf->cur_rd_index = 0; 1041 1042 memcopy(trkbuf->name,modctx->song.title,sizeof(modctx->song.title)); 1043 1044 for(i=0;i<31;i++) 1045 { 1046 memcopy(trkbuf->instruments[i].name,modctx->song.samples[i].name,sizeof(trkbuf->instruments[i].name)); 1047 } 1048 } 1049 1050 ll = modctx->last_l_sample; 1051 lr = modctx->last_r_sample; 1052 1053 for (i = 0; i < nbsample; i++) 1054 { 1055 //--------------------------------------- 1056 if( modctx->patternticks++ > modctx->patternticksaim ) 1057 { 1058 if( !modctx->patterndelay ) 1059 { 1060 nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]]; 1061 nptr = nptr + modctx->patternpos; 1062 cptr = modctx->channels; 1063 1064 modctx->patternticks = 0; 1065 modctx->patterntickse = 0; 1066 1067 for(c=0;c<modctx->number_of_channels;c++) 1068 { 1069 worknote((note*)(nptr+c), (channel*)(cptr+c),(char)(c+1),modctx); 1070 } 1071 1072 if( !modctx->jump_loop_effect ) 1073 modctx->patternpos += modctx->number_of_channels; 1074 else 1075 modctx->jump_loop_effect = 0; 1076 1077 if( modctx->patternpos == 64*modctx->number_of_channels ) 1078 { 1079 modctx->tablepos++; 1080 modctx->patternpos = 0; 1081 if(modctx->tablepos >= modctx->song.length) 1082 modctx->tablepos = 0; 1083 } 1084 } 1085 else 1086 { 1087 modctx->patterndelay--; 1088 modctx->patternticks = 0; 1089 modctx->patterntickse = 0; 1090 } 1091 1092 } 1093 1094 if( modctx->patterntickse++ > (modctx->patternticksaim/modctx->song.speed) ) 1095 { 1096 nptr = modctx->patterndata[modctx->song.patterntable[modctx->tablepos]]; 1097 nptr = nptr + modctx->patternpos; 1098 cptr = modctx->channels; 1099 1100 for(c=0;c<modctx->number_of_channels;c++) 1101 { 1102 workeffect(nptr+c, cptr+c); 1103 } 1104 1105 modctx->patterntickse = 0; 1106 } 1107 1108 //--------------------------------------- 1109 1110 if( trkbuf && !state_remaining_steps ) 1111 { 1112 if( trkbuf->nb_of_state < trkbuf->nb_max_of_state ) 1113 { 1114 memclear(&trkbuf->track_state_buf[trkbuf->nb_of_state],0,sizeof(tracker_state)); 1115 } 1116 } 1117 1118 l=0; 1119 r=0; 1120 1121 for(j =0, cptr = modctx->channels; j < modctx->number_of_channels ; j++, cptr++) 1122 { 1123 if( cptr->period != 0 ) 1124 { 1125 finalperiod = cptr->period - cptr->decalperiod - cptr->vibraperiod; 1126 if( finalperiod ) 1127 { 1128 cptr->samppos += ( (modctx->sampleticksconst<<10) / finalperiod ); 1129 } 1130 1131 cptr->ticks++; 1132 1133 if( cptr->replen<=2 ) 1134 { 1135 if( (cptr->samppos>>10) >= (cptr->length) ) 1136 { 1137 cptr->length = 0; 1138 cptr->reppnt = 0; 1139 1140 if( cptr->length ) 1141 cptr->samppos = cptr->samppos % (((unsigned long)cptr->length)<<10); 1142 else 1143 cptr->samppos = 0; 1144 } 1145 } 1146 else 1147 { 1148 if( (cptr->samppos>>10) >= (unsigned long)(cptr->replen+cptr->reppnt) ) 1149 { 1150 cptr->samppos = ((unsigned long)(cptr->reppnt)<<10) + (cptr->samppos % ((unsigned long)(cptr->replen+cptr->reppnt)<<10)); 1151 } 1152 } 1153 1154 k = cptr->samppos >> 10; 1155 1156 if( cptr->sampdata!=0 && ( ((j&3)==1) || ((j&3)==2) ) ) 1157 { 1158 r += ( cptr->sampdata[k] * cptr->volume ); 1159 } 1160 1161 if( cptr->sampdata!=0 && ( ((j&3)==0) || ((j&3)==3) ) ) 1162 { 1163 l += ( cptr->sampdata[k] * cptr->volume ); 1164 } 1165 1166 if( trkbuf && !state_remaining_steps ) 1167 { 1168 if( trkbuf->nb_of_state < trkbuf->nb_max_of_state ) 1169 { 1170 trkbuf->track_state_buf[trkbuf->nb_of_state].number_of_tracks = modctx->number_of_channels; 1171 trkbuf->track_state_buf[trkbuf->nb_of_state].buf_index = i; 1172 trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern = modctx->song.patterntable[modctx->tablepos]; 1173 trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_pos = modctx->patternpos / modctx->number_of_channels; 1174 trkbuf->track_state_buf[trkbuf->nb_of_state].cur_pattern_table_pos = modctx->tablepos; 1175 trkbuf->track_state_buf[trkbuf->nb_of_state].bpm = modctx->bpm; 1176 trkbuf->track_state_buf[trkbuf->nb_of_state].speed = modctx->song.speed; 1177 trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_effect = cptr->effect_code; 1178 trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_parameffect = cptr->parameffect; 1179 trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_period = finalperiod; 1180 trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].cur_volume = cptr->volume; 1181 trkbuf->track_state_buf[trkbuf->nb_of_state].tracks[j].instrument_number = (unsigned char)cptr->sampnum; 1182 } 1183 } 1184 } 1185 } 1186 1187 if( trkbuf && !state_remaining_steps ) 1188 { 1189 state_remaining_steps = trkbuf->sample_step; 1190 1191 if(trkbuf->nb_of_state < trkbuf->nb_max_of_state) 1192 trkbuf->nb_of_state++; 1193 } 1194 else 1195 { 1196 state_remaining_steps--; 1197 } 1198 1199 tl = (short)l; 1200 tr = (short)r; 1201 1202 if ( modctx->filter ) 1203 { 1204 // Filter 1205 l = (l+ll)>>1; 1206 r = (r+lr)>>1; 1207 } 1208 1209 if ( modctx->stereo_separation == 1 ) 1210 { 1211 // Left & Right Stereo panning 1212 l = (l+(r>>1)); 1213 r = (r+(l>>1)); 1214 } 1215 1216 // Level limitation 1217 if( l > 32767 ) l = 32767; 1218 if( l < -32768 ) l = -32768; 1219 if( r > 32767 ) r = 32767; 1220 if( r < -32768 ) r = -32768; 1221 1222 // Store the final sample. 1223 outbuffer[(i*2)] = l; 1224 outbuffer[(i*2)+1] = r; 1225 1226 ll = tl; 1227 lr = tr; 1228 1229 } 1230 1231 modctx->last_l_sample = ll; 1232 modctx->last_r_sample = lr; 1233 1234 modctx->samplenb = modctx->samplenb+nbsample; 1235 } 1236 else 1237 { 1238 for (i = 0; i < nbsample; i++) 1239 { 1240 // Mod not loaded. Return blank buffer. 1241 outbuffer[(i*2)] = 0; 1242 outbuffer[(i*2)+1] = 0; 1243 } 1244 1245 if(trkbuf) 1246 { 1247 trkbuf->nb_of_state = 0; 1248 trkbuf->cur_rd_index = 0; 1249 trkbuf->name[0] = 0; 1250 memclear(trkbuf->track_state_buf,0,sizeof(tracker_state) * trkbuf->nb_max_of_state); 1251 memclear(trkbuf->instruments,0,sizeof(trkbuf->instruments)); 1252 } 1253 } 1254 } 1255 } 1256 1257 void hxcmod_unload( modcontext * modctx ) 1258 { 1259 if(modctx) 1260 { 1261 memclear(&modctx->song,0,sizeof(modctx->song)); 1262 memclear(&modctx->sampledata,0,sizeof(modctx->sampledata)); 1263 memclear(&modctx->patterndata,0,sizeof(modctx->patterndata)); 1264 modctx->tablepos = 0; 1265 modctx->patternpos = 0; 1266 modctx->patterndelay = 0; 1267 modctx->jump_loop_effect = 0; 1268 modctx->bpm = 0; 1269 modctx->patternticks = 0; 1270 modctx->patterntickse = 0; 1271 modctx->patternticksaim = 0; 1272 modctx->sampleticksconst = 0; 1273 1274 modctx->samplenb = 0; 1275 1276 memclear(modctx->channels,0,sizeof(modctx->channels)); 1277 1278 modctx->number_of_channels = 0; 1279 1280 modctx->mod_loaded = 0; 1281 1282 modctx->last_r_sample = 0; 1283 modctx->last_l_sample = 0; 1284 } 1285 } 1286