2020-05-29 17:50:14 +02:00
|
|
|
/* 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 "audio/miles.h"
|
|
|
|
|
|
|
|
#include "common/config-manager.h"
|
|
|
|
#include "common/file.h"
|
|
|
|
#include "common/mutex.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
#include "common/textconsole.h"
|
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
// Miles Audio MT-32 / General MIDI driver
|
|
|
|
//
|
|
|
|
|
|
|
|
#define MILES_MT32_TIMBREBANK_STANDARD_ROLAND 0
|
|
|
|
#define MILES_MT32_TIMBREBANK_MELODIC_MODULE 127
|
|
|
|
|
|
|
|
const byte milesMT32SysExChansSetup[] = {
|
2020-07-27 14:43:21 +02:00
|
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
|
2020-05-29 17:50:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const byte milesMT32SysExPartialReserveTable[] = {
|
2020-07-27 14:43:21 +02:00
|
|
|
0x03, 0x04, 0x03, 0x04, 0x03, 0x04, 0x03, 0x04, 0x04
|
2020-05-29 17:50:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const byte milesMT32SysExInitReverb[] = {
|
2020-07-27 14:43:21 +02:00
|
|
|
0x00, 0x03, 0x02 // Reverb mode 0, reverb time 3, reverb level 2
|
2020-05-29 17:50:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
MidiDriver_Miles_Midi::MidiDriver_Miles_Midi(MusicType midiType, MilesMT32InstrumentEntry *instrumentTablePtr, uint16 instrumentTableCount) :
|
2020-07-27 14:43:21 +02:00
|
|
|
MidiDriver_MT32GM(midiType), _noteCounter(0) {
|
2020-05-29 17:50:14 +02:00
|
|
|
memset(_patchesBank, 0, sizeof(_patchesBank));
|
|
|
|
|
|
|
|
_instrumentTablePtr = instrumentTablePtr;
|
|
|
|
_instrumentTableCount = instrumentTableCount;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
// Disable user volume scaling by default. Most (all?)
|
|
|
|
// engines using Miles implement this themselves. Can
|
|
|
|
// be turned on using the property function.
|
|
|
|
_userVolumeScaling = false;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
setSourceNeutralVolume(MILES_DEFAULT_SOURCE_NEUTRAL_VOLUME);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MidiDriver_Miles_Midi::~MidiDriver_Miles_Midi() {
|
2020-07-27 14:43:21 +02:00
|
|
|
if (_instrumentTablePtr)
|
|
|
|
delete[] _instrumentTablePtr;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
for (int i = 0; i < MIDI_CHANNEL_COUNT; ++i) {
|
|
|
|
if (_midiChannels[i].unlockData)
|
|
|
|
delete _midiChannels[i].unlockData;
|
|
|
|
}
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
void MidiDriver_Miles_Midi::initControlData() {
|
|
|
|
for (int i = 0; i < MIDI_CHANNEL_COUNT; ++i) {
|
|
|
|
_controlData[i] = _midiChannels[i].currentData = new MilesMidiChannelControlData();
|
|
|
|
_midiChannels[i].unlockData = new MilesMidiChannelControlData();
|
|
|
|
_controlData[i]->volume = _controlData[i]->scaledVolume =
|
|
|
|
(_nativeMT32 ? MT32_DEFAULT_CHANNEL_VOLUME : GM_DEFAULT_CHANNEL_VOLUME);
|
|
|
|
if (_nativeMT32 && i >= 1 && i <= 8) {
|
|
|
|
_midiChannels[i].currentData->program = MT32_DEFAULT_INSTRUMENTS[i - 1];
|
|
|
|
_midiChannels[i].currentData->panPosition = MT32_DEFAULT_PANNING[i - 1];
|
|
|
|
}
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::initMidiDevice() {
|
2020-07-27 14:43:21 +02:00
|
|
|
MidiDriver_MT32GM::initMidiDevice();
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
// Additional Miles AIL specific initialization
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
if (_midiType == MT_MT32 && _nativeMT32) {
|
|
|
|
// init part/channel assignments
|
|
|
|
sysExMT32(milesMT32SysExChansSetup, 9, (0x10 << 14) | (0x00 << 7) | 0x0D, false, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
// partial reserve table
|
|
|
|
sysExMT32(milesMT32SysExPartialReserveTable, 9, (0x10 << 14) | (0x00 << 7) | 0x04, false, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
// init reverb
|
|
|
|
sysExMT32(milesMT32SysExInitReverb, 3, (0x10 << 14) | (0x00 << 7) | 0x01, false, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set Miles default controller values
|
|
|
|
// Note that AIL/MSS apparently did not get full support for GM until
|
|
|
|
// version 3.00 in 09/1994. Many games used the MT-32 driver to
|
|
|
|
// implement GM support. As a result, default parameters were only sent
|
|
|
|
// out on the MT-32 channels (2-10). Also, the default MT-32 instrument
|
|
|
|
// numbers were set on GM devices, even though they map to different
|
|
|
|
// instruments. This is reproduced here to prevent possible issues with
|
|
|
|
// games that depend on this behavior.
|
|
|
|
|
|
|
|
for (int i = 1; i < 10; ++i) {
|
|
|
|
// Volume 7F (max)
|
2020-07-27 14:43:21 +02:00
|
|
|
send(-1, MIDI_COMMAND_CONTROL_CHANGE | i, MIDI_CONTROLLER_VOLUME, 0x7F);
|
2020-05-29 17:50:14 +02:00
|
|
|
if (_midiType == MT_MT32) {
|
|
|
|
// Panning center - not the MT-32 default for all channels
|
2020-07-27 14:43:21 +02:00
|
|
|
send(-1, MIDI_COMMAND_CONTROL_CHANGE | i, MIDI_CONTROLLER_PANNING, 0x40);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
// Patch
|
2020-07-27 14:43:21 +02:00
|
|
|
if (i != MIDI_RHYTHM_CHANNEL) {
|
|
|
|
if (_midiType == MT_GM) {
|
|
|
|
// Send the MT-32 default instrument numbers out to GM devices.
|
|
|
|
send(-1, MIDI_COMMAND_PROGRAM_CHANGE | i, MT32_DEFAULT_INSTRUMENTS[i - 1], 0);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// The following settings are also sent out by the AIL driver:
|
|
|
|
// - Modulation 0
|
|
|
|
// - Expression 7F (max)
|
|
|
|
// - Sustain off
|
|
|
|
// - Pitch bend neutral
|
|
|
|
// These are the default MT-32 and GM settings, so it is not
|
|
|
|
// necessary to send these.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
// MIDI messages can be found at http://www.midi.org/techspecs/midimessages.php
|
2020-05-29 17:50:14 +02:00
|
|
|
void MidiDriver_Miles_Midi::send(int8 source, uint32 b) {
|
2020-07-27 14:43:21 +02:00
|
|
|
assert(source < MAXIMUM_SOURCES);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
byte command = b & 0xf0;
|
|
|
|
byte dataChannel = b & 0xf;
|
|
|
|
byte outputChannel = source < 0 ? dataChannel : _sources[source].channelMap[dataChannel];
|
2020-07-27 14:43:21 +02:00
|
|
|
|
2020-05-29 17:50:14 +02:00
|
|
|
MidiChannelEntry &outputChannelEntry = _midiChannels[outputChannel];
|
|
|
|
// Only send the message to the MIDI device if the channel is not locked or
|
|
|
|
// if the source that locked the channel is sending the message
|
2020-07-27 14:43:21 +02:00
|
|
|
bool channelLockedByOtherSource = source >= 0 && outputChannelEntry.locked &&
|
|
|
|
outputChannelEntry.currentData->source != source;
|
2020-05-29 17:50:14 +02:00
|
|
|
// Track controller changes on the current data if the MIDI message is sent out,
|
|
|
|
// or on the unlock data otherwise.
|
2020-07-27 14:43:21 +02:00
|
|
|
MilesMidiChannelControlData &controlData = channelLockedByOtherSource ?
|
|
|
|
*outputChannelEntry.unlockData : *outputChannelEntry.currentData;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
processEvent(source, b, outputChannel, controlData, channelLockedByOtherSource);
|
2020-07-25 13:20:15 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
if (command == MIDI_COMMAND_NOTE_OFF || command == MIDI_COMMAND_NOTE_ON || command == MIDI_COMMAND_PITCH_BEND ||
|
|
|
|
command == MIDI_COMMAND_POLYPHONIC_AFTERTOUCH || command == MIDI_COMMAND_CHANNEL_AFTERTOUCH) {
|
2020-05-29 17:50:14 +02:00
|
|
|
_noteCounter++;
|
|
|
|
if (controlData.usingCustomTimbre) {
|
|
|
|
// Remember that this timbre got used now
|
|
|
|
_customTimbres[controlData.currentCustomTimbreId].lastUsedNoteCounter = _noteCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
void MidiDriver_Miles_Midi::controlChange(byte outputChannel, byte controllerNumber, byte controllerValue,
|
|
|
|
int8 source, MidiChannelControlData &controlData, bool channelLockedByOtherSource) {
|
|
|
|
assert(source < MAXIMUM_SOURCES);
|
|
|
|
|
|
|
|
MilesMidiChannelControlData &milesControlData = channelLockedByOtherSource ?
|
|
|
|
*_midiChannels[outputChannel].unlockData : *_midiChannels[outputChannel].currentData;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
// XMIDI controllers
|
|
|
|
switch (controllerNumber) {
|
|
|
|
case MILES_CONTROLLER_SELECT_PATCH_BANK:
|
2020-07-27 14:43:21 +02:00
|
|
|
milesControlData.currentPatchBank = controllerValue;
|
2020-05-29 17:50:14 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_PROTECT_TIMBRE:
|
2020-07-27 14:43:21 +02:00
|
|
|
if (milesControlData.usingCustomTimbre) {
|
2020-05-29 17:50:14 +02:00
|
|
|
// custom timbre set on current channel
|
2020-07-27 14:43:21 +02:00
|
|
|
_customTimbres[milesControlData.currentCustomTimbreId].protectionEnabled = controllerValue >= 64;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_LOCK_CHANNEL:
|
|
|
|
if (source >= 0) {
|
|
|
|
if (controllerValue >= 0x40) {
|
|
|
|
lockChannel(source, outputChannel);
|
|
|
|
} else {
|
|
|
|
unlockChannel(outputChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_PROTECT_CHANNEL:
|
|
|
|
if (source >= 0 && !_midiChannels[outputChannel].locked) {
|
|
|
|
_midiChannels[outputChannel].lockProtected = controllerValue >= 0x40;
|
|
|
|
_midiChannels[outputChannel].protectedSource = controllerValue >= 0x40 ? source : -1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XMIDI MT-32 specific controllers
|
2020-06-26 22:23:08 +02:00
|
|
|
if (_midiType == MT_MT32 && _nativeMT32) {
|
2020-05-29 17:50:14 +02:00
|
|
|
switch (controllerNumber) {
|
|
|
|
case MILES_CONTROLLER_PATCH_REVERB:
|
2020-07-27 14:43:21 +02:00
|
|
|
writePatchByte(milesControlData.program, 6, controllerValue);
|
|
|
|
if (!channelLockedByOtherSource)
|
|
|
|
_driver->send(MIDI_COMMAND_PROGRAM_CHANGE | outputChannel | (milesControlData.program << 8));
|
2020-05-29 17:50:14 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_PATCH_BENDER:
|
2020-07-27 14:43:21 +02:00
|
|
|
writePatchByte(milesControlData.program, 4, controllerValue);
|
|
|
|
if (!channelLockedByOtherSource)
|
|
|
|
_driver->send(MIDI_COMMAND_PROGRAM_CHANGE | outputChannel | (milesControlData.program << 8));
|
2020-05-29 17:50:14 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_REVERB_MODE:
|
|
|
|
writeToSystemArea(1, controllerValue);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_REVERB_TIME:
|
|
|
|
writeToSystemArea(2, controllerValue);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_REVERB_LEVEL:
|
|
|
|
writeToSystemArea(3, controllerValue);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MILES_CONTROLLER_RHYTHM_KEY_TIMBRE:
|
2020-07-27 14:43:21 +02:00
|
|
|
if (milesControlData.usingCustomTimbre) {
|
2020-05-29 17:50:14 +02:00
|
|
|
// custom timbre is set on current channel
|
2020-07-27 14:43:21 +02:00
|
|
|
writeRhythmSetup(controllerValue, milesControlData.currentCustomTimbreId);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XMIDI MT-32 SysEx controllers
|
2020-07-27 14:43:21 +02:00
|
|
|
if (_midiType == MT_MT32 && (controllerNumber >= MILES_CONTROLLER_SYSEX_RANGE_BEGIN) &&
|
|
|
|
(controllerNumber <= MILES_CONTROLLER_SYSEX_RANGE_END)) {
|
KYRA/MIDI: Fix minor MT-32/GM issues (#2333)
* KYRA: Added GM initialization for Lands of Lore
The original interpreter of Lands of Lore uses two executables, which
both do initialization of MIDI devices. The main executable runs a
sysex file for GM devices; the intro executable does not.
ScummVM only does MIDI initialization once and it performs this like
the intro executable. I've added the GM initialization of the main
executable. Note that the initialization file consists mostly of
sysexes which alter the display of the SC-55, so it's not particulary
useful. Not many games did this though, so I thought it would be a
fun feature to include.
I also noticed that the check which distinguishes between the two
demo versions of LoL did not work properly; the useAltShapeHeader
flag was true for both versions. I've changed it to check for the
existence of a PAK file which was only included with one of the two
demos.
* MIDI: Delay parser after handling SysEx events
This changes the way delays between SysEx events in MIDI data are handled from
delaying the backend to delaying the MidiParser.
SysEx events require a delay between events to ensure correct processing by the
MIDI device. This is usually implemented by calling OSystem::delayMillis.
Some games use some form of MIDI sequence filled with SysEx messages to
initialize the MIDI device. This is handled by the MidiParser. Using the
delayMillis method causes the following MIDI events to be "bunched up" and be
executed in a shorter timespan than intended.
I've altered this by making the MidiParser stop parsing when a SysEx event is
encountered and not enough time has passed since the last SysEx. After enough
time has passed, the next SysEx is sent and parsing resumes. To facilitate
this, I've introduced an alternate sysExNoDelay fuction on the MidiDiver. This
does not execute the delay itself, but instead returns the required delay time,
so the parser can handle this instead.
I've currently only implemented this method for the Miles MT-32/GM driver. For
other driver implementations, this will call the regular sysEx method and
return a 0 delay time, so there will be no change in behavior.
This restores a sound effect at the end of the Legend of Kyrandia MT-32 MIDI
initialization. Before, the Note On and Note Off events would be transmitted
instantly after each other, causing the notes not to play.
* MIDI: Add instrument bank fallback
Some games rely on a feature of the Roland SC-55 v1.xx that corrects invalid
instrument banks. This feature was removed in later versions of the device and
not implemented in most other GM devices, causing some MIDI data (sound effects
mostly) to play incorrectly.
Specifically, this applies to Lands of Lore. For example, in the intro, the
sound effect of the ring sparkling uses an incorrect instrument bank. Depending
on how the MIDI device handles this, the sound will play correctly, with the
wrong instrument, or not at all.
This commit emulates the SC-55 functionality that corrects the invalid bank
numbers. I've implemented this (partially) on the MidiDriver, so that it can be
re-used for other games (Xeen 4 and 5 also have this issue with some sound
effects).
* KYRA: Start MIDI playback after selecting track
The MIDI parser would automatically start playback after loading MIDI data, but
KYRA engine games use MIDI files with multiple tracks. Because of this it was
necessary to immediately stop playback after loading MIDI data, and then select
the track that should be played for correct playback. The parser now has a
feature to disable automatically starting playback, so I've implemented this
for KYRA.
* KYRA: Improve stopping MIDI playback
In two places All Notes Off events were sent on all channels to stop MIDI
playback of the background music. However, this will also cut off any MIDI
sound effects which are playing. Where needed I've replaced this with simply
stopping playback of the background music; the parser will turn off any active
notes.
I've also made sure playback is stopped before freeing the music data
memory to prevent any issues.
I've added sending All Notes Off events when the game quits, just in case any
hanging notes are not ended by the parsers (should not really be a problem
though).
* MIDI/KYRA: Add pausing playback to MIDI parser
* KYRA: Fix invalid track selection
If a game would attempt to play an invalid track, the parser would start
playing the previously selected track. Fixed this so the parser will not start
playing.
2020-06-20 23:27:30 +02:00
|
|
|
if (!_nativeMT32)
|
|
|
|
return;
|
|
|
|
|
2020-05-29 17:50:14 +02:00
|
|
|
// send SysEx
|
|
|
|
byte sysExQueueNr = 0;
|
|
|
|
|
|
|
|
// figure out which queue is accessed
|
|
|
|
controllerNumber -= MILES_CONTROLLER_SYSEX_RANGE_BEGIN;
|
2020-06-04 15:04:05 +02:00
|
|
|
while (controllerNumber > MILES_CONTROLLER_SYSEX_COMMAND_FINAL_DATA) {
|
2020-05-29 17:50:14 +02:00
|
|
|
sysExQueueNr++;
|
2020-06-04 15:04:05 +02:00
|
|
|
controllerNumber -= (MILES_CONTROLLER_SYSEX_COMMAND_FINAL_DATA + 1);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
assert(sysExQueueNr < MILES_CONTROLLER_SYSEX_QUEUE_COUNT);
|
|
|
|
|
2020-06-26 16:27:33 +02:00
|
|
|
byte sysExPos = _milesSysExQueues[sysExQueueNr].dataPos;
|
2020-05-29 17:50:14 +02:00
|
|
|
bool sysExSend = false;
|
|
|
|
|
|
|
|
switch(controllerNumber) {
|
|
|
|
case MILES_CONTROLLER_SYSEX_COMMAND_ADDRESS1:
|
2020-07-27 14:43:21 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress &= 0x003FFF;
|
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress |= (controllerValue << 14);
|
2020-05-29 17:50:14 +02:00
|
|
|
break;
|
|
|
|
case MILES_CONTROLLER_SYSEX_COMMAND_ADDRESS2:
|
2020-07-27 14:43:21 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress &= 0x1FC07F;
|
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress |= (controllerValue << 7);
|
2020-05-29 17:50:14 +02:00
|
|
|
break;
|
|
|
|
case MILES_CONTROLLER_SYSEX_COMMAND_ADDRESS3:
|
2020-07-27 14:43:21 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress &= 0x1FFF80;
|
2020-06-26 16:27:33 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress |= controllerValue;
|
2020-05-29 17:50:14 +02:00
|
|
|
break;
|
|
|
|
case MILES_CONTROLLER_SYSEX_COMMAND_DATA:
|
|
|
|
if (sysExPos < MILES_CONTROLLER_SYSEX_QUEUE_SIZE) {
|
|
|
|
// Space left? put current byte into queue
|
2020-06-26 16:27:33 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].data[sysExPos] = controllerValue;
|
2020-05-29 17:50:14 +02:00
|
|
|
sysExPos++;
|
2020-06-26 16:27:33 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].dataPos = sysExPos;
|
2020-05-29 17:50:14 +02:00
|
|
|
if (sysExPos >= MILES_CONTROLLER_SYSEX_QUEUE_SIZE) {
|
|
|
|
// overflow? -> send it now
|
|
|
|
sysExSend = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-06-04 15:04:05 +02:00
|
|
|
case MILES_CONTROLLER_SYSEX_COMMAND_FINAL_DATA:
|
|
|
|
if (sysExPos < MILES_CONTROLLER_SYSEX_QUEUE_SIZE) {
|
|
|
|
// Space left? put current byte into queue
|
2020-06-26 16:27:33 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].data[sysExPos] = controllerValue;
|
2020-06-04 15:04:05 +02:00
|
|
|
sysExPos++;
|
|
|
|
// Do not increment dataPos. Subsequent Final Data commands will
|
|
|
|
// re-send the last address byte with the new controller value.
|
|
|
|
sysExSend = true;
|
|
|
|
}
|
2020-05-29 17:50:14 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sysExSend) {
|
|
|
|
if (sysExPos > 0) {
|
|
|
|
// data actually available? -> send it
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(_milesSysExQueues[sysExQueueNr].data, sysExPos, _milesSysExQueues[sysExQueueNr].targetAddress, false, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-06-04 15:04:05 +02:00
|
|
|
// Adjust target address to point at the final data byte, or at the
|
|
|
|
// end of the current data in case of an overflow
|
2020-07-27 14:43:21 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].targetAddress += _milesSysExQueues[sysExQueueNr].dataPos;
|
2020-06-04 15:04:05 +02:00
|
|
|
|
2020-05-29 17:50:14 +02:00
|
|
|
// reset queue data buffer
|
2020-06-26 16:27:33 +02:00
|
|
|
_milesSysExQueues[sysExQueueNr].dataPos = 0;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((controllerNumber >= MILES_CONTROLLER_XMIDI_RANGE_BEGIN) && (controllerNumber <= MILES_CONTROLLER_XMIDI_RANGE_END)) {
|
|
|
|
// XMIDI controllers? Don't send these to the MIDI device
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
// Handle other controllers and send message (if necessary)
|
|
|
|
MidiDriver_MT32GM::controlChange(outputChannel, controllerNumber, controllerValue, source, milesControlData, channelLockedByOtherSource);
|
|
|
|
}
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
bool MidiDriver_Miles_Midi::addActiveNote(uint8 outputChannel, uint8 note, int8 source) {
|
|
|
|
bool added = MidiDriver_MT32GM::addActiveNote(outputChannel, note, source);
|
|
|
|
|
|
|
|
if (added)
|
|
|
|
_midiChannels[outputChannel].activeNotes++;
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MidiDriver_Miles_Midi::removeActiveNote(uint8 outputChannel, uint8 note, int8 source) {
|
|
|
|
bool removed = MidiDriver_MT32GM::removeActiveNote(outputChannel, note, source);
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
if (_midiChannels[outputChannel].activeNotes == 0) {
|
|
|
|
warning("MILES-MIDI: active notes 0 on channel %d when turning off note %x", outputChannel, note);
|
|
|
|
} else {
|
|
|
|
_midiChannels[outputChannel].activeNotes--;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
return removed;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::removeActiveNotes(uint8 outputChannel, bool sustainedNotes) {
|
2020-07-27 14:43:21 +02:00
|
|
|
Common::StackLock lock(_activeNotesMutex);
|
|
|
|
|
|
|
|
// Remove sustained or non-sustained notes from the active notes registration
|
2020-05-29 17:50:14 +02:00
|
|
|
for (int i = 0; i < _maximumActiveNotes; ++i) {
|
|
|
|
if (_activeNotes[i].channel == outputChannel && _activeNotes[i].sustain == sustainedNotes) {
|
2020-07-27 14:43:21 +02:00
|
|
|
_activeNotes[i].clear();
|
2020-05-29 17:50:14 +02:00
|
|
|
if (_midiChannels[outputChannel].activeNotes == 0) {
|
2020-07-27 14:43:21 +02:00
|
|
|
warning("MILES-MIDI: active notes 0 on channel %d (sustained %i) when removing active notes", outputChannel, sustainedNotes);
|
2020-05-29 17:50:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
--_midiChannels[outputChannel].activeNotes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::lockChannel(uint8 source, uint8 dataChannel) {
|
2020-07-27 14:43:21 +02:00
|
|
|
assert(source < MAXIMUM_SOURCES);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
int8 lockChannel = findLockChannel();
|
|
|
|
if (lockChannel == -1)
|
|
|
|
// Try again, but consider lock protected channels
|
|
|
|
lockChannel = findLockChannel(true);
|
|
|
|
if (lockChannel == -1)
|
|
|
|
// Could not find a channel to lock
|
|
|
|
return;
|
|
|
|
|
|
|
|
stopNotesOnChannel(lockChannel);
|
|
|
|
|
|
|
|
_midiChannels[lockChannel].locked = true;
|
|
|
|
_midiChannels[lockChannel].lockDataChannel = dataChannel;
|
|
|
|
_sources[source].channelMap[dataChannel] = lockChannel;
|
|
|
|
// Copy current controller values so they can be restored when unlocking the channel
|
2020-07-27 14:43:21 +02:00
|
|
|
*_midiChannels[lockChannel].unlockData = *_midiChannels[lockChannel].currentData;
|
|
|
|
_midiChannels[lockChannel].currentData->source = source;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
// Send volume change to apply the new source volume
|
2020-07-27 14:43:21 +02:00
|
|
|
controlChange(lockChannel, MIDI_CONTROLLER_VOLUME, 0x7F, source, *_midiChannels[lockChannel].currentData);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
// Note that other controller values might be "inherited" from the source
|
|
|
|
// which was previously playing on the locked MIDI channel. The KYRA engine
|
|
|
|
// does not seem to take any precautions against this.
|
|
|
|
// Controllers could be set to default values here.
|
|
|
|
}
|
|
|
|
|
|
|
|
int8 MidiDriver_Miles_Midi::findLockChannel(bool useProtectedChannels) {
|
|
|
|
// Starting at the highest (non-rhythm) channel, find the channel
|
|
|
|
// with the least active notes that isn't already locked.
|
|
|
|
// If useProtectedChannels is false, channels that are protected
|
|
|
|
// from channel locking will not be considered.
|
|
|
|
int8 potentialLockChannel = -1;
|
|
|
|
uint8 notes = 255;
|
2020-07-27 14:43:21 +02:00
|
|
|
for (int i = MIDI_CHANNEL_COUNT - 1; i >= 0; --i) {
|
|
|
|
if (!isOutputChannelUsed(i) || i == MIDI_RHYTHM_CHANNEL || _midiChannels[i].locked ||
|
|
|
|
(!useProtectedChannels && _midiChannels[i].lockProtected))
|
2020-05-29 17:50:14 +02:00
|
|
|
continue;
|
|
|
|
if (_midiChannels[i].activeNotes < notes) {
|
|
|
|
potentialLockChannel = i;
|
|
|
|
notes = _midiChannels[i].activeNotes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return potentialLockChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::unlockChannel(uint8 outputChannel) {
|
|
|
|
MidiChannelEntry &channel = _midiChannels[outputChannel];
|
|
|
|
if (!channel.locked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stopNotesOnChannel(outputChannel);
|
|
|
|
|
|
|
|
// Unlock the channel
|
|
|
|
channel.locked = false;
|
2020-07-27 14:43:21 +02:00
|
|
|
_sources[channel.currentData->source].channelMap[channel.lockDataChannel] = channel.lockDataChannel;
|
2020-05-29 17:50:14 +02:00
|
|
|
channel.lockDataChannel = -1;
|
2020-07-27 14:43:21 +02:00
|
|
|
channel.currentData->source = channel.unlockData->source;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
// Send the unlock channel data to the MIDI device to reset the channel parameters
|
2020-07-27 14:43:21 +02:00
|
|
|
if (channel.unlockData->volume != 0xFF) {
|
|
|
|
controlChange(outputChannel, MIDI_CONTROLLER_VOLUME, channel.unlockData->volume, channel.currentData->source, *channel.currentData);
|
2020-05-29 17:50:14 +02:00
|
|
|
} else {
|
2020-07-27 14:43:21 +02:00
|
|
|
channel.currentData->volume = 0xFF;
|
|
|
|
}
|
|
|
|
if (channel.currentData->modulation != channel.unlockData->modulation)
|
|
|
|
controlChange(outputChannel, MIDI_CONTROLLER_MODULATION, channel.unlockData->modulation, channel.currentData->source, *channel.currentData);
|
|
|
|
if (channel.currentData->panPosition != channel.unlockData->panPosition)
|
|
|
|
controlChange(outputChannel, MIDI_CONTROLLER_PANNING, channel.unlockData->panPosition, channel.currentData->source, *channel.currentData);
|
|
|
|
if (channel.currentData->expression != channel.unlockData->expression)
|
|
|
|
controlChange(outputChannel, MIDI_CONTROLLER_EXPRESSION, channel.unlockData->expression, channel.currentData->source, *channel.currentData);
|
|
|
|
if (channel.currentData->sustain != channel.unlockData->sustain)
|
|
|
|
controlChange(outputChannel, MIDI_CONTROLLER_SUSTAIN, channel.unlockData->sustain ? 0x7F : 0x00, channel.currentData->source, *channel.currentData);
|
|
|
|
if (channel.currentData->currentPatchBank != channel.unlockData->currentPatchBank)
|
|
|
|
controlChange(outputChannel, MILES_CONTROLLER_SELECT_PATCH_BANK, channel.unlockData->currentPatchBank,
|
|
|
|
channel.currentData->source, *channel.currentData);
|
|
|
|
if (channel.unlockData->program != 0xFF && (channel.currentData->program != channel.unlockData->program ||
|
|
|
|
channel.currentData->currentPatchBank != channel.unlockData->currentPatchBank))
|
|
|
|
programChange(outputChannel, channel.unlockData->program, channel.currentData->source, *channel.currentData);
|
|
|
|
if (channel.currentData->pitchWheel != channel.unlockData->pitchWheel)
|
|
|
|
send(channel.currentData->source, MIDI_COMMAND_PITCH_BEND | outputChannel,
|
|
|
|
channel.unlockData->pitchWheel & 0x7F, (channel.unlockData->pitchWheel >> 7) & 0x7F);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::stopNotesOnChannel(uint8 outputChannelNumber) {
|
|
|
|
MidiChannelEntry &channel = _midiChannels[outputChannelNumber];
|
2020-07-27 14:43:21 +02:00
|
|
|
if (channel.currentData->sustain) {
|
|
|
|
controlChange(outputChannelNumber, MIDI_CONTROLLER_SUSTAIN, 0, channel.currentData->source, *channel.currentData);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
if (channel.activeNotes > 0) {
|
2020-07-27 14:43:21 +02:00
|
|
|
controlChange(outputChannelNumber, MIDI_CONTROLLER_ALL_NOTES_OFF, 0, channel.currentData->source, *channel.currentData);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 16:02:20 +02:00
|
|
|
void MidiDriver_Miles_Midi::stopAllNotes(bool stopSustainedNotes) {
|
2020-07-27 14:43:21 +02:00
|
|
|
MidiDriver_MT32GM::stopAllNotes(stopSustainedNotes);
|
2020-06-27 16:02:20 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
for (int i = 0; i < MIDI_CHANNEL_COUNT; ++i) {
|
|
|
|
if (isOutputChannelUsed(i))
|
|
|
|
_midiChannels[i].activeNotes = 0;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
void MidiDriver_Miles_Midi::programChange(byte outputChannel, byte patchId, int8 source,
|
|
|
|
MidiChannelControlData &controlData, bool channelLockedByOtherSource) {
|
|
|
|
MilesMidiChannelControlData &milesControlData = channelLockedByOtherSource ?
|
|
|
|
*_midiChannels[outputChannel].unlockData : *_midiChannels[outputChannel].currentData;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
if (_midiType == MT_MT32) {
|
2020-07-27 14:43:21 +02:00
|
|
|
byte channelPatchBank = milesControlData.currentPatchBank;
|
2020-05-29 17:50:14 +02:00
|
|
|
byte activePatchBank = _patchesBank[patchId];
|
|
|
|
|
|
|
|
//warning("patch channel %d, patch %x, bank %x", midiChannel, patchId, channelPatchBank);
|
|
|
|
|
|
|
|
if (channelPatchBank != activePatchBank) {
|
|
|
|
// associate patch with timbre
|
|
|
|
setupPatch(channelPatchBank, patchId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a custom patch, remember customTimbreId
|
|
|
|
int16 customTimbre = searchCustomTimbre(channelPatchBank, patchId);
|
|
|
|
if (customTimbre >= 0) {
|
2020-07-27 14:43:21 +02:00
|
|
|
milesControlData.usingCustomTimbre = true;
|
|
|
|
milesControlData.currentCustomTimbreId = customTimbre;
|
KYRA/MIDI: Fix minor MT-32/GM issues (#2333)
* KYRA: Added GM initialization for Lands of Lore
The original interpreter of Lands of Lore uses two executables, which
both do initialization of MIDI devices. The main executable runs a
sysex file for GM devices; the intro executable does not.
ScummVM only does MIDI initialization once and it performs this like
the intro executable. I've added the GM initialization of the main
executable. Note that the initialization file consists mostly of
sysexes which alter the display of the SC-55, so it's not particulary
useful. Not many games did this though, so I thought it would be a
fun feature to include.
I also noticed that the check which distinguishes between the two
demo versions of LoL did not work properly; the useAltShapeHeader
flag was true for both versions. I've changed it to check for the
existence of a PAK file which was only included with one of the two
demos.
* MIDI: Delay parser after handling SysEx events
This changes the way delays between SysEx events in MIDI data are handled from
delaying the backend to delaying the MidiParser.
SysEx events require a delay between events to ensure correct processing by the
MIDI device. This is usually implemented by calling OSystem::delayMillis.
Some games use some form of MIDI sequence filled with SysEx messages to
initialize the MIDI device. This is handled by the MidiParser. Using the
delayMillis method causes the following MIDI events to be "bunched up" and be
executed in a shorter timespan than intended.
I've altered this by making the MidiParser stop parsing when a SysEx event is
encountered and not enough time has passed since the last SysEx. After enough
time has passed, the next SysEx is sent and parsing resumes. To facilitate
this, I've introduced an alternate sysExNoDelay fuction on the MidiDiver. This
does not execute the delay itself, but instead returns the required delay time,
so the parser can handle this instead.
I've currently only implemented this method for the Miles MT-32/GM driver. For
other driver implementations, this will call the regular sysEx method and
return a 0 delay time, so there will be no change in behavior.
This restores a sound effect at the end of the Legend of Kyrandia MT-32 MIDI
initialization. Before, the Note On and Note Off events would be transmitted
instantly after each other, causing the notes not to play.
* MIDI: Add instrument bank fallback
Some games rely on a feature of the Roland SC-55 v1.xx that corrects invalid
instrument banks. This feature was removed in later versions of the device and
not implemented in most other GM devices, causing some MIDI data (sound effects
mostly) to play incorrectly.
Specifically, this applies to Lands of Lore. For example, in the intro, the
sound effect of the ring sparkling uses an incorrect instrument bank. Depending
on how the MIDI device handles this, the sound will play correctly, with the
wrong instrument, or not at all.
This commit emulates the SC-55 functionality that corrects the invalid bank
numbers. I've implemented this (partially) on the MidiDriver, so that it can be
re-used for other games (Xeen 4 and 5 also have this issue with some sound
effects).
* KYRA: Start MIDI playback after selecting track
The MIDI parser would automatically start playback after loading MIDI data, but
KYRA engine games use MIDI files with multiple tracks. Because of this it was
necessary to immediately stop playback after loading MIDI data, and then select
the track that should be played for correct playback. The parser now has a
feature to disable automatically starting playback, so I've implemented this
for KYRA.
* KYRA: Improve stopping MIDI playback
In two places All Notes Off events were sent on all channels to stop MIDI
playback of the background music. However, this will also cut off any MIDI
sound effects which are playing. Where needed I've replaced this with simply
stopping playback of the background music; the parser will turn off any active
notes.
I've also made sure playback is stopped before freeing the music data
memory to prevent any issues.
I've added sending All Notes Off events when the game quits, just in case any
hanging notes are not ended by the parsers (should not really be a problem
though).
* MIDI/KYRA: Add pausing playback to MIDI parser
* KYRA: Fix invalid track selection
If a game would attempt to play an invalid track, the parser would start
playing the previously selected track. Fixed this so the parser will not start
playing.
2020-06-20 23:27:30 +02:00
|
|
|
} else {
|
2020-07-27 14:43:21 +02:00
|
|
|
milesControlData.usingCustomTimbre = false;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
MidiDriver_MT32GM::programChange(outputChannel, patchId, source, milesControlData, channelLockedByOtherSource);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int16 MidiDriver_Miles_Midi::searchCustomTimbre(byte patchBank, byte patchId) {
|
|
|
|
byte customTimbreId = 0;
|
|
|
|
|
|
|
|
for (customTimbreId = 0; customTimbreId < MILES_MT32_CUSTOMTIMBRE_COUNT; customTimbreId++) {
|
|
|
|
if (_customTimbres[customTimbreId].used) {
|
2020-07-27 14:43:21 +02:00
|
|
|
if ((_customTimbres[customTimbreId].currentPatchBank == patchBank) &&
|
|
|
|
(_customTimbres[customTimbreId].currentPatchId == patchId)) {
|
2020-05-29 17:50:14 +02:00
|
|
|
return customTimbreId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MilesMT32InstrumentEntry *MidiDriver_Miles_Midi::searchCustomInstrument(byte patchBank, byte patchId) {
|
|
|
|
const MilesMT32InstrumentEntry *instrumentPtr = _instrumentTablePtr;
|
|
|
|
|
|
|
|
for (uint16 instrumentNr = 0; instrumentNr < _instrumentTableCount; instrumentNr++) {
|
|
|
|
if ((instrumentPtr->bankId == patchBank) && (instrumentPtr->patchId == patchId))
|
|
|
|
return instrumentPtr;
|
|
|
|
instrumentPtr++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:27:33 +02:00
|
|
|
void MidiDriver_Miles_Midi::setupPatch(byte patchBank, byte patchId, bool useSysExQueue) {
|
2020-05-29 17:50:14 +02:00
|
|
|
_patchesBank[patchId] = patchBank;
|
|
|
|
|
|
|
|
if (patchBank) {
|
|
|
|
// non-built-in bank
|
|
|
|
int16 customTimbreId = searchCustomTimbre(patchBank, patchId);
|
|
|
|
if (customTimbreId >= 0) {
|
|
|
|
// now available? -> use this timbre
|
2020-06-26 16:27:33 +02:00
|
|
|
writePatchTimbre(patchId, 2, customTimbreId, useSysExQueue); // Group MEMORY
|
2020-05-29 17:50:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for built-in bank (or timbres, that are not available) use default MT32 timbres
|
|
|
|
byte timbreId = patchId & 0x3F;
|
|
|
|
if (!(patchId & 0x40)) {
|
2020-06-26 16:27:33 +02:00
|
|
|
writePatchTimbre(patchId, 0, timbreId, useSysExQueue); // Group A
|
2020-05-29 17:50:14 +02:00
|
|
|
} else {
|
2020-06-26 16:27:33 +02:00
|
|
|
writePatchTimbre(patchId, 1, timbreId, useSysExQueue); // Group B
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::processXMIDITimbreChunk(const byte *timbreListPtr, uint32 timbreListSize) {
|
2020-06-26 22:23:08 +02:00
|
|
|
if (_midiType != MT_MT32)
|
|
|
|
// Some GM files contain timbre chunks, but custom patches cannot
|
|
|
|
// be loaded on a GM device.
|
|
|
|
return;
|
|
|
|
|
2020-05-29 17:50:14 +02:00
|
|
|
uint16 timbreCount = 0;
|
|
|
|
uint32 expectedSize = 0;
|
|
|
|
const byte *timbreListSeeker = timbreListPtr;
|
|
|
|
|
|
|
|
if (timbreListSize < 2) {
|
|
|
|
warning("MILES-MIDI: XMIDI-TIMB chunk - not enough bytes in chunk");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timbreCount = READ_LE_UINT16(timbreListPtr);
|
|
|
|
expectedSize = timbreCount * 2;
|
|
|
|
if (expectedSize > timbreListSize) {
|
|
|
|
warning("MILES-MIDI: XMIDI-TIMB chunk - size mismatch");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timbreListSeeker += 2;
|
|
|
|
|
|
|
|
while (timbreCount) {
|
|
|
|
const byte patchId = *timbreListSeeker++;
|
|
|
|
const byte patchBank = *timbreListSeeker++;
|
|
|
|
int16 customTimbreId = 0;
|
|
|
|
|
|
|
|
switch (patchBank) {
|
|
|
|
case MILES_MT32_TIMBREBANK_STANDARD_ROLAND:
|
|
|
|
case MILES_MT32_TIMBREBANK_MELODIC_MODULE:
|
|
|
|
// ignore those 2 banks
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Check, if this timbre was already loaded
|
|
|
|
customTimbreId = searchCustomTimbre(patchBank, patchId);
|
|
|
|
|
|
|
|
if (customTimbreId < 0) {
|
|
|
|
// currently not loaded, try to install it
|
|
|
|
installCustomTimbre(patchBank, patchId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timbreCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
int16 MidiDriver_Miles_Midi::installCustomTimbre(byte patchBank, byte patchId) {
|
|
|
|
switch(patchBank) {
|
|
|
|
case MILES_MT32_TIMBREBANK_STANDARD_ROLAND: // Standard Roland MT32 bank
|
|
|
|
case MILES_MT32_TIMBREBANK_MELODIC_MODULE: // Reserved for melodic mode
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Original driver did a search for custom timbre here
|
|
|
|
// and in case it was found, it would call setup_patch()
|
|
|
|
// we are called from within setup_patch(), so this isn't needed
|
|
|
|
|
|
|
|
int16 customTimbreId = -1;
|
|
|
|
int16 leastUsedTimbreId = -1;
|
|
|
|
uint32 leastUsedTimbreNoteCounter = _noteCounter;
|
|
|
|
const MilesMT32InstrumentEntry *instrumentPtr = NULL;
|
|
|
|
|
|
|
|
// Check, if requested instrument is actually available
|
|
|
|
instrumentPtr = searchCustomInstrument(patchBank, patchId);
|
|
|
|
if (!instrumentPtr) {
|
|
|
|
warning("MILES-MIDI: instrument not found during installCustomTimbre()");
|
|
|
|
return -1; // not found -> bail out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for an empty timbre slot
|
|
|
|
// or get the least used non-protected slot
|
|
|
|
for (byte customTimbreNr = 0; customTimbreNr < MILES_MT32_CUSTOMTIMBRE_COUNT; customTimbreNr++) {
|
|
|
|
if (!_customTimbres[customTimbreNr].used) {
|
|
|
|
// found an empty slot -> use this one
|
|
|
|
customTimbreId = customTimbreNr;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// used slot
|
|
|
|
if (!_customTimbres[customTimbreNr].protectionEnabled) {
|
|
|
|
// not protected
|
|
|
|
uint32 customTimbreNoteCounter = _customTimbres[customTimbreNr].lastUsedNoteCounter;
|
|
|
|
if (customTimbreNoteCounter < leastUsedTimbreNoteCounter) {
|
|
|
|
leastUsedTimbreId = customTimbreNr;
|
|
|
|
leastUsedTimbreNoteCounter = customTimbreNoteCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (customTimbreId < 0) {
|
|
|
|
// no empty slot found, check if we got a least used non-protected slot
|
|
|
|
if (leastUsedTimbreId < 0) {
|
|
|
|
// everything is protected, bail out
|
|
|
|
warning("MILES-MIDI: no non-protected timbre slots available during installCustomTimbre()");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
customTimbreId = leastUsedTimbreId;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup timbre slot
|
|
|
|
_customTimbres[customTimbreId].used = true;
|
|
|
|
_customTimbres[customTimbreId].currentPatchBank = patchBank;
|
|
|
|
_customTimbres[customTimbreId].currentPatchId = patchId;
|
|
|
|
_customTimbres[customTimbreId].lastUsedNoteCounter = _noteCounter;
|
|
|
|
_customTimbres[customTimbreId].protectionEnabled = false;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
uint32 targetAddress = ((0x08 << 14) | (0x00 << 7) | 0x00) + (customTimbreId * 0x100);
|
|
|
|
uint32 targetAddressCommon = targetAddress;
|
|
|
|
uint32 targetAddressPartial1 = targetAddress + 0x0E;
|
|
|
|
uint32 targetAddressPartial2 = targetAddress + 0x48;
|
|
|
|
uint32 targetAddressPartial3 = targetAddress + 0x82;
|
|
|
|
uint32 targetAddressPartial4 = targetAddress + 0xBC;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
byte parameterData[MILES_MT32_PATCHDATA_TOTAL_SIZE + 1];
|
|
|
|
uint16 parameterDataPos = 0;
|
|
|
|
|
|
|
|
memcpy(parameterData, instrumentPtr->commonParameter, MILES_MT32_PATCHDATA_COMMONPARAMETER_SIZE);
|
|
|
|
parameterDataPos += MILES_MT32_PATCHDATA_COMMONPARAMETER_SIZE;
|
|
|
|
memcpy(parameterData + parameterDataPos, instrumentPtr->partialParameters[0], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE);
|
|
|
|
parameterDataPos += MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE;
|
|
|
|
memcpy(parameterData + parameterDataPos, instrumentPtr->partialParameters[1], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE);
|
|
|
|
parameterDataPos += MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE;
|
|
|
|
memcpy(parameterData + parameterDataPos, instrumentPtr->partialParameters[2], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE);
|
|
|
|
parameterDataPos += MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE;
|
|
|
|
memcpy(parameterData + parameterDataPos, instrumentPtr->partialParameters[3], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE);
|
|
|
|
parameterDataPos += MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE;
|
|
|
|
parameterData[parameterDataPos] = MILES_MT32_SYSEX_TERMINATOR;
|
|
|
|
|
|
|
|
MT32SysEx(targetAddressCommon, parameterData);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// upload common parameter data
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(instrumentPtr->commonParameter, MILES_MT32_PATCHDATA_COMMONPARAMETER_SIZE, targetAddressCommon, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
// upload partial parameter data
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(instrumentPtr->partialParameters[0], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE, targetAddressPartial1, true);
|
|
|
|
sysExMT32(instrumentPtr->partialParameters[1], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE, targetAddressPartial2, true);
|
|
|
|
sysExMT32(instrumentPtr->partialParameters[2], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE, targetAddressPartial3, true);
|
|
|
|
sysExMT32(instrumentPtr->partialParameters[3], MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE, targetAddressPartial4, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-06-26 16:27:33 +02:00
|
|
|
setupPatch(patchBank, patchId, true);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
return customTimbreId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::writeRhythmSetup(byte note, byte customTimbreId) {
|
2020-07-27 14:43:21 +02:00
|
|
|
byte sysExData[1];
|
|
|
|
uint32 targetAddress = (0x03 << 14) | (0x01 << 7) | 0x10;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
targetAddress += ((note - 24) << 2);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
sysExData[0] = customTimbreId;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(sysExData, 1, targetAddress);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:27:33 +02:00
|
|
|
void MidiDriver_Miles_Midi::writePatchTimbre(byte patchId, byte timbreGroup, byte timbreId, bool useSysExQueue) {
|
2020-07-27 14:43:21 +02:00
|
|
|
byte sysExData[2];
|
|
|
|
uint32 targetAddress = (0x05 << 14) | (0x00 << 7) | 0x00;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
// write to patch memory (starts at 0x050000, each entry is 8 bytes)
|
2020-07-27 14:43:21 +02:00
|
|
|
targetAddress += (patchId << 3);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
sysExData[0] = timbreGroup; // 0 - group A, 1 - group B, 2 - memory, 3 - rhythm
|
|
|
|
sysExData[1] = timbreId; // timbre number (0-63)
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(sysExData, 2, targetAddress, useSysExQueue);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::writePatchByte(byte patchId, byte index, byte patchValue) {
|
2020-07-27 14:43:21 +02:00
|
|
|
byte sysExData[1];
|
|
|
|
uint32 targetAddress = (0x05 << 14) | (0x00 << 7) | 0x00;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
targetAddress += (patchId << 3) + index;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
sysExData[0] = patchValue;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(sysExData, 1, targetAddress);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::writeToSystemArea(byte index, byte value) {
|
2020-07-27 14:43:21 +02:00
|
|
|
byte sysExData[1];
|
|
|
|
uint32 targetAddress = (0x10 << 14) | (0x00 << 7) | 0x00;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
targetAddress += index;
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
sysExData[0] = value;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
sysExMT32(sysExData, 1, targetAddress);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
MidiDriver_Miles_Midi *MidiDriver_Miles_MT32_create(const Common::String &instrumentDataFilename) {
|
|
|
|
return MidiDriver_Miles_MIDI_create(MT_MT32, instrumentDataFilename);
|
|
|
|
}
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
MidiDriver_Miles_Midi *MidiDriver_Miles_MIDI_create(MusicType midiType, const Common::String &instrumentDataFilename) {
|
|
|
|
assert(midiType == MT_MT32 || midiType == MT_GM || midiType == MT_GS);
|
|
|
|
|
|
|
|
MilesMT32InstrumentEntry *instrumentTablePtr = NULL;
|
|
|
|
uint16 instrumentTableCount = 0;
|
|
|
|
|
|
|
|
if (midiType == MT_MT32 && !instrumentDataFilename.empty()) {
|
|
|
|
// Load MT32 instrument data from file SAMPLE.MT
|
|
|
|
Common::File *fileStream = new Common::File();
|
|
|
|
uint32 fileSize = 0;
|
|
|
|
byte *fileDataPtr = NULL;
|
|
|
|
uint32 fileDataOffset = 0;
|
|
|
|
uint32 fileDataLeft = 0;
|
|
|
|
|
|
|
|
byte curBankId;
|
|
|
|
byte curPatchId;
|
|
|
|
|
|
|
|
MilesMT32InstrumentEntry *instrumentPtr = NULL;
|
|
|
|
uint32 instrumentOffset;
|
|
|
|
uint16 instrumentDataSize;
|
|
|
|
|
|
|
|
if (!fileStream->open(instrumentDataFilename))
|
2020-06-04 15:04:05 +02:00
|
|
|
error("MILES-MIDI: could not open instrument file '%s'", instrumentDataFilename.c_str());
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
fileSize = fileStream->size();
|
|
|
|
|
|
|
|
fileDataPtr = new byte[fileSize];
|
|
|
|
|
|
|
|
if (fileStream->read(fileDataPtr, fileSize) != fileSize)
|
|
|
|
error("MILES-MIDI: error while reading instrument file");
|
|
|
|
fileStream->close();
|
|
|
|
delete fileStream;
|
|
|
|
|
|
|
|
// File is like this:
|
|
|
|
// [patch:BYTE] [bank:BYTE] [patchoffset:UINT32]
|
|
|
|
// ...
|
|
|
|
// until patch + bank are both 0xFF, which signals end of header
|
|
|
|
|
|
|
|
// First we check how many entries there are
|
|
|
|
fileDataOffset = 0;
|
|
|
|
fileDataLeft = fileSize;
|
|
|
|
while (1) {
|
|
|
|
if (fileDataLeft < 6)
|
|
|
|
error("MILES-MIDI: unexpected EOF in instrument file");
|
|
|
|
|
|
|
|
curPatchId = fileDataPtr[fileDataOffset++];
|
|
|
|
curBankId = fileDataPtr[fileDataOffset++];
|
|
|
|
|
|
|
|
if ((curBankId == 0xFF) && (curPatchId == 0xFF))
|
|
|
|
break;
|
|
|
|
|
|
|
|
fileDataOffset += 4; // skip over offset
|
|
|
|
instrumentTableCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instrumentTableCount == 0)
|
|
|
|
error("MILES-MIDI: no instruments in instrument file");
|
|
|
|
|
|
|
|
// Allocate space for instruments
|
|
|
|
instrumentTablePtr = new MilesMT32InstrumentEntry[instrumentTableCount];
|
|
|
|
|
|
|
|
// Now actually read all entries
|
|
|
|
instrumentPtr = instrumentTablePtr;
|
|
|
|
|
|
|
|
fileDataOffset = 0;
|
|
|
|
while (1) {
|
|
|
|
curPatchId = fileDataPtr[fileDataOffset++];
|
|
|
|
curBankId = fileDataPtr[fileDataOffset++];
|
|
|
|
|
|
|
|
if ((curBankId == 0xFF) && (curPatchId == 0xFF))
|
|
|
|
break;
|
|
|
|
|
|
|
|
instrumentOffset = READ_LE_UINT32(fileDataPtr + fileDataOffset);
|
|
|
|
fileDataOffset += 4;
|
|
|
|
|
|
|
|
instrumentPtr->bankId = curBankId;
|
|
|
|
instrumentPtr->patchId = curPatchId;
|
|
|
|
|
|
|
|
instrumentDataSize = READ_LE_UINT16(fileDataPtr + instrumentOffset);
|
|
|
|
if (instrumentDataSize != (MILES_MT32_PATCHDATA_TOTAL_SIZE + 2))
|
|
|
|
error("MILES-MIDI: unsupported instrument size");
|
|
|
|
|
|
|
|
instrumentOffset += 2;
|
|
|
|
// Copy common parameter data
|
|
|
|
memcpy(instrumentPtr->commonParameter, fileDataPtr + instrumentOffset, MILES_MT32_PATCHDATA_COMMONPARAMETER_SIZE);
|
|
|
|
instrumentOffset += MILES_MT32_PATCHDATA_COMMONPARAMETER_SIZE;
|
|
|
|
|
|
|
|
// Copy partial parameter data
|
|
|
|
for (byte partialNr = 0; partialNr < MILES_MT32_PATCHDATA_PARTIALPARAMETERS_COUNT; partialNr++) {
|
|
|
|
memcpy(&instrumentPtr->partialParameters[partialNr], fileDataPtr + instrumentOffset, MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE);
|
|
|
|
instrumentOffset += MILES_MT32_PATCHDATA_PARTIALPARAMETER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instrument read, next instrument please
|
|
|
|
instrumentPtr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free instrument file data
|
|
|
|
delete[] fileDataPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MidiDriver_Miles_Midi(midiType, instrumentTablePtr, instrumentTableCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::deinitSource(uint8 source) {
|
|
|
|
// Unlock and unprotect channels which were locked or protected by this source.
|
2020-07-27 14:43:21 +02:00
|
|
|
for (int i = 0; i < MIDI_CHANNEL_COUNT; ++i) {
|
2020-05-29 17:50:14 +02:00
|
|
|
if (!isOutputChannelUsed(i))
|
|
|
|
continue;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
if (_midiChannels[i].currentData->source == source && _midiChannels[i].locked) {
|
2020-05-29 17:50:14 +02:00
|
|
|
unlockChannel(i);
|
|
|
|
}
|
|
|
|
if (_midiChannels[i].lockProtected && _midiChannels[i].protectedSource == source) {
|
|
|
|
_midiChannels[i].lockProtected = false;
|
|
|
|
_midiChannels[i].protectedSource = -1;
|
|
|
|
}
|
2020-07-27 14:43:21 +02:00
|
|
|
if (_midiChannels[i].currentData->source == source)
|
|
|
|
_midiChannels[i].currentData->source = -1;
|
|
|
|
if (_midiChannels[i].unlockData->source == source)
|
|
|
|
_midiChannels[i].unlockData->source = -1;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
2020-07-27 14:43:21 +02:00
|
|
|
|
|
|
|
MidiDriver_MT32GM::deinitSource(source);
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MidiDriver_Miles_Midi::setSourceVolume(uint8 source, uint16 volume) {
|
2020-07-27 14:43:21 +02:00
|
|
|
assert(source < MAXIMUM_SOURCES);
|
2020-05-29 17:50:14 +02:00
|
|
|
|
|
|
|
_sources[source].volume = volume;
|
|
|
|
|
2020-07-27 14:43:21 +02:00
|
|
|
for (int i = 0; i < MIDI_CHANNEL_COUNT; ++i) {
|
2020-05-29 17:50:14 +02:00
|
|
|
if (!isOutputChannelUsed(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MidiChannelEntry &channel = _midiChannels[i];
|
2020-07-27 14:43:21 +02:00
|
|
|
MilesMidiChannelControlData *channelData = 0;
|
|
|
|
bool channelLockedByOtherSource = false;
|
2020-05-29 17:50:14 +02:00
|
|
|
// Apply the new source volume to this channel if this source is active
|
|
|
|
// on this channel, or if it was active on the channel before it was
|
|
|
|
// locked.
|
2020-07-27 14:43:21 +02:00
|
|
|
if (channel.currentData->source == source) {
|
|
|
|
channelData = channel.currentData;
|
|
|
|
} else if (channel.locked && channel.unlockData->source == source) {
|
|
|
|
channelData = channel.unlockData;
|
|
|
|
channelLockedByOtherSource = true;
|
2020-05-29 17:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channelData && channelData->volume != 0xFF)
|
2020-07-27 14:43:21 +02:00
|
|
|
controlChange(i, MIDI_CONTROLLER_VOLUME, channelData->volume, source, *channelData, channelLockedByOtherSource);
|
2020-06-26 16:27:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:50:14 +02:00
|
|
|
} // End of namespace Audio
|