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
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/config-manager.h"
#include "audio/mididrv_ms.h"
const uint8 MidiDriver_Multisource::MAXIMUM_SOURCES;
const uint16 MidiDriver_Multisource::DEFAULT_SOURCE_NEUTRAL_VOLUME;
const uint16 MidiDriver_Multisource::FADING_DELAY;
MidiDriver_Multisource::MidiSource::MidiSource() :
type(SOURCE_TYPE_UNDEFINED),
volume(DEFAULT_SOURCE_NEUTRAL_VOLUME),
neutralVolume(DEFAULT_SOURCE_NEUTRAL_VOLUME),
fadeStartVolume(0),
fadeEndVolume(0),
fadePassedTime(0),
fadeDuration(0) { }
MidiDriver_Multisource::ControllerDefaults::ControllerDefaults() :
// The -1 value indicates no default value should be set on the controller.
instrumentBank(-1),
drumkit(-1),
channelPressure(-1),
pitchBend(-1),
modulation(-1),
volume(-1),
panning(-1),
expression(-1),
sustain(-1),
rpn(-1),
pitchBendSensitivity(-1) {
Common::fill(program, program + ARRAYSIZE(program), -1);
}
MidiDriver_Multisource::MidiDriver_Multisource() :
_instrumentRemapping(nullptr),
_userVolumeScaling(false),
_userMusicVolume(192),
_userSfxVolume(192),
_userMute(false),
_timerRate(0),
_fadeDelay(0),
_timer_param(nullptr),
_timer_proc(nullptr) {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
// Default source type: 0 = music, 1+ = SFX
_sources[i].type = (i == 0 ? SOURCE_TYPE_MUSIC : SOURCE_TYPE_SFX);
}
}
void MidiDriver_Multisource::send(uint32 b) {
send(-1, b);
}
uint32 MidiDriver_Multisource::property(int prop, uint32 param) {
switch (prop) {
case PROP_USER_VOLUME_SCALING:
if (param == 0xFFFF)
return _userVolumeScaling ? 1 : 0;
_userVolumeScaling = param > 0;
break;
default:
return MidiDriver::property(prop, param);
}
return 0;
}
void MidiDriver_Multisource::startFade(uint16 duration, uint16 targetVolume) {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
startFade(i, duration, targetVolume);
}
}
void MidiDriver_Multisource::startFade(uint8 source, uint16 duration, uint16 targetVolume) {
Common::StackLock lock(_fadingMutex);
assert(source < MAXIMUM_SOURCES);
// Reset the number of microseconds which have passed since the start of
// the fade.
_sources[source].fadePassedTime = 0;
// Set start volume to current volume.
_sources[source].fadeStartVolume = _sources[source].volume;
_sources[source].fadeEndVolume = targetVolume;
// Convert to microseconds and set the duration. A duration > 0 will cause
// the fade to be processed by updateFading.
_sources[source].fadeDuration = duration * 1000;
}
void MidiDriver_Multisource::abortFade(FadeAbortType abortType) {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
abortFade(i, abortType);
}
}
void MidiDriver_Multisource::abortFade(uint8 source, FadeAbortType abortType) {
Common::StackLock lock(_fadingMutex);
assert(source < MAXIMUM_SOURCES);
if (!isFading(source)) {
// Nothing to abort.
return;
}
// Set the fade duration to 0. This will stop the fade from being processed
// by updateFading.
_sources[source].fadeDuration = 0;
// Now set the intended end volume.
uint16 newSourceVolume;
switch (abortType) {
case FADE_ABORT_TYPE_END_VOLUME:
newSourceVolume = _sources[source].fadeEndVolume;
break;
case FADE_ABORT_TYPE_START_VOLUME:
newSourceVolume = _sources[source].fadeStartVolume;
break;
case FADE_ABORT_TYPE_CURRENT_VOLUME:
default:
return;
}
setSourceVolume(source, newSourceVolume);
}
bool MidiDriver_Multisource::isFading() {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
if (isFading(i))
return true;
}
return false;
}
bool MidiDriver_Multisource::isFading(uint8 source) {
assert(source < MAXIMUM_SOURCES);
return _sources[source].fadeDuration > 0;
}
void MidiDriver_Multisource::updateFading() {
Common::StackLock lock(_fadingMutex);
// Decrease the fade delay by the time that has passed since the last
// fading update.
_fadeDelay -= (_fadeDelay < _timerRate ? _fadeDelay : _timerRate);
bool updatedVolume = false;
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
if (_sources[i].fadeDuration > 0) {
// This source has an active fade.
// Update the time that has passed since the start of the fade.
_sources[i].fadePassedTime += _timerRate;
if (_sources[i].fadePassedTime >= _sources[i].fadeDuration) {
// The fade has finished.
// Set the end volume.
setSourceVolume(i, _sources[i].fadeEndVolume);
updatedVolume = true;
// Stop further processing of this fade.
_sources[i].fadeDuration = 0;
} else if (_fadeDelay == 0) {
// The fade has not yet finished and the fade delay has run
// down. Waiting for the fade delay prevents sending out volume
// updates on every updateFading call, which can overflow
// slower MIDI hardware.
// Set the new volume value.
setSourceVolume(i, ((_sources[i].fadePassedTime * (_sources[i].fadeEndVolume - _sources[i].fadeStartVolume)) /
_sources[i].fadeDuration) + _sources[i].fadeStartVolume);
updatedVolume = true;
}
}
}
if (updatedVolume)
// Set the fade delay to delay the next volume update.
_fadeDelay = FADING_DELAY;
}
void MidiDriver_Multisource::setControllerDefault(ControllerDefaultType type) {
// Call the overload with the General MIDI default for the controller.
switch (type) {
case CONTROLLER_DEFAULT_PITCH_BEND:
setControllerDefault(type, MIDI_PITCH_BEND_DEFAULT);
break;
case CONTROLLER_DEFAULT_VOLUME:
setControllerDefault(type, 100);
break;
case CONTROLLER_DEFAULT_PANNING:
setControllerDefault(type, MIDI_PANNING_DEFAULT);
break;
case CONTROLLER_DEFAULT_EXPRESSION:
setControllerDefault(type, MIDI_EXPRESSION_DEFAULT);
break;
case CONTROLLER_DEFAULT_RPN:
setControllerDefault(type, MIDI_RPN_NULL);
break;
case CONTROLLER_DEFAULT_PITCH_BEND_SENSITIVITY:
setControllerDefault(type, GM_PITCH_BEND_SENSITIVITY_DEFAULT);
break;
case CONTROLLER_DEFAULT_PROGRAM:
case CONTROLLER_DEFAULT_INSTRUMENT_BANK:
case CONTROLLER_DEFAULT_DRUMKIT:
case CONTROLLER_DEFAULT_CHANNEL_PRESSURE:
case CONTROLLER_DEFAULT_MODULATION:
case CONTROLLER_DEFAULT_SUSTAIN:
default:
setControllerDefault(type, 0);
break;
}
}
void MidiDriver_Multisource::setControllerDefault(ControllerDefaultType type, int16 value) {
// Set the specified default value on _controllerDefaults on the field
// corresponding to the specified controller.
switch (type) {
case CONTROLLER_DEFAULT_PROGRAM:
Common::fill(_controllerDefaults.program, _controllerDefaults.program + ARRAYSIZE(_controllerDefaults.program), value);
break;
case CONTROLLER_DEFAULT_INSTRUMENT_BANK:
_controllerDefaults.instrumentBank = value;
break;
case CONTROLLER_DEFAULT_DRUMKIT:
_controllerDefaults.drumkit = value;
break;
case CONTROLLER_DEFAULT_CHANNEL_PRESSURE:
_controllerDefaults.channelPressure = value;
break;
case CONTROLLER_DEFAULT_PITCH_BEND:
_controllerDefaults.pitchBend = value;
break;
case CONTROLLER_DEFAULT_MODULATION:
_controllerDefaults.modulation = value;
break;
case CONTROLLER_DEFAULT_VOLUME:
_controllerDefaults.volume = value;
break;
case CONTROLLER_DEFAULT_PANNING:
_controllerDefaults.panning = value;
break;
case CONTROLLER_DEFAULT_EXPRESSION:
_controllerDefaults.expression = value;
break;
case CONTROLLER_DEFAULT_SUSTAIN:
_controllerDefaults.sustain = value;
break;
case CONTROLLER_DEFAULT_RPN:
_controllerDefaults.rpn = value;
break;
case CONTROLLER_DEFAULT_PITCH_BEND_SENSITIVITY:
_controllerDefaults.pitchBendSensitivity = value;
break;
default:
warning("MidiDriver_Multisource::setControllerDefault - Unknown controller default type %i", type);
break;
}
}
void MidiDriver_Multisource::setControllerDefaults(ControllerDefaultType type, int16 *value) {
// Set the specified default values on _controllerDefaults on the field
// corresponding to the specified controller.
switch (type) {
case CONTROLLER_DEFAULT_PROGRAM:
Common::copy(value, value + ARRAYSIZE(_controllerDefaults.program), _controllerDefaults.program);
break;
default:
warning("MidiDriver_Multisource::setControllerDefaults - Unsupported controller default type %i", type);
break;
}
}
void MidiDriver_Multisource::clearControllerDefault(ControllerDefaultType type) {
// Reset the default value for this controller to -1.
setControllerDefault(type, -1);
}
void MidiDriver_Multisource::deinitSource(uint8 source) {
abortFade(source, FADE_ABORT_TYPE_END_VOLUME);
// Stop all active notes for this source.
stopAllNotes(source, 0xFF);
}
void MidiDriver_Multisource::setSourceType(SourceType type) {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
setSourceType(i, type);
}
}
void MidiDriver_Multisource::setSourceType(uint8 source, SourceType type) {
assert(source < MAXIMUM_SOURCES);
_sources[source].type = type;
// A changed source type can mean a different user volume level should be
// used for this source. Calling applySourceVolume will apply the user
// volume.
applySourceVolume(source);
}
void MidiDriver_Multisource::setSourceVolume(uint16 volume) {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
setSourceVolume(i, volume);
}
}
void MidiDriver_Multisource::setSourceVolume(uint8 source, uint16 volume) {
assert(source < MAXIMUM_SOURCES);
_sources[source].volume = volume;
// Set the volume for active notes and/or MIDI channels for this source.
applySourceVolume(source);
}
void MidiDriver_Multisource::resetSourceVolume() {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
resetSourceVolume(i);
}
}
void MidiDriver_Multisource::resetSourceVolume(uint8 source) {
assert(source < MAXIMUM_SOURCES);
setSourceVolume(source, _sources[source].neutralVolume);
}
void MidiDriver_Multisource::setSourceNeutralVolume(uint16 volume) {
for (int i = 0; i < MAXIMUM_SOURCES; ++i) {
setSourceNeutralVolume(i, volume);
}
}
void MidiDriver_Multisource::setSourceNeutralVolume(uint8 source, uint16 volume) {
assert(source < MAXIMUM_SOURCES);
_sources[source].neutralVolume = volume;
}
void MidiDriver_Multisource::setInstrumentRemapping(const byte *instrumentRemapping) {
_instrumentRemapping = instrumentRemapping;
}
void MidiDriver_Multisource::syncSoundSettings() {
// Get user volume settings.
_userMusicVolume = MIN(256, ConfMan.getInt("music_volume"));
_userSfxVolume = MIN(256, ConfMan.getInt("sfx_volume"));
_userMute = ConfMan.getBool("mute");
// Calling applySourceVolume will apply the user volume.
applySourceVolume(0xFF);
}
void MidiDriver_Multisource::onTimer() {
updateFading();
if (_timer_proc && _timer_param)
_timer_proc(_timer_param);
}
int MidiDriver_NULL_Multisource::open() {
// Setup a timer callback so "fades" will end after the specified time
// (effectively becoming timers, because there is no audio).
_timerRate = getBaseTempo();
g_system->getTimerManager()->installTimerProc(timerCallback, _timerRate, this, "MidiDriver_NULL_Multisource");
return 0;
}
void MidiDriver_NULL_Multisource::close() {
g_system->getTimerManager()->removeTimerProc(timerCallback);
}
void MidiDriver_NULL_Multisource::timerCallback(void *data) {
MidiDriver_NULL_Multisource *driver = (MidiDriver_NULL_Multisource *)data;
driver->onTimer();
}
|