| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "lds_play.h" |
| 20 | |
| 21 | #include "file.h" |
| 22 | #include "loudness.h" |
| 23 | #include "opentyr.h" |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | const unsigned char op_table[9] = {0x00, 0x01, 0x02, 0x08, 0x09, 0x0a, 0x10, 0x11, 0x12}; |
| 30 | |
| 31 | /* A substantial amount of this code has been copied and adapted from adplug. |
| 32 | Thanks, guys! Adplug is awesome! :D */ |
| 33 | |
| 34 | /* Note frequency table (16 notes / octave) */ |
| 35 | static const Uint16 frequency[(13 * 15) - 3] = { |
| 36 | 343, 344, 345, 347, 348, 349, 350, 352, 353, 354, 356, 357, 358, |
| 37 | 359, 361, 362, 363, 365, 366, 367, 369, 370, 371, 373, 374, 375, |
| 38 | 377, 378, 379, 381, 382, 384, 385, 386, 388, 389, 391, 392, 393, |
| 39 | 395, 396, 398, 399, 401, 402, 403, 405, 406, 408, 409, 411, 412, |
| 40 | 414, 415, 417, 418, 420, 421, 423, 424, 426, 427, 429, 430, 432, |
| 41 | 434, 435, 437, 438, 440, 442, 443, 445, 446, 448, 450, 451, 453, |
| 42 | 454, 456, 458, 459, 461, 463, 464, 466, 468, 469, 471, 473, 475, |
| 43 | 476, 478, 480, 481, 483, 485, 487, 488, 490, 492, 494, 496, 497, |
| 44 | 499, 501, 503, 505, 506, 508, 510, 512, 514, 516, 518, 519, 521, |
| 45 | 523, 525, 527, 529, 531, 533, 535, 537, 538, 540, 542, 544, 546, |
| 46 | 548, 550, 552, 554, 556, 558, 560, 562, 564, 566, 568, 571, 573, |
| 47 | 575, 577, 579, 581, 583, 585, 587, 589, 591, 594, 596, 598, 600, |
| 48 | 602, 604, 607, 609, 611, 613, 615, 618, 620, 622, 624, 627, 629, |
| 49 | 631, 633, 636, 638, 640, 643, 645, 647, 650, 652, 654, 657, 659, |
| 50 | 662, 664, 666, 669, 671, 674, 676, 678, 681, 683 |
| 51 | }; |
| 52 | |
| 53 | /* Vibrato (sine) table */ |
| 54 | static const Uint8 vibtab[25 + (13 * 3)] = { |
| 55 | 0, 13, 25, 37, 50, 62, 74, 86, 98, 109, 120, 131, 142, 152, 162, |
| 56 | 171, 180, 189, 197, 205, 212, 219, 225, 231, 236, 240, 244, 247, |
| 57 | 250, 252, 254, 255, 255, 255, 254, 252, 250, 247, 244, 240, 236, |
| 58 | 231, 225, 219, 212, 205, 197, 189, 180, 171, 162, 152, 142, 131, |
| 59 | 120, 109, 98, 86, 74, 62, 50, 37, 25, 13 |
| 60 | }; |
| 61 | |
| 62 | /* Tremolo (sine * sine) table */ |
| 63 | static const Uint8 tremtab[128] = { |
| 64 | 0, 0, 1, 1, 2, 4, 5, 7, 10, 12, 15, 18, 21, 25, 29, 33, 37, 42, 47, |
| 65 | 52, 57, 62, 67, 73, 79, 85, 90, 97, 103, 109, 115, 121, 128, 134, |
| 66 | 140, 146, 152, 158, 165, 170, 176, 182, 188, 193, 198, 203, 208, |
| 67 | 213, 218, 222, 226, 230, 234, 237, 240, 243, 245, 248, 250, 251, |
| 68 | 253, 254, 254, 255, 255, 255, 254, 254, 253, 251, 250, 248, 245, |
| 69 | 243, 240, 237, 234, 230, 226, 222, 218, 213, 208, 203, 198, 193, |
| 70 | 188, 182, 176, 170, 165, 158, 152, 146, 140, 134, 127, 121, 115, |
| 71 | 109, 103, 97, 90, 85, 79, 73, 67, 62, 57, 52, 47, 42, 37, 33, 29, |
| 72 | 25, 21, 18, 15, 12, 10, 7, 5, 4, 2, 1, 1, 0 |
| 73 | }; |
| 74 | |
| 75 | static const Uint16 maxsound = 0x3f, maxpos = 0xff; |
| 76 | |
| 77 | static SoundBank *soundbank = NULL; |
| 78 | static Channel channel[9]; |
| 79 | static Position *positions = NULL; |
| 80 | |
| 81 | static Uint8 fmchip[0xff], jumping, fadeonoff, allvolume, hardfade, tempo_now, pattplay, tempo, regbd, chandelay[9], mode, pattlen; |
| 82 | static Uint16 posplay, jumppos, speed; |
| 83 | static Uint16 *patterns = NULL; |
| 84 | static Uint16 numpatch, numposi, mainvolume; |
| 85 | |
| 86 | bool playing, songlooped; |
| 87 | |
| 88 | bool lds_load(FILE *f, unsigned int music_offset, unsigned int music_size) |
| 89 | { |
| 90 | SoundBank *sb; |
| 91 | |
| 92 | fseek(f, music_offset, SEEK_SET); |
| 93 | |
| 94 | /* load header */ |
| 95 | fread_u8_die(&mode, 1, f); |
| 96 | if (mode > 2) |
| 97 | { |
| 98 | fprintf(stderr, "error: failed to load music\n"); |
| 99 | return false; |
| 100 | } |
| 101 | fread_u16_die(&speed, 1, f); |
| 102 | fread_u8_die(&tempo, 1, f); |
| 103 | fread_u8_die(&pattlen, 1, f); |
| 104 | fread_u8_die(chandelay, 9, f); |
| 105 | fread_u8_die(®bd, 1, f); |
| 106 | |
| 107 | /* load patches */ |
| 108 | fread_u16_die(&numpatch, 1, f); |
| 109 | |
| 110 | free(soundbank); |
| 111 | soundbank = malloc(sizeof(SoundBank) * numpatch); |
| 112 | |
| 113 | for (unsigned int i = 0; i < numpatch; i++) |
| 114 | { |
| 115 | sb = &soundbank[i]; |
| 116 | fread_u8_die( &sb->mod_misc, 1, f); |
| 117 | fread_u8_die( &sb->mod_vol, 1, f); |
| 118 | fread_u8_die( &sb->mod_ad, 1, f); |
| 119 | fread_u8_die( &sb->mod_sr, 1, f); |
| 120 | fread_u8_die( &sb->mod_wave, 1, f); |
| 121 | fread_u8_die( &sb->car_misc, 1, f); |
| 122 | fread_u8_die( &sb->car_vol, 1, f); |
| 123 | fread_u8_die( &sb->car_ad, 1, f); |
| 124 | fread_u8_die( &sb->car_sr, 1, f); |
| 125 | fread_u8_die( &sb->car_wave, 1, f); |
| 126 | fread_u8_die( &sb->feedback, 1, f); |
| 127 | fread_u8_die( &sb->keyoff, 1, f); |
| 128 | fread_u8_die( &sb->portamento, 1, f); |
| 129 | fread_u8_die( &sb->glide, 1, f); |
| 130 | fread_u8_die( &sb->finetune, 1, f); |
| 131 | fread_u8_die( &sb->vibrato, 1, f); |
| 132 | fread_u8_die( &sb->vibdelay, 1, f); |
| 133 | fread_u8_die( &sb->mod_trem, 1, f); |
| 134 | fread_u8_die( &sb->car_trem, 1, f); |
| 135 | fread_u8_die( &sb->tremwait, 1, f); |
| 136 | fread_u8_die( &sb->arpeggio, 1, f); |
| 137 | fread_u8_die( sb->arp_tab, 12, f); |
| 138 | fread_u16_die(&sb->start, 1, f); |
| 139 | fread_u16_die(&sb->size, 1, f); |
| 140 | fread_u8_die( &sb->fms, 1, f); |
| 141 | fread_u16_die(&sb->transp, 1, f); |
| 142 | fread_u8_die( &sb->midinst, 1, f); |
| 143 | fread_u8_die( &sb->midvelo, 1, f); |
| 144 | fread_u8_die( &sb->midkey, 1, f); |
| 145 | fread_u8_die( &sb->midtrans, 1, f); |
| 146 | fread_u8_die( &sb->middum1, 1, f); |
| 147 | fread_u8_die( &sb->middum2, 1, f); |
| 148 | } |
| 149 | |
| 150 | /* load positions */ |
| 151 | fread_u16_die(&numposi, 1, f); |
| 152 | |
| 153 | free(positions); |
| 154 | positions = malloc(sizeof(Position) * 9 * numposi); |
| 155 | |
| 156 | for (unsigned int i = 0; i < numposi; i++) |
| 157 | { |
| 158 | for (unsigned int j = 0; j < 9; j++) |
| 159 | { |
| 160 | /* |
| 161 | * patnum is a pointer inside the pattern space, but patterns are 16bit |
| 162 | * word fields anyway, so it ought to be an even number (hopefully) and |
| 163 | * we can just divide it by 2 to get our array index of 16bit words. |
| 164 | */ |
| 165 | fread_u16_die(&positions[i * 9 + j].patnum, 1, f); |
| 166 | fread_u8_die( &positions[i * 9 + j].transpose, 1, f); |
| 167 | positions[i * 9 + j].patnum /= 2; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* load patterns */ |
| 172 | fseek(f, 2, SEEK_CUR); /* ignore # of digital sounds (dunno what this is for) */ |
| 173 | |
| 174 | unsigned int remaining = music_size - (ftell(f) - music_offset); |
| 175 | size_t numpatterns = remaining / 2; |
| 176 | |
| 177 | free(patterns); |
| 178 | patterns = malloc(sizeof(Uint16) * numpatterns); |
| 179 | |
| 180 | fread_u16_die(patterns, numpatterns, f); |
| 181 | |
| 182 | lds_rewind(); |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | void lds_free(void) |
| 188 | { |
| 189 | free(soundbank); |
| 190 | soundbank = NULL; |
| 191 | |
| 192 | free(positions); |
| 193 | positions = NULL; |
| 194 | |
| 195 | free(patterns); |
| 196 | patterns = NULL; |
| 197 | } |
| 198 | |
| 199 | void lds_rewind(void) |
| 200 | { |
| 201 | int i; |
| 202 | |
| 203 | /* init all with 0 */ |
| 204 | tempo_now = 3; |
| 205 | playing = true; songlooped = false; |
| 206 | jumping = fadeonoff = allvolume = hardfade = pattplay = posplay = jumppos = mainvolume = 0; |
| 207 | memset(channel, 0, sizeof(channel)); |
| 208 | memset(fmchip, 0, sizeof(fmchip)); |
| 209 | |
| 210 | /* OPL2 init */ |
| 211 | opl_init(); /* Reset OPL chip */ |
| 212 | opl_write(1, 0x20); |
| 213 | opl_write(8, 0); |
| 214 | opl_write(0xbd, regbd); |
| 215 | |
| 216 | for(i = 0; i < 9; i++) { |
| 217 | opl_write(0x20 + op_table[i], 0); |
| 218 | opl_write(0x23 + op_table[i], 0); |
| 219 | opl_write(0x40 + op_table[i], 0x3f); |
| 220 | opl_write(0x43 + op_table[i], 0x3f); |
| 221 | opl_write(0x60 + op_table[i], 0xff); |
| 222 | opl_write(0x63 + op_table[i], 0xff); |
| 223 | opl_write(0x80 + op_table[i], 0xff); |
| 224 | opl_write(0x83 + op_table[i], 0xff); |
| 225 | opl_write(0xe0 + op_table[i], 0); |
| 226 | opl_write(0xe3 + op_table[i], 0); |
| 227 | opl_write(0xa0 + i, 0); |
| 228 | opl_write(0xb0 + i, 0); |
| 229 | opl_write(0xc0 + i, 0); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void lds_fade(Uint8 speed) |
| 234 | { |
| 235 | fadeonoff = speed; |
| 236 | } |
| 237 | |
| 238 | void lds_setregs(Uint8 reg, Uint8 val) |
| 239 | { |
| 240 | if(fmchip[reg] == val) return; |
| 241 | |
| 242 | fmchip[reg] = val; |
| 243 | opl_write(reg, val); |
| 244 | } |
| 245 | |
| 246 | void lds_setregs_adv(Uint8 reg, Uint8 mask, Uint8 val) |
| 247 | { |
| 248 | lds_setregs(reg, (fmchip[reg] & mask) | val); |
| 249 | } |
| 250 | |
| 251 | int lds_update(void) |
| 252 | { |
| 253 | Uint16 comword, freq, octave, chan, tune, wibc, tremc, arpreg; |
| 254 | int vbreak; |
| 255 | Uint8 level, regnum, comhi, comlo; |
| 256 | int i; |
| 257 | Channel *c; |
| 258 | |
| 259 | if(!playing) return false; |
| 260 | |
| 261 | /* handle fading */ |
| 262 | if(fadeonoff) |
| 263 | { |
| 264 | if(fadeonoff <= 128) { |
| 265 | if(allvolume > fadeonoff || allvolume == 0) |
| 266 | { |
| 267 | allvolume -= fadeonoff; |
| 268 | } else { |
| 269 | allvolume = 1; |
| 270 | fadeonoff = 0; |
| 271 | if(hardfade != 0) { |
| 272 | playing = false; |
| 273 | hardfade = 0; |
| 274 | for(i = 0; i < 9; i++) |
| 275 | { |
| 276 | channel[i].keycount = 1; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | } else { |
| 282 | if( (Uint8) ((allvolume + (0x100 - fadeonoff)) & 0xff) <= mainvolume) |
| 283 | { |
| 284 | allvolume += 0x100 - fadeonoff; |
| 285 | } else { |
| 286 | allvolume = mainvolume; |
| 287 | fadeonoff = 0; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* handle channel delay */ |
| 293 | for(chan = 0; chan < 9; chan++) { |
| 294 | c = &channel[chan]; |
| 295 | if(c->chancheat.chandelay) { |
| 296 | if(!(--c->chancheat.chandelay)) { |
| 297 | lds_playsound(c->chancheat.sound, chan, c->chancheat.high); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* handle notes */ |
| 303 | if(!tempo_now && positions) |
| 304 | { |
| 305 | vbreak = false; |
| 306 | for(chan = 0; chan < 9; chan++) |
| 307 | { |
| 308 | c = &channel[chan]; |
| 309 | if(!c->packwait) { |
| 310 | Uint16 patnum = positions[posplay * 9 + chan].patnum; |
| 311 | Uint8 transpose = positions[posplay * 9 + chan].transpose; |
| 312 | /*printf("> %p", positions);*/ |
| 313 | |
| 314 | comword = patterns[patnum + c->packpos]; |
| 315 | comhi = comword >> 8; comlo = comword & 0xff; |
| 316 | if(comword) { |
| 317 | if(comhi == 0x80) |
| 318 | { |
| 319 | c->packwait = comlo; |
| 320 | } else { |
| 321 | if(comhi >= 0x80) |
| 322 | { |
| 323 | switch(comhi) |
| 324 | { |
| 325 | case 0xff: |
| 326 | c->volcar = (((c->volcar & 0x3f) * comlo) >> 6) & 0x3f; |
| 327 | if(fmchip[0xc0 + chan] & 1) |
| 328 | c->volmod = (((c->volmod & 0x3f) * comlo) >> 6) & 0x3f; |
| 329 | break; |
| 330 | |
| 331 | case 0xfe: |
| 332 | tempo = comword & 0x3f; |
| 333 | break; |
| 334 | |
| 335 | case 0xfd: |
| 336 | c->nextvol = comlo; |
| 337 | break; |
| 338 | |
| 339 | case 0xfc: |
| 340 | playing = false; |
| 341 | /* in real player there's also full keyoff here, but we don't need it */ |
| 342 | break; |
| 343 | |
| 344 | case 0xfb: |
| 345 | c->keycount = 1; |
| 346 | break; |
| 347 | |
| 348 | case 0xfa: |
| 349 | vbreak = true; |
| 350 | jumppos = (posplay + 1) & maxpos; |
| 351 | break; |
| 352 | |
| 353 | case 0xf9: |
| 354 | vbreak = true; |
| 355 | jumppos = comlo & maxpos; |
| 356 | jumping = 1; |
| 357 | if(jumppos < posplay) |
| 358 | { |
| 359 | songlooped = true; |
| 360 | } |
| 361 | break; |
| 362 | |
| 363 | case 0xf8: |
| 364 | c->lasttune = 0; |
| 365 | break; |
| 366 | |
| 367 | case 0xf7: |
| 368 | c->vibwait = 0; |
| 369 | /* PASCAL: c->vibspeed = ((comlo >> 4) & 15) + 2; */ |
| 370 | c->vibspeed = (comlo >> 4) + 2; |
| 371 | c->vibrate = (comlo & 15) + 1; |
| 372 | break; |
| 373 | |
| 374 | case 0xf6: |
| 375 | c->glideto = comlo; |
| 376 | break; |
| 377 | |
| 378 | case 0xf5: |
| 379 | c->finetune = comlo; |
| 380 | break; |
| 381 | |
| 382 | case 0xf4: |
| 383 | if(!hardfade) |
| 384 | { |
| 385 | allvolume = mainvolume = comlo; |
| 386 | fadeonoff = 0; |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | case 0xf3: |
| 391 | if(!hardfade) |
| 392 | { |
| 393 | fadeonoff = comlo; |
| 394 | } |
| 395 | break; |
| 396 | |
| 397 | case 0xf2: |
| 398 | c->trmstay = comlo; |
| 399 | break; |
| 400 | |
| 401 | case 0xf1: /* panorama */ |
| 402 | |
| 403 | case 0xf0: /* progch */ |
| 404 | /* MIDI commands (unhandled) */ |
| 405 | /*AdPlug_LogWrite("CldsPlayer(): not handling MIDI command 0x%x, " |
| 406 | "value = 0x%x\n", comhi);*/ |
| 407 | break; |
| 408 | |
| 409 | default: |
| 410 | if(comhi < 0xa0) |
| 411 | { |
| 412 | c->glideto = comhi & 0x1f; |
| 413 | } else { |
| 414 | /*AdPlug_LogWrite("CldsPlayer(): unknown command 0x%x encountered!" |
| 415 | " value = 0x%x\n", comhi, comlo);*/ |
| 416 | } |
| 417 | break; |
| 418 | } |
| 419 | } else { |
| 420 | Uint8 sound; |
| 421 | Uint16 high; |
| 422 | Sint8 transp = transpose & 127; |
| 423 | /* |
| 424 | * Originally, in assembler code, the player first shifted |
| 425 | * logically left the transpose byte by 1 and then shifted |
| 426 | * arithmetically right the same byte to achieve the final, |
| 427 | * signed transpose value. Since we can't do arithmetic shifts |
| 428 | * in C, we just duplicate the 7th bit into the 8th one and |
| 429 | * discard the 8th one completely. |
| 430 | */ |
| 431 | |
| 432 | if(transpose & 64) |
| 433 | { |
| 434 | transp |= 128; |
| 435 | } |
| 436 | |
| 437 | if(transpose & 128) |
| 438 | { |
| 439 | sound = (comlo + transp) & maxsound; |
| 440 | high = comhi << 4; |
| 441 | } else { |
| 442 | sound = comlo & maxsound; |
| 443 | high = (comhi + transp) << 4; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | PASCAL: |
| 448 | sound = comlo & maxsound; |
| 449 | high = (comhi + (((transpose + 0x24) & 0xff) - 0x24)) << 4; |
| 450 | */ |
| 451 | |
| 452 | if(!chandelay[chan]) { |
| 453 | lds_playsound(sound, chan, high); |
| 454 | } else { |
| 455 | c->chancheat.chandelay = chandelay[chan]; |
| 456 | c->chancheat.sound = sound; |
| 457 | c->chancheat.high = high; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | c->packpos++; |
| 464 | } else { |
| 465 | c->packwait--; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | tempo_now = tempo; |
| 470 | /* |
| 471 | The continue table is updated here, but this is only used in the |
| 472 | original player, which can be paused in the middle of a song and then |
| 473 | unpaused. Since AdPlug does all this for us automatically, we don't |
| 474 | have a continue table here. The continue table update code is noted |
| 475 | here for reference only. |
| 476 | |
| 477 | if(!pattplay) { |
| 478 | conttab[speed & maxcont].position = posplay & 0xff; |
| 479 | conttab[speed & maxcont].tempo = tempo; |
| 480 | } |
| 481 | */ |
| 482 | pattplay++; |
| 483 | if(vbreak) |
| 484 | { |
| 485 | pattplay = 0; |
| 486 | for(i = 0; i < 9; i++) |
| 487 | { |
| 488 | channel[i].packpos = channel[i].packwait = 0; |
| 489 | } |
| 490 | posplay = jumppos; |
| 491 | } else { |
| 492 | if(pattplay >= pattlen) { |
| 493 | pattplay = 0; |
| 494 | for(i = 0; i < 9; i++) |
| 495 | { |
| 496 | channel[i].packpos = channel[i].packwait = 0; |
| 497 | } |
| 498 | posplay = (posplay + 1) & maxpos; |
| 499 | } |
| 500 | } |
| 501 | } else { |
| 502 | tempo_now--; |
| 503 | } |
| 504 | |
| 505 | /* make effects */ |
| 506 | for(chan = 0; chan < 9; chan++) { |
| 507 | c = &channel[chan]; |
| 508 | regnum = op_table[chan]; |
| 509 | if(c->keycount > 0) { |
| 510 | if(c->keycount == 1) |
| 511 | lds_setregs_adv(0xb0 + chan, 0xdf, 0); |
| 512 | c->keycount--; |
| 513 | } |
| 514 | |
| 515 | /* arpeggio */ |
| 516 | if(c->arp_size == 0) |
| 517 | arpreg = 0; |
| 518 | else { |
| 519 | arpreg = c->arp_tab[c->arp_pos] << 4; |
| 520 | if(arpreg == 0x800) { |
| 521 | if(c->arp_pos > 0) c->arp_tab[0] = c->arp_tab[c->arp_pos - 1]; |
| 522 | c->arp_size = 1; c->arp_pos = 0; |
| 523 | arpreg = c->arp_tab[0] << 4; |
| 524 | } |
| 525 | |
| 526 | if(c->arp_count == c->arp_speed) { |
| 527 | c->arp_pos++; |
| 528 | if(c->arp_pos >= c->arp_size) c->arp_pos = 0; |
| 529 | c->arp_count = 0; |
| 530 | } else |
| 531 | c->arp_count++; |
| 532 | } |
| 533 | |
| 534 | /* glide & portamento */ |
| 535 | if(c->lasttune && (c->lasttune != c->gototune)) { |
| 536 | if(c->lasttune > c->gototune) { |
| 537 | if(c->lasttune - c->gototune < c->portspeed) |
| 538 | c->lasttune = c->gototune; |
| 539 | else |
| 540 | c->lasttune -= c->portspeed; |
| 541 | } else { |
| 542 | if(c->gototune - c->lasttune < c->portspeed) |
| 543 | c->lasttune = c->gototune; |
| 544 | else |
| 545 | c->lasttune += c->portspeed; |
| 546 | } |
| 547 | |
| 548 | if(arpreg >= 0x800) |
| 549 | arpreg = c->lasttune - (arpreg ^ 0xff0) - 16; |
| 550 | else |
| 551 | arpreg += c->lasttune; |
| 552 | |
| 553 | freq = frequency[arpreg % (12 * 16)]; |
| 554 | octave = arpreg / (12 * 16) - 1; |
| 555 | lds_setregs(0xa0 + chan, freq & 0xff); |
| 556 | lds_setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); |
| 557 | } else { |
| 558 | /* vibrato */ |
| 559 | if(!c->vibwait) { |
| 560 | if(c->vibrate) { |
| 561 | wibc = vibtab[c->vibcount & 0x3f] * c->vibrate; |
| 562 | |
| 563 | if((c->vibcount & 0x40) == 0) |
| 564 | tune = c->lasttune + (wibc >> 8); |
| 565 | else |
| 566 | tune = c->lasttune - (wibc >> 8); |
| 567 | |
| 568 | if(arpreg >= 0x800) |
| 569 | tune = tune - (arpreg ^ 0xff0) - 16; |
| 570 | else |
| 571 | tune += arpreg; |
| 572 | |
| 573 | freq = frequency[tune % (12 * 16)]; |
| 574 | octave = tune / (12 * 16) - 1; |
| 575 | lds_setregs(0xa0 + chan, freq & 0xff); |
| 576 | lds_setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); |
| 577 | c->vibcount += c->vibspeed; |
| 578 | } else if(c->arp_size != 0) { /* no vibrato, just arpeggio */ |
| 579 | if(arpreg >= 0x800) |
| 580 | tune = c->lasttune - (arpreg ^ 0xff0) - 16; |
| 581 | else |
| 582 | tune = c->lasttune + arpreg; |
| 583 | |
| 584 | freq = frequency[tune % (12 * 16)]; |
| 585 | octave = tune / (12 * 16) - 1; |
| 586 | lds_setregs(0xa0 + chan, freq & 0xff); |
| 587 | lds_setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); |
| 588 | } |
| 589 | } else { /* no vibrato, just arpeggio */ |
| 590 | c->vibwait--; |
| 591 | |
| 592 | if(c->arp_size != 0) { |
| 593 | if(arpreg >= 0x800) |
| 594 | tune = c->lasttune - (arpreg ^ 0xff0) - 16; |
| 595 | else |
| 596 | tune = c->lasttune + arpreg; |
| 597 | |
| 598 | freq = frequency[tune % (12 * 16)]; |
| 599 | octave = tune / (12 * 16) - 1; |
| 600 | lds_setregs(0xa0 + chan, freq & 0xff); |
| 601 | lds_setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /* tremolo (modulator) */ |
| 607 | if(!c->trmwait) { |
| 608 | if(c->trmrate) { |
| 609 | tremc = tremtab[c->trmcount & 0x7f] * c->trmrate; |
| 610 | if((tremc >> 8) <= (c->volmod & 0x3f)) |
| 611 | level = (c->volmod & 0x3f) - (tremc >> 8); |
| 612 | else |
| 613 | level = 0; |
| 614 | |
| 615 | if(allvolume != 0 && (fmchip[0xc0 + chan] & 1)) |
| 616 | lds_setregs_adv(0x40 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f); |
| 617 | else |
| 618 | lds_setregs_adv(0x40 + regnum, 0xc0, level ^ 0x3f); |
| 619 | |
| 620 | c->trmcount += c->trmspeed; |
| 621 | } else if(allvolume != 0 && (fmchip[0xc0 + chan] & 1)) |
| 622 | lds_setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); |
| 623 | else |
| 624 | lds_setregs_adv(0x40 + regnum, 0xc0, (c->volmod ^ 0x3f) & 0x3f); |
| 625 | } else { |
| 626 | c->trmwait--; |
| 627 | if(allvolume != 0 && (fmchip[0xc0 + chan] & 1)) |
| 628 | lds_setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); |
| 629 | } |
| 630 | |
| 631 | /* tremolo (carrier) */ |
| 632 | if(!c->trcwait) { |
| 633 | if(c->trcrate) { |
| 634 | tremc = tremtab[c->trccount & 0x7f] * c->trcrate; |
| 635 | if((tremc >> 8) <= (c->volcar & 0x3f)) |
| 636 | level = (c->volcar & 0x3f) - (tremc >> 8); |
| 637 | else |
| 638 | level = 0; |
| 639 | |
| 640 | if(allvolume != 0) |
| 641 | lds_setregs_adv(0x43 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f); |
| 642 | else |
| 643 | lds_setregs_adv(0x43 + regnum, 0xc0, level ^ 0x3f); |
| 644 | c->trccount += c->trcspeed; |
| 645 | } else if(allvolume != 0) |
| 646 | lds_setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); |
| 647 | else |
| 648 | lds_setregs_adv(0x43 + regnum, 0xc0, (c->volcar ^ 0x3f) & 0x3f); |
| 649 | } else { |
| 650 | c->trcwait--; |
| 651 | if(allvolume != 0) |
| 652 | lds_setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return (!playing || songlooped) ? false : true; |
| 657 | } |
| 658 | |
| 659 | void lds_playsound(int inst_number, int channel_number, int tunehigh) |
| 660 | { |
| 661 | Channel *c = &channel[channel_number]; /* current channel */ |
| 662 | SoundBank *i = &soundbank[inst_number]; /* current instrument */ |
| 663 | Uint32 regnum = op_table[channel_number]; /* channel's OPL2 register */ |
| 664 | Uint8 volcalc, octave; |
| 665 | Uint16 freq; |
| 666 | |
| 667 | /* set fine tune */ |
| 668 | tunehigh += ((i->finetune + c->finetune + 0x80) & 0xff) - 0x80; |
| 669 | |
| 670 | /* arpeggio handling */ |
| 671 | if(!i->arpeggio) { |
| 672 | Uint16 arpcalc = i->arp_tab[0] << 4; |
| 673 | |
| 674 | if(arpcalc > 0x800) |
| 675 | tunehigh = tunehigh - (arpcalc ^ 0xff0) - 16; |
| 676 | else |
| 677 | tunehigh += arpcalc; |
| 678 | } |
| 679 | |
| 680 | /* glide handling */ |
| 681 | if(c->glideto != 0) { |
| 682 | c->gototune = tunehigh; |
| 683 | c->portspeed = c->glideto; |
| 684 | c->glideto = c->finetune = 0; |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | /* set modulator registers */ |
| 689 | lds_setregs(0x20 + regnum, i->mod_misc); |
| 690 | volcalc = i->mod_vol; |
| 691 | if(!c->nextvol || !(i->feedback & 1)) |
| 692 | c->volmod = volcalc; |
| 693 | else |
| 694 | c->volmod = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6)); |
| 695 | |
| 696 | if((i->feedback & 1) == 1 && allvolume != 0) |
| 697 | lds_setregs(0x40 + regnum, ((c->volmod & 0xc0) | (((c->volmod & 0x3f) * allvolume) >> 8)) ^ 0x3f); |
| 698 | else |
| 699 | lds_setregs(0x40 + regnum, c->volmod ^ 0x3f); |
| 700 | lds_setregs(0x60 + regnum, i->mod_ad); |
| 701 | lds_setregs(0x80 + regnum, i->mod_sr); |
| 702 | lds_setregs(0xe0 + regnum, i->mod_wave); |
| 703 | |
| 704 | /* Set carrier registers */ |
| 705 | lds_setregs(0x23 + regnum, i->car_misc); |
| 706 | volcalc = i->car_vol; |
| 707 | if(!c->nextvol) |
| 708 | c->volcar = volcalc; |
| 709 | else |
| 710 | c->volcar = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6)); |
| 711 | |
| 712 | if(allvolume) |
| 713 | lds_setregs(0x43 + regnum, ((c->volcar & 0xc0) | (((c->volcar & 0x3f) * allvolume) >> 8)) ^ 0x3f); |
| 714 | else |
| 715 | lds_setregs(0x43 + regnum, c->volcar ^ 0x3f); |
| 716 | lds_setregs(0x63 + regnum, i->car_ad); |
| 717 | lds_setregs(0x83 + regnum, i->car_sr); |
| 718 | lds_setregs(0xe3 + regnum, i->car_wave); |
| 719 | lds_setregs(0xc0 + channel_number, i->feedback); |
| 720 | lds_setregs_adv(0xb0 + channel_number, 0xdf, 0); /* key off */ |
| 721 | |
| 722 | freq = frequency[tunehigh % (12 * 16)]; |
| 723 | octave = tunehigh / (12 * 16) - 1; |
| 724 | if(!i->glide) { |
| 725 | if(!i->portamento || !c->lasttune) { |
| 726 | lds_setregs(0xa0 + channel_number, freq & 0xff); |
| 727 | lds_setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8)); |
| 728 | c->lasttune = c->gototune = tunehigh; |
| 729 | } else { |
| 730 | c->gototune = tunehigh; |
| 731 | c->portspeed = i->portamento; |
| 732 | lds_setregs_adv(0xb0 + channel_number, 0xdf, 0x20); /* key on */ |
| 733 | } |
| 734 | } else { |
| 735 | lds_setregs(0xa0 + channel_number, freq & 0xff); |
| 736 | lds_setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8)); |
| 737 | c->lasttune = tunehigh; |
| 738 | c->gototune = tunehigh + ((i->glide + 0x80) & 0xff) - 0x80; /* set destination */ |
| 739 | c->portspeed = i->portamento; |
| 740 | } |
| 741 | |
| 742 | if(!i->vibrato) |
| 743 | c->vibwait = c->vibspeed = c->vibrate = 0; |
| 744 | else { |
| 745 | c->vibwait = i->vibdelay; |
| 746 | /* PASCAL: c->vibspeed = ((i->vibrato >> 4) & 15) + 1; */ |
| 747 | c->vibspeed = (i->vibrato >> 4) + 2; |
| 748 | c->vibrate = (i->vibrato & 15) + 1; |
| 749 | } |
| 750 | |
| 751 | if(!(c->trmstay & 0xf0)) { |
| 752 | c->trmwait = (i->tremwait & 0xf0) >> 3; |
| 753 | /* PASCAL: c->trmspeed = (i->mod_trem >> 4) & 15; */ |
| 754 | c->trmspeed = i->mod_trem >> 4; |
| 755 | c->trmrate = i->mod_trem & 15; |
| 756 | c->trmcount = 0; |
| 757 | } |
| 758 | |
| 759 | if(!(c->trmstay & 0x0f)) { |
| 760 | c->trcwait = (i->tremwait & 15) << 1; |
| 761 | /* PASCAL: c->trcspeed = (i->car_trem >> 4) & 15; */ |
| 762 | c->trcspeed = i->car_trem >> 4; |
| 763 | c->trcrate = i->car_trem & 15; |
| 764 | c->trccount = 0; |
| 765 | } |
| 766 | |
| 767 | c->arp_size = i->arpeggio & 15; |
| 768 | c->arp_speed = i->arpeggio >> 4; |
| 769 | memcpy(c->arp_tab, i->arp_tab, 12); |
| 770 | c->keycount = i->keyoff; |
| 771 | c->nextvol = c->glideto = c->finetune = c->vibcount = c->arp_pos = c->arp_count = 0; |
| 772 | } |