Mercurial > audlegacy
annotate Plugins/Input/adplug/core/u6m.cpp @ 919:c19c3ea7d29d trunk
[svn] - fading fix
| author | nenolod |
|---|---|
| date | Thu, 06 Apr 2006 11:03:26 -0700 |
| parents | 2b06eb5e472d |
| children | 6ad7eb96dd26 |
| rev | line source |
|---|---|
| 359 | 1 /* |
| 2 * Adplug - Replayer for many OPL2/OPL3 audio file formats. | |
| 3 * Copyright (C) 1999 - 2003 Simon Peter, <dn.tlp@gmx.net>, et al. | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Lesser General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2.1 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library 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 GNU | |
| 13 * Lesser General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Lesser General Public | |
| 16 * License along with this library; if not, write to the Free Software | |
| 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 * | |
| 19 * u6m.cpp - Ultima 6 Music Player by Marc Winterrowd. | |
| 20 * This code extends the Adlib Winamp plug-in by Simon Peter <dn.tlp@gmx.net> | |
| 21 */ | |
| 22 | |
| 23 #include "u6m.h" | |
| 24 | |
| 25 CPlayer *Cu6mPlayer::factory(Copl *newopl) | |
| 26 { | |
| 27 return new Cu6mPlayer(newopl); | |
| 28 } | |
| 29 | |
| 30 bool Cu6mPlayer::load(const std::string &filename, const CFileProvider &fp) | |
| 31 { | |
| 32 // file validation section | |
| 33 // this section only checks a few *necessary* conditions | |
| 34 unsigned long filesize, decompressed_filesize; | |
| 35 binistream *f; | |
| 36 | |
| 37 f = fp.open(filename); if(!f) return false; | |
| 38 filesize = fp.filesize(f); | |
| 39 | |
| 40 if (filesize >= 6) | |
| 41 { | |
| 42 // check if the file has a valid pseudo-header | |
| 43 unsigned char pseudo_header[6]; | |
| 44 f->readString((char *)pseudo_header, 6); | |
| 45 decompressed_filesize = pseudo_header[0] + (pseudo_header[1] << 8); | |
| 46 | |
| 47 if (!( (pseudo_header[2]==0) && (pseudo_header[3]==0) && | |
| 48 (pseudo_header[4] + ((pseudo_header[5] & 0x1)<<8) == 0x100) && | |
| 49 (decompressed_filesize > (filesize-4)) )) | |
| 50 { | |
| 51 fp.close(f); | |
| 52 return(false); | |
| 53 } | |
| 54 } | |
| 55 else | |
| 56 { | |
| 57 fp.close(f); | |
| 58 return(false); | |
| 59 } | |
| 60 | |
| 61 // load section | |
| 62 song_data = new unsigned char[decompressed_filesize]; | |
| 63 unsigned char* compressed_song_data = new unsigned char[filesize-4]; | |
| 64 | |
| 65 f->seek(4); | |
| 66 f->readString((char *)compressed_song_data, filesize - 4); | |
| 67 fp.close(f); | |
| 68 | |
| 69 // attempt to decompress the song data | |
| 70 // if unsuccessful, deallocate song_data[] on the spot, and return(false) | |
| 71 data_block source, destination; | |
| 72 source.size = filesize-4; | |
| 73 source.data = compressed_song_data; | |
| 74 destination.size = decompressed_filesize; | |
| 75 destination.data = song_data; | |
| 76 | |
| 77 if (!lzw_decompress(source,destination)) | |
| 78 { | |
| 79 delete[] compressed_song_data; | |
| 80 delete[] song_data; | |
| 81 return(false); | |
| 82 } | |
| 83 | |
| 84 // deallocation section | |
| 85 delete[] compressed_song_data; | |
| 86 | |
| 87 rewind(0); | |
| 88 return (true); | |
| 89 } | |
| 90 | |
| 91 | |
| 92 bool Cu6mPlayer::update() | |
| 93 { | |
| 94 if (!driver_active) | |
| 95 { | |
| 96 driver_active = true; | |
| 97 dec_clip(read_delay); | |
| 98 if (read_delay == 0) | |
| 99 { | |
| 100 command_loop(); | |
| 101 } | |
| 102 | |
| 103 // on all Adlib channels: freq slide/vibrato, mute factor slide | |
| 104 for (int i = 0; i < 9; i++) | |
| 105 { | |
| 106 if (channel_freq_signed_delta[i]!=0) | |
| 107 // frequency slide + mute factor slide | |
| 108 { | |
| 109 // freq slide | |
| 110 freq_slide(i); | |
| 111 | |
| 112 // mute factor slide | |
| 113 if (carrier_mf_signed_delta[i]!=0) | |
| 114 { | |
| 115 mf_slide(i); | |
| 116 } | |
| 117 } | |
| 118 else | |
| 119 // vibrato + mute factor slide | |
| 120 { | |
| 121 // vibrato | |
| 122 if ((vb_multiplier[i]!=0) && ((channel_freq[i].hi & 0x20)==0x20)) | |
| 123 { | |
| 124 vibrato(i); | |
| 125 } | |
| 126 | |
| 127 // mute factor slide | |
| 128 if (carrier_mf_signed_delta[i]!=0) | |
| 129 { | |
| 130 mf_slide(i); | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 driver_active = false; | |
| 136 } | |
| 137 | |
| 138 return !songend; | |
| 139 } | |
| 140 | |
| 141 | |
| 142 void Cu6mPlayer::rewind(int subsong) | |
| 143 { | |
| 144 played_ticks = 0; | |
| 145 songend = false; | |
| 146 | |
| 147 // set the driver's internal variables | |
| 148 byte_pair freq_word = {0,0}; | |
| 149 | |
| 150 driver_active = false; | |
| 151 song_pos = 0; | |
| 152 loop_position = 0; // position of the loop point | |
| 153 read_delay = 0; // delay (in timer ticks) before further song data is read | |
| 154 | |
| 155 for (int i = 0; i < 9; i++) | |
| 156 { | |
| 157 // frequency | |
| 158 channel_freq_signed_delta[i] = 0; | |
| 159 channel_freq[i] = freq_word; // Adlib freq settings for each channel | |
| 160 | |
| 161 // vibrato ("vb") | |
| 162 vb_current_value[i] = 0; | |
| 163 vb_double_amplitude[i] = 0; | |
| 164 vb_multiplier[i] = 0; | |
| 165 vb_direction_flag[i] = 0; | |
| 166 | |
| 167 // mute factor ("mf") == ~(volume) | |
| 168 carrier_mf[i] = 0; | |
| 169 carrier_mf_signed_delta[i] = 0; | |
| 170 carrier_mf_mod_delay_backup[i] = 0; | |
| 171 carrier_mf_mod_delay[i] = 0; | |
| 172 } | |
| 173 | |
| 174 while (!subsong_stack.empty()) // empty subsong stack | |
| 175 subsong_stack.pop(); | |
| 176 | |
| 177 opl->init(); | |
| 178 out_adlib(1,32); // go to OPL2 mode | |
| 179 } | |
| 180 | |
| 181 | |
| 182 float Cu6mPlayer::getrefresh() | |
| 183 { | |
| 184 return ((float)60); // the Ultima 6 music driver expects to be called at 60 Hz | |
| 185 } | |
| 186 | |
| 187 | |
| 188 // ============================================================================================ | |
| 189 // | |
| 190 // | |
| 191 // Functions called by load() | |
| 192 // | |
| 193 // | |
| 194 // ============================================================================================ | |
| 195 | |
| 196 | |
| 197 // decompress from memory to memory | |
| 198 bool Cu6mPlayer::lzw_decompress(Cu6mPlayer::data_block source, Cu6mPlayer::data_block dest) | |
| 199 { | |
| 200 bool end_marker_reached = false; | |
| 201 int codeword_size = 9; | |
| 202 long bits_read = 0; | |
| 203 int next_free_codeword = 0x102; | |
| 204 int dictionary_size = 0x200; | |
| 205 MyDict dictionary = MyDict(); | |
| 206 std::stack<unsigned char> root_stack; | |
| 207 | |
| 208 long bytes_written = 0; | |
| 209 | |
| 210 int cW; | |
|
637
2b06eb5e472d
[svn] Sync with upstream. Drop hardware OPL2/3 support, it throws warnings and is not used on most modern machines. Added var inits where GCC 4.0 thought it was a good idea.
chainsaw
parents:
359
diff
changeset
|
211 int pW = 0; |
| 359 | 212 unsigned char C; |
| 213 | |
| 214 while (!end_marker_reached) | |
| 215 { | |
| 216 cW = get_next_codeword(bits_read, source.data, codeword_size); | |
| 217 switch (cW) | |
| 218 { | |
| 219 // re-init the dictionary | |
| 220 case 0x100: | |
| 221 codeword_size = 9; | |
| 222 next_free_codeword = 0x102; | |
| 223 dictionary_size = 0x200; | |
| 224 dictionary.reset(); | |
| 225 cW = get_next_codeword(bits_read, source.data, codeword_size); | |
| 226 output_root((unsigned char)cW, dest.data, bytes_written); | |
| 227 break; | |
| 228 // end of compressed file has been reached | |
| 229 case 0x101: | |
| 230 end_marker_reached = true; | |
| 231 break; | |
| 232 // (cW <> 0x100) && (cW <> 0x101) | |
| 233 default: | |
| 234 if (cW < next_free_codeword) // codeword is already in the dictionary | |
| 235 { | |
| 236 // create the string associated with cW (on the stack) | |
| 237 get_string(cW,dictionary,root_stack); | |
| 238 C = root_stack.top(); | |
| 239 // output the string represented by cW | |
| 240 while (!root_stack.empty()) | |
| 241 { | |
| 242 output_root(root_stack.top(), dest.data, bytes_written); | |
| 243 root_stack.pop(); | |
| 244 } | |
| 245 // add pW+C to the dictionary | |
| 246 dictionary.add(C,pW); | |
| 247 | |
| 248 next_free_codeword++; | |
| 249 if (next_free_codeword >= dictionary_size) | |
| 250 { | |
| 251 if (codeword_size < max_codeword_length) | |
| 252 { | |
| 253 codeword_size += 1; | |
| 254 dictionary_size *= 2; | |
| 255 } | |
| 256 } | |
| 257 } | |
| 258 else // codeword is not yet defined | |
| 259 { | |
| 260 // create the string associated with pW (on the stack) | |
| 261 get_string(pW,dictionary,root_stack); | |
| 262 C = root_stack.top(); | |
| 263 // output the string represented by pW | |
| 264 while (!root_stack.empty()) | |
| 265 { | |
| 266 output_root(root_stack.top(), dest.data, bytes_written); | |
| 267 root_stack.pop(); | |
| 268 } | |
| 269 // output the char C | |
| 270 output_root(C, dest.data, bytes_written); | |
| 271 | |
| 272 // the new dictionary entry must correspond to cW | |
| 273 // if it doesn't, something is wrong with the lzw-compressed data. | |
| 274 if (cW != next_free_codeword) | |
| 275 { | |
| 276 /* printf("cW != next_free_codeword!\n"); | |
| 277 exit(-1); */ | |
| 278 return false; | |
| 279 } | |
| 280 // add pW+C to the dictionary | |
| 281 dictionary.add(C,pW); | |
| 282 | |
| 283 next_free_codeword++; | |
| 284 if (next_free_codeword >= dictionary_size) | |
| 285 { | |
| 286 if (codeword_size < max_codeword_length) | |
| 287 { | |
| 288 codeword_size += 1; | |
| 289 dictionary_size *= 2; | |
| 290 } | |
| 291 } | |
| 292 }; | |
| 293 break; | |
| 294 } | |
| 295 // shift roles - the current cW becomes the new pW | |
| 296 pW = cW; | |
| 297 } | |
| 298 | |
| 299 return(true); // indicate successful decompression | |
| 300 } | |
| 301 | |
| 302 | |
| 303 // -------------------- | |
| 304 // Additional functions | |
| 305 // -------------------- | |
| 306 | |
| 307 | |
| 308 // Read the next code word from the source buffer | |
| 309 int Cu6mPlayer::get_next_codeword (long& bits_read, unsigned char *source, int codeword_size) | |
| 310 { | |
| 311 unsigned char b0,b1,b2; | |
| 312 int codeword; | |
| 313 | |
| 314 b0 = source[bits_read/8]; | |
| 315 b1 = source[bits_read/8+1]; | |
| 316 b2 = source[bits_read/8+2]; | |
| 317 | |
| 318 codeword = ((b2 << 16) + (b1 << 8) + b0); | |
| 319 codeword = codeword >> (bits_read % 8); | |
| 320 switch (codeword_size) | |
| 321 { | |
| 322 case 0x9: | |
| 323 codeword = codeword & 0x1ff; | |
| 324 break; | |
| 325 case 0xa: | |
| 326 codeword = codeword & 0x3ff; | |
| 327 break; | |
| 328 case 0xb: | |
| 329 codeword = codeword & 0x7ff; | |
| 330 break; | |
| 331 case 0xc: | |
| 332 codeword = codeword & 0xfff; | |
| 333 break; | |
| 334 default: | |
| 335 codeword = -1; // indicates that an error has occurred | |
| 336 break; | |
| 337 } | |
| 338 | |
| 339 bits_read += codeword_size; | |
| 340 return (codeword); | |
| 341 } | |
| 342 | |
| 343 | |
| 344 // output a root to memory | |
| 345 void Cu6mPlayer::output_root(unsigned char root, unsigned char *destination, long& position) | |
| 346 { | |
| 347 destination[position] = root; | |
| 348 position++; | |
| 349 } | |
| 350 | |
| 351 | |
| 352 // output the string represented by a codeword | |
| 353 void Cu6mPlayer::get_string(int codeword, Cu6mPlayer::MyDict& dictionary, std::stack<unsigned char>& root_stack) | |
| 354 { | |
| 355 unsigned char root; | |
| 356 int current_codeword; | |
| 357 | |
| 358 current_codeword = codeword; | |
| 359 | |
| 360 while (current_codeword > 0xff) | |
| 361 { | |
| 362 root = dictionary.get_root(current_codeword); | |
| 363 current_codeword = dictionary.get_codeword(current_codeword); | |
| 364 root_stack.push(root); | |
| 365 } | |
| 366 | |
| 367 // push the root at the leaf | |
| 368 root_stack.push((unsigned char)current_codeword); | |
| 369 } | |
| 370 | |
| 371 | |
| 372 // ============================================================================================ | |
| 373 // | |
| 374 // | |
| 375 // Functions called by update() | |
| 376 // | |
| 377 // | |
| 378 // ============================================================================================ | |
| 379 | |
| 380 | |
| 381 // This function reads the song data and executes the embedded commands. | |
| 382 void Cu6mPlayer::command_loop() | |
| 383 { | |
| 384 unsigned char command_byte; // current command byte | |
| 385 int command_nibble_hi; // command byte, bits 4-7 | |
| 386 int command_nibble_lo; // command byte, bite 0-3 | |
| 387 bool repeat_loop = true; // | |
| 388 | |
| 389 do | |
| 390 { | |
| 391 // extract low and high command nibbles | |
| 392 command_byte = read_song_byte(); // implicitly increments song_pos | |
| 393 command_nibble_hi = command_byte >> 4; | |
| 394 command_nibble_lo = command_byte & 0xf; | |
| 395 | |
| 396 switch (command_nibble_hi) | |
| 397 { | |
| 398 case 0x0: command_0(command_nibble_lo); break; | |
| 399 case 0x1: command_1(command_nibble_lo); break; | |
| 400 case 0x2: command_2(command_nibble_lo); break; | |
| 401 case 0x3: command_3(command_nibble_lo); break; | |
| 402 case 0x4: command_4(command_nibble_lo); break; | |
| 403 case 0x5: command_5(command_nibble_lo); break; | |
| 404 case 0x6: command_6(command_nibble_lo); break; | |
| 405 case 0x7: command_7(command_nibble_lo); break; | |
| 406 case 0x8: | |
| 407 switch (command_nibble_lo) | |
| 408 { | |
| 409 case 1: command_81(); break; | |
| 410 case 2: command_82(); repeat_loop = false; break; | |
| 411 case 3: command_83(); break; | |
| 412 case 5: command_85(); break; | |
| 413 case 6: command_86(); break; | |
| 414 default: break; // maybe generate an error? | |
| 415 } | |
| 416 break; | |
| 417 case 0xE: command_E(); break; | |
| 418 case 0xF: command_F(); break; | |
| 419 default: break; // maybe generate an error? | |
| 420 } | |
| 421 | |
| 422 } while (repeat_loop); | |
| 423 } | |
| 424 | |
| 425 | |
| 426 // -------------------------------------------------------- | |
| 427 // The commands supported by the U6 music file format | |
| 428 // -------------------------------------------------------- | |
| 429 | |
| 430 // ---------------------------------------- | |
| 431 // Set octave and frequency, note off | |
| 432 // Format: 0c nn | |
| 433 // c = channel, nn = packed Adlib frequency | |
| 434 // ---------------------------------------- | |
| 435 void Cu6mPlayer::command_0(int channel) | |
| 436 { | |
| 437 unsigned char freq_byte; | |
| 438 byte_pair freq_word; | |
| 439 | |
| 440 freq_byte = read_song_byte(); | |
| 441 freq_word = expand_freq_byte(freq_byte); | |
| 442 set_adlib_freq(channel,freq_word); | |
| 443 } | |
| 444 | |
| 445 | |
| 446 // --------------------------------------------------- | |
| 447 // Set octave and frequency, old note off, new note on | |
| 448 // Format: 1c nn | |
| 449 // c = channel, nn = packed Adlib frequency | |
| 450 // --------------------------------------------------- | |
| 451 void Cu6mPlayer::command_1(int channel) | |
| 452 { | |
| 453 unsigned char freq_byte; | |
| 454 byte_pair freq_word; | |
| 455 | |
| 456 vb_direction_flag[channel] = 0; | |
| 457 vb_current_value[channel] = 0; | |
| 458 | |
| 459 freq_byte = read_song_byte(); | |
| 460 freq_word = expand_freq_byte(freq_byte); | |
| 461 set_adlib_freq(channel,freq_word); | |
| 462 | |
| 463 freq_word.hi = freq_word.hi | 0x20; // note on | |
| 464 set_adlib_freq(channel,freq_word); | |
| 465 } | |
| 466 | |
| 467 | |
| 468 // ---------------------------------------- | |
| 469 // Set octave and frequency, note on | |
| 470 // Format: 2c nn | |
| 471 // c = channel, nn = packed Adlib frequency | |
| 472 // ---------------------------------------- | |
| 473 void Cu6mPlayer::command_2(int channel) | |
| 474 { | |
| 475 unsigned char freq_byte; | |
| 476 byte_pair freq_word; | |
| 477 | |
| 478 freq_byte = read_song_byte(); | |
| 479 freq_word = expand_freq_byte(freq_byte); | |
| 480 freq_word.hi = freq_word.hi | 0x20; // note on | |
| 481 set_adlib_freq(channel,freq_word); | |
| 482 } | |
| 483 | |
| 484 | |
| 485 // -------------------------------------- | |
| 486 // Set "carrier mute factor"==not(volume) | |
| 487 // Format: 3c nn | |
| 488 // c = channel, nn = mute factor | |
| 489 // -------------------------------------- | |
| 490 void Cu6mPlayer::command_3(int channel) | |
| 491 { | |
| 492 unsigned char mf_byte; | |
| 493 | |
| 494 carrier_mf_signed_delta[channel] = 0; | |
| 495 mf_byte = read_song_byte(); | |
| 496 set_carrier_mf(channel,mf_byte); | |
| 497 } | |
| 498 | |
| 499 | |
| 500 // ---------------------------------------- | |
| 501 // set "modulator mute factor"==not(volume) | |
| 502 // Format: 4c nn | |
| 503 // c = channel, nn = mute factor | |
| 504 // ---------------------------------------- | |
| 505 void Cu6mPlayer::command_4(int channel) | |
| 506 { | |
| 507 unsigned char mf_byte; | |
| 508 | |
| 509 mf_byte = read_song_byte(); | |
| 510 set_modulator_mf(channel,mf_byte); | |
| 511 } | |
| 512 | |
| 513 | |
| 514 // -------------------------------------------- | |
| 515 // Set portamento (pitch slide) | |
| 516 // Format: 5c nn | |
| 517 // c = channel, nn = signed channel pitch delta | |
| 518 // -------------------------------------------- | |
| 519 void Cu6mPlayer::command_5(int channel) | |
| 520 { | |
| 521 channel_freq_signed_delta[channel] = read_signed_song_byte(); | |
| 522 } | |
| 523 | |
| 524 | |
| 525 // -------------------------------------------- | |
| 526 // Set vibrato paramters | |
| 527 // Format: 6c mn | |
| 528 // c = channel | |
| 529 // m = vibrato double amplitude | |
| 530 // n = vibrato multiplier | |
| 531 // -------------------------------------------- | |
| 532 void Cu6mPlayer::command_6(int channel) | |
| 533 { | |
| 534 unsigned char vb_parameters; | |
| 535 | |
| 536 vb_parameters = read_song_byte(); | |
| 537 vb_double_amplitude[channel] = vb_parameters >> 4; // high nibble | |
| 538 vb_multiplier[channel] = vb_parameters & 0xF; // low nibble | |
| 539 } | |
| 540 | |
| 541 | |
| 542 // ---------------------------------------- | |
| 543 // Assign Adlib instrument to Adlib channel | |
| 544 // Format: 7c nn | |
| 545 // c = channel, nn = instrument number | |
| 546 // ---------------------------------------- | |
| 547 void Cu6mPlayer::command_7(int channel) | |
| 548 { | |
| 549 int instrument_offset = instrument_offsets[read_song_byte()]; | |
| 550 out_adlib_opcell(channel, false, 0x20, *(song_data + instrument_offset+0)); | |
| 551 out_adlib_opcell(channel, false, 0x40, *(song_data + instrument_offset+1)); | |
| 552 out_adlib_opcell(channel, false, 0x60, *(song_data + instrument_offset+2)); | |
| 553 out_adlib_opcell(channel, false, 0x80, *(song_data + instrument_offset+3)); | |
| 554 out_adlib_opcell(channel, false, 0xE0, *(song_data + instrument_offset+4)); | |
| 555 out_adlib_opcell(channel, true, 0x20, *(song_data + instrument_offset+5)); | |
| 556 out_adlib_opcell(channel, true, 0x40, *(song_data + instrument_offset+6)); | |
| 557 out_adlib_opcell(channel, true, 0x60, *(song_data + instrument_offset+7)); | |
| 558 out_adlib_opcell(channel, true, 0x80, *(song_data + instrument_offset+8)); | |
| 559 out_adlib_opcell(channel, true, 0xE0, *(song_data + instrument_offset+9)); | |
| 560 out_adlib(0xC0+channel, *(song_data + instrument_offset+10)); | |
| 561 } | |
| 562 | |
| 563 | |
| 564 // ------------------------------------------- | |
| 565 // Branch to a new subsong | |
| 566 // Format: 81 nn aa bb | |
| 567 // nn == number of times to repeat the subsong | |
| 568 // aa == subsong offset (low byte) | |
| 569 // bb == subsong offset (high byte) | |
| 570 // ------------------------------------------- | |
| 571 void Cu6mPlayer::command_81() | |
| 572 { | |
| 573 subsong_info new_ss_info; | |
| 574 | |
| 575 new_ss_info.subsong_repetitions = read_song_byte(); | |
| 576 new_ss_info.subsong_start = read_song_byte(); new_ss_info.subsong_start += read_song_byte() << 8; | |
| 577 new_ss_info.continue_pos = song_pos; | |
| 578 | |
| 579 subsong_stack.push(new_ss_info); | |
| 580 song_pos = new_ss_info.subsong_start; | |
| 581 } | |
| 582 | |
| 583 | |
| 584 // ------------------------------------------------------------ | |
| 585 // Stop interpreting commands for this timer tick | |
| 586 // Format: 82 nn | |
| 587 // nn == delay (in timer ticks) until further data will be read | |
| 588 // ------------------------------------------------------------ | |
| 589 void Cu6mPlayer::command_82() | |
| 590 { | |
| 591 read_delay = read_song_byte(); | |
| 592 } | |
| 593 | |
| 594 | |
| 595 // ----------------------------- | |
| 596 // Adlib instrument data follows | |
| 597 // Format: 83 nn <11 bytes> | |
| 598 // nn == instrument number | |
| 599 // ----------------------------- | |
| 600 void Cu6mPlayer::command_83() | |
| 601 { | |
| 602 unsigned char instrument_number = read_song_byte(); | |
| 603 instrument_offsets[instrument_number] = song_pos; | |
| 604 song_pos += 11; | |
| 605 } | |
| 606 | |
| 607 | |
| 608 // ---------------------------------------------- | |
| 609 // Set -1 mute factor slide (upward volume slide) | |
| 610 // Format: 85 cn | |
| 611 // c == channel | |
| 612 // n == slide delay | |
| 613 // ---------------------------------------------- | |
| 614 void Cu6mPlayer::command_85() | |
| 615 { | |
| 616 unsigned char data_byte = read_song_byte(); | |
| 617 int channel = data_byte >> 4; // high nibble | |
| 618 unsigned char slide_delay = data_byte & 0xF; // low nibble | |
| 619 carrier_mf_signed_delta[channel] = +1; | |
| 620 carrier_mf_mod_delay[channel] = slide_delay + 1; | |
| 621 carrier_mf_mod_delay_backup[channel] = slide_delay + 1; | |
| 622 } | |
| 623 | |
| 624 | |
| 625 // ------------------------------------------------ | |
| 626 // Set +1 mute factor slide (downward volume slide) | |
| 627 // Format: 86 cn | |
| 628 // c == channel | |
| 629 // n == slide speed | |
| 630 // ------------------------------------------------ | |
| 631 void Cu6mPlayer::command_86() | |
| 632 { | |
| 633 unsigned char data_byte = read_song_byte(); | |
| 634 int channel = data_byte >> 4; // high nibble | |
| 635 unsigned char slide_delay = data_byte & 0xF; // low nibble | |
| 636 carrier_mf_signed_delta[channel] = -1; | |
| 637 carrier_mf_mod_delay[channel] = slide_delay + 1; | |
| 638 carrier_mf_mod_delay_backup[channel] = slide_delay + 1; | |
| 639 } | |
| 640 | |
| 641 | |
| 642 // -------------- | |
| 643 // Set loop point | |
| 644 // Format: E? | |
| 645 // -------------- | |
| 646 void Cu6mPlayer::command_E() | |
| 647 { | |
| 648 loop_position = song_pos; | |
| 649 } | |
| 650 | |
| 651 | |
| 652 // --------------------------- | |
| 653 // Return from current subsong | |
| 654 // Format: F? | |
| 655 // --------------------------- | |
| 656 void Cu6mPlayer::command_F() | |
| 657 { | |
| 658 if (!subsong_stack.empty()) | |
| 659 { | |
| 660 subsong_info temp = subsong_stack.top(); | |
| 661 subsong_stack.pop(); | |
| 662 temp.subsong_repetitions--; | |
| 663 if (temp.subsong_repetitions==0) | |
| 664 { | |
| 665 song_pos = temp.continue_pos; | |
| 666 } | |
| 667 else | |
| 668 { | |
| 669 song_pos = temp.subsong_start; | |
| 670 subsong_stack.push(temp); | |
| 671 } | |
| 672 } | |
| 673 else | |
| 674 { | |
| 675 song_pos = loop_position; | |
| 676 songend = true; | |
| 677 } | |
| 678 } | |
| 679 | |
| 680 | |
| 681 // -------------------- | |
| 682 // Additional functions | |
| 683 // -------------------- | |
| 684 | |
| 685 // This function decrements its argument, without allowing it to become negative. | |
| 686 void Cu6mPlayer::dec_clip(int& param) | |
| 687 { | |
| 688 param--; | |
| 689 if (param < 0) { param = 0; } | |
| 690 } | |
| 691 | |
| 692 | |
| 693 // Returns the byte at the current song position. | |
| 694 // Side effect: increments song_pos. | |
| 695 unsigned char Cu6mPlayer::read_song_byte() | |
| 696 { | |
| 697 unsigned char song_byte; | |
| 698 song_byte = song_data[song_pos]; | |
| 699 song_pos++; | |
| 700 return(song_byte); | |
| 701 } | |
| 702 | |
| 703 | |
| 704 // Same as read_song_byte(), except that it returns a signed byte | |
| 705 signed char Cu6mPlayer::read_signed_song_byte() | |
| 706 { | |
| 707 unsigned char song_byte; | |
| 708 int signed_value; | |
| 709 song_byte = *(song_data + song_pos); | |
| 710 song_pos++; | |
| 711 if (song_byte <= 127) | |
| 712 { | |
| 713 signed_value = song_byte; | |
| 714 } | |
| 715 else | |
| 716 { | |
| 717 signed_value = (int)song_byte - 0x100; | |
| 718 } | |
| 719 return((signed char)signed_value); | |
| 720 } | |
| 721 | |
| 722 | |
| 723 Cu6mPlayer::byte_pair Cu6mPlayer::expand_freq_byte(unsigned char freq_byte) | |
| 724 { | |
| 725 const byte_pair freq_table[24] = | |
| 726 { | |
| 727 {0x00,0x00}, {0x58,0x01}, {0x82,0x01}, {0xB0,0x01}, | |
| 728 {0xCC,0x01}, {0x03,0x02}, {0x41,0x02}, {0x86,0x02}, | |
| 729 {0x00,0x00}, {0x6A,0x01}, {0x96,0x01}, {0xC7,0x01}, | |
| 730 {0xE4,0x01}, {0x1E,0x02}, {0x5F,0x02}, {0xA8,0x02}, | |
| 731 {0x00,0x00}, {0x47,0x01}, {0x6E,0x01}, {0x9A,0x01}, | |
| 732 {0xB5,0x01}, {0xE9,0x01}, {0x24,0x02}, {0x66,0x02} | |
| 733 }; | |
| 734 | |
| 735 int packed_freq; | |
| 736 int octave; | |
| 737 byte_pair freq_word; | |
| 738 | |
| 739 packed_freq = freq_byte & 0x1F; | |
| 740 octave = freq_byte >> 5; | |
| 741 | |
| 742 // range check (not present in the original U6 music driver) | |
| 743 if (packed_freq >= 24) { packed_freq = 0; } | |
| 744 | |
| 745 freq_word.hi = freq_table[packed_freq].hi + (octave << 2); | |
| 746 freq_word.lo = freq_table[packed_freq].lo; | |
| 747 | |
| 748 return(freq_word); | |
| 749 } | |
| 750 | |
| 751 | |
| 752 void Cu6mPlayer::set_adlib_freq(int channel,Cu6mPlayer::byte_pair freq_word) | |
| 753 { | |
| 754 out_adlib(0xA0+channel,freq_word.lo); | |
| 755 out_adlib(0xB0+channel,freq_word.hi); | |
| 756 // update the Adlib register backups | |
| 757 channel_freq[channel] = freq_word; | |
| 758 } | |
| 759 | |
| 760 | |
| 761 // this function sets the Adlib frequency, but does not update the register backups | |
| 762 void Cu6mPlayer::set_adlib_freq_no_update(int channel,Cu6mPlayer::byte_pair freq_word) | |
| 763 { | |
| 764 out_adlib(0xA0+channel,freq_word.lo); | |
| 765 out_adlib(0xB0+channel,freq_word.hi); | |
| 766 } | |
| 767 | |
| 768 | |
| 769 void Cu6mPlayer::set_carrier_mf(int channel,unsigned char mute_factor) | |
| 770 { | |
| 771 out_adlib_opcell(channel,true,0x40,mute_factor); | |
| 772 carrier_mf[channel] = mute_factor; | |
| 773 } | |
| 774 | |
| 775 | |
| 776 void Cu6mPlayer::set_modulator_mf(int channel,unsigned char mute_factor) | |
| 777 { | |
| 778 out_adlib_opcell(channel,false,0x40,mute_factor); | |
| 779 } | |
| 780 | |
| 781 | |
| 782 void Cu6mPlayer::freq_slide(int channel) | |
| 783 { | |
| 784 byte_pair freq = channel_freq[channel]; | |
| 785 | |
| 786 long freq_word = freq.lo + (freq.hi << 8) + channel_freq_signed_delta[channel]; | |
| 787 if (freq_word < 0) { freq_word += 0x10000; } | |
| 788 if (freq_word > 0xFFFF) { freq_word -= 0x10000; } | |
| 789 | |
| 790 freq.lo = freq_word & 0xFF; | |
| 791 freq.hi = (freq_word >> 8) & 0xFF; | |
| 792 set_adlib_freq(channel,freq); | |
| 793 } | |
| 794 | |
| 795 | |
| 796 void Cu6mPlayer::vibrato(int channel) | |
| 797 { | |
| 798 byte_pair freq; | |
| 799 | |
| 800 if (vb_current_value[channel] >= vb_double_amplitude[channel]) | |
| 801 { vb_direction_flag[channel] = 1; } | |
| 802 else if (vb_current_value[channel] <= 0) | |
| 803 { vb_direction_flag[channel] = 0; } | |
| 804 | |
| 805 if (vb_direction_flag[channel]==0) | |
| 806 { vb_current_value[channel]++; } | |
| 807 else | |
| 808 { vb_current_value[channel]--; } | |
| 809 | |
| 810 long freq_word = channel_freq[channel].lo + (channel_freq[channel].hi << 8); | |
| 811 freq_word += (vb_current_value[channel] - (vb_double_amplitude[channel] >> 1)) | |
| 812 * vb_multiplier[channel]; | |
| 813 if (freq_word < 0) { freq_word += 0x10000; } | |
| 814 if (freq_word > 0xFFFF) { freq_word -= 0x10000; } | |
| 815 | |
| 816 freq.lo = freq_word & 0xFF; | |
| 817 freq.hi = (freq_word >> 8) & 0xFF; | |
| 818 set_adlib_freq_no_update(channel,freq); | |
| 819 } | |
| 820 | |
| 821 | |
| 822 void Cu6mPlayer::mf_slide(int channel) | |
| 823 { | |
| 824 carrier_mf_mod_delay[channel]--; | |
| 825 if (carrier_mf_mod_delay[channel]==0) | |
| 826 { | |
| 827 carrier_mf_mod_delay[channel] = carrier_mf_mod_delay_backup[channel]; | |
| 828 int current_mf = carrier_mf[channel] + carrier_mf_signed_delta[channel]; | |
| 829 if (current_mf > 0x3F) | |
| 830 { | |
| 831 current_mf = 0x3F; | |
| 832 carrier_mf_signed_delta[channel] = 0; | |
| 833 } | |
| 834 else if (current_mf < 0) | |
| 835 { | |
| 836 current_mf = 0; | |
| 837 carrier_mf_signed_delta[channel] = 0; | |
| 838 } | |
| 839 | |
| 840 set_carrier_mf(channel,(unsigned char)current_mf); | |
| 841 } | |
| 842 } | |
| 843 | |
| 844 | |
| 845 void Cu6mPlayer::out_adlib(unsigned char adlib_register, unsigned char adlib_data) | |
| 846 { | |
| 847 opl->write(adlib_register,adlib_data); | |
| 848 } | |
| 849 | |
| 850 | |
| 851 void Cu6mPlayer::out_adlib_opcell(int channel, bool carrier, unsigned char adlib_register, unsigned char out_byte) | |
| 852 { | |
| 853 const unsigned char adlib_channel_to_carrier_offset[9] = | |
| 854 {0x03,0x04,0x05,0x0B,0x0C,0x0D,0x13,0x14,0x15}; | |
| 855 const unsigned char adlib_channel_to_modulator_offset[9] = | |
| 856 {0x00,0x01,0x02,0x08,0x09,0x0A,0x10,0x11,0x12}; | |
| 857 | |
| 858 if (carrier) | |
| 859 { | |
| 860 out_adlib(adlib_register+adlib_channel_to_carrier_offset[channel],out_byte); | |
| 861 } | |
| 862 else | |
| 863 { | |
| 864 out_adlib(adlib_register+adlib_channel_to_modulator_offset[channel],out_byte); | |
| 865 } | |
| 866 } | |
| 867 | |
| 868 | |
| 869 // ============================================================================================ | |
| 870 // | |
| 871 // | |
| 872 // The Dictionary | |
| 873 // | |
| 874 // | |
| 875 // ============================================================================================ | |
| 876 | |
| 877 | |
| 878 Cu6mPlayer::MyDict::MyDict() | |
| 879 { | |
| 880 dict_size = default_dict_size; | |
| 881 dictionary = new dict_entry[dict_size-0x100]; // don't allocate space for the roots | |
| 882 contains = 0x102; | |
| 883 } | |
| 884 | |
| 885 | |
| 886 Cu6mPlayer::MyDict::MyDict(int max_size) | |
| 887 { | |
| 888 dict_size = max_size; | |
| 889 dictionary = new dict_entry[dict_size-0x100]; // don't allocate space for the roots | |
| 890 contains = 0x102; | |
| 891 } | |
| 892 | |
| 893 | |
| 894 // re-initializes the dictionary | |
| 895 void Cu6mPlayer::MyDict::reset() | |
| 896 { | |
| 897 contains = 0x102; | |
| 898 } | |
| 899 | |
| 900 | |
| 901 // Note: If the dictionary is already full, this function does nothing. | |
| 902 void Cu6mPlayer::MyDict::add(unsigned char root, int codeword) | |
| 903 { | |
| 904 if (contains < dict_size) | |
| 905 { | |
| 906 dictionary[contains-0x100].root = root; | |
| 907 dictionary[contains-0x100].codeword = codeword; | |
| 908 contains++; | |
| 909 } | |
| 910 } | |
| 911 | |
| 912 | |
| 913 unsigned char Cu6mPlayer::MyDict::get_root(int codeword) | |
| 914 { | |
| 915 return (dictionary[codeword-0x100].root); | |
| 916 } | |
| 917 | |
| 918 | |
| 919 int Cu6mPlayer::MyDict::get_codeword(int codeword) | |
| 920 { | |
| 921 return (dictionary[codeword-0x100].codeword); | |
| 922 } | |
| 923 |
