2009-07-01 23:11:56 +00: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.
|
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2009-07-01 23:11:56 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2009-07-01 23:11:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-07-01 23:11:56 +00:00
|
|
|
*
|
|
|
|
*/
|
2009-08-09 18:59:22 +00:00
|
|
|
|
2009-07-01 23:11:56 +00:00
|
|
|
#include "common/scummsys.h"
|
|
|
|
#include "common/stream.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/debug.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2009-07-01 23:11:56 +00:00
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/mods/maxtrax.h"
|
2009-07-04 16:24:15 +00:00
|
|
|
|
2009-08-05 18:00:13 +00:00
|
|
|
// test for engines using this class.
|
2011-10-28 10:32:14 +00:00
|
|
|
#if defined(AUDIO_MODS_MAXTRAX_H)
|
2009-08-05 18:00:13 +00:00
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
namespace {
|
2009-08-08 09:18:23 +00:00
|
|
|
|
|
|
|
enum { K_VALUE = 0x9fd77, PREF_PERIOD = 0x8fd77, PERIOD_LIMIT = 0x6f73d };
|
|
|
|
enum { NO_BEND = 64 << 7, MAX_BEND_RANGE = 24 };
|
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
int32 precalcNote(byte baseNote, int16 tune, byte octave) {
|
2009-08-18 11:34:55 +00:00
|
|
|
return K_VALUE + 0x3C000 - ((baseNote << 14) + (tune << 11) / 3) / 3 - (octave << 16);
|
2009-08-05 19:26:00 +00:00
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
int32 calcVolumeDelta(int32 delta, uint16 time, uint16 vBlankFreq) {
|
|
|
|
const int32 div = time * vBlankFreq;
|
|
|
|
// div <= 1000 means time to small (or even 0)
|
|
|
|
return (div <= 1000) ? delta : (1000 * delta) / div;
|
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
|
2009-08-07 18:02:06 +00:00
|
|
|
int32 calcTempo(const uint16 tempo, uint16 vBlankFreq) {
|
|
|
|
return (int32)(((uint32)(tempo & 0xFFF0) << 8) / (uint16)(5 * vBlankFreq));
|
|
|
|
}
|
2009-08-07 21:00:34 +00:00
|
|
|
|
2009-08-08 09:18:23 +00:00
|
|
|
void nullFunc(int) {}
|
|
|
|
|
2009-08-18 11:34:55 +00:00
|
|
|
// Function to calculate 2^x, where x is a fixedpoint number with 16 fraction bits
|
|
|
|
// using exp would be more accurate and needs less space if mathlibrary is already linked
|
2016-02-14 09:27:44 +00:00
|
|
|
// but this function should be faster and doesn't use floats
|
2009-08-18 11:34:55 +00:00
|
|
|
#if 1
|
|
|
|
inline uint32 pow2Fixed(int32 val) {
|
2009-08-20 15:10:32 +00:00
|
|
|
static const uint16 tablePow2[] = {
|
|
|
|
0, 178, 356, 535, 714, 893, 1073, 1254, 1435, 1617, 1799, 1981, 2164, 2348, 2532, 2716,
|
|
|
|
2902, 3087, 3273, 3460, 3647, 3834, 4022, 4211, 4400, 4590, 4780, 4971, 5162, 5353, 5546, 5738,
|
|
|
|
5932, 6125, 6320, 6514, 6710, 6906, 7102, 7299, 7496, 7694, 7893, 8092, 8292, 8492, 8693, 8894,
|
|
|
|
9096, 9298, 9501, 9704, 9908, 10113, 10318, 10524, 10730, 10937, 11144, 11352, 11560, 11769, 11979, 12189,
|
|
|
|
12400, 12611, 12823, 13036, 13249, 13462, 13676, 13891, 14106, 14322, 14539, 14756, 14974, 15192, 15411, 15630,
|
|
|
|
15850, 16071, 16292, 16514, 16737, 16960, 17183, 17408, 17633, 17858, 18084, 18311, 18538, 18766, 18995, 19224,
|
|
|
|
19454, 19684, 19915, 20147, 20379, 20612, 20846, 21080, 21315, 21550, 21786, 22023, 22260, 22498, 22737, 22977,
|
|
|
|
23216, 23457, 23698, 23940, 24183, 24426, 24670, 24915, 25160, 25406, 25652, 25900, 26148, 26396, 26645, 26895,
|
|
|
|
27146, 27397, 27649, 27902, 28155, 28409, 28664, 28919, 29175, 29432, 29690, 29948, 30207, 30466, 30727, 30988,
|
|
|
|
31249, 31512, 31775, 32039, 32303, 32568, 32834, 33101, 33369, 33637, 33906, 34175, 34446, 34717, 34988, 35261,
|
|
|
|
35534, 35808, 36083, 36359, 36635, 36912, 37190, 37468, 37747, 38028, 38308, 38590, 38872, 39155, 39439, 39724,
|
|
|
|
40009, 40295, 40582, 40870, 41158, 41448, 41738, 42029, 42320, 42613, 42906, 43200, 43495, 43790, 44087, 44384,
|
|
|
|
44682, 44981, 45280, 45581, 45882, 46184, 46487, 46791, 47095, 47401, 47707, 48014, 48322, 48631, 48940, 49251,
|
|
|
|
49562, 49874, 50187, 50500, 50815, 51131, 51447, 51764, 52082, 52401, 52721, 53041, 53363, 53685, 54008, 54333,
|
|
|
|
54658, 54983, 55310, 55638, 55966, 56296, 56626, 56957, 57289, 57622, 57956, 58291, 58627, 58964, 59301, 59640,
|
|
|
|
59979, 60319, 60661, 61003, 61346, 61690, 62035, 62381, 62727, 63075, 63424, 63774, 64124, 64476, 64828, 65182,
|
|
|
|
0
|
|
|
|
};
|
2009-08-18 11:34:55 +00:00
|
|
|
const uint16 whole = val >> 16;
|
|
|
|
const uint8 index = (uint8)(val >> 8);
|
|
|
|
// calculate fractional part.
|
|
|
|
const uint16 base = tablePow2[index];
|
|
|
|
// linear interpolation and add 1.0
|
|
|
|
uint32 exponent = ((uint32)(uint16)(tablePow2[index + 1] - base) * (uint8)val) + ((uint32)base << 8) + (1 << 24);
|
|
|
|
|
|
|
|
if (whole < 24) {
|
|
|
|
// shift away all but the last fractional bit which is used for rounding,
|
|
|
|
// then round to nearest integer
|
|
|
|
exponent = ((exponent >> (23 - whole)) + 1) >> 1;
|
|
|
|
} else if (whole < 32) {
|
|
|
|
// no need to round here
|
|
|
|
exponent <<= whole - 24;
|
|
|
|
} else if (val > 0) {
|
|
|
|
// overflow
|
|
|
|
exponent = 0xFFFFFFFF;
|
|
|
|
} else {
|
|
|
|
// negative integer, test if >= -0.5
|
|
|
|
exponent = (val >= -0x8000) ? 1 : 0;
|
|
|
|
}
|
|
|
|
return exponent;
|
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
#else
|
2009-08-18 11:34:55 +00:00
|
|
|
inline uint32 pow2Fixed(int32 val) {
|
|
|
|
return (uint32)(expf((float)val * (float)(0.69314718055994530942 / (1 << 16))) + 0.5f);
|
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-26 18:36:12 +00:00
|
|
|
} // End of anonymous namespace
|
2009-08-05 19:26:00 +00:00
|
|
|
|
2009-07-04 16:24:15 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
2009-08-10 11:47:49 +00:00
|
|
|
MaxTrax::MaxTrax(int rate, bool stereo, uint16 vBlankFreq, uint16 maxScores)
|
|
|
|
: Paula(stereo, rate, rate / vBlankFreq),
|
2009-08-05 19:26:00 +00:00
|
|
|
_patch(),
|
|
|
|
_scores(),
|
|
|
|
_numScores() {
|
2009-08-10 11:47:49 +00:00
|
|
|
_playerCtx.maxScoreNum = maxScores;
|
|
|
|
_playerCtx.vBlankFreq = vBlankFreq;
|
|
|
|
_playerCtx.frameUnit = (uint16)((1000 << 8) / vBlankFreq);
|
2009-07-06 18:51:22 +00:00
|
|
|
_playerCtx.scoreIndex = -1;
|
2009-08-07 16:21:30 +00:00
|
|
|
_playerCtx.volume = 0x40;
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-07-18 17:27:31 +00:00
|
|
|
_playerCtx.tempo = 120;
|
2009-07-06 18:51:22 +00:00
|
|
|
_playerCtx.tempoTime = 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
_playerCtx.filterOn = true;
|
|
|
|
_playerCtx.syncCallBack = &nullFunc;
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-08-08 09:18:23 +00:00
|
|
|
resetPlayer();
|
2009-07-18 00:04:28 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_channelCtx); ++i)
|
2009-08-08 09:18:23 +00:00
|
|
|
_channelCtx[i].regParamNumber = 0;
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaxTrax::~MaxTrax() {
|
|
|
|
stopMusic();
|
|
|
|
freePatches();
|
|
|
|
freeScores();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaxTrax::interrupt() {
|
2009-07-06 17:37:54 +00:00
|
|
|
// a5 - maxtraxm a4 . globaldata
|
|
|
|
|
2009-07-10 13:38:13 +00:00
|
|
|
// TODO
|
2009-07-06 17:37:54 +00:00
|
|
|
// test for changes in shared struct and make changes
|
|
|
|
// specifically all used channels get marked altered
|
|
|
|
|
|
|
|
_playerCtx.ticks += _playerCtx.tickUnit;
|
|
|
|
const int32 millis = _playerCtx.ticks >> 8; // d4
|
2009-08-08 09:18:23 +00:00
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) {
|
2009-08-08 09:18:23 +00:00
|
|
|
VoiceContext &voice = _voiceCtx[i];
|
|
|
|
if (voice.stopEventTime >= 0) {
|
|
|
|
assert(voice.channel);
|
|
|
|
voice.stopEventTime -= (voice.channel < &_channelCtx[kNumChannels]) ? _playerCtx.tickUnit : _playerCtx.frameUnit;
|
|
|
|
if (voice.stopEventTime <= 0 && voice.status > VoiceContext::kStatusRelease) {
|
|
|
|
if ((voice.channel->flags & ChannelContext::kFlagDamper) != 0)
|
|
|
|
voice.hasDamper = true;
|
|
|
|
else
|
|
|
|
voice.status = VoiceContext::kStatusRelease;
|
2009-07-14 02:15:31 +00:00
|
|
|
}
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-08 09:18:23 +00:00
|
|
|
if (_playerCtx.scoreIndex >= 0) {
|
2009-07-10 16:01:40 +00:00
|
|
|
const Event *curEvent = _playerCtx.nextEvent;
|
2009-07-14 02:15:31 +00:00
|
|
|
int32 eventDelta = _playerCtx.nextEventTime - millis;
|
|
|
|
for (; eventDelta <= 0; eventDelta += (++curEvent)->startTime) {
|
2009-07-06 17:37:54 +00:00
|
|
|
const byte cmd = curEvent->command;
|
2009-08-07 18:02:06 +00:00
|
|
|
ChannelContext &channel = _channelCtx[curEvent->parameter & 0x0F];
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-07-19 00:58:01 +00:00
|
|
|
// outPutEvent(*curEvent);
|
|
|
|
// debug("CurTime, EventDelta, NextDelta: %d, %d, %d", millis, eventDelta, eventDelta + curEvent[1].startTime );
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-07-14 02:15:31 +00:00
|
|
|
if (cmd < 0x80) { // Note
|
2009-08-07 18:02:06 +00:00
|
|
|
const int8 voiceIndex = noteOn(channel, cmd, (curEvent->parameter & 0xF0) >> 1, kPriorityScore);
|
2009-08-08 09:18:23 +00:00
|
|
|
if (voiceIndex >= 0)
|
2009-08-27 16:29:56 +00:00
|
|
|
_voiceCtx[voiceIndex].stopEventTime = MAX<int32>(0, (eventDelta + curEvent->stopTime) << 8);
|
2009-07-14 02:15:31 +00:00
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
} else {
|
|
|
|
switch (cmd) {
|
2009-07-14 02:15:31 +00:00
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
case 0x80: // TEMPO
|
2009-08-07 18:02:06 +00:00
|
|
|
if ((_playerCtx.tickUnit >> 8) > curEvent->stopTime) {
|
|
|
|
_playerCtx.tickUnit = calcTempo(curEvent->parameter << 4, _playerCtx.vBlankFreq);
|
2009-07-06 17:37:54 +00:00
|
|
|
_playerCtx.tempoTime = 0;
|
|
|
|
} else {
|
|
|
|
_playerCtx.tempoStart = _playerCtx.tempo;
|
2009-08-07 18:02:06 +00:00
|
|
|
_playerCtx.tempoDelta = (curEvent->parameter << 4) - _playerCtx.tempoStart;
|
|
|
|
_playerCtx.tempoTime = (curEvent->stopTime << 8);
|
2009-07-06 17:37:54 +00:00
|
|
|
_playerCtx.tempoTicks = 0;
|
|
|
|
}
|
|
|
|
break;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
case 0xC0: // PROGRAM
|
2009-08-07 18:02:06 +00:00
|
|
|
channel.patch = &_patch[curEvent->stopTime & (kNumPatches - 1)];
|
2009-07-06 17:37:54 +00:00
|
|
|
break;
|
2009-07-14 02:15:31 +00:00
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
case 0xE0: // BEND
|
2009-08-07 18:02:06 +00:00
|
|
|
channel.pitchBend = ((curEvent->stopTime & 0x7F00) >> 1) | (curEvent->stopTime & 0x7f);
|
2009-07-14 02:15:31 +00:00
|
|
|
channel.pitchReal = (((int32)channel.pitchBendRange * channel.pitchBend) >> 5) - (channel.pitchBendRange << 8);
|
|
|
|
channel.isAltered = true;
|
2009-07-06 17:37:54 +00:00
|
|
|
break;
|
2009-07-14 02:15:31 +00:00
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
case 0xFF: // END
|
|
|
|
if (_playerCtx.musicLoop) {
|
2009-07-14 02:15:31 +00:00
|
|
|
curEvent = _scores[_playerCtx.scoreIndex].events;
|
|
|
|
eventDelta = curEvent->startTime - millis;
|
2009-07-06 17:37:54 +00:00
|
|
|
_playerCtx.ticks = 0;
|
|
|
|
} else
|
2009-08-08 09:18:23 +00:00
|
|
|
_playerCtx.scoreIndex = -1;
|
2009-07-14 02:15:31 +00:00
|
|
|
// stop processing for this tick
|
|
|
|
goto endOfEventLoop;
|
|
|
|
|
2009-08-07 18:02:06 +00:00
|
|
|
case 0xA0: // SPECIAL
|
2013-01-24 19:15:13 +00:00
|
|
|
switch (curEvent->stopTime >> 8) {
|
2009-08-07 18:02:06 +00:00
|
|
|
case 0x01: // SPECIAL_SYNC
|
2009-08-08 09:18:23 +00:00
|
|
|
_playerCtx.syncCallBack(curEvent->stopTime & 0xFF);
|
2009-08-07 18:02:06 +00:00
|
|
|
break;
|
|
|
|
case 0x02: // SPECIAL_BEGINREP
|
|
|
|
// we allow a depth of 4 loops
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_playerCtx.repeatPoint); ++i) {
|
|
|
|
if (!_playerCtx.repeatPoint[i]) {
|
|
|
|
_playerCtx.repeatPoint[i] = curEvent;
|
|
|
|
_playerCtx.repeatCount[i] = curEvent->stopTime & 0xFF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x03: // SPECIAL_ENDREP
|
|
|
|
for (int i = ARRAYSIZE(_playerCtx.repeatPoint) - 1; i >= 0; --i) {
|
|
|
|
if (_playerCtx.repeatPoint[i]) {
|
|
|
|
if (_playerCtx.repeatCount[i]--)
|
|
|
|
curEvent = _playerCtx.repeatPoint[i]; // gets incremented by 1 at end of loop
|
|
|
|
else
|
|
|
|
_playerCtx.repeatPoint[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-11-28 14:38:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-08-07 18:02:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
case 0xB0: // CONTROL
|
2009-08-07 21:00:34 +00:00
|
|
|
controlCh(channel, (byte)(curEvent->stopTime >> 8), (byte)curEvent->stopTime);
|
|
|
|
break;
|
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
default:
|
|
|
|
debug("Unhandled Command");
|
|
|
|
outPutEvent(*curEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-14 02:15:31 +00:00
|
|
|
endOfEventLoop:
|
2009-07-06 17:37:54 +00:00
|
|
|
_playerCtx.nextEvent = curEvent;
|
2009-07-14 02:15:31 +00:00
|
|
|
_playerCtx.nextEventTime = eventDelta + millis;
|
2009-07-06 17:37:54 +00:00
|
|
|
|
|
|
|
// tempoEffect
|
|
|
|
if (_playerCtx.tempoTime) {
|
|
|
|
_playerCtx.tempoTicks += _playerCtx.tickUnit;
|
2009-07-14 02:15:31 +00:00
|
|
|
uint16 newTempo = _playerCtx.tempoStart;
|
2009-07-06 17:37:54 +00:00
|
|
|
if (_playerCtx.tempoTicks < _playerCtx.tempoTime) {
|
2009-07-17 00:07:36 +00:00
|
|
|
newTempo += (uint16)((_playerCtx.tempoTicks * _playerCtx.tempoDelta) / _playerCtx.tempoTime);
|
2009-07-06 17:37:54 +00:00
|
|
|
} else {
|
|
|
|
_playerCtx.tempoTime = 0;
|
2009-07-14 02:15:31 +00:00
|
|
|
newTempo += _playerCtx.tempoDelta;
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
2009-08-10 12:09:31 +00:00
|
|
|
_playerCtx.tickUnit = calcTempo(newTempo, _playerCtx.vBlankFreq);
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-07-14 02:15:31 +00:00
|
|
|
// Handling of Envelopes and Portamento
|
2009-07-10 13:38:13 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) {
|
2009-07-14 02:15:31 +00:00
|
|
|
VoiceContext &voice = _voiceCtx[i];
|
2009-07-10 13:38:13 +00:00
|
|
|
if (!voice.channel)
|
|
|
|
continue;
|
2009-07-14 02:15:31 +00:00
|
|
|
const ChannelContext &channel = *voice.channel;
|
|
|
|
const Patch &patch = *voice.patch;
|
|
|
|
|
2009-07-10 13:38:13 +00:00
|
|
|
switch (voice.status) {
|
|
|
|
case VoiceContext::kStatusSustain:
|
2009-08-08 09:18:23 +00:00
|
|
|
// we need to check if some voices have no sustainSample.
|
|
|
|
// in that case they are finished after the attackSample is done
|
|
|
|
if (voice.dmaOff && Paula::getChannelDmaCount((byte)i) >= voice.dmaOff ) {
|
|
|
|
voice.dmaOff = 0;
|
2009-08-18 11:34:55 +00:00
|
|
|
voice.isBlocked = 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
voice.priority = 0;
|
2009-08-08 09:23:09 +00:00
|
|
|
// disable it in next tick
|
|
|
|
voice.stopEventTime = 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
}
|
2009-08-18 11:34:55 +00:00
|
|
|
if (!channel.isAltered && !voice.hasPortamento && !channel.modulation)
|
2009-07-10 13:38:13 +00:00
|
|
|
continue;
|
2009-07-14 02:15:31 +00:00
|
|
|
// Update Volume and Period
|
2009-07-10 13:38:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VoiceContext::kStatusHalt:
|
|
|
|
killVoice((byte)i);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case VoiceContext::kStatusStart:
|
|
|
|
if (patch.attackLen) {
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.envelope = patch.attackPtr;
|
2009-07-10 13:38:13 +00:00
|
|
|
const uint16 duration = voice.envelope->duration;
|
|
|
|
voice.envelopeLeft = patch.attackLen;
|
|
|
|
voice.ticksLeft = duration << 8;
|
|
|
|
voice.status = VoiceContext::kStatusAttack;
|
2009-08-05 19:26:00 +00:00
|
|
|
voice.incrVolume = calcVolumeDelta((int32)voice.envelope->volume, duration, _playerCtx.vBlankFreq);
|
2009-07-14 02:15:31 +00:00
|
|
|
// Process Envelope
|
2009-07-10 13:38:13 +00:00
|
|
|
} else {
|
|
|
|
voice.status = VoiceContext::kStatusSustain;
|
|
|
|
voice.baseVolume = patch.volume;
|
2009-07-14 02:15:31 +00:00
|
|
|
// Update Volume and Period
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VoiceContext::kStatusRelease:
|
|
|
|
if (patch.releaseLen) {
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.envelope = patch.attackPtr + patch.attackLen;
|
2009-07-10 13:38:13 +00:00
|
|
|
const uint16 duration = voice.envelope->duration;
|
|
|
|
voice.envelopeLeft = patch.releaseLen;
|
|
|
|
voice.ticksLeft = duration << 8;
|
|
|
|
voice.status = VoiceContext::kStatusDecay;
|
2009-08-05 19:26:00 +00:00
|
|
|
voice.incrVolume = calcVolumeDelta((int32)voice.envelope->volume - voice.baseVolume, duration, _playerCtx.vBlankFreq);
|
2009-07-14 02:15:31 +00:00
|
|
|
// Process Envelope
|
2009-07-10 13:38:13 +00:00
|
|
|
} else {
|
|
|
|
voice.status = VoiceContext::kStatusHalt;
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.lastVolume = 0;
|
|
|
|
// Send Audio Packet
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
voice.stopEventTime = -1;
|
2009-07-10 13:38:13 +00:00
|
|
|
break;
|
2019-11-28 14:38:43 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
2009-07-14 02:15:31 +00:00
|
|
|
|
|
|
|
// Process Envelope
|
|
|
|
const uint16 envUnit = _playerCtx.frameUnit;
|
|
|
|
if (voice.envelope) {
|
|
|
|
if (voice.ticksLeft > envUnit) { // envelope still active
|
2009-08-27 16:29:56 +00:00
|
|
|
voice.baseVolume = (uint16) MIN<int32>(MAX<int32>(0, voice.baseVolume + voice.incrVolume), 0x8000);
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.ticksLeft -= envUnit;
|
|
|
|
// Update Volume and Period
|
|
|
|
|
|
|
|
} else { // next or last Envelope
|
2009-07-10 13:38:13 +00:00
|
|
|
voice.baseVolume = voice.envelope->volume;
|
2009-07-10 16:01:40 +00:00
|
|
|
assert(voice.envelopeLeft > 0);
|
2009-07-10 13:38:13 +00:00
|
|
|
if (--voice.envelopeLeft) {
|
|
|
|
++voice.envelope;
|
|
|
|
const uint16 duration = voice.envelope->duration;
|
|
|
|
voice.ticksLeft = duration << 8;
|
2009-08-05 19:26:00 +00:00
|
|
|
voice.incrVolume = calcVolumeDelta((int32)voice.envelope->volume - voice.baseVolume, duration, _playerCtx.vBlankFreq);
|
2009-07-14 02:15:31 +00:00
|
|
|
// Update Volume and Period
|
2009-07-10 13:38:13 +00:00
|
|
|
} else if (voice.status == VoiceContext::kStatusDecay) {
|
|
|
|
voice.status = VoiceContext::kStatusHalt;
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.envelope = 0;
|
|
|
|
voice.lastVolume = 0;
|
|
|
|
// Send Audio Packet
|
2009-07-10 13:38:13 +00:00
|
|
|
} else {
|
|
|
|
assert(voice.status == VoiceContext::kStatusAttack);
|
|
|
|
voice.status = VoiceContext::kStatusSustain;
|
2009-07-10 16:01:40 +00:00
|
|
|
voice.envelope = 0;
|
2009-07-14 02:15:31 +00:00
|
|
|
// Update Volume and Period
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-14 02:15:31 +00:00
|
|
|
}
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-07-14 02:15:31 +00:00
|
|
|
// Update Volume and Period
|
|
|
|
if (voice.status >= VoiceContext::kStatusDecay) {
|
|
|
|
// Calc volume
|
2009-07-10 13:38:13 +00:00
|
|
|
uint16 vol = (voice.noteVolume < (1 << 7)) ? (voice.noteVolume * _playerCtx.volume) >> 7 : _playerCtx.volume;
|
|
|
|
if (voice.baseVolume < (1 << 15))
|
2009-07-10 16:01:40 +00:00
|
|
|
vol = (uint16)(((uint32)vol * voice.baseVolume) >> 15);
|
2009-07-10 13:38:13 +00:00
|
|
|
if (voice.channel->volume < (1 << 7))
|
|
|
|
vol = (vol * voice.channel->volume) >> 7;
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.lastVolume = (byte)MIN(vol, (uint16)0x64);
|
2009-07-10 13:38:13 +00:00
|
|
|
|
2009-07-14 02:15:31 +00:00
|
|
|
// Calc Period
|
|
|
|
if (voice.hasPortamento) {
|
|
|
|
voice.portaTicks += envUnit;
|
2009-07-13 22:49:34 +00:00
|
|
|
if ((uint16)(voice.portaTicks >> 8) >= channel.portamentoTime) {
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.hasPortamento = false;
|
2009-07-10 13:38:13 +00:00
|
|
|
voice.baseNote = voice.endNote;
|
2009-07-17 01:42:58 +00:00
|
|
|
voice.preCalcNote = precalcNote(voice.baseNote, patch.tune, voice.octave);
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.lastPeriod = calcNote(voice);
|
2009-08-18 11:34:55 +00:00
|
|
|
} else if (channel.isAltered || channel.modulation)
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.lastPeriod = calcNote(voice);
|
2009-07-10 13:38:13 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 02:15:31 +00:00
|
|
|
// Send Audio Packet
|
2009-08-05 19:26:00 +00:00
|
|
|
Paula::setChannelPeriod((byte)i, (voice.lastPeriod) ? voice.lastPeriod : 1000);
|
|
|
|
Paula::setChannelVolume((byte)i, (voice.lastPeriod) ? voice.lastVolume : 0);
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
2009-07-14 02:15:31 +00:00
|
|
|
for (ChannelContext *c = _channelCtx; c != &_channelCtx[ARRAYSIZE(_channelCtx)]; ++c)
|
|
|
|
c->isAltered = false;
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-08-18 11:34:55 +00:00
|
|
|
#ifdef MAXTRAX_HAS_MODULATION
|
2009-08-08 09:18:23 +00:00
|
|
|
// original player had _playerCtx.sineValue = _playerCtx.frameUnit >> 2
|
|
|
|
// this should fit the comments that modtime=1000 is one second ?
|
2009-08-18 11:34:55 +00:00
|
|
|
_playerCtx.sineValue += _playerCtx.frameUnit;
|
|
|
|
#endif
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-07 21:00:34 +00:00
|
|
|
void MaxTrax::controlCh(ChannelContext &channel, const byte command, const byte data) {
|
|
|
|
switch (command) {
|
|
|
|
case 0x01: // modulation level MSB
|
|
|
|
channel.modulation = data << 8;
|
|
|
|
break;
|
|
|
|
case 0x21: // modulation level LSB
|
2017-08-12 05:21:28 +00:00
|
|
|
channel.modulation = (channel.modulation & 0xFF00) | ((data * 2) & 0xFF);
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x05: // portamento time MSB
|
|
|
|
channel.portamentoTime = data << 7;
|
|
|
|
break;
|
|
|
|
case 0x25: // portamento time LSB
|
2017-08-12 05:21:28 +00:00
|
|
|
channel.portamentoTime = (channel.portamentoTime & 0x3f80) | data;
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x06: // data entry MSB
|
2009-08-08 09:18:23 +00:00
|
|
|
if (channel.regParamNumber == 0) {
|
|
|
|
channel.pitchBendRange = (int8)MIN((uint8)MAX_BEND_RANGE, (uint8)data);
|
|
|
|
channel.pitchReal = (((int32)channel.pitchBendRange * channel.pitchBend) >> 5) - (channel.pitchBendRange << 8);
|
|
|
|
channel.isAltered = true;
|
|
|
|
}
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x07: // Main Volume MSB
|
|
|
|
channel.volume = (data == 0) ? 0 : data + 1;
|
|
|
|
channel.isAltered = true;
|
|
|
|
break;
|
|
|
|
case 0x0A: // Pan
|
|
|
|
if (data > 0x40 || (data == 0x40 && ((&channel - _channelCtx) & 1) != 0))
|
|
|
|
channel.flags |= ChannelContext::kFlagRightChannel;
|
|
|
|
else
|
|
|
|
channel.flags &= ~ChannelContext::kFlagRightChannel;
|
|
|
|
break;
|
|
|
|
case 0x10: // GPC as Modulation Time MSB
|
|
|
|
channel.modulationTime = data << 7;
|
|
|
|
break;
|
|
|
|
case 0x30: // GPC as Modulation Time LSB
|
2017-08-12 05:21:28 +00:00
|
|
|
channel.modulationTime = (channel.modulationTime & 0x3f80) | data;
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x11: // GPC as Microtonal Set MSB
|
|
|
|
channel.microtonal = data << 8;
|
|
|
|
break;
|
|
|
|
case 0x31: // GPC as Microtonal Set LSB
|
2017-08-12 05:21:28 +00:00
|
|
|
channel.microtonal = (channel.microtonal & 0xFF00) | ((data * 2) & 0xFF);
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x40: // Damper Pedal
|
|
|
|
if ((data & 0x40) != 0)
|
|
|
|
channel.flags |= ChannelContext::kFlagDamper;
|
|
|
|
else {
|
|
|
|
channel.flags &= ~ChannelContext::kFlagDamper;
|
|
|
|
// release all dampered voices on this channel
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) {
|
|
|
|
if (_voiceCtx[i].channel == &channel && _voiceCtx[i].hasDamper) {
|
|
|
|
_voiceCtx[i].hasDamper = false;
|
|
|
|
_voiceCtx[i].status = VoiceContext::kStatusRelease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x41: // Portamento off/on
|
|
|
|
if ((data & 0x40) != 0)
|
|
|
|
channel.flags |= ChannelContext::kFlagPortamento;
|
|
|
|
else
|
|
|
|
channel.flags &= ~ChannelContext::kFlagPortamento;
|
|
|
|
break;
|
2010-01-25 01:39:44 +00:00
|
|
|
case 0x50: // Microtonal off/on
|
2009-08-07 21:00:34 +00:00
|
|
|
if ((data & 0x40) != 0)
|
|
|
|
channel.flags |= ChannelContext::kFlagMicrotonal;
|
|
|
|
else
|
|
|
|
channel.flags &= ~ChannelContext::kFlagMicrotonal;
|
|
|
|
break;
|
|
|
|
case 0x51: // Audio Filter off/on
|
|
|
|
Paula::setAudioFilter(data > 0x40 || (data == 0x40 && _playerCtx.filterOn));
|
|
|
|
break;
|
|
|
|
case 0x65: // RPN MSB
|
2017-08-11 19:52:44 +00:00
|
|
|
channel.regParamNumber = (data << 8) | (channel.regParamNumber & 0xFF);
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x64: // RPN LSB
|
2017-08-11 19:52:44 +00:00
|
|
|
channel.regParamNumber = (channel.regParamNumber & 0xFF00) | data;
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x79: // Reset All Controllers
|
2009-08-08 09:18:23 +00:00
|
|
|
resetChannel(channel, ((&channel - _channelCtx) & 1) != 0);
|
2009-08-07 21:00:34 +00:00
|
|
|
break;
|
|
|
|
case 0x7E: // MONO mode
|
|
|
|
channel.flags |= ChannelContext::kFlagMono;
|
|
|
|
goto allNotesOff;
|
|
|
|
case 0x7F: // POLY mode
|
|
|
|
channel.flags &= ~ChannelContext::kFlagMono;
|
|
|
|
// Fallthrough
|
|
|
|
case 0x7B: // All Notes Off
|
|
|
|
allNotesOff:
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) {
|
|
|
|
if (_voiceCtx[i].channel == &channel) {
|
|
|
|
if ((channel.flags & ChannelContext::kFlagDamper) != 0)
|
|
|
|
_voiceCtx[i].hasDamper = true;
|
|
|
|
else
|
|
|
|
_voiceCtx[i].status = VoiceContext::kStatusRelease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x78: // All Sounds Off
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) {
|
|
|
|
if (_voiceCtx[i].channel == &channel)
|
2009-08-08 09:18:23 +00:00
|
|
|
killVoice((byte)i);
|
2009-08-07 21:00:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-11-28 14:38:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-08-07 21:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 18:02:06 +00:00
|
|
|
void MaxTrax::setTempo(const uint16 tempo) {
|
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
_playerCtx.tickUnit = calcTempo(tempo, _playerCtx.vBlankFreq);
|
|
|
|
}
|
|
|
|
|
2009-08-08 09:18:23 +00:00
|
|
|
void MaxTrax::resetPlayer() {
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i)
|
|
|
|
killVoice((byte)i);
|
|
|
|
|
2009-08-10 12:09:31 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_channelCtx); ++i) {
|
2009-08-08 09:18:23 +00:00
|
|
|
_channelCtx[i].flags = 0;
|
|
|
|
_channelCtx[i].lastNote = (uint8)-1;
|
|
|
|
resetChannel(_channelCtx[i], (i & 1) != 0);
|
2009-08-10 12:09:31 +00:00
|
|
|
_channelCtx[i].patch = (i < kNumChannels) ? &_patch[i] : 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MAXTRAX_HAS_MICROTONAL
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_microtonal); ++i)
|
|
|
|
_microtonal[i] = (int16)(i << 8);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-04 16:24:15 +00:00
|
|
|
void MaxTrax::stopMusic() {
|
2009-07-18 17:27:31 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
_playerCtx.scoreIndex = -1;
|
2009-08-08 09:18:23 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) {
|
|
|
|
if (_voiceCtx[i].channel < &_channelCtx[kNumChannels])
|
|
|
|
killVoice((byte)i);
|
|
|
|
}
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
bool MaxTrax::playSong(int songIndex, bool loop) {
|
2009-07-06 17:37:54 +00:00
|
|
|
if (songIndex < 0 || songIndex >= _numScores)
|
|
|
|
return false;
|
2009-07-18 00:04:28 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2009-08-08 09:18:23 +00:00
|
|
|
_playerCtx.scoreIndex = -1;
|
|
|
|
resetPlayer();
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_playerCtx.repeatPoint); ++i)
|
|
|
|
_playerCtx.repeatPoint[i] = 0;
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-07-10 13:38:13 +00:00
|
|
|
setTempo(_playerCtx.tempoInitial << 4);
|
2009-08-07 16:21:30 +00:00
|
|
|
Paula::setAudioFilter(_playerCtx.filterOn);
|
2009-08-08 09:18:23 +00:00
|
|
|
_playerCtx.musicLoop = loop;
|
2009-07-19 00:44:54 +00:00
|
|
|
_playerCtx.tempoTime = 0;
|
2009-07-06 17:37:54 +00:00
|
|
|
_playerCtx.scoreIndex = songIndex;
|
2009-07-18 17:27:31 +00:00
|
|
|
_playerCtx.ticks = 0;
|
|
|
|
|
2009-09-13 10:32:55 +00:00
|
|
|
_playerCtx.nextEvent = _scores[songIndex].events;
|
2009-08-05 19:26:00 +00:00
|
|
|
_playerCtx.nextEventTime = _playerCtx.nextEvent->startTime;
|
2009-07-19 00:44:54 +00:00
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
Paula::startPaula();
|
2009-07-06 18:15:50 +00:00
|
|
|
return true;
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
|
|
|
|
2009-08-05 19:26:00 +00:00
|
|
|
void MaxTrax::advanceSong(int advance) {
|
|
|
|
Common::StackLock lock(_mutex);
|
2009-08-08 09:18:23 +00:00
|
|
|
if (_playerCtx.scoreIndex >= 0) {
|
|
|
|
const Event *cev = _playerCtx.nextEvent;
|
|
|
|
if (cev) {
|
|
|
|
for (; advance > 0; --advance) {
|
2010-01-25 01:39:44 +00:00
|
|
|
// TODO - check for boundaries
|
2009-08-08 09:18:23 +00:00
|
|
|
for (; cev->command != 0xFF && (cev->command != 0xA0 || (cev->stopTime >> 8) != 0x00); ++cev)
|
|
|
|
; // no end_command or special_command + end
|
|
|
|
}
|
|
|
|
_playerCtx.nextEvent = cev;
|
2009-08-05 19:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
void MaxTrax::killVoice(byte num) {
|
|
|
|
VoiceContext &voice = _voiceCtx[num];
|
|
|
|
voice.channel = 0;
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.envelope = 0;
|
2009-07-06 17:37:54 +00:00
|
|
|
voice.status = VoiceContext::kStatusFree;
|
2009-08-18 11:34:55 +00:00
|
|
|
voice.isBlocked = 0;
|
2009-07-19 14:02:19 +00:00
|
|
|
voice.hasDamper = false;
|
2009-07-14 02:15:31 +00:00
|
|
|
voice.hasPortamento = false;
|
2009-07-06 17:37:54 +00:00
|
|
|
voice.priority = 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
voice.stopEventTime = -1;
|
2009-07-19 14:02:19 +00:00
|
|
|
voice.dmaOff = 0;
|
2009-08-13 00:07:37 +00:00
|
|
|
voice.lastVolume = 0;
|
2009-08-18 11:34:55 +00:00
|
|
|
voice.tieBreak = 0;
|
2009-07-17 00:58:26 +00:00
|
|
|
//voice.uinqueId = 0;
|
2009-07-06 17:37:54 +00:00
|
|
|
|
|
|
|
// "stop" voice, set period to 1, vol to 0
|
2009-07-10 16:01:40 +00:00
|
|
|
Paula::disableChannel(num);
|
2009-07-06 17:37:54 +00:00
|
|
|
Paula::setChannelPeriod(num, 1);
|
|
|
|
Paula::setChannelVolume(num, 0);
|
|
|
|
}
|
|
|
|
|
2009-08-18 11:34:55 +00:00
|
|
|
int8 MaxTrax::pickvoice(uint pick, int16 pri) {
|
2009-07-13 21:27:58 +00:00
|
|
|
enum { kPrioFlagFixedSide = 1 << 3 };
|
2009-08-18 11:34:55 +00:00
|
|
|
pick &= 3;
|
2009-07-13 21:27:58 +00:00
|
|
|
if ((pri & (kPrioFlagFixedSide)) == 0) {
|
|
|
|
const bool leftSide = (uint)(pick - 1) > 1;
|
2009-08-18 11:34:55 +00:00
|
|
|
const int leftBest = MIN(_voiceCtx[0].status, _voiceCtx[3].status);
|
|
|
|
const int rightBest = MIN(_voiceCtx[1].status, _voiceCtx[2].status);
|
2009-07-13 21:27:58 +00:00
|
|
|
const int sameSide = (leftSide) ? leftBest : rightBest;
|
|
|
|
const int otherSide = leftBest + rightBest - sameSide;
|
|
|
|
|
2009-07-19 14:02:19 +00:00
|
|
|
if (sameSide > VoiceContext::kStatusRelease && otherSide <= VoiceContext::kStatusRelease)
|
2009-07-13 21:27:58 +00:00
|
|
|
pick ^= 1; // switches sides
|
|
|
|
}
|
2009-08-18 11:34:55 +00:00
|
|
|
pri &= ~kPrioFlagFixedSide;
|
2009-07-13 21:27:58 +00:00
|
|
|
|
2009-07-19 14:02:19 +00:00
|
|
|
for (int i = 2; i > 0; --i) {
|
2009-08-18 11:34:55 +00:00
|
|
|
VoiceContext *voice = &_voiceCtx[pick];
|
|
|
|
VoiceContext *alternate = &_voiceCtx[pick ^ 3];
|
|
|
|
|
|
|
|
const uint16 voiceVal = voice->status << 8 | voice->lastVolume;
|
|
|
|
const uint16 altVal = alternate->status << 8 | alternate->lastVolume;
|
|
|
|
|
2010-01-25 01:39:44 +00:00
|
|
|
if (voiceVal + voice->tieBreak > altVal
|
2009-08-18 11:34:55 +00:00
|
|
|
|| voice->isBlocked > alternate->isBlocked) {
|
|
|
|
|
|
|
|
// this is somewhat different to the original player,
|
|
|
|
// but has a similar result
|
|
|
|
voice->tieBreak = 0;
|
|
|
|
alternate->tieBreak = 1;
|
2009-07-13 21:27:58 +00:00
|
|
|
|
|
|
|
pick ^= 3; // switch channels
|
2009-08-18 11:34:55 +00:00
|
|
|
VoiceContext *tmp = voice;
|
2009-07-13 21:27:58 +00:00
|
|
|
voice = alternate;
|
|
|
|
alternate = tmp;
|
|
|
|
}
|
|
|
|
|
2009-07-19 14:02:19 +00:00
|
|
|
if (voice->isBlocked || voice->priority > pri) {
|
2009-08-18 11:34:55 +00:00
|
|
|
// if not already done, switch sides and try again
|
|
|
|
pick ^= 1;
|
|
|
|
continue;
|
2009-07-13 21:27:58 +00:00
|
|
|
}
|
2024-05-14 06:13:14 +00:00
|
|
|
// succeeded
|
2009-07-13 21:27:58 +00:00
|
|
|
return (int8)pick;
|
|
|
|
}
|
|
|
|
// failed
|
2009-08-08 09:18:23 +00:00
|
|
|
debug(5, "MaxTrax: could not find channel for note");
|
2009-07-13 21:27:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-07-17 01:42:58 +00:00
|
|
|
uint16 MaxTrax::calcNote(const VoiceContext &voice) {
|
2009-07-10 16:01:40 +00:00
|
|
|
const ChannelContext &channel = *voice.channel;
|
2009-07-17 00:07:36 +00:00
|
|
|
int16 bend = channel.pitchReal;
|
2009-08-08 09:18:23 +00:00
|
|
|
|
|
|
|
#ifdef MAXTRAX_HAS_MICROTONAL
|
|
|
|
if (voice.hasPortamento) {
|
|
|
|
if ((channel.flags & ChannelContext::kFlagMicrotonal) != 0)
|
|
|
|
bend += (int16)(((_microtonal[voice.endNote] - _microtonal[voice.baseNote]) * voice.portaTicks) >> 8) / channel.portamentoTime;
|
|
|
|
else
|
|
|
|
bend += (int16)(((int8)(voice.endNote - voice.baseNote)) * voice.portaTicks) / channel.portamentoTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((channel.flags & ChannelContext::kFlagMicrotonal) != 0)
|
|
|
|
bend += _microtonal[voice.baseNote];
|
|
|
|
#else
|
2009-07-17 00:07:36 +00:00
|
|
|
if (voice.hasPortamento)
|
2009-07-17 00:58:26 +00:00
|
|
|
bend += (int16)(((int8)(voice.endNote - voice.baseNote)) * voice.portaTicks) / channel.portamentoTime;
|
2009-08-08 09:18:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MAXTRAX_HAS_MODULATION
|
2009-08-18 11:34:55 +00:00
|
|
|
static const uint8 tableSine[] = {
|
|
|
|
0, 5, 12, 18, 24, 30, 37, 43, 49, 55, 61, 67, 73, 79, 85, 91,
|
|
|
|
97, 103, 108, 114, 120, 125, 131, 136, 141, 146, 151, 156, 161, 166, 171, 176,
|
|
|
|
180, 184, 189, 193, 197, 201, 205, 208, 212, 215, 219, 222, 225, 228, 230, 233,
|
|
|
|
236, 238, 240, 242, 244, 246, 247, 249, 250, 251, 252, 253, 254, 254, 255, 255,
|
|
|
|
255, 255, 255, 254, 254, 253, 252, 251, 250, 249, 247, 246, 244, 242, 240, 238,
|
|
|
|
236, 233, 230, 228, 225, 222, 219, 215, 212, 208, 205, 201, 197, 193, 189, 184,
|
|
|
|
180, 176, 171, 166, 161, 156, 151, 146, 141, 136, 131, 125, 120, 114, 108, 103,
|
|
|
|
97, 91, 85, 79, 73, 67, 61, 55, 49, 43, 37, 30, 24, 18, 12, 5
|
|
|
|
};
|
2009-08-08 09:18:23 +00:00
|
|
|
if (channel.modulation) {
|
|
|
|
if ((channel.flags & ChannelContext::kFlagModVolume) == 0) {
|
2009-08-18 11:34:55 +00:00
|
|
|
const uint8 sineByte = _playerCtx.sineValue / channel.modulationTime;
|
2009-08-20 15:10:32 +00:00
|
|
|
const uint8 sineIndex = sineByte & 0x7F;
|
|
|
|
const int16 modVal = ((uint32)(uint16)(tableSine[sineIndex] + (sineIndex ? 1 : 0)) * channel.modulation) >> 8;
|
2009-08-18 11:34:55 +00:00
|
|
|
bend = (sineByte < 0x80) ? bend + modVal : bend - modVal;
|
2010-01-25 01:39:44 +00:00
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-07-10 13:38:13 +00:00
|
|
|
|
2009-07-17 00:07:36 +00:00
|
|
|
// tone = voice.baseNote << 8 + microtonal
|
|
|
|
// bend = channelPitch + porta + modulation
|
2009-07-17 00:58:26 +00:00
|
|
|
|
2009-07-17 01:42:58 +00:00
|
|
|
const int32 tone = voice.preCalcNote + (bend << 6) / 3;
|
2009-07-08 22:59:50 +00:00
|
|
|
|
2009-08-18 11:34:55 +00:00
|
|
|
return (tone >= PERIOD_LIMIT) ? (uint16)pow2Fixed(tone) : 0;
|
2009-07-08 22:59:50 +00:00
|
|
|
}
|
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
int8 MaxTrax::noteOn(ChannelContext &channel, const byte note, uint16 volume, uint16 pri) {
|
2009-08-08 09:18:23 +00:00
|
|
|
#ifdef MAXTRAX_HAS_MICROTONAL
|
|
|
|
if (channel.microtonal >= 0)
|
|
|
|
_microtonal[note % 127] = channel.microtonal;
|
|
|
|
#endif
|
|
|
|
|
2009-07-06 17:37:54 +00:00
|
|
|
if (!volume)
|
|
|
|
return -1;
|
|
|
|
|
2009-07-10 16:01:40 +00:00
|
|
|
const Patch &patch = *channel.patch;
|
2009-07-19 14:02:19 +00:00
|
|
|
if (!patch.samplePtr || patch.sampleTotalLen == 0)
|
2009-07-06 17:37:54 +00:00
|
|
|
return -1;
|
|
|
|
int8 voiceNum = -1;
|
2009-08-07 21:00:34 +00:00
|
|
|
if ((channel.flags & ChannelContext::kFlagMono) == 0) {
|
2009-08-18 11:34:55 +00:00
|
|
|
voiceNum = pickvoice((channel.flags & ChannelContext::kFlagRightChannel) != 0 ? 1 : 0, pri);
|
2009-08-07 21:00:34 +00:00
|
|
|
} else {
|
2011-08-07 10:07:54 +00:00
|
|
|
VoiceContext *voice = ARRAYEND(_voiceCtx);
|
2016-05-31 11:10:35 +00:00
|
|
|
for (voiceNum = ARRAYSIZE(_voiceCtx); voiceNum >= 0 && voice->channel != &channel; voiceNum--, voice--)
|
2009-07-06 17:37:54 +00:00
|
|
|
;
|
2009-08-07 21:00:34 +00:00
|
|
|
if (voiceNum < 0)
|
2009-08-18 11:34:55 +00:00
|
|
|
voiceNum = pickvoice((channel.flags & ChannelContext::kFlagRightChannel) != 0 ? 1 : 0, pri);
|
2009-08-07 21:00:34 +00:00
|
|
|
else if (voice->status >= VoiceContext::kStatusSustain && (channel.flags & ChannelContext::kFlagPortamento) != 0) {
|
2009-07-06 17:37:54 +00:00
|
|
|
// reset previous porta
|
2009-07-14 02:15:31 +00:00
|
|
|
if (voice->hasPortamento)
|
2009-07-13 22:49:34 +00:00
|
|
|
voice->baseNote = voice->endNote;
|
2009-07-17 01:42:58 +00:00
|
|
|
voice->preCalcNote = precalcNote(voice->baseNote, patch.tune, voice->octave);
|
2009-08-07 21:00:34 +00:00
|
|
|
voice->noteVolume = (_playerCtx.handleVolume) ? volume + 1 : 128;
|
2009-07-13 22:49:34 +00:00
|
|
|
voice->portaTicks = 0;
|
2009-07-14 02:15:31 +00:00
|
|
|
voice->hasPortamento = true;
|
2009-07-13 22:49:34 +00:00
|
|
|
voice->endNote = channel.lastNote = note;
|
2009-08-07 21:00:34 +00:00
|
|
|
return voiceNum;
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
}
|
2009-08-07 21:00:34 +00:00
|
|
|
|
|
|
|
if (voiceNum >= 0) {
|
|
|
|
VoiceContext &voice = _voiceCtx[voiceNum];
|
|
|
|
voice.hasDamper = false;
|
2009-08-18 11:34:55 +00:00
|
|
|
voice.isBlocked = 0;
|
2009-08-07 21:00:34 +00:00
|
|
|
voice.hasPortamento = false;
|
|
|
|
if (voice.channel)
|
|
|
|
killVoice(voiceNum);
|
|
|
|
voice.channel = &channel;
|
|
|
|
voice.patch = &patch;
|
|
|
|
voice.baseNote = note;
|
|
|
|
|
|
|
|
// always base octave on the note in the command, regardless of porta
|
|
|
|
const int32 plainNote = precalcNote(note, patch.tune, 0);
|
|
|
|
// calculate which sample to use
|
2009-08-18 11:34:55 +00:00
|
|
|
const int useOctave = (plainNote <= PREF_PERIOD) ? 0 : MIN<int32>((plainNote + 0xFFFF - PREF_PERIOD) >> 16, patch.sampleOctaves - 1);
|
2009-08-07 21:00:34 +00:00
|
|
|
voice.octave = (byte)useOctave;
|
2009-08-18 11:34:55 +00:00
|
|
|
// adjust precalculated value
|
2009-08-07 21:00:34 +00:00
|
|
|
voice.preCalcNote = plainNote - (useOctave << 16);
|
|
|
|
|
2022-10-27 08:43:30 +00:00
|
|
|
// next calculate the actual period which depends on whether porta is enabled
|
2009-08-07 21:00:34 +00:00
|
|
|
if (&channel < &_channelCtx[kNumChannels] && (channel.flags & ChannelContext::kFlagPortamento) != 0) {
|
|
|
|
if ((channel.flags & ChannelContext::kFlagMono) != 0 && channel.lastNote < 0x80 && channel.lastNote != note) {
|
|
|
|
voice.portaTicks = 0;
|
|
|
|
voice.baseNote = channel.lastNote;
|
|
|
|
voice.endNote = note;
|
|
|
|
voice.hasPortamento = true;
|
|
|
|
voice.preCalcNote = precalcNote(voice.baseNote, patch.tune, voice.octave);
|
|
|
|
}
|
|
|
|
channel.lastNote = note;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-08-07 21:00:34 +00:00
|
|
|
voice.lastPeriod = calcNote(voice);
|
2009-07-13 22:49:34 +00:00
|
|
|
|
2009-08-07 21:00:34 +00:00
|
|
|
voice.priority = (byte)pri;
|
|
|
|
voice.status = VoiceContext::kStatusStart;
|
|
|
|
|
|
|
|
voice.noteVolume = (_playerCtx.handleVolume) ? volume + 1 : 128;
|
|
|
|
voice.baseVolume = 0;
|
|
|
|
|
|
|
|
// TODO: since the original player is using the OS-functions, more than 1 sample could be queued up already
|
|
|
|
// get samplestart for the given octave
|
|
|
|
const int8 *samplePtr = patch.samplePtr + (patch.sampleTotalLen << useOctave) - patch.sampleTotalLen;
|
|
|
|
if (patch.sampleAttackLen) {
|
|
|
|
Paula::setChannelSampleStart(voiceNum, samplePtr);
|
|
|
|
Paula::setChannelSampleLen(voiceNum, (patch.sampleAttackLen << useOctave) / 2);
|
|
|
|
|
|
|
|
Paula::enableChannel(voiceNum);
|
|
|
|
// wait for dma-clear
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patch.sampleTotalLen > patch.sampleAttackLen) {
|
|
|
|
Paula::setChannelSampleStart(voiceNum, samplePtr + (patch.sampleAttackLen << useOctave));
|
|
|
|
Paula::setChannelSampleLen(voiceNum, ((patch.sampleTotalLen - patch.sampleAttackLen) << useOctave) / 2);
|
2009-08-18 11:34:55 +00:00
|
|
|
if (!patch.sampleAttackLen)
|
|
|
|
Paula::enableChannel(voiceNum); // need to enable channel
|
2009-08-07 21:00:34 +00:00
|
|
|
// another pointless wait for DMA-Clear???
|
2009-07-06 17:37:54 +00:00
|
|
|
|
2009-08-07 21:00:34 +00:00
|
|
|
} else { // no sustain sample
|
|
|
|
// this means we must stop playback after the attacksample finished
|
|
|
|
// so we queue up an "empty" sample and note that we need to kill the sample after dma finished
|
|
|
|
Paula::setChannelSampleStart(voiceNum, 0);
|
|
|
|
Paula::setChannelSampleLen(voiceNum, 0);
|
|
|
|
Paula::setChannelDmaCount(voiceNum);
|
|
|
|
voice.dmaOff = 1;
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
2009-08-18 11:34:55 +00:00
|
|
|
|
|
|
|
Paula::setChannelPeriod(voiceNum, (voice.lastPeriod) ? voice.lastPeriod : 1000);
|
|
|
|
Paula::setChannelVolume(voiceNum, 0);
|
2009-07-06 17:37:54 +00:00
|
|
|
}
|
|
|
|
return voiceNum;
|
|
|
|
}
|
|
|
|
|
2009-07-10 15:04:41 +00:00
|
|
|
void MaxTrax::resetChannel(ChannelContext &chan, bool rightChannel) {
|
2009-08-07 16:21:30 +00:00
|
|
|
chan.modulation = 0;
|
|
|
|
chan.modulationTime = 1000;
|
|
|
|
chan.microtonal = -1;
|
2009-07-13 22:49:34 +00:00
|
|
|
chan.portamentoTime = 500;
|
2009-08-08 09:18:23 +00:00
|
|
|
chan.pitchBend = NO_BEND;
|
2009-07-10 15:04:41 +00:00
|
|
|
chan.pitchReal = 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
chan.pitchBendRange = MAX_BEND_RANGE;
|
2009-07-10 15:04:41 +00:00
|
|
|
chan.volume = 128;
|
2009-08-08 09:18:23 +00:00
|
|
|
chan.flags &= ~(ChannelContext::kFlagPortamento | ChannelContext::kFlagMicrotonal | ChannelContext::kFlagRightChannel);
|
2009-07-14 02:15:31 +00:00
|
|
|
chan.isAltered = true;
|
2009-07-10 15:04:41 +00:00
|
|
|
if (rightChannel)
|
2009-08-08 09:18:23 +00:00
|
|
|
chan.flags |= ChannelContext::kFlagRightChannel;
|
2009-07-10 15:04:41 +00:00
|
|
|
}
|
|
|
|
|
2009-07-04 16:24:15 +00:00
|
|
|
void MaxTrax::freeScores() {
|
|
|
|
if (_scores) {
|
|
|
|
for (int i = 0; i < _numScores; ++i)
|
2009-07-09 16:18:35 +00:00
|
|
|
delete[] _scores[i].events;
|
|
|
|
delete[] _scores;
|
2009-07-04 16:24:15 +00:00
|
|
|
_scores = 0;
|
|
|
|
}
|
|
|
|
_numScores = 0;
|
2009-08-08 09:18:23 +00:00
|
|
|
_playerCtx.tempo = 120;
|
|
|
|
_playerCtx.filterOn = true;
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaxTrax::freePatches() {
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_patch); ++i) {
|
|
|
|
delete[] _patch[i].samplePtr;
|
|
|
|
delete[] _patch[i].attackPtr;
|
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
memset(_patch, 0, sizeof(_patch));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaxTrax::setSignalCallback(void (*callback) (int)) {
|
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
_playerCtx.syncCallBack = (callback == 0) ? nullFunc : callback;
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
2009-07-17 00:07:36 +00:00
|
|
|
|
|
|
|
int MaxTrax::playNote(byte note, byte patch, uint16 duration, uint16 volume, bool rightSide) {
|
2009-07-18 00:04:28 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2009-07-17 00:07:36 +00:00
|
|
|
assert(patch < ARRAYSIZE(_patch));
|
|
|
|
|
|
|
|
ChannelContext &channel = _channelCtx[kNumChannels];
|
|
|
|
channel.flags = (rightSide) ? ChannelContext::kFlagRightChannel : 0;
|
|
|
|
channel.isAltered = false;
|
|
|
|
channel.patch = &_patch[patch];
|
|
|
|
const int8 voiceIndex = noteOn(channel, note, (byte)volume, kPriorityNote);
|
2009-08-29 12:44:51 +00:00
|
|
|
if (voiceIndex >= 0) {
|
2009-08-08 09:18:23 +00:00
|
|
|
_voiceCtx[voiceIndex].stopEventTime = duration << 8;
|
2009-08-29 12:44:51 +00:00
|
|
|
Paula::startPaula();
|
|
|
|
}
|
2009-07-17 00:07:36 +00:00
|
|
|
return voiceIndex;
|
|
|
|
}
|
2009-07-04 16:24:15 +00:00
|
|
|
|
|
|
|
bool MaxTrax::load(Common::SeekableReadStream &musicData, bool loadScores, bool loadSamples) {
|
2009-07-18 00:04:28 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2009-07-04 16:24:15 +00:00
|
|
|
stopMusic();
|
|
|
|
if (loadSamples)
|
|
|
|
freePatches();
|
|
|
|
if (loadScores)
|
|
|
|
freeScores();
|
2009-08-10 11:47:49 +00:00
|
|
|
const char *errorMsg = 0;
|
2009-07-04 16:24:15 +00:00
|
|
|
// 0x0000: 4 Bytes Header "MXTX"
|
|
|
|
// 0x0004: uint16 tempo
|
2010-01-25 01:39:44 +00:00
|
|
|
// 0x0006: uint16 flags. bit0 = lowpassfilter, bit1 = attackvolume, bit15 = microtonal
|
2009-08-08 09:18:23 +00:00
|
|
|
if (musicData.size() < 10 || musicData.readUint32BE() != 0x4D585458) {
|
2009-07-04 16:24:15 +00:00
|
|
|
warning("Maxtrax: File is not a Maxtrax Module");
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-10 13:38:13 +00:00
|
|
|
const uint16 songTempo = musicData.readUint16BE();
|
2009-07-04 16:24:15 +00:00
|
|
|
const uint16 flags = musicData.readUint16BE();
|
2009-07-10 13:38:13 +00:00
|
|
|
if (loadScores) {
|
|
|
|
_playerCtx.tempoInitial = songTempo;
|
|
|
|
_playerCtx.filterOn = (flags & 1) != 0;
|
|
|
|
_playerCtx.handleVolume = (flags & 2) != 0;
|
|
|
|
}
|
2009-07-04 16:24:15 +00:00
|
|
|
|
2009-07-10 13:38:13 +00:00
|
|
|
if (flags & (1 << 15)) {
|
2009-08-08 09:18:23 +00:00
|
|
|
debug(5, "Maxtrax: Song has microtonal");
|
|
|
|
#ifdef MAXTRAX_HAS_MICROTONAL
|
|
|
|
if (loadScores) {
|
2009-07-10 13:38:13 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_microtonal); ++i)
|
|
|
|
_microtonal[i] = musicData.readUint16BE();
|
2009-08-08 09:18:23 +00:00
|
|
|
} else
|
2009-07-10 13:38:13 +00:00
|
|
|
musicData.skip(128 * 2);
|
2009-08-08 09:18:23 +00:00
|
|
|
#else
|
|
|
|
musicData.skip(128 * 2);
|
|
|
|
#endif
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int scoresLoaded = 0;
|
|
|
|
// uint16 number of Scores
|
|
|
|
const uint16 scoresInFile = musicData.readUint16BE();
|
|
|
|
|
2009-08-08 09:18:23 +00:00
|
|
|
if (musicData.err() || musicData.eos())
|
|
|
|
goto ioError;
|
|
|
|
|
2009-07-04 16:24:15 +00:00
|
|
|
if (loadScores) {
|
2009-07-06 17:37:54 +00:00
|
|
|
const uint16 tempScores = MIN(scoresInFile, _playerCtx.maxScoreNum);
|
2009-08-08 09:18:23 +00:00
|
|
|
Score *curScore = new Score[tempScores];
|
|
|
|
if (!curScore)
|
|
|
|
goto allocError;
|
|
|
|
_scores = curScore;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-08-08 09:18:23 +00:00
|
|
|
for (scoresLoaded = 0; scoresLoaded < tempScores; ++scoresLoaded, ++curScore) {
|
2009-07-04 16:24:15 +00:00
|
|
|
const uint32 numEvents = musicData.readUint32BE();
|
2009-07-10 16:01:40 +00:00
|
|
|
Event *curEvent = new Event[numEvents];
|
2009-08-08 09:18:23 +00:00
|
|
|
if (!curEvent)
|
|
|
|
goto allocError;
|
2009-07-10 16:01:40 +00:00
|
|
|
curScore->events = curEvent;
|
2009-07-04 16:24:15 +00:00
|
|
|
for (int j = numEvents; j > 0; --j, ++curEvent) {
|
|
|
|
curEvent->command = musicData.readByte();
|
|
|
|
curEvent->parameter = musicData.readByte();
|
|
|
|
curEvent->startTime = musicData.readUint16BE();
|
|
|
|
curEvent->stopTime = musicData.readUint16BE();
|
|
|
|
}
|
|
|
|
curScore->numEvents = numEvents;
|
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
_numScores = scoresLoaded;
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (loadSamples) {
|
2009-08-08 09:18:23 +00:00
|
|
|
// skip over remaining scores in file
|
|
|
|
for (int i = scoresInFile - scoresLoaded; i > 0; --i)
|
|
|
|
musicData.skip(musicData.readUint32BE() * 6);
|
|
|
|
|
|
|
|
// uint16 number of Samples
|
|
|
|
const uint16 wavesInFile = musicData.readUint16BE();
|
2009-07-04 16:24:15 +00:00
|
|
|
for (int i = wavesInFile; i > 0; --i) {
|
|
|
|
// load disksample structure
|
|
|
|
const uint16 number = musicData.readUint16BE();
|
|
|
|
assert(number < ARRAYSIZE(_patch));
|
|
|
|
|
2009-08-10 11:47:49 +00:00
|
|
|
Patch &curPatch = _patch[number];
|
|
|
|
if (curPatch.attackPtr || curPatch.samplePtr) {
|
|
|
|
delete curPatch.attackPtr;
|
|
|
|
curPatch.attackPtr = 0;
|
|
|
|
delete curPatch.samplePtr;
|
|
|
|
curPatch.samplePtr = 0;
|
|
|
|
}
|
2009-07-17 00:07:36 +00:00
|
|
|
curPatch.tune = musicData.readSint16BE();
|
2009-07-04 16:24:15 +00:00
|
|
|
curPatch.volume = musicData.readUint16BE();
|
|
|
|
curPatch.sampleOctaves = musicData.readUint16BE();
|
2009-07-06 17:37:54 +00:00
|
|
|
curPatch.sampleAttackLen = musicData.readUint32BE();
|
|
|
|
const uint32 sustainLen = musicData.readUint32BE();
|
|
|
|
curPatch.sampleTotalLen = curPatch.sampleAttackLen + sustainLen;
|
2009-07-04 16:24:15 +00:00
|
|
|
// each octave the number of samples doubles.
|
2009-07-06 17:37:54 +00:00
|
|
|
const uint32 totalSamples = curPatch.sampleTotalLen * ((1 << curPatch.sampleOctaves) - 1);
|
2009-07-04 16:24:15 +00:00
|
|
|
curPatch.attackLen = musicData.readUint16BE();
|
|
|
|
curPatch.releaseLen = musicData.readUint16BE();
|
|
|
|
const uint32 totalEnvs = curPatch.attackLen + curPatch.releaseLen;
|
|
|
|
|
|
|
|
// Allocate space for both attack and release Segment.
|
|
|
|
Envelope *envPtr = new Envelope[totalEnvs];
|
2009-08-08 09:18:23 +00:00
|
|
|
if (!envPtr)
|
|
|
|
goto allocError;
|
2009-07-04 16:24:15 +00:00
|
|
|
// Attack Segment
|
|
|
|
curPatch.attackPtr = envPtr;
|
|
|
|
// Release Segment
|
|
|
|
// curPatch.releasePtr = envPtr + curPatch.attackLen;
|
|
|
|
|
|
|
|
// Read Attack and Release Segments
|
|
|
|
for (int j = totalEnvs; j > 0; --j, ++envPtr) {
|
|
|
|
envPtr->duration = musicData.readUint16BE();
|
|
|
|
envPtr->volume = musicData.readUint16BE();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read Samples
|
2009-07-10 16:01:40 +00:00
|
|
|
int8 *allocSamples = new int8[totalSamples];
|
2009-08-08 09:18:23 +00:00
|
|
|
if (!allocSamples)
|
|
|
|
goto allocError;
|
2009-07-10 16:01:40 +00:00
|
|
|
curPatch.samplePtr = allocSamples;
|
|
|
|
musicData.read(allocSamples, totalSamples);
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
2009-08-08 09:18:23 +00:00
|
|
|
}
|
|
|
|
if (!musicData.err() && !musicData.eos())
|
|
|
|
return true;
|
|
|
|
ioError:
|
2009-08-10 11:47:49 +00:00
|
|
|
errorMsg = "Maxtrax: Encountered IO-Error";
|
2009-08-08 09:18:23 +00:00
|
|
|
allocError:
|
2009-08-10 11:47:49 +00:00
|
|
|
if (!errorMsg)
|
|
|
|
errorMsg = "Maxtrax: Could not allocate Memory";
|
|
|
|
|
2009-08-24 17:51:47 +00:00
|
|
|
warning("%s", errorMsg);
|
2009-08-10 11:47:49 +00:00
|
|
|
if (loadSamples)
|
|
|
|
freePatches();
|
|
|
|
if (loadScores)
|
|
|
|
freeScores();
|
2009-08-08 09:18:23 +00:00
|
|
|
return false;
|
2009-07-04 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 11:34:55 +00:00
|
|
|
#if !defined(NDEBUG) && 0
|
2009-08-05 19:26:00 +00:00
|
|
|
void MaxTrax::outPutEvent(const Event &ev, int num) {
|
|
|
|
struct {
|
|
|
|
byte cmd;
|
|
|
|
const char *name;
|
|
|
|
const char *param;
|
|
|
|
} COMMANDS[] = {
|
|
|
|
{0x80, "TEMPO ", "TEMPO, N/A "},
|
|
|
|
{0xa0, "SPECIAL ", "CHAN, SPEC # | VAL"},
|
|
|
|
{0xb0, "CONTROL ", "CHAN, CTRL # | VAL"},
|
|
|
|
{0xc0, "PROGRAM ", "CHANNEL, PROG # "},
|
|
|
|
{0xe0, "BEND ", "CHANNEL, BEND VALUE"},
|
|
|
|
{0xf0, "SYSEX ", "TYPE, SIZE "},
|
|
|
|
{0xf8, "REALTIME", "REALTIME, N/A "},
|
|
|
|
{0xff, "END ", "N/A, N/A "},
|
|
|
|
{0xff, "NOTE ", "VOL | CHAN, STOP"},
|
|
|
|
};
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < ARRAYSIZE(COMMANDS) - 1 && ev.command != COMMANDS[i].cmd; ++i)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (num == -1)
|
|
|
|
debug("Event : %02X %s %s %02X %04X %04X", ev.command, COMMANDS[i].name, COMMANDS[i].param, ev.parameter, ev.startTime, ev.stopTime);
|
|
|
|
else
|
|
|
|
debug("Event %3d: %02X %s %s %02X %04X %04X", num, ev.command, COMMANDS[i].name, COMMANDS[i].param, ev.parameter, ev.startTime, ev.stopTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaxTrax::outPutScore(const Score &sc, int num) {
|
|
|
|
if (num == -1)
|
|
|
|
debug("score : %i Events", sc.numEvents);
|
|
|
|
else
|
|
|
|
debug("score %2d: %i Events", num, sc.numEvents);
|
|
|
|
for (uint i = 0; i < sc.numEvents; ++i)
|
|
|
|
outPutEvent(sc.events[i], i);
|
|
|
|
debug("");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void MaxTrax::outPutEvent(const Event &ev, int num) {}
|
|
|
|
void MaxTrax::outPutScore(const Score &sc, int num) {}
|
|
|
|
#endif // #ifndef NDEBUG
|
|
|
|
|
2013-01-26 18:33:27 +00:00
|
|
|
} // End of namespace Audio
|
2009-08-05 18:00:13 +00:00
|
|
|
|
2011-10-28 10:32:14 +00:00
|
|
|
#endif // #if defined(AUDIO_MODS_MAXTRAX_H)
|