1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/debug.h"
#include "common/system.h"
#include "audio/fmopl.h"
#include "audio/mpu401.h"
#include "audio/mididrv.h"
namespace Parallaction {
const uint kNumVoices = 9;
// adlib FM voices 0-5
const uint kNumMelodic = 6;
// adlib FM voice 6 and 7-8
const uint kNumPercussion = 5;
// mask for maximum volume level
#define LEVEL_MASK 0x7f
struct OPLOperator {
uint8 characteristic; // amplitude modulation, vibrato, envelope, keyboard scaling, modulator frequency
uint8 levels;
uint8 attackDecay;
uint8 sustainRelease;
uint8 waveform;
};
struct MelodicProgram {
OPLOperator op[2];
uint8 feedbackAlgo;
};
struct PercussionNote {
OPLOperator op[2];
uint8 feedbackAlgo;
uint8 percussion;
uint8 valid;
uint16 frequency;
uint8 octave;
};
static const MelodicProgram melodicPrograms[128] = {
{{{ 0x1, 0x51, 0xf2, 0xb2, 0x0 }, { 0x11, 0x0, 0xf2, 0xa2, 0x0 }}, 0x0 },
{{{ 0xc2, 0x4b, 0xf1, 0x53, 0x0 }, { 0xd2, 0x0, 0xf2, 0x74, 0x0 }}, 0x4 },
{{{ 0x81, 0x9d, 0xf2, 0x74, 0x0 }, { 0x13, 0x0, 0xf2, 0xf1, 0x0 }}, 0x6 },
{{{ 0x3, 0x4f, 0xf1, 0x53, 0x0 }, { 0x17, 0x3, 0xf2, 0x74, 0x0 }}, 0x6 },
{{{ 0xd1, 0x81, 0x81, 0x73, 0x2 }, { 0xd4, 0x0, 0xe1, 0x34, 0x0 }}, 0x3 },
{{{ 0x1, 0x0, 0x94, 0xa6, 0x0 }, { 0x2, 0x0, 0x83, 0x26, 0x0 }}, 0x1 },
{{{ 0xf3, 0x84, 0x81, 0x2, 0x1 }, { 0x55, 0x80, 0xdd, 0x3, 0x0 }}, 0x4 },
{{{ 0x5, 0x8a, 0xf2, 0x26, 0x0 }, { 0x1, 0x80, 0xf3, 0x48, 0x0 }}, 0x0 },
{{{ 0x32, 0x0, 0xb1, 0x14, 0x0 }, { 0x12, 0x0, 0xfd, 0x36, 0x0 }}, 0x3 },
{{{ 0x1, 0x0, 0x82, 0xa, 0x2 }, { 0x2, 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, 0x0, 0x0 }, { 0x0, 0x0, 0x0, 0x0, 0x0 }}, 0x0, 0x0, 0x0, 0x3fc, 0x4 },
{{{ 0x0, 0x0, 0x0, 0x0, 0x0 }, { 0x0, 0x0, 0x0, 0x0, 0x0 }}, 0x0, 0x0, 0x0, 0x3fc, 0x4 }
};
const uint16 melodicFrequencies[36] = {
0x55, 0x5a, 0x60, 0x66, 0x6c, 0x72, 0x79, 0x80, 0x88,
0x90, 0x99, 0xa1, 0xab, 0xb5, 0xc0, 0xcc, 0xd8, 0xe5,
0xf2, 0x101, 0x110, 0x120, 0x132, 0x143, 0x156, 0x16b, 0x181,
0x198, 0x1b0, 0x1ca, 0x1e5, 0x202, 0x220, 0x241, 0x263, 0x286
};
class AdLibDriver;
class AdLibChannel : public MidiChannel_MPU401 {
public:
void reset();
uint8 _program;
uint8 _volume;
uint8 _pedal;
};
struct MelodicVoice {
bool _used;
uint8 _channel;
uint8 _program;
uint8 _key;
uint32 _timestamp;
uint16 _frequency;
int8 _octave;
};
class AdLibDriver : public MidiDriver {
public:
AdLibDriver(Audio::Mixer *mixer) {
for (uint i = 0; i < 16; ++i)
_channels[i].init(this, i);
_isOpen = false;
_opl = NULL;
memset(_voices, 0, sizeof(_voices));
_lastVoice = 0;
_percussionMask = 0;
_adlibTimerProc = NULL;
_adlibTimerParam = NULL;
}
int open() override;
void close() override;
void send(uint32 b) override;
MidiChannel *allocateChannel() override;
MidiChannel *getPercussionChannel() override { return &_channels[9]; }
bool isOpen() const override { return _isOpen; }
uint32 getBaseTempo() override { return 1000000 / OPL::OPL::kDefaultCallbackFrequency; }
void setTimerCallback(void *timerParam, Common::TimerManager::TimerProc timerProc) override {
_adlibTimerProc = timerProc;
_adlibTimerParam = timerParam;
}
protected:
OPL::OPL *_opl;
AdLibChannel _channels[16];
MelodicVoice _voices[kNumMelodic];
uint8 _notesPerPercussion[kNumPercussion];
uint _lastVoice;
uint8 _percussionMask;
void noteOff(uint8 channel, uint8 note);
void noteOn(uint8 channel, uint8 note, uint8 velocity);
void allNotesOff();
void setModulationWheel(uint8 channel, uint8 value);
void setFootController(uint8 channel, uint8 value);
void setVolume(uint8 channel, uint8 value);
void setPitchBend(uint8 channel, int16 value);
void playNote(uint8 voice, uint8 octave, uint16 frequency);
void programOperatorSimple(uint8 offset, const OPLOperator &op);
void programOperator(uint8 offset, const OPLOperator &op);
void setOperatorLevel(uint8 offset, const OPLOperator &op, uint8 velocity, uint8 channel, bool forceVolume);
void setupPercussion(const PercussionNote ¬e);
void playPercussion(uint8 channel, const PercussionNote ¬e, uint8 velocity);
void programMelodicVoice(uint8 voice, uint8 program);
void playMelodicNote(uint8 voice, uint8 channel, uint8 note, uint8 velocity);
void muteMelodicVoice(uint8 voice);
void initVoices();
private:
void onTimer();
Common::TimerManager::TimerProc _adlibTimerProc;
void *_adlibTimerParam;
bool _isOpen;
};
MidiDriver *createAdLibDriver() {
return new AdLibDriver(g_system->getMixer());
}
void AdLibChannel::reset() {
_program = 0;
_volume = 127;
_pedal = 0;
}
/*
bit 7 - Clear: AM depth is 1 dB
bit 6 - Clear: Vibrato depth is 7 cent
bit 5 - Set: Rhythm enabled (6 melodic voices)
bit 4 - Bass drum off
bit 3 - Snare drum off
bit 2 - Tom tom off
bit 1 - Cymbal off
bit 0 - Hi Hat off
*/
const uint8 kDefaultPercussionMask = 0x20;
int AdLibDriver::open() {
if (_isOpen)
return MERR_ALREADY_OPEN;
_isOpen = true;
_opl = OPL::Config::create();
_opl->init();
_opl->writeReg(0x1, 0x20); // set bit 5 (enable all waveforms)
// Reset the OPL registers.
for (uint i = 0; i < kNumVoices; ++i) {
_opl->writeReg(0xA0 + i, 0); // frequency
_opl->writeReg(0xB0 + i, 0); // key on
_opl->writeReg(0xC0 + i, 0); // feedback
}
_opl->writeReg(0xBD, kDefaultPercussionMask);
initVoices();
_opl->start(new Common::Functor0Mem<void, AdLibDriver>(this, &AdLibDriver::onTimer));
return 0;
}
void AdLibDriver::close() {
if (!_isOpen)
return;
_isOpen = false;
delete _opl;
}
void AdLibDriver::send(uint32 b) {
uint channel = b & 0xf;
uint cmd = (b >> 4) & 0xf;
uint param1 = (b >> 8) & 0xff;
uint param2 = (b >> 16) & 0xff;
switch (cmd) {
case 8:
noteOff(channel, param1);
break;
case 9:
// TODO: map volume?
noteOn(channel, param1, param2);
break;
case 11:
// controller change
switch (param1) {
case 1:
setModulationWheel(channel, param2);
break;
case 4:
setFootController(channel, param2);
break;
case 7:
setVolume(channel, param2);
break;
case 123:
// all notes off
allNotesOff();
break;
default:
break;
}
break;
case 12:
// program change
_channels[channel]._program = param1;
break;
case 14:
setPitchBend(channel, (param1 | (param2 << 7)) - 0x2000);
break;
default:
break;
}
}
void AdLibDriver::noteOff(uint8 channel, uint8 note) {
if (channel == 9) {
if (note < 35 || note > 81)
return;
_percussionMask &= ~(1 << percussionNotes[note - 35].percussion);
_opl->writeReg(0xBD, _percussionMask);
return;
}
for (int i = kNumMelodic - 1; i >= 0; --i) {
if (_voices[i]._channel != channel)
continue;
if (_voices[i]._key != note)
continue;
muteMelodicVoice(i);
_voices[i]._used = false;
return;
}
//debug(1, "failed to find voice off for channel %d, note %d", channel, note);
}
void AdLibDriver::noteOn(uint8 channel, uint8 note, uint8 velocity) {
if (channel == 9) {
if (note < 35 || note > 81)
return;
const PercussionNote &info = percussionNotes[note - 35];
if (!info.valid)
return;
if (note != _notesPerPercussion[info.percussion]) {
setupPercussion(info);
_notesPerPercussion[info.percussion] = note;
}
playPercussion(channel, info, velocity);
return;
}
if (velocity == 0) {
noteOff(channel, note);
return;
}
// We want to play a note on a melodic (voice) channel.
// First, look for a voice playing the same note with the same program.
for (uint i = 0; i < kNumMelodic; ++i) {
if (_voices[i]._channel != channel || _voices[i]._key != note)
continue;
if (_voices[i]._program != _channels[channel]._program)
continue;
muteMelodicVoice(i);
playMelodicNote(i, channel, note, velocity);
return;
}
// The loops below try to start at _lastVoice and find a voice to use.
// They ignore _lastVoice itself, and update _lastVoice if they succeed.
// Then, try finding a melodic voice with the same program.
for (uint i = (_lastVoice + 1) % kNumMelodic; i != _lastVoice; i = (i + 1) % kNumMelodic) {
if (_voices[i]._used)
continue;
if (_voices[i]._program != _channels[channel]._program)
continue;
playMelodicNote(i, channel, note, velocity);
_lastVoice = i;
return;
}
// Then, try finding a free melodic voice of any kind.
for (uint i = (_lastVoice + 1) % kNumMelodic; i != _lastVoice; i = (i + 1) % kNumMelodic) {
if (_voices[i]._used)
continue;
programMelodicVoice(i, _channels[channel]._program);
playMelodicNote(i, channel, note, velocity);
_lastVoice = i;
return;
}
// Then just try finding a melodic voice with the same program,
// and steal it.
for (uint i = (_lastVoice + 1) % kNumMelodic; i != _lastVoice; i = (i + 1) % kNumMelodic) {
if (_voices[i]._program != _channels[channel]._program)
continue;
muteMelodicVoice(i);
playMelodicNote(i, channel, note, velocity);
_lastVoice = i;
return;
}
// Finally, just take control of the channel used least recently.
uint voiceId = 0;
uint32 bestTimestamp = 0xffffffff;
for (uint i = 0; i < kNumMelodic; ++i)
if (bestTimestamp > _voices[i]._timestamp) {
voiceId = i;
bestTimestamp = _voices[i]._timestamp;
}
//debug(1, "ran out of voices for channel %d, note %d, program %d: reused voice %d", channel, note, _channels[channel]._program, voiceId);
programMelodicVoice(voiceId, _channels[channel]._program);
playMelodicNote(voiceId, channel, note, velocity);
_lastVoice = voiceId;
}
// TODO: this doesn't match original
void AdLibDriver::allNotesOff() {
for (uint i = 0; i < kNumMelodic; ++i) {
muteMelodicVoice(i);
_voices[i]._used = false;
}
_percussionMask = kDefaultPercussionMask;
_opl->writeReg(0xBD, kDefaultPercussionMask);
}
void AdLibDriver::setModulationWheel(uint8 channel, uint8 value) {
if (value >= 64)
_percussionMask |= 0x80;
else
_percussionMask &= 0x7f;
_opl->writeReg(0xBD, _percussionMask);
}
void AdLibDriver::setFootController(uint8 channel, uint8 value) {
_channels[channel]._pedal = (value >= 64);
}
void AdLibDriver::setVolume(uint8 channel, uint8 value) {
_channels[channel]._volume = value;
}
void AdLibDriver::setPitchBend(uint8 channel, int16 value) {
for (uint i = 0; i < kNumMelodic; ++i) {
if (_voices[i]._channel != channel || !_voices[i]._used)
continue;
// index into frequency table
uint f = 12 + (_voices[i]._key % 12);
int16 bendAmount = value;
if (bendAmount > 0) {
// bend up two semitones
bendAmount *= (melodicFrequencies[f + 2] - melodicFrequencies[f]);
} else {
// bend down two semitones
bendAmount *= (melodicFrequencies[f] - melodicFrequencies[f - 2]);
}
bendAmount /= 0x2000;
bendAmount += melodicFrequencies[f]; // add the base frequency
playNote(i, _voices[i]._octave, bendAmount);
_voices[i]._timestamp = g_system->getMillis();
}
}
void AdLibDriver::playNote(uint8 voice, uint8 octave, uint16 frequency) {
/* Percussions are always fed keyOn = 0 even to set the note, as they are activated using the
BD register instead. I wonder if they can just be fed the same value as melodic voice and
be done with it. */
uint8 keyOn = (voice < kNumMelodic) ? 0x20 : 0;
// key on, octave, high 2 bits of frequency
_opl->writeReg(0xB0 + voice, keyOn | ((octave & 7) << 2) | ((frequency >> 8) & 3));
// low 8 bits of frequency
_opl->writeReg(0xA0 + voice, frequency & 0xff);
}
void AdLibDriver::programOperatorSimple(uint8 offset, const OPLOperator &op) {
_opl->writeReg(0x40 + offset, op.levels & LEVEL_MASK);
_opl->writeReg(0x60 + offset, op.attackDecay);
_opl->writeReg(0x80 + offset, op.sustainRelease);
}
void AdLibDriver::programOperator(uint8 offset, const OPLOperator &op) {
_opl->writeReg(0x20 + offset, op.characteristic);
_opl->writeReg(0x60 + offset, op.attackDecay);
_opl->writeReg(0x80 + offset, op.sustainRelease);
_opl->writeReg(0xE0 + offset, op.waveform);
_opl->writeReg(0x40 + offset, op.levels);
}
const uint16 adlibLogVolume[] = {
0, 37, 58, 73, 85, 95, 103, 110, 116, 121, 127, 131, 135, 139, 143, 146,
149, 153, 155, 158, 161, 163, 165, 168, 170, 172, 174, 176, 178, 179, 181, 183,
184, 186, 188, 189, 191, 192, 193, 195, 196, 197, 198, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 219,
220, 221, 222, 223, 223, 224, 225, 226, 226, 227, 228, 228, 229, 230, 231, 231,
232, 233, 233, 234, 234, 235, 236, 236, 237, 237, 238, 239, 239, 240, 240, 241,
241, 242, 242, 243, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 248, 249,
249, 250, 250, 251, 251, 252, 252, 253, 253, 253, 254, 254, 255, 255, 256, 256,
256
};
void AdLibDriver::setOperatorLevel(uint8 offset, const OPLOperator &op, uint8 velocity, uint8 channel, bool forceVolume) {
uint8 programLevel = LEVEL_MASK;
if (!forceVolume)
programLevel -= (op.levels & LEVEL_MASK);
uint32 noteLevel = adlibLogVolume[velocity];
uint32 channelLevel = adlibLogVolume[_channels[channel]._volume];
// programLevel comes from the static data and is probably already in the correct logarithmic scale
uint32 finalLevel = LEVEL_MASK - ((noteLevel * channelLevel * programLevel) >> 16);
// high 2 bits are scaling level, the rest is (inversed) volume
_opl->writeReg(0x40 + offset, (op.levels & 0xc0) | (finalLevel & 0x3f));
}
const uint8 operatorOffsetsForPercussion[] = {
0x11, // hi-hat
0x15, // cymbal
0x12, // tom tom
0x14 // snare drum
};
void AdLibDriver::setupPercussion(const PercussionNote ¬e) {
if (note.percussion < 4) {
// simple percussion (1 operator)
// turn off relevant percussion
_percussionMask &= ~(1 << note.percussion);
_opl->writeReg(0xBD, _percussionMask);
programOperatorSimple(operatorOffsetsForPercussion[note.percussion], note.op[0]);
return;
}
// bass drum (2 operators)
// turn off bass drum
_percussionMask &= ~(0x10);
_opl->writeReg(0xBD, _percussionMask);
programOperator(0x10, note.op[0]);
programOperator(0x13, note.op[1]);
_opl->writeReg(0xC0 + 6, note.feedbackAlgo);
}
void AdLibDriver::playPercussion(uint8 channel, const PercussionNote ¬e, uint8 velocity) {
if (note.percussion < 4) {
// simple percussion (1 operator)
// turn off relevant percussion
_percussionMask &= ~(1 << note.percussion);
_opl->writeReg(0xBD, _percussionMask);
setOperatorLevel(operatorOffsetsForPercussion[note.percussion], note.op[0], velocity, channel, true);
if (note.percussion == 2) {
// tom tom
playNote(8, note.octave, note.frequency);
} else if (note.percussion == 3) {
// snare drum
playNote(7, note.octave, note.frequency);
}
// turn on relevant percussion
_percussionMask |= (1 << note.percussion);
_opl->writeReg(0xBD, _percussionMask);
return;
}
// turn off bass drum
_percussionMask &= ~(0x10);
_opl->writeReg(0xBD, _percussionMask);
if (note.feedbackAlgo & 1) {
// operators 1 and 2 in additive synthesis
setOperatorLevel(0x10, note.op[0], velocity, channel, true);
setOperatorLevel(0x13, note.op[1], velocity, channel, true);
} else {
// operator 2 is modulating operator 1
setOperatorLevel(0x13, note.op[1], velocity, channel, true);
}
playNote(6, note.octave, note.frequency);
// turn on bass drum
_percussionMask |= 0x10;
_opl->writeReg(0xBD, _percussionMask);
}
const uint8 offset1ForMelodic[kNumVoices] = { 0x0, 0x1, 0x2, 0x8, 0x9, 0xa, 0x10, 0x11, 0x12 };
const uint8 offset2ForMelodic[kNumVoices] = { 0x3, 0x4, 0x5, 0xb, 0xc, 0xd, 0x13, 0x14, 0x15 };
void AdLibDriver::programMelodicVoice(uint8 voice, uint8 program) {
assert(program < 128);
assert(voice < kNumMelodic);
const MelodicProgram &info = melodicPrograms[program];
uint8 offset1 = offset1ForMelodic[voice];
uint8 offset2 = offset2ForMelodic[voice];
// Start at lowest volume.
_opl->writeReg(0x40 + offset1, LEVEL_MASK);
_opl->writeReg(0x40 + offset2, LEVEL_MASK);
muteMelodicVoice(voice);
programOperator(offset1, info.op[0]);
programOperator(offset2, info.op[1]);
_opl->writeReg(0xC0 + voice, info.feedbackAlgo);
}
void AdLibDriver::playMelodicNote(uint8 voice, uint8 channel, uint8 note, uint8 velocity) {
assert(voice < kNumMelodic);
uint8 octave = note / 12;
uint8 f = 12 + (note % 12);
if (octave > 7)
octave = 7;
const MelodicProgram &info = melodicPrograms[_channels[channel]._program];
uint8 offset1 = offset1ForMelodic[voice];
uint8 offset2 = offset2ForMelodic[voice];
if (info.feedbackAlgo & 1) {
setOperatorLevel(offset1, info.op[0], velocity, channel, false);
setOperatorLevel(offset2, info.op[1], velocity, channel, false);
} else {
setOperatorLevel(offset2, info.op[1], velocity, channel, true);
}
playNote(voice, octave, melodicFrequencies[f]);
_voices[voice]._program = _channels[channel]._program;
_voices[voice]._key = note;
_voices[voice]._channel = channel;
_voices[voice]._timestamp = g_system->getMillis();
_voices[voice]._frequency = melodicFrequencies[f];
_voices[voice]._octave = octave;
_voices[voice]._used = true;
}
void AdLibDriver::muteMelodicVoice(uint8 voice) {
_opl->writeReg(0xB0 + voice, 0 | ((_voices[voice]._octave & 7) << 2) | ((_voices[voice]._frequency >> 8) & 3));
}
MidiChannel *AdLibDriver::allocateChannel() {
for (uint i = 0; i < 16; ++i) {
if (i == 9)
continue;
if (_channels[i].allocate())
return &_channels[i];
}
return NULL;
}
void AdLibDriver::onTimer() {
if (_adlibTimerProc)
(*_adlibTimerProc)(_adlibTimerParam);
}
void AdLibDriver::initVoices() {
_percussionMask = kDefaultPercussionMask;
_opl->writeReg(0xBD, _percussionMask);
for (uint i = 0; i < 16; ++i)
_channels[i].reset();
for (uint i = 0; i < kNumMelodic; ++i) {
_voices[i]._key = 0xff;
_voices[i]._program = 0xff;
_voices[i]._channel = 0xff;
_voices[i]._timestamp = 0;
_voices[i]._frequency = 0;
_voices[i]._octave = 0;
_voices[i]._used = false;
}
for (uint i = 0; i < kNumPercussion; ++i)
_notesPerPercussion[i] = 0xff;
_lastVoice = 0;
}
} // namespace Parallaction
|