diff options
author | Max Horn | 2011-02-09 01:09:01 +0000 |
---|---|---|
committer | Max Horn | 2011-02-09 01:09:01 +0000 |
commit | 42ab839dd6c8a1570b232101eb97f4e54de57935 (patch) | |
tree | 3b763d8913a87482b793e0348c88b9a5f40eecc9 /audio/mods | |
parent | 386203a3d6ce1abf457c9110d695408ec5f01b85 (diff) | |
download | scummvm-rg350-42ab839dd6c8a1570b232101eb97f4e54de57935.tar.gz scummvm-rg350-42ab839dd6c8a1570b232101eb97f4e54de57935.tar.bz2 scummvm-rg350-42ab839dd6c8a1570b232101eb97f4e54de57935.zip |
AUDIO: Rename sound/ dir to audio/
svn-id: r55850
Diffstat (limited to 'audio/mods')
-rw-r--r-- | audio/mods/infogrames.cpp | 470 | ||||
-rw-r--r-- | audio/mods/infogrames.h | 148 | ||||
-rw-r--r-- | audio/mods/maxtrax.cpp | 1040 | ||||
-rw-r--r-- | audio/mods/maxtrax.h | 225 | ||||
-rw-r--r-- | audio/mods/module.cpp | 252 | ||||
-rw-r--r-- | audio/mods/module.h | 90 | ||||
-rw-r--r-- | audio/mods/paula.cpp | 212 | ||||
-rw-r--r-- | audio/mods/paula.h | 210 | ||||
-rw-r--r-- | audio/mods/protracker.cpp | 466 | ||||
-rw-r--r-- | audio/mods/protracker.h | 57 | ||||
-rw-r--r-- | audio/mods/rjp1.cpp | 582 | ||||
-rw-r--r-- | audio/mods/rjp1.h | 50 | ||||
-rw-r--r-- | audio/mods/soundfx.cpp | 275 | ||||
-rw-r--r-- | audio/mods/soundfx.h | 53 | ||||
-rw-r--r-- | audio/mods/tfmx.cpp | 1193 | ||||
-rw-r--r-- | audio/mods/tfmx.h | 284 |
16 files changed, 5607 insertions, 0 deletions
diff --git a/audio/mods/infogrames.cpp b/audio/mods/infogrames.cpp new file mode 100644 index 0000000000..27e42c637b --- /dev/null +++ b/audio/mods/infogrames.cpp @@ -0,0 +1,470 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "audio/mods/infogrames.h" +#include "common/endian.h" +#include "common/file.h" +#include "common/memstream.h" + +namespace Audio { + +Infogrames::Instruments::Instruments() { + init(); +} + +Infogrames::Instruments::~Instruments() { + delete[] _sampleData; +} + +void Infogrames::Instruments::init() { + int i; + + for (i = 0; i < 32; i++) { + _samples[i].data = 0; + _samples[i].dataRepeat = 0; + _samples[i].length = 0; + _samples[i].lengthRepeat = 0; + } + _count = 0; + _sampleData = 0; +} + +bool Infogrames::Instruments::load(const char *ins) { + Common::File f; + + if (f.open(ins)) + return load(f); + return false; +} + +bool Infogrames::Instruments::load(Common::SeekableReadStream &ins) { + int i; + int32 fsize; + int32 offset[32]; + int32 offsetRepeat[32]; + int32 dataOffset; + + unload(); + + fsize = ins.readUint32BE(); + dataOffset = fsize; + for (i = 0; (i < 32) && !ins.eos(); i++) { + offset[i] = ins.readUint32BE(); + offsetRepeat[i] = ins.readUint32BE(); + if ((offset[i] > fsize) || (offsetRepeat[i] > fsize) || + (offset[i] < (ins.pos() + 4)) || + (offsetRepeat[i] < (ins.pos() + 4))) { + // Definitely no real entry anymore + ins.seek(-8, SEEK_CUR); + break; + } + + dataOffset = MIN(dataOffset, MIN(offset[i], offsetRepeat[i])); + ins.skip(4); // Unknown + _samples[i].length = ins.readUint16BE() * 2; + _samples[i].lengthRepeat = ins.readUint16BE() * 2; + } + + if (dataOffset >= fsize) + return false; + + _count = i; + _sampleData = new int8[fsize - dataOffset]; + ins.seek(dataOffset + 4); + ins.read(_sampleData, fsize - dataOffset); + + for (i--; i >= 0; i--) { + _samples[i].data = _sampleData + (offset[i] - dataOffset); + _samples[i].dataRepeat = _sampleData + (offsetRepeat[i] - dataOffset); + } + + return true; +} + +void Infogrames::Instruments::unload() { + delete[] _sampleData; + init(); +} + +const uint8 Infogrames::tickCount[] = + {2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96}; +const uint16 Infogrames::periods[] = + {0x6ACC, 0x64CC, 0x5F25, 0x59CE, 0x54C3, 0x5003, 0x4B86, 0x4747, 0x4346, + 0x3F8B, 0x3BF3, 0x3892, 0x3568, 0x3269, 0x2F93, 0x2CEA, 0x2A66, 0x2801, + 0x2566, 0x23A5, 0x21AF, 0x1FC4, 0x1DFE, 0x1C4E, 0x1ABC, 0x1936, 0x17CC, + 0x1676, 0x1533, 0x1401, 0x12E4, 0x11D5, 0x10D4, 0x0FE3, 0x0EFE, 0x0E26, + 0x0D5B, 0x0C9B, 0x0BE5, 0x0B3B, 0x0A9B, 0x0A02, 0x0972, 0x08E9, 0x0869, + 0x07F1, 0x077F, 0x0713, 0x06AD, 0x064D, 0x05F2, 0x059D, 0x054D, 0x0500, + 0x04B8, 0x0475, 0x0435, 0x03F8, 0x03BF, 0x038A, 0x0356, 0x0326, 0x02F9, + 0x02CF, 0x02A6, 0x0280, 0x025C, 0x023A, 0x021A, 0x01FC, 0x01E0, 0x01C5, + 0x01AB, 0x0193, 0x017D, 0x0167, 0x0153, 0x0140, 0x012E, 0x011D, 0x010D, + 0x00FE, 0x00F0, 0x00E2, 0x00D6, 0x00CA, 0x00BE, 0x00B4, 0x00AA, 0x00A0, + 0x0097, 0x008F, 0x0087, 0x007F, 0x0078, 0x0070, 0x0060, 0x0050, 0x0040, + 0x0030, 0x0020, 0x0010, 0x0000, 0x0000, 0x0020, 0x2020, 0x2020, 0x2020, + 0x2020, 0x3030, 0x3030, 0x3020, 0x2020, 0x2020, 0x2020, 0x2020, 0x2020, + 0x2020, 0x2020, 0x2020, 0x2090, 0x4040, 0x4040, 0x4040, 0x4040, 0x4040, + 0x4040, 0x4040, 0x400C, 0x0C0C, 0x0C0C, 0x0C0C, 0x0C0C, 0x0C40, 0x4040, + 0x4040, 0x4040, 0x0909, 0x0909, 0x0909, 0x0101, 0x0101, 0x0101, 0x0101, + 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x4040, 0x4040, 0x4040, + 0x0A0A, 0x0A0A, 0x0A0A, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, + 0x0202, 0x0202, 0x0202, 0x0202, 0x4040, 0x4040, 0x2000}; + +Infogrames::Infogrames(Instruments &ins, bool stereo, int rate, + int interruptFreq) : Paula(stereo, rate, interruptFreq) { + _instruments = &ins; + _data = 0; + _repCount = -1; + + reset(); +} + +Infogrames::~Infogrames() { + delete[] _data; +} + +void Infogrames::init() { + int i; + + _volume = 0; + _period = 0; + _sample = 0; + _speedCounter = _speed; + + for (i = 0; i < 4; i++) { + _chn[i].cmds = 0; + _chn[i].cmdBlocks = 0; + _chn[i].volSlide.finetuneNeg = 0; + _chn[i].volSlide.finetunePos = 0; + _chn[i].volSlide.data = 0; + _chn[i].volSlide.amount = 0; + _chn[i].volSlide.dataOffset = 0; + _chn[i].volSlide.flags = 0; + _chn[i].volSlide.curDelay1 = 0; + _chn[i].volSlide.curDelay2 = 0; + _chn[i].periodSlide.finetuneNeg = 0; + _chn[i].periodSlide.finetunePos = 0; + _chn[i].periodSlide.data = 0; + _chn[i].periodSlide.amount = 0; + _chn[i].periodSlide.dataOffset = 0; + _chn[i].periodSlide.flags = 0; + _chn[i].periodSlide.curDelay1 = 0; + _chn[i].periodSlide.curDelay2 = 0; + _chn[i].period = 0; + _chn[i].flags = 0x81; + _chn[i].ticks = 0; + _chn[i].tickCount = 0; + _chn[i].periodMod = 0; + } + + _end = (_data == 0); +} + +void Infogrames::reset() { + int i; + + stopPlay(); + init(); + + _volSlideBlocks = 0; + _periodSlideBlocks = 0; + _subSong = 0; + _cmdBlocks = 0; + _speedCounter = 0; + _speed = 0; + + for (i = 0; i < 4; i++) + _chn[i].cmdBlockIndices = 0; +} + +bool Infogrames::load(const char *dum) { + Common::File f; + + if (f.open(dum)) + return load(f); + return false; +} + +bool Infogrames::load(Common::SeekableReadStream &dum) { + int subSong = 0; + int i; + uint32 size; + + size = dum.size(); + if (size < 20) + return false; + + _data = new uint8[size]; + dum.seek(0); + dum.read(_data, size); + + Common::MemoryReadStream dataStr(_data, size); + + dataStr.seek(subSong * 2); + dataStr.seek(dataStr.readUint16BE()); + _subSong = _data + dataStr.pos(); + if (_subSong > (_data + size)) + return false; + + _speedCounter = dataStr.readUint16BE(); + _speed = _speedCounter; + _volSlideBlocks = _subSong + dataStr.readUint16BE(); + _periodSlideBlocks = _subSong + dataStr.readUint16BE(); + for (i = 0; i < 4; i++) { + _chn[i].cmdBlockIndices = _subSong + dataStr.readUint16BE(); + _chn[i].flags = 0x81; + } + _cmdBlocks = _data + dataStr.pos() + 2; + + if ((_volSlideBlocks > (_data + size)) || + (_periodSlideBlocks > (_data + size)) || + (_chn[0].cmdBlockIndices > (_data + size)) || + (_chn[1].cmdBlockIndices > (_data + size)) || + (_chn[2].cmdBlockIndices > (_data + size)) || + (_chn[3].cmdBlockIndices > (_data + size)) || + (_cmdBlocks > (_data + size))) + return false; + + startPaula(); + return true; +} + +void Infogrames::unload() { + stopPlay(); + + delete[] _data; + _data = 0; + + clearVoices(); + reset(); +} + +void Infogrames::getNextSample(Channel &chn) { + byte *data; + byte cmdBlock = 0; + uint16 cmd; + bool cont = false; + + if (chn.flags & 64) + return; + + if (chn.flags & 1) { + chn.flags &= ~1; + chn.cmdBlocks = chn.cmdBlockIndices; + } else { + chn.flags &= ~1; + if (_speedCounter == 0) + chn.ticks--; + if (chn.ticks != 0) { + _volume = MAX((int16) 0, tune(chn.volSlide, 0)); + _period = tune(chn.periodSlide, chn.period); + return; + } else { + chn.ticks = chn.tickCount; + cont = true; + } + } + + while (1) { + while (cont || ((cmdBlock = *chn.cmdBlocks) != 0xFF)) { + if (!cont) { + chn.cmdBlocks++; + chn.cmds = _subSong + + READ_BE_UINT16(_cmdBlocks + (cmdBlock * 2)); + } else + cont = false; + while ((cmd = *chn.cmds) != 0xFF) { + chn.cmds++; + if (cmd & 128) + { + switch (cmd & 0xE0) { + case 0x80: // 100xxxxx - Set ticks + chn.ticks = tickCount[cmd & 0xF]; + chn.tickCount = tickCount[cmd & 0xF]; + break; + case 0xA0: // 101xxxxx - Set sample + _sample = cmd & 0x1F; + break; + case 0xC0: // 110xxxxx - Set volume slide/finetune + data = _volSlideBlocks + (cmd & 0x1F) * 13; + chn.volSlide.flags = (*data & 0x80) | 1; + chn.volSlide.amount = *data++ & 0x7F; + chn.volSlide.data = data; + chn.volSlide.dataOffset = 0; + chn.volSlide.finetunePos = 0; + chn.volSlide.finetuneNeg = 0; + chn.volSlide.curDelay1 = 0; + chn.volSlide.curDelay2 = 0; + break; + case 0xE0: // 111xxxxx - Extended + switch (cmd & 0x1F) { + case 0: // Set period modifier + chn.periodMod = (int8) *chn.cmds++; + break; + case 1: // Set continuous period slide + chn.periodSlide.data = + _periodSlideBlocks + *chn.cmds++ * 13 + 1; + chn.periodSlide.amount = 0; + chn.periodSlide.dataOffset = 0; + chn.periodSlide.finetunePos = 0; + chn.periodSlide.finetuneNeg = 0; + chn.periodSlide.curDelay1 = 0; + chn.periodSlide.curDelay2 = 0; + chn.periodSlide.flags = 0x81; + break; + case 2: // Set non-continuous period slide + chn.periodSlide.data = + _periodSlideBlocks + *chn.cmds++ * 13 + 1; + chn.periodSlide.amount = 0; + chn.periodSlide.dataOffset = 0; + chn.periodSlide.finetunePos = 0; + chn.periodSlide.finetuneNeg = 0; + chn.periodSlide.curDelay1 = 0; + chn.periodSlide.curDelay2 = 0; + chn.periodSlide.flags = 1; + break; + case 3: // NOP + break; + default: + warning("Unknown Infogrames command: %X", cmd); + } + break; + } + } else { // 0xxxxxxx - Set period + if (cmd != 0) + cmd += chn.periodMod; + chn.period = periods[cmd]; + chn.volSlide.dataOffset = 0; + chn.volSlide.finetunePos = 0; + chn.volSlide.finetuneNeg = 0; + chn.volSlide.curDelay1 = 0; + chn.volSlide.curDelay2 = 0; + chn.volSlide.flags |= 1; + chn.volSlide.flags &= ~4; + chn.periodSlide.dataOffset = 0; + chn.periodSlide.finetunePos = 0; + chn.periodSlide.finetuneNeg = 0; + chn.periodSlide.curDelay1 = 0; + chn.periodSlide.curDelay2 = 0; + chn.periodSlide.flags |= 1; + chn.periodSlide.flags &= ~4; + _volume = MAX((int16) 0, tune(chn.volSlide, 0)); + _period = tune(chn.periodSlide, chn.period); + return; + } + } + } + if (!(chn.flags & 32)) { + chn.flags |= 0x40; + _volume = 0; + return; + } else + chn.cmdBlocks = chn.cmdBlockIndices; + } +} + +int16 Infogrames::tune(Slide &slide, int16 start) const { + byte *data; + uint8 off; + + data = slide.data + slide.dataOffset; + + if (slide.flags & 1) + slide.finetunePos += (int8) data[1]; + slide.flags &= ~1; + + start += slide.finetunePos - slide.finetuneNeg; + if (start < 0) + start = 0; + + if (slide.flags & 4) + return start; + + slide.curDelay1++; + if (slide.curDelay1 != data[2]) + return start; + slide.curDelay2++; + slide.curDelay1 = 0; + if (slide.curDelay2 == data[0]) { + slide.curDelay2 = 0; + off = slide.dataOffset + 3; + if (off == 12) { + if (slide.flags == 0) { + slide.flags |= 4; + return start; + } else { + slide.curDelay2 = 0; + slide.finetuneNeg += slide.amount; + off = 3; + } + } + slide.dataOffset = off; + } + slide.flags |= 1; + return start; +} + +void Infogrames::interrupt() { + int chn; + + if (!_data) { + clearVoices(); + return; + } + + _speedCounter--; + _sample = 0xFF; + for (chn = 0; chn < 4; chn++) { + _volume = 0; + _period = 0; + getNextSample(_chn[chn]); + setChannelVolume(chn, _volume); + setChannelPeriod(chn, _period); + if ((_sample != 0xFF) && (_sample < _instruments->_count)) { + setChannelData(chn, + _instruments->_samples[_sample].data, + _instruments->_samples[_sample].dataRepeat, + _instruments->_samples[_sample].length, + _instruments->_samples[_sample].lengthRepeat); + _sample = 0xFF; + } + } + if (_speedCounter == 0) + _speedCounter = _speed; + + // End reached? + if ((_chn[0].flags & 64) && (_chn[1].flags & 64) && + (_chn[2].flags & 64) && (_chn[3].flags & 64)) { + if (_repCount > 0) { + _repCount--; + init(); + } else if (_repCount != -1) { + stopPaula(); + } else { + init(); + } + } +} + +} // End of namespace Audio diff --git a/audio/mods/infogrames.h b/audio/mods/infogrames.h new file mode 100644 index 0000000000..c7abebf24e --- /dev/null +++ b/audio/mods/infogrames.h @@ -0,0 +1,148 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +/** + * @file + * Sound decoder used in engines: + * - gob + */ + +#ifndef SOUND_MODS_INFOGRAMES_H +#define SOUND_MODS_INFOGRAMES_H + +#include "audio/mods/paula.h" +#include "common/stream.h" + +namespace Audio { + +/** A player for the Infogrames/RobHubbard2 format */ +class Infogrames : public Paula { +public: + class Instruments { + public: + Instruments(); + template<typename T> Instruments(T ins) { + init(); + bool result = load(ins); + assert(result); + } + ~Instruments(); + + bool load(Common::SeekableReadStream &ins); + bool load(const char *ins); + void unload(); + + uint8 getCount() const { return _count; } + + protected: + struct Sample { + int8 *data; + int8 *dataRepeat; + uint32 length; + uint32 lengthRepeat; + } _samples[32]; + + uint8 _count; + int8 *_sampleData; + + void init(); + + friend class Infogrames; + }; + + Infogrames(Instruments &ins, bool stereo = false, int rate = 44100, + int interruptFreq = 0); + ~Infogrames(); + + Instruments *getInstruments() const { return _instruments; } + bool getRepeating() const { return _repCount != 0; } + void setRepeating (int32 repCount) { _repCount = repCount; } + + bool load(Common::SeekableReadStream &dum); + bool load(const char *dum); + void unload(); + void restart() { + if (_data) { + // Use the mutex here to ensure we do not call init() + // while data is being read by the mixer thread. + _mutex.lock(); + init(); + startPlay(); + _mutex.unlock(); + } + } + +protected: + Instruments *_instruments; + + static const uint8 tickCount[]; + static const uint16 periods[]; + byte *_data; + int32 _repCount; + + byte *_subSong; + byte *_cmdBlocks; + byte *_volSlideBlocks; + byte *_periodSlideBlocks; + uint8 _speedCounter; + uint8 _speed; + + uint16 _volume; + int16 _period; + uint8 _sample; + + struct Slide { + byte *data; + int8 amount; + uint8 dataOffset; + int16 finetuneNeg; + int16 finetunePos; + uint8 curDelay1; + uint8 curDelay2; + uint8 flags; // 0: Apply finetune modifier, 2: Don't slide, 7: Continuous + }; + struct Channel { + byte *cmdBlockIndices; + byte *cmdBlocks; + byte *cmds; + uint8 ticks; + uint8 tickCount; + Slide volSlide; + Slide periodSlide; + int16 period; + int8 periodMod; + uint8 flags; // 0: Need init, 5: Loop cmdBlocks, 6: Ignore channel + } _chn[4]; + + void init(); + void reset(); + void getNextSample(Channel &chn); + int16 tune(Slide &slide, int16 start) const; + virtual void interrupt(); +}; + +} // End of namespace Audio + +#endif diff --git a/audio/mods/maxtrax.cpp b/audio/mods/maxtrax.cpp new file mode 100644 index 0000000000..a577c72eed --- /dev/null +++ b/audio/mods/maxtrax.cpp @@ -0,0 +1,1040 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "common/scummsys.h" +#include "common/endian.h" +#include "common/stream.h" +#include "common/util.h" +#include "common/debug.h" + +#include "audio/mods/maxtrax.h" + +// test for engines using this class. +#if defined(SOUND_MODS_MAXTRAX_H) + +namespace { + +enum { K_VALUE = 0x9fd77, PREF_PERIOD = 0x8fd77, PERIOD_LIMIT = 0x6f73d }; +enum { NO_BEND = 64 << 7, MAX_BEND_RANGE = 24 }; + +int32 precalcNote(byte baseNote, int16 tune, byte octave) { + return K_VALUE + 0x3C000 - ((baseNote << 14) + (tune << 11) / 3) / 3 - (octave << 16); +} + +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; +} + +int32 calcTempo(const uint16 tempo, uint16 vBlankFreq) { + return (int32)(((uint32)(tempo & 0xFFF0) << 8) / (uint16)(5 * vBlankFreq)); +} + +void nullFunc(int) {} + +// 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 +// but this function should be faster and doesnt use floats +#if 1 +inline uint32 pow2Fixed(int32 val) { + 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 + }; + 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; +} +#else +inline uint32 pow2Fixed(int32 val) { + return (uint32)(expf((float)val * (float)(0.69314718055994530942 / (1 << 16))) + 0.5f); +} +#endif + +} // End of namespace + +namespace Audio { + +MaxTrax::MaxTrax(int rate, bool stereo, uint16 vBlankFreq, uint16 maxScores) + : Paula(stereo, rate, rate / vBlankFreq), + _patch(), + _scores(), + _numScores() { + _playerCtx.maxScoreNum = maxScores; + _playerCtx.vBlankFreq = vBlankFreq; + _playerCtx.frameUnit = (uint16)((1000 << 8) / vBlankFreq); + _playerCtx.scoreIndex = -1; + _playerCtx.volume = 0x40; + + _playerCtx.tempo = 120; + _playerCtx.tempoTime = 0; + _playerCtx.filterOn = true; + _playerCtx.syncCallBack = &nullFunc; + + resetPlayer(); + for (int i = 0; i < ARRAYSIZE(_channelCtx); ++i) + _channelCtx[i].regParamNumber = 0; +} + +MaxTrax::~MaxTrax() { + stopMusic(); + freePatches(); + freeScores(); +} + +void MaxTrax::interrupt() { + // a5 - maxtraxm a4 . globaldata + + // TODO + // 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 + + for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) { + 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; + } + } + } + + if (_playerCtx.scoreIndex >= 0) { + const Event *curEvent = _playerCtx.nextEvent; + int32 eventDelta = _playerCtx.nextEventTime - millis; + for (; eventDelta <= 0; eventDelta += (++curEvent)->startTime) { + const byte cmd = curEvent->command; + ChannelContext &channel = _channelCtx[curEvent->parameter & 0x0F]; + + // outPutEvent(*curEvent); + // debug("CurTime, EventDelta, NextDelta: %d, %d, %d", millis, eventDelta, eventDelta + curEvent[1].startTime ); + + if (cmd < 0x80) { // Note + const int8 voiceIndex = noteOn(channel, cmd, (curEvent->parameter & 0xF0) >> 1, kPriorityScore); + if (voiceIndex >= 0) + _voiceCtx[voiceIndex].stopEventTime = MAX<int32>(0, (eventDelta + curEvent->stopTime) << 8); + + } else { + switch (cmd) { + + case 0x80: // TEMPO + if ((_playerCtx.tickUnit >> 8) > curEvent->stopTime) { + _playerCtx.tickUnit = calcTempo(curEvent->parameter << 4, _playerCtx.vBlankFreq); + _playerCtx.tempoTime = 0; + } else { + _playerCtx.tempoStart = _playerCtx.tempo; + _playerCtx.tempoDelta = (curEvent->parameter << 4) - _playerCtx.tempoStart; + _playerCtx.tempoTime = (curEvent->stopTime << 8); + _playerCtx.tempoTicks = 0; + } + break; + + case 0xC0: // PROGRAM + channel.patch = &_patch[curEvent->stopTime & (kNumPatches - 1)]; + break; + + case 0xE0: // BEND + channel.pitchBend = ((curEvent->stopTime & 0x7F00) >> 1) | (curEvent->stopTime & 0x7f); + channel.pitchReal = (((int32)channel.pitchBendRange * channel.pitchBend) >> 5) - (channel.pitchBendRange << 8); + channel.isAltered = true; + break; + + case 0xFF: // END + if (_playerCtx.musicLoop) { + curEvent = _scores[_playerCtx.scoreIndex].events; + eventDelta = curEvent->startTime - millis; + _playerCtx.ticks = 0; + } else + _playerCtx.scoreIndex = -1; + // stop processing for this tick + goto endOfEventLoop; + + case 0xA0: // SPECIAL + switch (curEvent->stopTime >> 8){ + case 0x01: // SPECIAL_SYNC + _playerCtx.syncCallBack(curEvent->stopTime & 0xFF); + 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; + } + break; + + case 0xB0: // CONTROL + controlCh(channel, (byte)(curEvent->stopTime >> 8), (byte)curEvent->stopTime); + break; + + default: + debug("Unhandled Command"); + outPutEvent(*curEvent); + } + } + } +endOfEventLoop: + _playerCtx.nextEvent = curEvent; + _playerCtx.nextEventTime = eventDelta + millis; + + // tempoEffect + if (_playerCtx.tempoTime) { + _playerCtx.tempoTicks += _playerCtx.tickUnit; + uint16 newTempo = _playerCtx.tempoStart; + if (_playerCtx.tempoTicks < _playerCtx.tempoTime) { + newTempo += (uint16)((_playerCtx.tempoTicks * _playerCtx.tempoDelta) / _playerCtx.tempoTime); + } else { + _playerCtx.tempoTime = 0; + newTempo += _playerCtx.tempoDelta; + } + _playerCtx.tickUnit = calcTempo(newTempo, _playerCtx.vBlankFreq); + } + } + + // Handling of Envelopes and Portamento + for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) { + VoiceContext &voice = _voiceCtx[i]; + if (!voice.channel) + continue; + const ChannelContext &channel = *voice.channel; + const Patch &patch = *voice.patch; + + switch (voice.status) { + case VoiceContext::kStatusSustain: + // 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; + voice.isBlocked = 0; + voice.priority = 0; + // disable it in next tick + voice.stopEventTime = 0; + } + if (!channel.isAltered && !voice.hasPortamento && !channel.modulation) + continue; + // Update Volume and Period + break; + + case VoiceContext::kStatusHalt: + killVoice((byte)i); + continue; + + case VoiceContext::kStatusStart: + if (patch.attackLen) { + voice.envelope = patch.attackPtr; + const uint16 duration = voice.envelope->duration; + voice.envelopeLeft = patch.attackLen; + voice.ticksLeft = duration << 8; + voice.status = VoiceContext::kStatusAttack; + voice.incrVolume = calcVolumeDelta((int32)voice.envelope->volume, duration, _playerCtx.vBlankFreq); + // Process Envelope + } else { + voice.status = VoiceContext::kStatusSustain; + voice.baseVolume = patch.volume; + // Update Volume and Period + } + break; + + case VoiceContext::kStatusRelease: + if (patch.releaseLen) { + voice.envelope = patch.attackPtr + patch.attackLen; + const uint16 duration = voice.envelope->duration; + voice.envelopeLeft = patch.releaseLen; + voice.ticksLeft = duration << 8; + voice.status = VoiceContext::kStatusDecay; + voice.incrVolume = calcVolumeDelta((int32)voice.envelope->volume - voice.baseVolume, duration, _playerCtx.vBlankFreq); + // Process Envelope + } else { + voice.status = VoiceContext::kStatusHalt; + voice.lastVolume = 0; + // Send Audio Packet + } + voice.stopEventTime = -1; + break; + } + + // Process Envelope + const uint16 envUnit = _playerCtx.frameUnit; + if (voice.envelope) { + if (voice.ticksLeft > envUnit) { // envelope still active + voice.baseVolume = (uint16) MIN<int32>(MAX<int32>(0, voice.baseVolume + voice.incrVolume), 0x8000); + voice.ticksLeft -= envUnit; + // Update Volume and Period + + } else { // next or last Envelope + voice.baseVolume = voice.envelope->volume; + assert(voice.envelopeLeft > 0); + if (--voice.envelopeLeft) { + ++voice.envelope; + const uint16 duration = voice.envelope->duration; + voice.ticksLeft = duration << 8; + voice.incrVolume = calcVolumeDelta((int32)voice.envelope->volume - voice.baseVolume, duration, _playerCtx.vBlankFreq); + // Update Volume and Period + } else if (voice.status == VoiceContext::kStatusDecay) { + voice.status = VoiceContext::kStatusHalt; + voice.envelope = 0; + voice.lastVolume = 0; + // Send Audio Packet + } else { + assert(voice.status == VoiceContext::kStatusAttack); + voice.status = VoiceContext::kStatusSustain; + voice.envelope = 0; + // Update Volume and Period + } + } + } + + // Update Volume and Period + if (voice.status >= VoiceContext::kStatusDecay) { + // Calc volume + uint16 vol = (voice.noteVolume < (1 << 7)) ? (voice.noteVolume * _playerCtx.volume) >> 7 : _playerCtx.volume; + if (voice.baseVolume < (1 << 15)) + vol = (uint16)(((uint32)vol * voice.baseVolume) >> 15); + if (voice.channel->volume < (1 << 7)) + vol = (vol * voice.channel->volume) >> 7; + voice.lastVolume = (byte)MIN(vol, (uint16)0x64); + + // Calc Period + if (voice.hasPortamento) { + voice.portaTicks += envUnit; + if ((uint16)(voice.portaTicks >> 8) >= channel.portamentoTime) { + voice.hasPortamento = false; + voice.baseNote = voice.endNote; + voice.preCalcNote = precalcNote(voice.baseNote, patch.tune, voice.octave); + } + voice.lastPeriod = calcNote(voice); + } else if (channel.isAltered || channel.modulation) + voice.lastPeriod = calcNote(voice); + } + + // Send Audio Packet + Paula::setChannelPeriod((byte)i, (voice.lastPeriod) ? voice.lastPeriod : 1000); + Paula::setChannelVolume((byte)i, (voice.lastPeriod) ? voice.lastVolume : 0); + } + for (ChannelContext *c = _channelCtx; c != &_channelCtx[ARRAYSIZE(_channelCtx)]; ++c) + c->isAltered = false; + +#ifdef MAXTRAX_HAS_MODULATION + // original player had _playerCtx.sineValue = _playerCtx.frameUnit >> 2 + // this should fit the comments that modtime=1000 is one second ? + _playerCtx.sineValue += _playerCtx.frameUnit; +#endif +} + +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 + channel.modulation = (channel.modulation & 0xFF00) || ((data * 2) & 0xFF); + break; + case 0x05: // portamento time MSB + channel.portamentoTime = data << 7; + break; + case 0x25: // portamento time LSB + channel.portamentoTime = (channel.portamentoTime & 0x3f80) || data; + break; + case 0x06: // data entry MSB + 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; + } + 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 + channel.modulationTime = (channel.modulationTime & 0x3f80) || data; + break; + case 0x11: // GPC as Microtonal Set MSB + channel.microtonal = data << 8; + break; + case 0x31: // GPC as Microtonal Set LSB + channel.microtonal = (channel.microtonal & 0xFF00) || ((data * 2) & 0xFF); + 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; + case 0x50: // Microtonal off/on + 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 + channel.regParamNumber = (data << 8) || (channel.regParamNumber & 0xFF); + break; + case 0x64: // RPN LSB + channel.regParamNumber = (channel.regParamNumber & 0xFF00) || data; + break; + case 0x79: // Reset All Controllers + resetChannel(channel, ((&channel - _channelCtx) & 1) != 0); + 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) + killVoice((byte)i); + } + break; + } +} + +void MaxTrax::setTempo(const uint16 tempo) { + Common::StackLock lock(_mutex); + _playerCtx.tickUnit = calcTempo(tempo, _playerCtx.vBlankFreq); +} + +void MaxTrax::resetPlayer() { + for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) + killVoice((byte)i); + + for (int i = 0; i < ARRAYSIZE(_channelCtx); ++i) { + _channelCtx[i].flags = 0; + _channelCtx[i].lastNote = (uint8)-1; + resetChannel(_channelCtx[i], (i & 1) != 0); + _channelCtx[i].patch = (i < kNumChannels) ? &_patch[i] : 0; + } + +#ifdef MAXTRAX_HAS_MICROTONAL + for (int i = 0; i < ARRAYSIZE(_microtonal); ++i) + _microtonal[i] = (int16)(i << 8); +#endif +} + +void MaxTrax::stopMusic() { + Common::StackLock lock(_mutex); + _playerCtx.scoreIndex = -1; + for (int i = 0; i < ARRAYSIZE(_voiceCtx); ++i) { + if (_voiceCtx[i].channel < &_channelCtx[kNumChannels]) + killVoice((byte)i); + } +} + +bool MaxTrax::playSong(int songIndex, bool loop) { + if (songIndex < 0 || songIndex >= _numScores) + return false; + Common::StackLock lock(_mutex); + _playerCtx.scoreIndex = -1; + resetPlayer(); + for (int i = 0; i < ARRAYSIZE(_playerCtx.repeatPoint); ++i) + _playerCtx.repeatPoint[i] = 0; + + setTempo(_playerCtx.tempoInitial << 4); + Paula::setAudioFilter(_playerCtx.filterOn); + _playerCtx.musicLoop = loop; + _playerCtx.tempoTime = 0; + _playerCtx.scoreIndex = songIndex; + _playerCtx.ticks = 0; + + _playerCtx.nextEvent = _scores[songIndex].events; + _playerCtx.nextEventTime = _playerCtx.nextEvent->startTime; + + Paula::startPaula(); + return true; +} + +void MaxTrax::advanceSong(int advance) { + Common::StackLock lock(_mutex); + if (_playerCtx.scoreIndex >= 0) { + const Event *cev = _playerCtx.nextEvent; + if (cev) { + for (; advance > 0; --advance) { + // TODO - check for boundaries + for (; cev->command != 0xFF && (cev->command != 0xA0 || (cev->stopTime >> 8) != 0x00); ++cev) + ; // no end_command or special_command + end + } + _playerCtx.nextEvent = cev; + } + } +} + +void MaxTrax::killVoice(byte num) { + VoiceContext &voice = _voiceCtx[num]; + voice.channel = 0; + voice.envelope = 0; + voice.status = VoiceContext::kStatusFree; + voice.isBlocked = 0; + voice.hasDamper = false; + voice.hasPortamento = false; + voice.priority = 0; + voice.stopEventTime = -1; + voice.dmaOff = 0; + voice.lastVolume = 0; + voice.tieBreak = 0; + //voice.uinqueId = 0; + + // "stop" voice, set period to 1, vol to 0 + Paula::disableChannel(num); + Paula::setChannelPeriod(num, 1); + Paula::setChannelVolume(num, 0); +} + +int8 MaxTrax::pickvoice(uint pick, int16 pri) { + enum { kPrioFlagFixedSide = 1 << 3 }; + pick &= 3; + if ((pri & (kPrioFlagFixedSide)) == 0) { + const bool leftSide = (uint)(pick - 1) > 1; + const int leftBest = MIN(_voiceCtx[0].status, _voiceCtx[3].status); + const int rightBest = MIN(_voiceCtx[1].status, _voiceCtx[2].status); + const int sameSide = (leftSide) ? leftBest : rightBest; + const int otherSide = leftBest + rightBest - sameSide; + + if (sameSide > VoiceContext::kStatusRelease && otherSide <= VoiceContext::kStatusRelease) + pick ^= 1; // switches sides + } + pri &= ~kPrioFlagFixedSide; + + for (int i = 2; i > 0; --i) { + 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; + + if (voiceVal + voice->tieBreak > altVal + || voice->isBlocked > alternate->isBlocked) { + + // this is somewhat different to the original player, + // but has a similar result + voice->tieBreak = 0; + alternate->tieBreak = 1; + + pick ^= 3; // switch channels + VoiceContext *tmp = voice; + voice = alternate; + alternate = tmp; + } + + if (voice->isBlocked || voice->priority > pri) { + // if not already done, switch sides and try again + pick ^= 1; + continue; + } + // succeded + return (int8)pick; + } + // failed + debug(5, "MaxTrax: could not find channel for note"); + return -1; +} + +uint16 MaxTrax::calcNote(const VoiceContext &voice) { + const ChannelContext &channel = *voice.channel; + int16 bend = channel.pitchReal; + +#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 + if (voice.hasPortamento) + bend += (int16)(((int8)(voice.endNote - voice.baseNote)) * voice.portaTicks) / channel.portamentoTime; +#endif + +#ifdef MAXTRAX_HAS_MODULATION + 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 + }; + if (channel.modulation) { + if ((channel.flags & ChannelContext::kFlagModVolume) == 0) { + const uint8 sineByte = _playerCtx.sineValue / channel.modulationTime; + const uint8 sineIndex = sineByte & 0x7F; + const int16 modVal = ((uint32)(uint16)(tableSine[sineIndex] + (sineIndex ? 1 : 0)) * channel.modulation) >> 8; + bend = (sineByte < 0x80) ? bend + modVal : bend - modVal; + } + } +#endif + + // tone = voice.baseNote << 8 + microtonal + // bend = channelPitch + porta + modulation + + const int32 tone = voice.preCalcNote + (bend << 6) / 3; + + return (tone >= PERIOD_LIMIT) ? (uint16)pow2Fixed(tone) : 0; +} + +int8 MaxTrax::noteOn(ChannelContext &channel, const byte note, uint16 volume, uint16 pri) { +#ifdef MAXTRAX_HAS_MICROTONAL + if (channel.microtonal >= 0) + _microtonal[note % 127] = channel.microtonal; +#endif + + if (!volume) + return -1; + + const Patch &patch = *channel.patch; + if (!patch.samplePtr || patch.sampleTotalLen == 0) + return -1; + int8 voiceNum = -1; + if ((channel.flags & ChannelContext::kFlagMono) == 0) { + voiceNum = pickvoice((channel.flags & ChannelContext::kFlagRightChannel) != 0 ? 1 : 0, pri); + } else { + VoiceContext *voice = _voiceCtx + ARRAYSIZE(_voiceCtx) - 1; + for (voiceNum = ARRAYSIZE(_voiceCtx) - 1; voiceNum >= 0 && voice->channel != &channel; --voiceNum, --voice) + ; + if (voiceNum < 0) + voiceNum = pickvoice((channel.flags & ChannelContext::kFlagRightChannel) != 0 ? 1 : 0, pri); + else if (voice->status >= VoiceContext::kStatusSustain && (channel.flags & ChannelContext::kFlagPortamento) != 0) { + // reset previous porta + if (voice->hasPortamento) + voice->baseNote = voice->endNote; + voice->preCalcNote = precalcNote(voice->baseNote, patch.tune, voice->octave); + voice->noteVolume = (_playerCtx.handleVolume) ? volume + 1 : 128; + voice->portaTicks = 0; + voice->hasPortamento = true; + voice->endNote = channel.lastNote = note; + return voiceNum; + } + } + + if (voiceNum >= 0) { + VoiceContext &voice = _voiceCtx[voiceNum]; + voice.hasDamper = false; + voice.isBlocked = 0; + 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 + const int useOctave = (plainNote <= PREF_PERIOD) ? 0 : MIN<int32>((plainNote + 0xFFFF - PREF_PERIOD) >> 16, patch.sampleOctaves - 1); + voice.octave = (byte)useOctave; + // adjust precalculated value + voice.preCalcNote = plainNote - (useOctave << 16); + + // next calculate the actual period which depends on wether porta is enabled + 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; + } + + voice.lastPeriod = calcNote(voice); + + 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); + if (!patch.sampleAttackLen) + Paula::enableChannel(voiceNum); // need to enable channel + // another pointless wait for DMA-Clear??? + + } 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; + } + + Paula::setChannelPeriod(voiceNum, (voice.lastPeriod) ? voice.lastPeriod : 1000); + Paula::setChannelVolume(voiceNum, 0); + } + return voiceNum; +} + +void MaxTrax::resetChannel(ChannelContext &chan, bool rightChannel) { + chan.modulation = 0; + chan.modulationTime = 1000; + chan.microtonal = -1; + chan.portamentoTime = 500; + chan.pitchBend = NO_BEND; + chan.pitchReal = 0; + chan.pitchBendRange = MAX_BEND_RANGE; + chan.volume = 128; + chan.flags &= ~(ChannelContext::kFlagPortamento | ChannelContext::kFlagMicrotonal | ChannelContext::kFlagRightChannel); + chan.isAltered = true; + if (rightChannel) + chan.flags |= ChannelContext::kFlagRightChannel; +} + +void MaxTrax::freeScores() { + if (_scores) { + for (int i = 0; i < _numScores; ++i) + delete[] _scores[i].events; + delete[] _scores; + _scores = 0; + } + _numScores = 0; + _playerCtx.tempo = 120; + _playerCtx.filterOn = true; +} + +void MaxTrax::freePatches() { + for (int i = 0; i < ARRAYSIZE(_patch); ++i) { + delete[] _patch[i].samplePtr; + delete[] _patch[i].attackPtr; + } + memset(_patch, 0, sizeof(_patch)); +} + +void MaxTrax::setSignalCallback(void (*callback) (int)) { + Common::StackLock lock(_mutex); + _playerCtx.syncCallBack = (callback == 0) ? nullFunc : callback; +} + +int MaxTrax::playNote(byte note, byte patch, uint16 duration, uint16 volume, bool rightSide) { + Common::StackLock lock(_mutex); + 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); + if (voiceIndex >= 0) { + _voiceCtx[voiceIndex].stopEventTime = duration << 8; + Paula::startPaula(); + } + return voiceIndex; +} + +bool MaxTrax::load(Common::SeekableReadStream &musicData, bool loadScores, bool loadSamples) { + Common::StackLock lock(_mutex); + stopMusic(); + if (loadSamples) + freePatches(); + if (loadScores) + freeScores(); + const char *errorMsg = 0; + // 0x0000: 4 Bytes Header "MXTX" + // 0x0004: uint16 tempo + // 0x0006: uint16 flags. bit0 = lowpassfilter, bit1 = attackvolume, bit15 = microtonal + if (musicData.size() < 10 || musicData.readUint32BE() != 0x4D585458) { + warning("Maxtrax: File is not a Maxtrax Module"); + return false; + } + const uint16 songTempo = musicData.readUint16BE(); + const uint16 flags = musicData.readUint16BE(); + if (loadScores) { + _playerCtx.tempoInitial = songTempo; + _playerCtx.filterOn = (flags & 1) != 0; + _playerCtx.handleVolume = (flags & 2) != 0; + } + + if (flags & (1 << 15)) { + debug(5, "Maxtrax: Song has microtonal"); +#ifdef MAXTRAX_HAS_MICROTONAL + if (loadScores) { + for (int i = 0; i < ARRAYSIZE(_microtonal); ++i) + _microtonal[i] = musicData.readUint16BE(); + } else + musicData.skip(128 * 2); +#else + musicData.skip(128 * 2); +#endif + } + + int scoresLoaded = 0; + // uint16 number of Scores + const uint16 scoresInFile = musicData.readUint16BE(); + + if (musicData.err() || musicData.eos()) + goto ioError; + + if (loadScores) { + const uint16 tempScores = MIN(scoresInFile, _playerCtx.maxScoreNum); + Score *curScore = new Score[tempScores]; + if (!curScore) + goto allocError; + _scores = curScore; + + for (scoresLoaded = 0; scoresLoaded < tempScores; ++scoresLoaded, ++curScore) { + const uint32 numEvents = musicData.readUint32BE(); + Event *curEvent = new Event[numEvents]; + if (!curEvent) + goto allocError; + curScore->events = curEvent; + 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; + } + _numScores = scoresLoaded; + } + + if (loadSamples) { + // 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(); + for (int i = wavesInFile; i > 0; --i) { + // load disksample structure + const uint16 number = musicData.readUint16BE(); + assert(number < ARRAYSIZE(_patch)); + + Patch &curPatch = _patch[number]; + if (curPatch.attackPtr || curPatch.samplePtr) { + delete curPatch.attackPtr; + curPatch.attackPtr = 0; + delete curPatch.samplePtr; + curPatch.samplePtr = 0; + } + curPatch.tune = musicData.readSint16BE(); + curPatch.volume = musicData.readUint16BE(); + curPatch.sampleOctaves = musicData.readUint16BE(); + curPatch.sampleAttackLen = musicData.readUint32BE(); + const uint32 sustainLen = musicData.readUint32BE(); + curPatch.sampleTotalLen = curPatch.sampleAttackLen + sustainLen; + // each octave the number of samples doubles. + const uint32 totalSamples = curPatch.sampleTotalLen * ((1 << curPatch.sampleOctaves) - 1); + 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]; + if (!envPtr) + goto allocError; + // 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 + int8 *allocSamples = new int8[totalSamples]; + if (!allocSamples) + goto allocError; + curPatch.samplePtr = allocSamples; + musicData.read(allocSamples, totalSamples); + } + } + if (!musicData.err() && !musicData.eos()) + return true; +ioError: + errorMsg = "Maxtrax: Encountered IO-Error"; +allocError: + if (!errorMsg) + errorMsg = "Maxtrax: Could not allocate Memory"; + + warning("%s", errorMsg); + if (loadSamples) + freePatches(); + if (loadScores) + freeScores(); + return false; +} + +#if !defined(NDEBUG) && 0 +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 + +} // End of namespace Audio + +#endif // #if defined(SOUND_MODS_MAXTRAX_H) diff --git a/audio/mods/maxtrax.h b/audio/mods/maxtrax.h new file mode 100644 index 0000000000..2f890afe2d --- /dev/null +++ b/audio/mods/maxtrax.h @@ -0,0 +1,225 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +// see if all engines using this class are DISABLED +#if !defined(ENABLE_KYRA) + +// normal Header Guard +#elif !defined SOUND_MODS_MAXTRAX_H +#define SOUND_MODS_MAXTRAX_H + +// #define MAXTRAX_HAS_MODULATION +// #define MAXTRAX_HAS_MICROTONAL + +#include "audio/mods/paula.h" + +namespace Audio { + +class MaxTrax : public Paula { +public: + MaxTrax(int rate, bool stereo, uint16 vBlankFreq = 50, uint16 maxScores = 128); + virtual ~MaxTrax(); + + bool load(Common::SeekableReadStream &musicData, bool loadScores = true, bool loadSamples = true); + bool playSong(int songIndex, bool loop = false); + void advanceSong(int advance = 1); + int playNote(byte note, byte patch, uint16 duration, uint16 volume, bool rightSide); + void setVolume(const byte volume) { Common::StackLock lock(_mutex); _playerCtx.volume = volume; } + void setTempo(const uint16 tempo); + void stopMusic(); + /** + * Set a callback function for sync-events. + * @param callback Callback function, will be called synchronously, so DONT modify the player + * directly in response + */ + void setSignalCallback(void (*callback) (int)); + +protected: + void interrupt(); + +private: + enum { kNumPatches = 64, kNumVoices = 4, kNumChannels = 16, kNumExtraChannels = 1 }; + enum { kPriorityScore, kPriorityNote, kPrioritySound }; + +#ifdef MAXTRAX_HAS_MICROTONAL + int16 _microtonal[128]; +#endif + + struct Event { + uint16 startTime; + uint16 stopTime; + byte command; + byte parameter; + }; + + const struct Score { + const Event *events; + uint32 numEvents; + } *_scores; + + int _numScores; + + struct { + uint32 sineValue; + uint16 vBlankFreq; + int32 ticks; + int32 tickUnit; + uint16 frameUnit; + + uint16 maxScoreNum; + uint16 tempo; + uint16 tempoInitial; + uint16 tempoStart; + int16 tempoDelta; + int32 tempoTime; + int32 tempoTicks; + + byte volume; + + bool filterOn; + bool handleVolume; + bool musicLoop; + + int scoreIndex; + const Event *nextEvent; + int32 nextEventTime; + + void (*syncCallBack) (int); + const Event *repeatPoint[4]; + byte repeatCount[4]; + } _playerCtx; + + struct Envelope { + uint16 duration; + uint16 volume; + }; + + struct Patch { + const Envelope *attackPtr; + //Envelope *releasePtr; + uint16 attackLen; + uint16 releaseLen; + + int16 tune; + uint16 volume; + + // this was the SampleData struct in the assembler source + const int8 *samplePtr; + uint32 sampleTotalLen; + uint32 sampleAttackLen; + uint16 sampleOctaves; + } _patch[kNumPatches]; + + struct ChannelContext { + const Patch *patch; + uint16 regParamNumber; + + uint16 modulation; + uint16 modulationTime; + + int16 microtonal; + + uint16 portamentoTime; + + int16 pitchBend; + int16 pitchReal; + int8 pitchBendRange; + + uint8 volume; +// uint8 voicesActive; + + enum { + kFlagRightChannel = 1 << 0, + kFlagPortamento = 1 << 1, + kFlagDamper = 1 << 2, + kFlagMono = 1 << 3, + kFlagMicrotonal = 1 << 4, + kFlagModVolume = 1 << 5 + }; + byte flags; + bool isAltered; + + uint8 lastNote; +// uint8 program; + + } _channelCtx[kNumChannels + kNumExtraChannels]; + + struct VoiceContext { + ChannelContext *channel; + const Patch *patch; + const Envelope *envelope; +// uint32 uinqueId; + int32 preCalcNote; + uint32 ticksLeft; + int32 portaTicks; + int32 incrVolume; +// int32 periodOffset; + uint16 envelopeLeft; + uint16 noteVolume; + uint16 baseVolume; + uint16 lastPeriod; + byte baseNote; + byte endNote; + byte octave; +// byte number; +// byte link; + enum { + kStatusFree, + kStatusHalt, + kStatusDecay, + kStatusRelease, + kStatusSustain, + kStatusAttack, + kStatusStart + }; + uint8 isBlocked; + uint8 priority; + byte status; + byte lastVolume; + byte tieBreak; + bool hasDamper; + bool hasPortamento; + byte dmaOff; + + int32 stopEventTime; + } _voiceCtx[kNumVoices]; + + void controlCh(ChannelContext &channel, byte command, byte data); + void freePatches(); + void freeScores(); + void resetChannel(ChannelContext &chan, bool rightChannel); + void resetPlayer(); + + int8 pickvoice(uint pick, int16 pri); + uint16 calcNote(const VoiceContext &voice); + int8 noteOn(ChannelContext &channel, byte note, uint16 volume, uint16 pri); + void killVoice(byte num); + + static void outPutEvent(const Event &ev, int num = -1); + static void outPutScore(const Score &sc, int num = -1); +}; +} // End of namespace Audio + +#endif // !defined SOUND_MODS_MAXTRAX_H diff --git a/audio/mods/module.cpp b/audio/mods/module.cpp new file mode 100644 index 0000000000..0da6923b5d --- /dev/null +++ b/audio/mods/module.cpp @@ -0,0 +1,252 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "audio/mods/module.h" + +#include "common/util.h" +#include "common/endian.h" + +namespace Modules { + +const int16 Module::periods[16][60] = { + {1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960 , 906, + 856 , 808 , 762 , 720 , 678 , 640 , 604 , 570 , 538 , 508 , 480 , 453, + 428 , 404 , 381 , 360 , 339 , 320 , 302 , 285 , 269 , 254 , 240 , 226, + 214 , 202 , 190 , 180 , 170 , 160 , 151 , 143 , 135 , 127 , 120 , 113, + 107 , 101 , 95 , 90 , 85 , 80 , 75 , 71 , 67 , 63 , 60 , 56 }, + {1700, 1604, 1514, 1430, 1348, 1274, 1202, 1134, 1070, 1010, 954 , 900, + 850 , 802 , 757 , 715 , 674 , 637 , 601 , 567 , 535 , 505 , 477 , 450, + 425 , 401 , 379 , 357 , 337 , 318 , 300 , 284 , 268 , 253 , 239 , 225, + 213 , 201 , 189 , 179 , 169 , 159 , 150 , 142 , 134 , 126 , 119 , 113, + 106 , 100 , 94 , 89 , 84 , 79 , 75 , 71 , 67 , 63 , 59 , 56 }, + {1688, 1592, 1504, 1418, 1340, 1264, 1194, 1126, 1064, 1004, 948 , 894, + 844 , 796 , 752 , 709 , 670 , 632 , 597 , 563 , 532 , 502 , 474 , 447, + 422 , 398 , 376 , 355 , 335 , 316 , 298 , 282 , 266 , 251 , 237 , 224, + 211 , 199 , 188 , 177 , 167 , 158 , 149 , 141 , 133 , 125 , 118 , 112, + 105 , 99 , 94 , 88 , 83 , 79 , 74 , 70 , 66 , 62 , 59 , 56 }, + {1676, 1582, 1492, 1408, 1330, 1256, 1184, 1118, 1056, 996 , 940 , 888, + 838 , 791 , 746 , 704 , 665 , 628 , 592 , 559 , 528 , 498 , 470 , 444, + 419 , 395 , 373 , 352 , 332 , 314 , 296 , 280 , 264 , 249 , 235 , 222, + 209 , 198 , 187 , 176 , 166 , 157 , 148 , 140 , 132 , 125 , 118 , 111, + 104 , 99 , 93 , 88 , 83 , 78 , 74 , 70 , 66 , 62 , 59 , 55 }, + {1664, 1570, 1482, 1398, 1320, 1246, 1176, 1110, 1048, 990 , 934 , 882, + 832 , 785 , 741 , 699 , 660 , 623 , 588 , 555 , 524 , 495 , 467 , 441, + 416 , 392 , 370 , 350 , 330 , 312 , 294 , 278 , 262 , 247 , 233 , 220, + 208 , 196 , 185 , 175 , 165 , 156 , 147 , 139 , 131 , 124 , 117 , 110, + 104 , 98 , 92 , 87 , 82 , 78 , 73 , 69 , 65 , 62 , 58 , 55 }, + {1652, 1558, 1472, 1388, 1310, 1238, 1168, 1102, 1040, 982 , 926 , 874, + 826 , 779 , 736 , 694 , 655 , 619 , 584 , 551 , 520 , 491 , 463 , 437, + 413 , 390 , 368 , 347 , 328 , 309 , 292 , 276 , 260 , 245 , 232 , 219, + 206 , 195 , 184 , 174 , 164 , 155 , 146 , 138 , 130 , 123 , 116 , 109, + 103 , 97 , 92 , 87 , 82 , 77 , 73 , 69 , 65 , 61 , 58 , 54 }, + {1640, 1548, 1460, 1378, 1302, 1228, 1160, 1094, 1032, 974 , 920 , 868, + 820 , 774 , 730 , 689 , 651 , 614 , 580 , 547 , 516 , 487 , 460 , 434, + 410 , 387 , 365 , 345 , 325 , 307 , 290 , 274 , 258 , 244 , 230 , 217, + 205 , 193 , 183 , 172 , 163 , 154 , 145 , 137 , 129 , 122 , 115 , 109, + 102 , 96 , 91 , 86 , 81 , 77 , 72 , 68 , 64 , 61 , 57 , 54 }, + {1628, 1536, 1450, 1368, 1292, 1220, 1150, 1086, 1026, 968 , 914 , 862, + 814 , 768 , 725 , 684 , 646 , 610 , 575 , 543 , 513 , 484 , 457 , 431, + 407 , 384 , 363 , 342 , 323 , 305 , 288 , 272 , 256 , 242 , 228 , 216, + 204 , 192 , 181 , 171 , 161 , 152 , 144 , 136 , 128 , 121 , 114 , 108, + 102 , 96 , 90 , 85 , 80 , 76 , 72 , 68 , 64 , 60 , 57 , 54 }, + {1814, 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, + 907 , 856 , 808 , 762 , 720 , 678 , 640 , 604 , 570 , 538 , 508 , 480, + 453 , 428 , 404 , 381 , 360 , 339 , 320 , 302 , 285 , 269 , 254 , 240, + 226 , 214 , 202 , 190 , 180 , 170 , 160 , 151 , 143 , 135 , 127 , 120, + 113 , 107 , 101 , 95 , 90 , 85 , 80 , 75 , 71 , 67 , 63 , 60 }, + {1800, 1700, 1604, 1514, 1430, 1350, 1272, 1202, 1134, 1070, 1010, 954, + 900 , 850 , 802 , 757 , 715 , 675 , 636 , 601 , 567 , 535 , 505 , 477, + 450 , 425 , 401 , 379 , 357 , 337 , 318 , 300 , 284 , 268 , 253 , 238, + 225 , 212 , 200 , 189 , 179 , 169 , 159 , 150 , 142 , 134 , 126 , 119, + 112 , 106 , 100 , 94 , 89 , 84 , 79 , 75 , 71 , 67 , 63 , 59 }, + {1788, 1688, 1592, 1504, 1418, 1340, 1264, 1194, 1126, 1064, 1004, 948, + 894 , 844 , 796 , 752 , 709 , 670 , 632 , 597 , 563 , 532 , 502 , 474, + 447 , 422 , 398 , 376 , 355 , 335 , 316 , 298 , 282 , 266 , 251 , 237, + 223 , 211 , 199 , 188 , 177 , 167 , 158 , 149 , 141 , 133 , 125 , 118, + 111 , 105 , 99 , 94 , 88 , 83 , 79 , 74 , 70 , 66 , 62 , 59 }, + {1774, 1676, 1582, 1492, 1408, 1330, 1256, 1184, 1118, 1056, 996 , 940, + 887 , 838 , 791 , 746 , 704 , 665 , 628 , 592 , 559 , 528 , 498 , 470, + 444 , 419 , 395 , 373 , 352 , 332 , 314 , 296 , 280 , 264 , 249 , 235, + 222 , 209 , 198 , 187 , 176 , 166 , 157 , 148 , 140 , 132 , 125 , 118, + 111 , 104 , 99 , 93 , 88 , 83 , 78 , 74 , 70 , 66 , 62 , 59 }, + {1762, 1664, 1570, 1482, 1398, 1320, 1246, 1176, 1110, 1048, 988 , 934, + 881 , 832 , 785 , 741 , 699 , 660 , 623 , 588 , 555 , 524 , 494 , 467, + 441 , 416 , 392 , 370 , 350 , 330 , 312 , 294 , 278 , 262 , 247 , 233, + 220 , 208 , 196 , 185 , 175 , 165 , 156 , 147 , 139 , 131 , 123 , 117, + 110 , 104 , 98 , 92 , 87 , 82 , 78 , 73 , 69 , 65 , 61 , 58 }, + {1750, 1652, 1558, 1472, 1388, 1310, 1238, 1168, 1102, 1040, 982 , 926, + 875 , 826 , 779 , 736 , 694 , 655 , 619 , 584 , 551 , 520 , 491 , 463, + 437 , 413 , 390 , 368 , 347 , 328 , 309 , 292 , 276 , 260 , 245 , 232, + 219 , 206 , 195 , 184 , 174 , 164 , 155 , 146 , 138 , 130 , 123 , 116, + 109 , 103 , 97 , 92 , 87 , 82 , 77 , 73 , 69 , 65 , 61 , 58 }, + {1736, 1640, 1548, 1460, 1378, 1302, 1228, 1160, 1094, 1032, 974 , 920, + 868 , 820 , 774 , 730 , 689 , 651 , 614 , 580 , 547 , 516 , 487 , 460, + 434 , 410 , 387 , 365 , 345 , 325 , 307 , 290 , 274 , 258 , 244 , 230, + 217 , 205 , 193 , 183 , 172 , 163 , 154 , 145 , 137 , 129 , 122 , 115, + 108 , 102 , 96 , 91 , 86 , 81 , 77 , 72 , 68 , 64 , 61 , 57 }, + {1724, 1628, 1536, 1450, 1368, 1292, 1220, 1150, 1086, 1026, 968 , 914, + 862 , 814 , 768 , 725 , 684 , 646 , 610 , 575 , 543 , 513 , 484 , 457, + 431 , 407 , 384 , 363 , 342 , 323 , 305 , 288 , 272 , 256 , 242 , 228, + 216 , 203 , 192 , 181 , 171 , 161 , 152 , 144 , 136 , 128 , 121 , 114, + 108 , 101 , 96 , 90 , 85 , 80 , 76 , 72 , 68 , 64 , 60 , 57 }}; + +const uint32 Module::signatures[] = { + MKID_BE('M.K.'), MKID_BE('M!K!'), MKID_BE('FLT4') +}; + +bool Module::load(Common::SeekableReadStream &st, int offs) { + if (offs) { + // Load the module with the common sample data + load(st, 0); + } + + st.seek(offs); + st.read(songname, 20); + songname[20] = '\0'; + + for (int i = 0; i < NUM_SAMPLES; ++i) { + st.read(sample[i].name, 22); + sample[i].name[22] = '\0'; + sample[i].len = 2 * st.readUint16BE(); + + sample[i].finetune = st.readByte(); + assert(sample[i].finetune < 0x10); + + sample[i].vol = st.readByte(); + sample[i].repeat = 2 * st.readUint16BE(); + sample[i].replen = 2 * st.readUint16BE(); + } + + songlen = st.readByte(); + undef = st.readByte(); + + st.read(songpos, 128); + + sig = st.readUint32BE(); + + bool foundSig = false; + for (int i = 0; i < ARRAYSIZE(signatures); i++) { + if (sig == signatures[i]) { + foundSig = true; + break; + } + } + + if (!foundSig) { + warning("No known signature found in protracker module"); + return false; + } + + int maxpattern = 0; + for (int i = 0; i < 128; ++i) + if (maxpattern < songpos[i]) + maxpattern = songpos[i]; + + pattern = new pattern_t[maxpattern + 1]; + + for (int i = 0; i <= maxpattern; ++i) { + for (int j = 0; j < 64; ++j) { + for (int k = 0; k < 4; ++k) { + uint32 note = st.readUint32BE(); + pattern[i][j][k].sample = (note & 0xf0000000) >> 24 | (note & 0x0000f000) >> 12; + pattern[i][j][k].period = (note >> 16) & 0xfff; + pattern[i][j][k].effect = note & 0xfff; + pattern[i][j][k].note = periodToNote((note >> 16) & 0xfff); + } + } + } + + for (int i = 0; i < NUM_SAMPLES; ++i) { + if (offs) { + // Restore information for modules that use common sample data + for (int j = 0; j < NUM_SAMPLES; ++j) { + if (!scumm_stricmp((const char *)commonSamples[j].name, (const char *)sample[i].name)) { + sample[i].len = commonSamples[j].len; + st.seek(commonSamples[j].offs); + break; + } + } + } else { + // Store information for modules that use common sample data + memcpy(commonSamples[i].name, sample[i].name, 22); + commonSamples[i].len = sample[i].len; + commonSamples[i].offs = st.pos(); + + } + + if (!sample[i].len) { + sample[i].data = 0; + } else { + sample[i].data = new int8[sample[i].len]; + st.read((byte *)sample[i].data, sample[i].len); + } + } + + return true; +} + +Module::Module() { + pattern = 0; + for (int i = 0; i < NUM_SAMPLES; ++i) { + sample[i].data = 0; + } +} + +Module::~Module() { + delete[] pattern; + for (int i = 0; i < NUM_SAMPLES; ++i) { + delete[] sample[i].data; + } +} + +byte Module::periodToNote(int16 period, byte finetune) { + int16 diff1; + int16 diff2; + + diff1 = ABS(periods[finetune][0] - period); + if (diff1 == 0) + return 0; + + for (int i = 1; i < 60; i++) { + diff2 = ABS(periods[finetune][i] - period); + if (diff2 == 0) + return i; + else if (diff2 > diff1) + return i-1; + diff1 = diff2; + } + return 59; +} + +int16 Module::noteToPeriod(byte note, byte finetune) { + if (finetune > 15) + finetune = 15; + if (note > 59) + note = 59; + + return periods[finetune][note]; +} + +} // End of namespace Modules diff --git a/audio/mods/module.h b/audio/mods/module.h new file mode 100644 index 0000000000..550b63617e --- /dev/null +++ b/audio/mods/module.h @@ -0,0 +1,90 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#ifndef SOUND_MODS_MODULE_H +#define SOUND_MODS_MODULE_H + +#include "common/stream.h" + +namespace Modules { + +#include "common/pack-start.h" // START STRUCT PACKING + +struct note_t { + byte sample; + byte note; + uint16 period; + uint16 effect; +} PACKED_STRUCT; + +#include "common/pack-end.h" // END STRUCT PACKING + +typedef note_t pattern_t[64][4]; + +struct sample_t { + byte name[23]; + uint16 len; + byte finetune; + byte vol; + uint16 repeat; + uint16 replen; + int8 *data; +}; + +struct sample_offs { + byte name[23]; + uint16 len; + uint32 offs; +}; + +class Module { +public: + byte songname[21]; + + static const int NUM_SAMPLES = 31; + sample_t sample[NUM_SAMPLES]; + sample_offs commonSamples[NUM_SAMPLES]; + + byte songlen; + byte undef; + byte songpos[128]; + uint32 sig; + pattern_t *pattern; + + Module(); + ~Module(); + + bool load(Common::SeekableReadStream &stream, int offs); + static byte periodToNote(int16 period, byte finetune = 0); + static int16 noteToPeriod(byte note, byte finetune = 0); + +private: + static const int16 periods[16][60]; + static const uint32 signatures[]; +}; + +} // End of namespace Modules + +#endif diff --git a/audio/mods/paula.cpp b/audio/mods/paula.cpp new file mode 100644 index 0000000000..ef841ac9bf --- /dev/null +++ b/audio/mods/paula.cpp @@ -0,0 +1,212 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "audio/mods/paula.h" +#include "audio/null.h" + +namespace Audio { + +Paula::Paula(bool stereo, int rate, uint interruptFreq) : + _stereo(stereo), _rate(rate), _periodScale((double)kPalPaulaClock / rate), _intFreq(interruptFreq) { + + clearVoices(); + _voice[0].panning = 191; + _voice[1].panning = 63; + _voice[2].panning = 63; + _voice[3].panning = 191; + + if (_intFreq == 0) + _intFreq = _rate; + + _curInt = 0; + _timerBase = 1; + _playing = false; + _end = true; +} + +Paula::~Paula() { +} + +void Paula::clearVoice(byte voice) { + assert(voice < NUM_VOICES); + + _voice[voice].data = 0; + _voice[voice].dataRepeat = 0; + _voice[voice].length = 0; + _voice[voice].lengthRepeat = 0; + _voice[voice].period = 0; + _voice[voice].volume = 0; + _voice[voice].offset = Offset(0); + _voice[voice].dmaCount = 0; +} + +int Paula::readBuffer(int16 *buffer, const int numSamples) { + Common::StackLock lock(_mutex); + + memset(buffer, 0, numSamples * 2); + if (!_playing) { + return numSamples; + } + + if (_stereo) + return readBufferIntern<true>(buffer, numSamples); + else + return readBufferIntern<false>(buffer, numSamples); +} + + +template<bool stereo> +inline int mixBuffer(int16 *&buf, const int8 *data, Paula::Offset &offset, frac_t rate, int neededSamples, uint bufSize, byte volume, byte panning) { + int samples; + for (samples = 0; samples < neededSamples && offset.int_off < bufSize; ++samples) { + const int32 tmp = ((int32) data[offset.int_off]) * volume; + if (stereo) { + *buf++ += (tmp * (255 - panning)) >> 7; + *buf++ += (tmp * (panning)) >> 7; + } else + *buf++ += tmp; + + // Step to next source sample + offset.rem_off += rate; + if (offset.rem_off >= (frac_t)FRAC_ONE) { + offset.int_off += fracToInt(offset.rem_off); + offset.rem_off &= FRAC_LO_MASK; + } + } + + return samples; +} + +template<bool stereo> +int Paula::readBufferIntern(int16 *buffer, const int numSamples) { + int samples = _stereo ? numSamples / 2 : numSamples; + while (samples > 0) { + + // Handle 'interrupts'. This gives subclasses the chance to adjust the channel data + // (e.g. insert new samples, do pitch bending, whatever). + if (_curInt == 0) { + _curInt = _intFreq; + interrupt(); + } + + // Compute how many samples to generate: at most the requested number of samples, + // of course, but we may stop earlier when an 'interrupt' is expected. + const uint nSamples = MIN((uint)samples, _curInt); + + // Loop over the four channels of the emulated Paula chip + for (int voice = 0; voice < NUM_VOICES; voice++) { + // No data, or paused -> skip channel + if (!_voice[voice].data || (_voice[voice].period <= 0)) + continue; + + // The Paula chip apparently run at 7.0937892 MHz in the PAL + // version and at 7.1590905 MHz in the NTSC version. We divide this + // by the requested the requested output sampling rate _rate + // (typically 44.1 kHz or 22.05 kHz) obtaining the value _periodScale. + // This is then divided by the "period" of the channel we are + // processing, to obtain the correct output 'rate'. + frac_t rate = doubleToFrac(_periodScale / _voice[voice].period); + // Cap the volume + _voice[voice].volume = MIN((byte) 0x40, _voice[voice].volume); + + + Channel &ch = _voice[voice]; + int16 *p = buffer; + int neededSamples = nSamples; + assert(ch.offset.int_off < ch.length); + + // Mix the generated samples into the output buffer + neededSamples -= mixBuffer<stereo>(p, ch.data, ch.offset, rate, neededSamples, ch.length, ch.volume, ch.panning); + + // Wrap around if necessary + if (ch.offset.int_off >= ch.length) { + // Important: Wrap around the offset *before* updating the voice length. + // Otherwise, if length != lengthRepeat we would wrap incorrectly. + // Note: If offset >= 2*len ever occurs, the following would be wrong; + // instead of subtracting, we then should compute the modulus using "%=". + // Since that requires a division and is slow, and shouldn't be necessary + // in practice anyway, we only use subtraction. + ch.offset.int_off -= ch.length; + ch.dmaCount++; + + ch.data = ch.dataRepeat; + ch.length = ch.lengthRepeat; + } + + // If we have not yet generated enough samples, and looping is active: loop! + if (neededSamples > 0 && ch.length > 2) { + // Repeat as long as necessary. + while (neededSamples > 0) { + // Mix the generated samples into the output buffer + neededSamples -= mixBuffer<stereo>(p, ch.data, ch.offset, rate, neededSamples, ch.length, ch.volume, ch.panning); + + if (ch.offset.int_off >= ch.length) { + // Wrap around. See also the note above. + ch.offset.int_off -= ch.length; + ch.dmaCount++; + } + } + } + + } + buffer += _stereo ? nSamples * 2 : nSamples; + _curInt -= nSamples; + samples -= nSamples; + } + return numSamples; +} + +} // End of namespace Audio + + +// Plugin interface +// (This can only create a null driver since apple II gs support seeems not to be implemented +// and also is not part of the midi driver architecture. But we need the plugin for the options +// menu in the launcher and for MidiDriver::detectDevice() which is more or less used by all engines.) + +class AmigaMusicPlugin : public NullMusicPlugin { +public: + const char *getName() const { + return _s("Amiga Audio Emulator"); + } + + const char *getId() const { + return "amiga"; + } + + MusicDevices getDevices() const; +}; + +MusicDevices AmigaMusicPlugin::getDevices() const { + MusicDevices devices; + devices.push_back(MusicDevice(this, "", MT_AMIGA)); + return devices; +} + +//#if PLUGIN_ENABLED_DYNAMIC(AMIGA) + //REGISTER_PLUGIN_DYNAMIC(AMIGA, PLUGIN_TYPE_MUSIC, AmigaMusicPlugin); +//#else + REGISTER_PLUGIN_STATIC(AMIGA, PLUGIN_TYPE_MUSIC, AmigaMusicPlugin); +//#endif diff --git a/audio/mods/paula.h b/audio/mods/paula.h new file mode 100644 index 0000000000..f6f159d5a6 --- /dev/null +++ b/audio/mods/paula.h @@ -0,0 +1,210 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#ifndef SOUND_MODS_PAULA_H +#define SOUND_MODS_PAULA_H + +#include "audio/audiostream.h" +#include "common/frac.h" +#include "common/mutex.h" + +namespace Audio { + +/** + * Emulation of the "Paula" Amiga music chip + * The interrupt frequency specifies the number of mixed wavesamples between + * calls of the interrupt method + */ +class Paula : public AudioStream { +public: + static const int NUM_VOICES = 4; + enum { + kPalSystemClock = 7093790, + kNtscSystemClock = 7159090, + kPalCiaClock = kPalSystemClock / 10, + kNtscCiaClock = kNtscSystemClock / 10, + kPalPaulaClock = kPalSystemClock / 2, + kNtscPauleClock = kNtscSystemClock / 2 + }; + + /* TODO: Document this */ + struct Offset { + uint int_off; // integral part of the offset + frac_t rem_off; // fractional part of the offset, at least 0 and less than 1 + + explicit Offset(int off = 0) : int_off(off), rem_off(0) {} + }; + + Paula(bool stereo = false, int rate = 44100, uint interruptFreq = 0); + ~Paula(); + + bool playing() const { return _playing; } + void setTimerBaseValue( uint32 ticksPerSecond ) { _timerBase = ticksPerSecond; } + uint32 getTimerBaseValue() { return _timerBase; } + void setSingleInterrupt(uint sampleDelay) { assert(sampleDelay < _intFreq); _curInt = sampleDelay; } + void setSingleInterruptUnscaled(uint timerDelay) { + setSingleInterrupt((uint)(((double)timerDelay * getRate()) / _timerBase)); + } + void setInterruptFreq(uint sampleDelay) { _intFreq = sampleDelay; _curInt = 0; } + void setInterruptFreqUnscaled(uint timerDelay) { + setInterruptFreq((uint)(((double)timerDelay * getRate()) / _timerBase)); + } + void clearVoice(byte voice); + void clearVoices() { for (int i = 0; i < NUM_VOICES; ++i) clearVoice(i); } + void startPlay() { _playing = true; } + void stopPlay() { _playing = false; } + void pausePlay(bool pause) { _playing = !pause; } + +// AudioStream API + int readBuffer(int16 *buffer, const int numSamples); + bool isStereo() const { return _stereo; } + bool endOfData() const { return _end; } + int getRate() const { return _rate; } + +protected: + struct Channel { + const int8 *data; + const int8 *dataRepeat; + uint32 length; + uint32 lengthRepeat; + int16 period; + byte volume; + Offset offset; + byte panning; // For stereo mixing: 0 = far left, 255 = far right + int dmaCount; + }; + + bool _end; + Common::Mutex _mutex; + + virtual void interrupt() = 0; + + void startPaula() { + _playing = true; + _end = false; + } + + void stopPaula() { + _playing = false; + _end = true; + } + + void setChannelPanning(byte channel, byte panning) { + assert(channel < NUM_VOICES); + _voice[channel].panning = panning; + } + + void disableChannel(byte channel) { + assert(channel < NUM_VOICES); + _voice[channel].data = 0; + } + + void enableChannel(byte channel) { + assert(channel < NUM_VOICES); + Channel &ch = _voice[channel]; + ch.data = ch.dataRepeat; + ch.length = ch.lengthRepeat; + // actually first 2 bytes are dropped? + ch.offset = Offset(0); + // ch.period = ch.periodRepeat; + } + + void setChannelPeriod(byte channel, int16 period) { + assert(channel < NUM_VOICES); + _voice[channel].period = period; + } + + void setChannelVolume(byte channel, byte volume) { + assert(channel < NUM_VOICES); + _voice[channel].volume = volume; + } + + void setChannelSampleStart(byte channel, const int8 *data) { + assert(channel < NUM_VOICES); + _voice[channel].dataRepeat = data; + } + + void setChannelSampleLen(byte channel, uint32 length) { + assert(channel < NUM_VOICES); + assert(length < 32768/2); + _voice[channel].lengthRepeat = 2 * length; + } + + void setChannelData(uint8 channel, const int8 *data, const int8 *dataRepeat, uint32 length, uint32 lengthRepeat, int32 offset = 0) { + assert(channel < NUM_VOICES); + + Channel &ch = _voice[channel]; + + ch.dataRepeat = data; + ch.lengthRepeat = length; + enableChannel(channel); + ch.offset = Offset(offset); + + ch.dataRepeat = dataRepeat; + ch.lengthRepeat = lengthRepeat; + } + + void setChannelOffset(byte channel, Offset offset) { + assert(channel < NUM_VOICES); + _voice[channel].offset = offset; + } + + Offset getChannelOffset(byte channel) { + assert(channel < NUM_VOICES); + return _voice[channel].offset; + } + + int getChannelDmaCount(byte channel) { + assert(channel < NUM_VOICES); + return _voice[channel].dmaCount; + } + + void setChannelDmaCount(byte channel, int dmaVal = 0) { + assert(channel < NUM_VOICES); + _voice[channel].dmaCount = dmaVal; + } + + void setAudioFilter(bool enable) { + // TODO: implement + } + +private: + Channel _voice[NUM_VOICES]; + + const bool _stereo; + const int _rate; + const double _periodScale; + uint _intFreq; + uint _curInt; + uint32 _timerBase; + bool _playing; + + template<bool stereo> + int readBufferIntern(int16 *buffer, const int numSamples); +}; + +} // End of namespace Audio + +#endif diff --git a/audio/mods/protracker.cpp b/audio/mods/protracker.cpp new file mode 100644 index 0000000000..6051338900 --- /dev/null +++ b/audio/mods/protracker.cpp @@ -0,0 +1,466 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "audio/mods/protracker.h" +#include "audio/mods/paula.h" +#include "audio/mods/module.h" + +#include "audio/audiostream.h" + +namespace Modules { + +class ProtrackerStream : public ::Audio::Paula { +private: + Module _module; + + int _tick; + int _row; + int _pos; + + int _speed; + int _bpm; + + // For effect 0xB - Jump To Pattern; + bool _hasJumpToPattern; + int _jumpToPattern; + + // For effect 0xD - PatternBreak; + bool _hasPatternBreak; + int _skipRow; + + // For effect 0xE6 - Pattern Loop + bool _hasPatternLoop; + int _patternLoopCount; + int _patternLoopRow; + + // For effect 0xEE - Pattern Delay + byte _patternDelay; + + static const int16 sinetable[]; + + struct { + byte sample; + uint16 period; + Offset offset; + + byte vol; + byte finetune; + + // For effect 0x0 - Arpeggio + bool arpeggio; + byte arpeggioNotes[3]; + + // For effect 0x3 - Porta to note + uint16 portaToNote; + byte portaToNoteSpeed; + + // For effect 0x4 - Vibrato + int vibrato; + byte vibratoPos; + byte vibratoSpeed; + byte vibratoDepth; + + // For effect 0xED - Delay sample + byte delaySample; + byte delaySampleTick; + } _track[4]; + +public: + ProtrackerStream(Common::SeekableReadStream *stream, int offs, int rate, bool stereo); + +private: + void interrupt(); + + void doPorta(int track) { + if (_track[track].portaToNote && _track[track].portaToNoteSpeed) { + if (_track[track].period < _track[track].portaToNote) { + _track[track].period += _track[track].portaToNoteSpeed; + if (_track[track].period > _track[track].portaToNote) + _track[track].period = _track[track].portaToNote; + } else if (_track[track].period > _track[track].portaToNote) { + _track[track].period -= _track[track].portaToNoteSpeed; + if (_track[track].period < _track[track].portaToNote) + _track[track].period = _track[track].portaToNote; + } + } + } + void doVibrato(int track) { + _track[track].vibrato = + (_track[track].vibratoDepth * sinetable[_track[track].vibratoPos]) / 128; + _track[track].vibratoPos += _track[track].vibratoSpeed; + _track[track].vibratoPos %= 64; + } + void doVolSlide(int track, byte ex, byte ey) { + int vol = _track[track].vol; + if (ex == 0) + vol -= ey; + else if (ey == 0) + vol += ex; + + if (vol < 0) + vol = 0; + else if (vol > 64) + vol = 64; + + _track[track].vol = vol; + } + + void updateRow(); + void updateEffects(); + +}; + +const int16 ProtrackerStream::sinetable[64] = { + 0, 24, 49, 74, 97, 120, 141, 161, + 180, 197, 212, 224, 235, 244, 250, 253, + 255, 253, 250, 244, 235, 224, 212, 197, + 180, 161, 141, 120, 97, 74, 49, 24, + 0, -24, -49, -74, -97, -120, -141, -161, + -180, -197, -212, -224, -235, -244, -250, -253, + -255, -253, -250, -244, -235, -224, -212, -197, + -180, -161, -141, -120, -97, -74, -49, -24 +}; + +ProtrackerStream::ProtrackerStream(Common::SeekableReadStream *stream, int offs, int rate, bool stereo) : + Paula(stereo, rate, rate/50) { + bool result = _module.load(*stream, offs); + assert(result); + + _tick = _row = _pos = 0; + + _speed = 6; + _bpm = 125; + + _hasJumpToPattern = false; + _jumpToPattern = 0; + + _hasPatternBreak = false; + _skipRow = 0; + + _hasPatternLoop = false; + _patternLoopCount = 0; + _patternLoopRow = 0; + + _patternDelay = 0; + + memset(_track, 0, sizeof(_track)); + + startPaula(); +} + +void ProtrackerStream::updateRow() { + for (int track = 0; track < 4; track++) { + _track[track].arpeggio = false; + _track[track].vibrato = 0; + _track[track].delaySampleTick = 0; + const note_t note = + _module.pattern[_module.songpos[_pos]][_row][track]; + + const int effect = note.effect >> 8; + + if (note.sample) { + if (_track[track].sample != note.sample) { + _track[track].vibratoPos = 0; + } + _track[track].sample = note.sample; + _track[track].finetune = _module.sample[note.sample - 1].finetune; + _track[track].vol = _module.sample[note.sample - 1].vol; + } + + if (note.period) { + if (effect != 3 && effect != 5) { + if (_track[track].finetune) + _track[track].period = _module.noteToPeriod(note.note, _track[track].finetune); + else + _track[track].period = note.period; + _track[track].offset = Offset(0); + } + } + + const byte exy = note.effect & 0xff; + const byte ex = (note.effect >> 4) & 0xf; + const byte ey = note.effect & 0xf; + + int vol; + switch (effect) { + case 0x0: + if (exy) { + _track[track].arpeggio = true; + if (note.period) { + _track[track].arpeggioNotes[0] = note.note; + _track[track].arpeggioNotes[1] = note.note + ex; + _track[track].arpeggioNotes[2] = note.note + ey; + } + } + break; + case 0x1: + break; + case 0x2: + break; + case 0x3: + if (note.period) + _track[track].portaToNote = note.period; + if (exy) + _track[track].portaToNoteSpeed = exy; + break; + case 0x4: + if (exy) { + _track[track].vibratoSpeed = ex; + _track[track].vibratoDepth = ey; + } + break; + case 0x5: + doPorta(track); + doVolSlide(track, ex, ey); + break; + case 0x6: + doVibrato(track); + doVolSlide(track, ex, ey); + break; + case 0x9: // Set sample offset + if (exy) { + _track[track].offset = Offset(exy * 256); + setChannelOffset(track, _track[track].offset); + } + break; + case 0xA: + break; + case 0xB: + _hasJumpToPattern = true; + _jumpToPattern = exy; + break; + case 0xC: + _track[track].vol = exy; + break; + case 0xD: + _hasPatternBreak = true; + _skipRow = ex * 10 + ey; + break; + case 0xE: + switch (ex) { + case 0x0: // Switch filters off + break; + case 0x1: // Fine slide up + _track[track].period -= exy; + break; + case 0x2: // Fine slide down + _track[track].period += exy; + break; + case 0x5: // Set finetune + _track[track].finetune = ey; + _module.sample[_track[track].sample].finetune = ey; + if (note.period) { + if (ey) + _track[track].period = _module.noteToPeriod(note.note, ey); + else + _track[track].period = note.period; + } + break; + case 0x6: + if (ey == 0) { + _patternLoopRow = _row; + } else { + _patternLoopCount++; + if (_patternLoopCount <= ey) + _hasPatternLoop = true; + else + _patternLoopCount = 0; + } + break; + case 0x9: + break; // Retrigger note + case 0xA: // Fine volume slide up + vol = _track[track].vol + ey; + if (vol > 64) + vol = 64; + _track[track].vol = vol; + break; + case 0xB: // Fine volume slide down + vol = _track[track].vol - ey; + if (vol < 0) + vol = 0; + _track[track].vol = vol; + break; + case 0xD: // Delay sample + _track[track].delaySampleTick = ey; + _track[track].delaySample = _track[track].sample; + _track[track].sample = 0; + _track[track].vol = 0; + break; + case 0xE: // Pattern delay + _patternDelay = ey; + break; + default: + warning("Unimplemented effect %X", note.effect); + } + break; + + case 0xF: + if (exy < 0x20) { + _speed = exy; + } else { + _bpm = exy; + setInterruptFreq((int) (getRate() / (_bpm * 0.4))); + } + break; + default: + warning("Unimplemented effect %X", note.effect); + } + } +} + +void ProtrackerStream::updateEffects() { + for (int track = 0; track < 4; track++) { + _track[track].vibrato = 0; + + const note_t note = + _module.pattern[_module.songpos[_pos]][_row][track]; + + const int effect = note.effect >> 8; + + const int exy = note.effect & 0xff; + const int ex = (note.effect >> 4) & 0xf; + const int ey = (note.effect) & 0xf; + + switch (effect) { + case 0x0: + if (exy) { + const int idx = (_tick == 1) ? 0 : (_tick % 3); + _track[track].period = + _module.noteToPeriod(_track[track].arpeggioNotes[idx], + _track[track].finetune); + } + break; + case 0x1: + _track[track].period -= exy; + break; + case 0x2: + _track[track].period += exy; + break; + case 0x3: + doPorta(track); + break; + case 0x4: + doVibrato(track); + break; + case 0x5: + doPorta(track); + doVolSlide(track, ex, ey); + break; + case 0x6: + doVibrato(track); + doVolSlide(track, ex, ey); + break; + case 0xA: + doVolSlide(track, ex, ey); + break; + case 0xE: + switch (ex) { + case 0x6: + break; // Pattern loop + case 0x9: // Retrigger note + if (ey && (_tick % ey) == 0) + _track[track].offset = Offset(0); + break; + case 0xD: // Delay sample + if (_tick == _track[track].delaySampleTick) { + _track[track].sample = _track[track].delaySample; + _track[track].offset = Offset(0); + if (_track[track].sample) + _track[track].vol = _module.sample[_track[track].sample - 1].vol; + } + break; + } + break; + } + } +} + +void ProtrackerStream::interrupt() { + int track; + + for (track = 0; track < 4; track++) { + _track[track].offset = getChannelOffset(track); + if (_tick == 0 && _track[track].arpeggio) { + _track[track].period = _module.noteToPeriod(_track[track].arpeggioNotes[0], + _track[track].finetune); + } + } + + if (_tick == 0) { + if (_hasJumpToPattern) { + _hasJumpToPattern = false; + _pos = _jumpToPattern; + _row = 0; + } else if (_hasPatternBreak) { + _hasPatternBreak = false; + _row = _skipRow; + _pos = (_pos + 1) % _module.songlen; + _patternLoopRow = 0; + } else if (_hasPatternLoop) { + _hasPatternLoop = false; + _row = _patternLoopRow; + } + if (_row >= 64) { + _row = 0; + _pos = (_pos + 1) % _module.songlen; + _patternLoopRow = 0; + } + + updateRow(); + } else + updateEffects(); + + _tick = (_tick + 1) % (_speed + _patternDelay * _speed); + if (_tick == 0) { + _row++; + _patternDelay = 0; + } + + for (track = 0; track < 4; track++) { + setChannelVolume(track, _track[track].vol); + setChannelPeriod(track, _track[track].period + _track[track].vibrato); + if (_track[track].sample) { + sample_t &sample = _module.sample[_track[track].sample - 1]; + setChannelData(track, + sample.data, + sample.replen > 2 ? sample.data + sample.repeat : 0, + sample.len, + sample.replen); + setChannelOffset(track, _track[track].offset); + _track[track].sample = 0; + } + } +} + +} // End of namespace Modules + +namespace Audio { + +AudioStream *makeProtrackerStream(Common::SeekableReadStream *stream, int offs, int rate, bool stereo) { + return new Modules::ProtrackerStream(stream, offs, rate, stereo); +} + +} // End of namespace Audio diff --git a/audio/mods/protracker.h b/audio/mods/protracker.h new file mode 100644 index 0000000000..af722637c7 --- /dev/null +++ b/audio/mods/protracker.h @@ -0,0 +1,57 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +/** + * @file + * Sound decoder used in engines: + * - agos + * - parallaction + */ + +#ifndef SOUND_MODS_PROTRACKER_H +#define SOUND_MODS_PROTRACKER_H + +#include "common/stream.h" + +namespace Audio { + +class AudioStream; + +/* + * Factory function for ProTracker streams. Reads all data from the + * given ReadStream and creates an AudioStream from this. No reference + * to the 'stream' object is kept, so you can safely delete it after + * invoking this factory. + * + * @param stream the ReadStream from which to read the ProTracker data + * @param rate TODO + * @param stereo TODO + * @return a new AudioStream, or NULL, if an error occurred + */ +AudioStream *makeProtrackerStream(Common::SeekableReadStream *stream, int offs = 0, int rate = 44100, bool stereo = true); + +} // End of namespace Audio + +#endif diff --git a/audio/mods/rjp1.cpp b/audio/mods/rjp1.cpp new file mode 100644 index 0000000000..7423abb668 --- /dev/null +++ b/audio/mods/rjp1.cpp @@ -0,0 +1,582 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "common/debug.h" +#include "common/endian.h" + +#include "audio/mods/paula.h" +#include "audio/mods/rjp1.h" +#include "audio/audiostream.h" + +namespace Audio { + +struct Rjp1Channel { + const int8 *waveData; + const int8 *modulatePeriodData; + const int8 *modulateVolumeData; + const int8 *envelopeData; + uint16 volumeScale; + int16 volume; + uint16 modulatePeriodBase; + uint32 modulatePeriodLimit; + uint32 modulatePeriodIndex; + uint16 modulateVolumeBase; + uint32 modulateVolumeLimit; + uint32 modulateVolumeIndex; + uint8 freqStep; + uint32 freqInc; + uint32 freqInit; + const uint8 *noteData; + const uint8 *sequenceOffsets; + const uint8 *sequenceData; + uint8 loopSeqCount; + uint8 loopSeqCur; + uint8 loopSeq2Count; + uint8 loopSeq2Cur; + bool active; + int16 modulatePeriodInit; + int16 modulatePeriodNext; + bool setupNewNote; + int8 envelopeMode; + int8 envelopeScale; + int8 envelopeEnd1; + int8 envelopeEnd2; + int8 envelopeStart; + int8 envelopeVolume; + uint8 currentInstrument; + const int8 *data; + uint16 pos; + uint16 len; + uint16 repeatPos; + uint16 repeatLen; + bool isSfx; +}; + +class Rjp1 : public Paula { +public: + + struct Vars { + int8 *instData; + uint8 *songData[7]; + uint8 activeChannelsMask; + uint8 currentChannel; + int subsongsCount; + int instrumentsCount; + }; + + Rjp1(int rate, bool stereo); + virtual ~Rjp1(); + + bool load(Common::SeekableReadStream *songData, Common::SeekableReadStream *instrumentsData); + void unload(); + + void startPattern(int ch, int pat); + void startSong(int song); + +protected: + + void startSequence(uint8 channelNum, uint8 seqNum); + void turnOffChannel(Rjp1Channel *channel); + void playChannel(Rjp1Channel *channel); + void turnOnChannel(Rjp1Channel *channel); + bool executeSfxSequenceOp(Rjp1Channel *channel, uint8 code, const uint8 *&p); + bool executeSongSequenceOp(Rjp1Channel *channel, uint8 code, const uint8 *&p); + void playSongSequence(Rjp1Channel *channel); + void modulateVolume(Rjp1Channel *channel); + void modulatePeriod(Rjp1Channel *channel); + void setupNote(Rjp1Channel *channel, int16 freq); + void setupInstrument(Rjp1Channel *channel, uint8 num); + void setRelease(Rjp1Channel *channel); + void modulateVolumeEnvelope(Rjp1Channel *channel); + void setSustain(Rjp1Channel *channel); + void setDecay(Rjp1Channel *channel); + void modulateVolumeWaveform(Rjp1Channel *channel); + void setVolume(Rjp1Channel *channel); + + void stopPaulaChannel(uint8 channel); + void setupPaulaChannel(uint8 channel, const int8 *waveData, uint16 offset, uint16 len, uint16 repeatPos, uint16 repeatLen); + + virtual void interrupt(); + + Vars _vars; + Rjp1Channel _channelsTable[4]; + + static const int16 _periodsTable[]; + static const int _periodsCount; +}; + +Rjp1::Rjp1(int rate, bool stereo) + : Paula(stereo, rate, rate / 50) { + memset(&_vars, 0, sizeof(_vars)); + memset(_channelsTable, 0, sizeof(_channelsTable)); +} + +Rjp1::~Rjp1() { + unload(); +} + +bool Rjp1::load(Common::SeekableReadStream *songData, Common::SeekableReadStream *instrumentsData) { + if (songData->readUint32BE() == MKID_BE('RJP1') && songData->readUint32BE() == MKID_BE('SMOD')) { + for (int i = 0; i < 7; ++i) { + uint32 size = songData->readUint32BE(); + _vars.songData[i] = (uint8 *)malloc(size); + if (!_vars.songData[i]) + return false; + + songData->read(_vars.songData[i], size); + switch (i) { + case 0: + _vars.instrumentsCount = size / 32; + break; + case 1: + break; + case 2: + // sequence index to offsets, 1 per channel + _vars.subsongsCount = size / 4; + break; + case 3: + case 4: + // sequence offsets + break; + case 5: + case 6: + // sequence data + break; + } + } + + if (instrumentsData->readUint32BE() == MKID_BE('RJP1')) { + uint32 size = instrumentsData->size() - 4; + _vars.instData = (int8 *)malloc(size); + if (!_vars.instData) + return false; + + instrumentsData->read(_vars.instData, size); + + } + } + + debug(5, "Rjp1::load() _instrumentsCount = %d _subsongsCount = %d", _vars.instrumentsCount, _vars.subsongsCount); + return true; +} + +void Rjp1::unload() { + for (int i = 0; i < 7; ++i) { + free(_vars.songData[i]); + } + free(_vars.instData); + memset(&_vars, 0, sizeof(_vars)); + memset(_channelsTable, 0, sizeof(_channelsTable)); +} + +void Rjp1::startPattern(int ch, int pat) { + Rjp1Channel *channel = &_channelsTable[ch]; + _vars.activeChannelsMask |= 1 << ch; + channel->sequenceData = READ_BE_UINT32(_vars.songData[4] + pat * 4) + _vars.songData[6]; + channel->loopSeqCount = 6; + channel->loopSeqCur = channel->loopSeq2Cur = 1; + channel->active = true; + channel->isSfx = true; + // "start" Paula audiostream + startPaula(); +} + +void Rjp1::startSong(int song) { + if (song == 0 || song >= _vars.subsongsCount) { + warning("Invalid subsong number %d, defaulting to 1", song); + song = 1; + } + const uint8 *p = _vars.songData[2] + (song & 0x3F) * 4; + for (int i = 0; i < 4; ++i) { + uint8 seq = *p++; + if (seq) { + startSequence(i, seq); + } + } + // "start" Paula audiostream + startPaula(); +} + +void Rjp1::startSequence(uint8 channelNum, uint8 seqNum) { + Rjp1Channel *channel = &_channelsTable[channelNum]; + _vars.activeChannelsMask |= 1 << channelNum; + if (seqNum != 0) { + const uint8 *p = READ_BE_UINT32(_vars.songData[3] + seqNum * 4) + _vars.songData[5]; + uint8 seq = *p++; + channel->sequenceOffsets = p; + channel->sequenceData = READ_BE_UINT32(_vars.songData[4] + seq * 4) + _vars.songData[6]; + channel->loopSeqCount = 6; + channel->loopSeqCur = channel->loopSeq2Cur = 1; + channel->active = true; + } else { + channel->active = false; + turnOffChannel(channel); + } +} + +void Rjp1::turnOffChannel(Rjp1Channel *channel) { + stopPaulaChannel(channel - _channelsTable); +} + +void Rjp1::playChannel(Rjp1Channel *channel) { + if (channel->active) { + turnOnChannel(channel); + if (channel->sequenceData) { + playSongSequence(channel); + } + modulateVolume(channel); + modulatePeriod(channel); + } +} + +void Rjp1::turnOnChannel(Rjp1Channel *channel) { + if (channel->setupNewNote) { + channel->setupNewNote = false; + setupPaulaChannel(channel - _channelsTable, channel->data, channel->pos, channel->len, channel->repeatPos, channel->repeatLen); + } +} + +bool Rjp1::executeSfxSequenceOp(Rjp1Channel *channel, uint8 code, const uint8 *&p) { + bool loop = true; + switch (code & 7) { + case 0: + _vars.activeChannelsMask &= ~(1 << _vars.currentChannel); + loop = false; + stopPaula(); + break; + case 1: + setRelease(channel); + loop = false; + break; + case 2: + channel->loopSeqCount = *p++; + break; + case 3: + channel->loopSeq2Count = *p++; + break; + case 4: + code = *p++; + if (code != 0) { + setupInstrument(channel, code); + } + break; + case 7: + loop = false; + break; + } + return loop; +} + +bool Rjp1::executeSongSequenceOp(Rjp1Channel *channel, uint8 code, const uint8 *&p) { + bool loop = true; + const uint8 *offs; + switch (code & 7) { + case 0: + offs = channel->sequenceOffsets; + channel->loopSeq2Count = 1; + while (1) { + code = *offs++; + if (code != 0) { + channel->sequenceOffsets = offs; + p = READ_BE_UINT32(_vars.songData[4] + code * 4) + _vars.songData[6]; + break; + } else { + code = offs[0]; + if (code == 0) { + p = 0; + channel->active = false; + _vars.activeChannelsMask &= ~(1 << _vars.currentChannel); + loop = false; + break; + } else if (code & 0x80) { + code = offs[1]; + offs = READ_BE_UINT32(_vars.songData[3] + code * 4) + _vars.songData[5]; + } else { + offs -= code; + } + } + } + break; + case 1: + setRelease(channel); + loop = false; + break; + case 2: + channel->loopSeqCount = *p++; + break; + case 3: + channel->loopSeq2Count = *p++; + break; + case 4: + code = *p++; + if (code != 0) { + setupInstrument(channel, code); + } + break; + case 5: + channel->volumeScale = *p++; + break; + case 6: + channel->freqStep = *p++; + channel->freqInc = READ_BE_UINT32(p); p += 4; + channel->freqInit = 0; + break; + case 7: + loop = false; + break; + } + return loop; +} + +void Rjp1::playSongSequence(Rjp1Channel *channel) { + const uint8 *p = channel->sequenceData; + --channel->loopSeqCur; + if (channel->loopSeqCur == 0) { + --channel->loopSeq2Cur; + if (channel->loopSeq2Cur == 0) { + bool loop = true; + do { + uint8 code = *p++; + if (code & 0x80) { + if (channel->isSfx) { + loop = executeSfxSequenceOp(channel, code, p); + } else { + loop = executeSongSequenceOp(channel, code, p); + } + } else { + code >>= 1; + if (code < _periodsCount) { + setupNote(channel, _periodsTable[code]); + } + loop = false; + } + } while (loop); + channel->sequenceData = p; + channel->loopSeq2Cur = channel->loopSeq2Count; + } + channel->loopSeqCur = channel->loopSeqCount; + } +} + +void Rjp1::modulateVolume(Rjp1Channel *channel) { + modulateVolumeEnvelope(channel); + modulateVolumeWaveform(channel); + setVolume(channel); +} + +void Rjp1::modulatePeriod(Rjp1Channel *channel) { + if (channel->modulatePeriodData) { + uint32 per = channel->modulatePeriodIndex; + int period = (channel->modulatePeriodData[per] * channel->modulatePeriodInit) / 128; + period = -period; + if (period < 0) { + period /= 2; + } + channel->modulatePeriodNext = period + channel->modulatePeriodInit; + ++per; + if (per == channel->modulatePeriodLimit) { + per = channel->modulatePeriodBase * 2; + } + channel->modulatePeriodIndex = per; + } + if (channel->freqStep != 0) { + channel->freqInit += channel->freqInc; + --channel->freqStep; + } + setChannelPeriod(channel - _channelsTable, channel->freqInit + channel->modulatePeriodNext); +} + +void Rjp1::setupNote(Rjp1Channel *channel, int16 period) { + const uint8 *note = channel->noteData; + if (note) { + channel->modulatePeriodInit = channel->modulatePeriodNext = period; + channel->freqInit = 0; + const int8 *e = (const int8 *)_vars.songData[1] + READ_BE_UINT16(note + 12); + channel->envelopeData = e; + channel->envelopeStart = e[1]; + channel->envelopeScale = e[1] - e[0]; + channel->envelopeEnd2 = e[2]; + channel->envelopeEnd1 = e[2]; + channel->envelopeMode = 4; + channel->data = channel->waveData; + channel->pos = READ_BE_UINT16(note + 16); + channel->len = channel->pos + READ_BE_UINT16(note + 18); + channel->setupNewNote = true; + } +} + +void Rjp1::setupInstrument(Rjp1Channel *channel, uint8 num) { + if (channel->currentInstrument != num) { + channel->currentInstrument = num; + const uint8 *p = _vars.songData[0] + num * 32; + channel->noteData = p; + channel->repeatPos = READ_BE_UINT16(p + 20); + channel->repeatLen = READ_BE_UINT16(p + 22); + channel->volumeScale = READ_BE_UINT16(p + 14); + channel->modulatePeriodBase = READ_BE_UINT16(p + 24); + channel->modulatePeriodIndex = 0; + channel->modulatePeriodLimit = READ_BE_UINT16(p + 26) * 2; + channel->modulateVolumeBase = READ_BE_UINT16(p + 28); + channel->modulateVolumeIndex = 0; + channel->modulateVolumeLimit = READ_BE_UINT16(p + 30) * 2; + channel->waveData = _vars.instData + READ_BE_UINT32(p); + uint32 off = READ_BE_UINT32(p + 4); + if (off) { + channel->modulatePeriodData = _vars.instData + off; + } + off = READ_BE_UINT32(p + 8); + if (off) { + channel->modulateVolumeData = _vars.instData + off; + } + } +} + +void Rjp1::setRelease(Rjp1Channel *channel) { + const int8 *e = channel->envelopeData; + if (e) { + channel->envelopeStart = 0; + channel->envelopeScale = -channel->envelopeVolume; + channel->envelopeEnd2 = e[5]; + channel->envelopeEnd1 = e[5]; + channel->envelopeMode = -1; + } +} + +void Rjp1::modulateVolumeEnvelope(Rjp1Channel *channel) { + if (channel->envelopeMode) { + int16 es = channel->envelopeScale; + if (es) { + int8 m = channel->envelopeEnd1; + if (m == 0) { + es = 0; + } else { + es *= m; + m = channel->envelopeEnd2; + if (m == 0) { + es = 0; + } else { + es /= m; + } + } + } + channel->envelopeVolume = channel->envelopeStart - es; + --channel->envelopeEnd1; + if (channel->envelopeEnd1 == -1) { + switch (channel->envelopeMode) { + case 0: + break; + case 2: + setSustain(channel); + break; + case 4: + setDecay(channel); + break; + case -1: + setSustain(channel); + break; + default: + error("Unhandled envelope mode %d", channel->envelopeMode); + break; + } + return; + } + } + channel->volume = channel->envelopeVolume; +} + +void Rjp1::setSustain(Rjp1Channel *channel) { + channel->envelopeMode = 0; +} + +void Rjp1::setDecay(Rjp1Channel *channel) { + const int8 *e = channel->envelopeData; + if (e) { + channel->envelopeStart = e[3]; + channel->envelopeScale = e[3] - e[1]; + channel->envelopeEnd2 = e[4]; + channel->envelopeEnd1 = e[4]; + channel->envelopeMode = 2; + } +} + +void Rjp1::modulateVolumeWaveform(Rjp1Channel *channel) { + if (channel->modulateVolumeData) { + uint32 i = channel->modulateVolumeIndex; + channel->volume += channel->modulateVolumeData[i] * channel->volume / 128; + ++i; + if (i == channel->modulateVolumeLimit) { + i = channel->modulateVolumeBase * 2; + } + channel->modulateVolumeIndex = i; + } +} + +void Rjp1::setVolume(Rjp1Channel *channel) { + channel->volume = (channel->volume * channel->volumeScale) / 64; + channel->volume = CLIP<int16>(channel->volume, 0, 64); + setChannelVolume(channel - _channelsTable, channel->volume); +} + +void Rjp1::stopPaulaChannel(uint8 channel) { + clearVoice(channel); +} + +void Rjp1::setupPaulaChannel(uint8 channel, const int8 *waveData, uint16 offset, uint16 len, uint16 repeatPos, uint16 repeatLen) { + if (waveData) { + setChannelData(channel, waveData, waveData + repeatPos * 2, len * 2, repeatLen * 2, offset * 2); + } +} + +void Rjp1::interrupt() { + for (int i = 0; i < 4; ++i) { + _vars.currentChannel = i; + playChannel(&_channelsTable[i]); + } +} + +const int16 Rjp1::_periodsTable[] = { + 0x01C5, 0x01E0, 0x01FC, 0x021A, 0x023A, 0x025C, 0x0280, 0x02A6, 0x02D0, + 0x02FA, 0x0328, 0x0358, 0x00E2, 0x00F0, 0x00FE, 0x010D, 0x011D, 0x012E, + 0x0140, 0x0153, 0x0168, 0x017D, 0x0194, 0x01AC, 0x0071, 0x0078, 0x007F, + 0x0087, 0x008F, 0x0097, 0x00A0, 0x00AA, 0x00B4, 0x00BE, 0x00CA, 0x00D6 +}; + +const int Rjp1::_periodsCount = ARRAYSIZE(_periodsTable); + +AudioStream *makeRjp1Stream(Common::SeekableReadStream *songData, Common::SeekableReadStream *instrumentsData, int num, int rate, bool stereo) { + Rjp1 *stream = new Rjp1(rate, stereo); + if (stream->load(songData, instrumentsData)) { + if (num < 0) { + stream->startPattern(3, -num); + } else { + stream->startSong(num); + } + return stream; + } + delete stream; + return 0; +} + +} // End of namespace Audio diff --git a/audio/mods/rjp1.h b/audio/mods/rjp1.h new file mode 100644 index 0000000000..e1960921b2 --- /dev/null +++ b/audio/mods/rjp1.h @@ -0,0 +1,50 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +/** + * @file + * Sound decoder used in engines: + * - queen + */ + +#ifndef SOUND_MODS_RJP1_H +#define SOUND_MODS_RJP1_H + +#include "common/stream.h" + +namespace Audio { + +class AudioStream; + +/* + * Factory function for RichardJoseph1 modules. Reads all data from the + * given songData and instrumentsData streams and creates an AudioStream + * from this. No references to these stream objects are kept. + */ +AudioStream *makeRjp1Stream(Common::SeekableReadStream *songData, Common::SeekableReadStream *instrumentsData, int num, int rate = 44100, bool stereo = true); + +} // End of namespace Audio + +#endif diff --git a/audio/mods/soundfx.cpp b/audio/mods/soundfx.cpp new file mode 100644 index 0000000000..06a1e29514 --- /dev/null +++ b/audio/mods/soundfx.cpp @@ -0,0 +1,275 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "common/endian.h" + +#include "audio/mods/paula.h" +#include "audio/mods/soundfx.h" +#include "audio/audiostream.h" + +namespace Audio { + +struct SoundFxInstrument { + char name[23]; + uint16 len; + uint8 finetune; + uint8 volume; + uint16 repeatPos; + uint16 repeatLen; + int8 *data; +}; + +class SoundFx : public Paula { +public: + + enum { + NUM_CHANNELS = 4, + NUM_INSTRUMENTS = 15 + }; + + SoundFx(int rate, bool stereo); + virtual ~SoundFx(); + + bool load(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb); + void play(); + +protected: + + void handlePattern(int ch, uint32 pat); + void updateEffects(int ch); + void handleTick(); + + void disablePaulaChannel(uint8 channel); + void setupPaulaChannel(uint8 channel, const int8 *data, uint16 len, uint16 repeatPos, uint16 repeatLen); + + virtual void interrupt(); + + uint8 _ticks; + uint16 _delay; + SoundFxInstrument _instruments[NUM_INSTRUMENTS]; + uint8 _numOrders; + uint8 _curOrder; + uint16 _curPos; + uint8 _ordersTable[128]; + uint8 *_patternData; + uint16 _effects[NUM_CHANNELS]; +}; + +SoundFx::SoundFx(int rate, bool stereo) + : Paula(stereo, rate) { + setTimerBaseValue(kPalCiaClock); + _ticks = 0; + _delay = 0; + memset(_instruments, 0, sizeof(_instruments)); + _numOrders = 0; + _curOrder = 0; + _curPos = 0; + memset(_ordersTable, 0, sizeof(_ordersTable)); + _patternData = 0; + memset(_effects, 0, sizeof(_effects)); +} + +SoundFx::~SoundFx() { + free(_patternData); + for (int i = 0; i < NUM_INSTRUMENTS; ++i) { + free(_instruments[i].data); + } +} + +bool SoundFx::load(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb) { + int instrumentsSize[15]; + if (!loadCb) { + for (int i = 0; i < NUM_INSTRUMENTS; ++i) { + instrumentsSize[i] = data->readUint32BE(); + } + } + uint8 tag[4]; + data->read(tag, 4); + if (memcmp(tag, "SONG", 4) != 0) { + return false; + } + _delay = data->readUint16BE(); + data->skip(7 * 2); + for (int i = 0; i < NUM_INSTRUMENTS; ++i) { + SoundFxInstrument *ins = &_instruments[i]; + data->read(ins->name, 22); ins->name[22] = 0; + ins->len = data->readUint16BE(); + ins->finetune = data->readByte(); + ins->volume = data->readByte(); + ins->repeatPos = data->readUint16BE(); + ins->repeatLen = data->readUint16BE(); + } + _numOrders = data->readByte(); + data->skip(1); + data->read(_ordersTable, 128); + int maxOrder = 0; + for (int i = 0; i < _numOrders; ++i) { + if (_ordersTable[i] > maxOrder) { + maxOrder = _ordersTable[i]; + } + } + int patternSize = (maxOrder + 1) * 4 * 4 * 64; + _patternData = (uint8 *)malloc(patternSize); + if (!_patternData) { + return false; + } + data->read(_patternData, patternSize); + for (int i = 0; i < NUM_INSTRUMENTS; ++i) { + SoundFxInstrument *ins = &_instruments[i]; + if (!loadCb) { + if (instrumentsSize[i] != 0) { + assert(ins->len <= 1 || ins->len * 2 <= instrumentsSize[i]); + assert(ins->repeatLen <= 1 || (ins->repeatPos + ins->repeatLen) * 2 <= instrumentsSize[i]); + ins->data = (int8 *)malloc(instrumentsSize[i]); + if (!ins->data) { + return false; + } + data->read(ins->data, instrumentsSize[i]); + } + } else { + if (ins->name[0]) { + ins->name[8] = '\0'; + ins->data = (int8 *)(*loadCb)(ins->name, 0); + if (!ins->data) { + return false; + } + } + } + } + return true; +} + +void SoundFx::play() { + _curPos = 0; + _curOrder = 0; + _ticks = 0; + setInterruptFreqUnscaled(_delay); + startPaula(); +} + +void SoundFx::handlePattern(int ch, uint32 pat) { + uint16 note1 = pat >> 16; + uint16 note2 = pat & 0xFFFF; + if (note1 == 0xFFFD) { // PIC + _effects[ch] = 0; + return; + } + _effects[ch] = note2; + if (note1 == 0xFFFE) { // STP + disablePaulaChannel(ch); + return; + } + int ins = (note2 & 0xF000) >> 12; + if (ins != 0) { + SoundFxInstrument *i = &_instruments[ins - 1]; + setupPaulaChannel(ch, i->data, i->len, i->repeatPos, i->repeatLen); + int effect = (note2 & 0xF00) >> 8; + int volume = i->volume; + switch (effect) { + case 5: // volume up + volume += (note2 & 0xFF); + if (volume > 63) { + volume = 63; + } + break; + case 6: // volume down + volume -= (note2 & 0xFF); + if (volume < 0) { + volume = 0; + } + break; + } + setChannelVolume(ch, volume); + } + if (note1 != 0) { + setChannelPeriod(ch, note1); + } +} + +void SoundFx::updateEffects(int ch) { + // updateEffects() is a no-op in all Delphine Software games using SoundFx : FW,OS,Cruise,AW + if (_effects[ch] != 0) { + switch (_effects[ch]) { + case 1: // appreggiato + case 2: // pitchbend + case 3: // ledon, enable low-pass filter + case 4: // ledoff, disable low-pass filter + case 7: // set step up + case 8: // set step down + warning("Unhandled effect %d", _effects[ch]); + break; + } + } +} + +void SoundFx::handleTick() { + ++_ticks; + if (_ticks != 6) { + for (int ch = 0; ch < 4; ++ch) { + updateEffects(ch); + } + } else { + _ticks = 0; + const uint8 *patternData = _patternData + _ordersTable[_curOrder] * 1024 + _curPos; + for (int ch = 0; ch < 4; ++ch) { + handlePattern(ch, READ_BE_UINT32(patternData)); + patternData += 4; + } + _curPos += 4 * 4; + if (_curPos >= 1024) { + _curPos = 0; + ++_curOrder; + if (_curOrder == _numOrders) { + stopPaula(); + } + } + } +} + +void SoundFx::disablePaulaChannel(uint8 channel) { + disableChannel(channel); +} + +void SoundFx::setupPaulaChannel(uint8 channel, const int8 *data, uint16 len, uint16 repeatPos, uint16 repeatLen) { + if (data && len > 1) { + setChannelData(channel, data, data + repeatPos * 2, len * 2, repeatLen * 2); + } +} + +void SoundFx::interrupt() { + handleTick(); +} + +AudioStream *makeSoundFxStream(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb, int rate, bool stereo) { + SoundFx *stream = new SoundFx(rate, stereo); + if (stream->load(data, loadCb)) { + stream->play(); + return stream; + } + delete stream; + return 0; +} + +} // End of namespace Audio diff --git a/audio/mods/soundfx.h b/audio/mods/soundfx.h new file mode 100644 index 0000000000..089c19d292 --- /dev/null +++ b/audio/mods/soundfx.h @@ -0,0 +1,53 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +/** + * @file + * Sound decoder used in engines: + * - cine + */ + +#ifndef SOUND_MODS_SOUNDFX_H +#define SOUND_MODS_SOUNDFX_H + +#include "common/stream.h" + +namespace Audio { + +class AudioStream; + +typedef byte *(*LoadSoundFxInstrumentCallback)(const char *name, uint32 *size); + +/* + * Factory function for SoundFX modules. Reads all data from the + * given data stream and creates an AudioStream from this (no references to the + * stream object is kept). If loadCb is non 0, then instruments are loaded using + * it, buffers returned are free'd at the end of playback. + */ +AudioStream *makeSoundFxStream(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb, int rate = 44100, bool stereo = true); + +} // End of namespace Audio + +#endif diff --git a/audio/mods/tfmx.cpp b/audio/mods/tfmx.cpp new file mode 100644 index 0000000000..8c69a75ebd --- /dev/null +++ b/audio/mods/tfmx.cpp @@ -0,0 +1,1193 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "common/scummsys.h" +#include "common/endian.h" +#include "common/stream.h" +#include "common/util.h" +#include "common/debug.h" + +#include "audio/mods/tfmx.h" + +// test for engines using this class. +#if defined(SOUND_MODS_TFMX_H) + +// couple debug-functions +namespace { + +#if 0 +void displayPatternstep(const void * const vptr); +void displayMacroStep(const void * const vptr); +#endif + +static const uint16 noteIntervalls[64] = { + 1710, 1614, 1524, 1438, 1357, 1281, 1209, 1141, 1077, 1017, 960, 908, + 856, 810, 764, 720, 680, 642, 606, 571, 539, 509, 480, 454, + 428, 404, 381, 360, 340, 320, 303, 286, 270, 254, 240, 227, + 214, 202, 191, 180, 170, 160, 151, 143, 135, 127, 120, 113, + 214, 202, 191, 180, 170, 160, 151, 143, 135, 127, 120, 113, + 214, 202, 191, 180 +}; + +} // End of anonymous namespace + +namespace Audio { + +Tfmx::Tfmx(int rate, bool stereo) + : Paula(stereo, rate), + _resource(), + _resourceSample(), + _playerCtx(), + _deleteResource(false) { + + _playerCtx.stopWithLastPattern = false; + + for (int i = 0; i < kNumVoices; ++i) + _channelCtx[i].paulaChannel = (byte)i; + + _playerCtx.volume = 0x40; + _playerCtx.patternSkip = 6; + stopSongImpl(); + + setTimerBaseValue(kPalCiaClock); + setInterruptFreqUnscaled(kPalDefaultCiaVal); +} + +Tfmx::~Tfmx() { + freeResourceDataImpl(); +} + +void Tfmx::interrupt() { + assert(!_end); + ++_playerCtx.tickCount; + + for (int i = 0; i < kNumVoices; ++i) { + if (_channelCtx[i].dmaIntCount) { + // wait for DMA Interupts to happen + int doneDma = getChannelDmaCount(i); + if (doneDma >= _channelCtx[i].dmaIntCount) { + _channelCtx[i].dmaIntCount = 0; + _channelCtx[i].macroRun = true; + } + } + } + + for (int i = 0; i < kNumVoices; ++i) { + ChannelContext &channel = _channelCtx[i]; + + if (channel.sfxLockTime >= 0) + --channel.sfxLockTime; + else { + channel.sfxLocked = false; + channel.customMacroPrio = 0; + } + + // externally queued macros + if (channel.customMacro) { + const byte * const noteCmd = (const byte *)&channel.customMacro; + channel.sfxLocked = false; + noteCommand(noteCmd[0], noteCmd[1], (noteCmd[2] & 0xF0) | (uint8)i, noteCmd[3]); + channel.customMacro = 0; + channel.sfxLocked = (channel.customMacroPrio != 0); + } + + // apply timebased effects on Parameters + if (channel.macroSfxRun > 0) + effects(channel); + + // see if we have to run the macro-program + if (channel.macroRun) { + if (!channel.macroWait) + macroRun(channel); + else + --channel.macroWait; + } + + Paula::setChannelPeriod(i, channel.period); + if (channel.macroSfxRun >= 0) + channel.macroSfxRun = 1; + + // TODO: handling pending DMAOff? + } + + // Patterns are only processed each _playerCtx.timerCount + 1 tick + if (_playerCtx.song >= 0 && !_playerCtx.patternCount--) { + _playerCtx.patternCount = _playerCtx.patternSkip; + advancePatterns(); + } +} + +void Tfmx::effects(ChannelContext &channel) { + // addBegin + if (channel.addBeginLength) { + channel.sampleStart += channel.addBeginDelta; + Paula::setChannelSampleStart(channel.paulaChannel, getSamplePtr(channel.sampleStart)); + if (!(--channel.addBeginCount)) { + channel.addBeginCount = channel.addBeginLength; + channel.addBeginDelta = -channel.addBeginDelta; + } + } + + // vibrato + if (channel.vibLength) { + channel.vibValue += channel.vibDelta; + if (--channel.vibCount == 0) { + channel.vibCount = channel.vibLength; + channel.vibDelta = -channel.vibDelta; + } + if (!channel.portaDelta) { + // 16x16 bit multiplication, casts needed for the right results + channel.period = (uint16)(((uint32)channel.refPeriod * (uint16)((1 << 11) + channel.vibValue)) >> 11); + } + } + + // portamento + if (channel.portaDelta && !(--channel.portaCount)) { + channel.portaCount = channel.portaSkip; + + bool resetPorta = true; + const uint16 period = channel.refPeriod; + uint16 portaVal = channel.portaValue; + + if (period > portaVal) { + portaVal = ((uint32)portaVal * (uint16)((1 << 8) + channel.portaDelta)) >> 8; + resetPorta = (period <= portaVal); + + } else if (period < portaVal) { + portaVal = ((uint32)portaVal * (uint16)((1 << 8) - channel.portaDelta)) >> 8; + resetPorta = (period >= portaVal); + } + + if (resetPorta) { + channel.portaDelta = 0; + channel.portaValue = period & 0x7FF; + } else + channel.period = channel.portaValue = portaVal & 0x7FF; + } + + // envelope + if (channel.envSkip && !channel.envCount--) { + channel.envCount = channel.envSkip; + + const int8 endVol = channel.envEndVolume; + int8 volume = channel.volume; + bool resetEnv = true; + + if (endVol > volume) { + volume += channel.envDelta; + resetEnv = endVol <= volume; + } else { + volume -= channel.envDelta; + resetEnv = volume <= 0 || endVol >= volume; + } + + if (resetEnv) { + channel.envSkip = 0; + volume = endVol; + } + channel.volume = volume; + } + + // Fade + if (_playerCtx.fadeDelta && !(--_playerCtx.fadeCount)) { + _playerCtx.fadeCount = _playerCtx.fadeSkip; + + _playerCtx.volume += _playerCtx.fadeDelta; + if (_playerCtx.volume == _playerCtx.fadeEndVolume) + _playerCtx.fadeDelta = 0; + } + + // Volume + const uint8 finVol = _playerCtx.volume * channel.volume >> 6; + Paula::setChannelVolume(channel.paulaChannel, finVol); +} + +void Tfmx::macroRun(ChannelContext &channel) { + bool deferWait = channel.deferWait; + for (;;) { + const byte *const macroPtr = (const byte *)(getMacroPtr(channel.macroOffset) + channel.macroStep); + ++channel.macroStep; + + switch (macroPtr[0]) { + case 0x00: // Reset + DMA Off. Parameters: deferWait, addset, vol + clearEffects(channel); + // FT + case 0x13: // DMA Off. Parameters: deferWait, addset, vol + // TODO: implement PArameters + Paula::disableChannel(channel.paulaChannel); + channel.deferWait = deferWait = (macroPtr[1] != 0); + if (deferWait) { + // if set, then we expect a DMA On in the same tick. + channel.period = 4; + //Paula::setChannelPeriod(channel.paulaChannel, channel.period); + Paula::setChannelSampleLen(channel.paulaChannel, 1); + // in this state we then need to allow some commands that normally + // would halt the macroprogamm to continue instead. + // those commands are: Wait, WaitDMA, AddPrevNote, AddNote, SetNote, <unknown Cmd> + // DMA On is affected aswell + // TODO remember time disabled, remember pending dmaoff?. + } + + if (macroPtr[2] || macroPtr[3]) { + channel.volume = (macroPtr[2] ? 0 : channel.relVol * 3) + macroPtr[3]; + Paula::setChannelVolume(channel.paulaChannel, channel.volume); + } + continue; + + case 0x01: // DMA On + // TODO: Parameter macroPtr[1] - en-/disable effects + channel.dmaIntCount = 0; + if (deferWait) { + // TODO + // there is actually a small delay in the player, but I think that + // only allows to clear DMA-State on real Hardware + } + Paula::setChannelPeriod(channel.paulaChannel, channel.period); + Paula::enableChannel(channel.paulaChannel); + channel.deferWait = deferWait = false; + continue; + + case 0x02: // Set Beginn. Parameters: SampleOffset(L) + channel.addBeginLength = 0; + channel.sampleStart = READ_BE_UINT32(macroPtr) & 0xFFFFFF; + Paula::setChannelSampleStart(channel.paulaChannel, getSamplePtr(channel.sampleStart)); + continue; + + case 0x03: // SetLength. Parameters: SampleLength(W) + channel.sampleLen = READ_BE_UINT16(¯oPtr[2]); + Paula::setChannelSampleLen(channel.paulaChannel, channel.sampleLen); + continue; + + case 0x04: // Wait. Parameters: Ticks to wait(W). + // TODO: some unknown Parameter? (macroPtr[1] & 1) + channel.macroWait = READ_BE_UINT16(¯oPtr[2]); + break; + + case 0x10: // Loop Key Up. Parameters: Loopcount, MacroStep(W) + if (channel.keyUp) + continue; + // FT + case 0x05: // Loop. Parameters: Loopcount, MacroStep(W) + if (channel.macroLoopCount != 0) { + if (channel.macroLoopCount == 0xFF) + channel.macroLoopCount = macroPtr[1]; + channel.macroStep = READ_BE_UINT16(¯oPtr[2]); + } + --channel.macroLoopCount; + continue; + + case 0x06: // Jump. Parameters: MacroIndex, MacroStep(W) + // channel.macroIndex = macroPtr[1] & (kMaxMacroOffsets - 1); + channel.macroOffset = _resource->macroOffset[macroPtr[1] & (kMaxMacroOffsets - 1)]; + channel.macroStep = READ_BE_UINT16(¯oPtr[2]); + channel.macroLoopCount = 0xFF; + continue; + + case 0x07: // Stop Macro + channel.macroRun = false; + --channel.macroStep; + return; + + case 0x08: // AddNote. Parameters: Note, Finetune(W) + setNoteMacro(channel, channel.note + macroPtr[1], READ_BE_UINT16(¯oPtr[2])); + break; + + case 0x09: // SetNote. Parameters: Note, Finetune(W) + setNoteMacro(channel, macroPtr[1], READ_BE_UINT16(¯oPtr[2])); + break; + + case 0x0A: // Clear Effects + clearEffects(channel); + continue; + + case 0x0B: // Portamento. Parameters: count, speed + channel.portaSkip = macroPtr[1]; + channel.portaCount = 1; + // if porta is already running, then keep using old value + if (!channel.portaDelta) + channel.portaValue = channel.refPeriod; + channel.portaDelta = READ_BE_UINT16(¯oPtr[2]); + continue; + + case 0x0C: // Vibrato. Parameters: Speed, intensity + channel.vibLength = macroPtr[1]; + channel.vibCount = macroPtr[1] / 2; + channel.vibDelta = macroPtr[3]; + // TODO: Perhaps a bug, vibValue could be left uninitialised + if (!channel.portaDelta) { + channel.period = channel.refPeriod; + channel.vibValue = 0; + } + continue; + + case 0x0D: // Add Volume. Parameters: note, addNoteFlag, volume + if (macroPtr[2] == 0xFE) + setNoteMacro(channel, channel.note + macroPtr[1], 0); + channel.volume = channel.relVol * 3 + macroPtr[3]; + continue; + + case 0x0E: // Set Volume. Parameters: note, addNoteFlag, volume + if (macroPtr[2] == 0xFE) + setNoteMacro(channel, channel.note + macroPtr[1], 0); + channel.volume = macroPtr[3]; + continue; + + case 0x0F: // Envelope. Parameters: speed, count, endvol + channel.envDelta = macroPtr[1]; + channel.envCount = channel.envSkip = macroPtr[2]; + channel.envEndVolume = macroPtr[3]; + continue; + + case 0x11: // Add Beginn. Parameters: times, Offset(W) + channel.addBeginLength = channel.addBeginCount = macroPtr[1]; + channel.addBeginDelta = (int16)READ_BE_UINT16(¯oPtr[2]); + channel.sampleStart += channel.addBeginDelta; + Paula::setChannelSampleStart(channel.paulaChannel, getSamplePtr(channel.sampleStart)); + continue; + + case 0x12: // Add Length. Parameters: added Length(W) + channel.sampleLen += (int16)READ_BE_UINT16(¯oPtr[2]); + Paula::setChannelSampleLen(channel.paulaChannel, channel.sampleLen); + continue; + + case 0x14: // Wait key up. Parameters: wait cycles + if (channel.keyUp || channel.macroLoopCount == 0) { + channel.macroLoopCount = 0xFF; + continue; + } else if (channel.macroLoopCount == 0xFF) + channel.macroLoopCount = macroPtr[3]; + --channel.macroLoopCount; + --channel.macroStep; + return; + + case 0x15: // Subroutine. Parameters: MacroIndex, Macrostep(W) + channel.macroReturnOffset = channel.macroOffset; + channel.macroReturnStep = channel.macroStep; + + channel.macroOffset = _resource->macroOffset[macroPtr[1] & (kMaxMacroOffsets - 1)]; + channel.macroStep = READ_BE_UINT16(¯oPtr[2]); + // TODO: MI does some weird stuff there. Figure out which varioables need to be set + continue; + + case 0x16: // Return from Sub. + channel.macroOffset = channel.macroReturnOffset; + channel.macroStep = channel.macroReturnStep; + continue; + + case 0x17: // Set Period. Parameters: Period(W) + channel.refPeriod = READ_BE_UINT16(¯oPtr[2]); + if (!channel.portaDelta) { + channel.period = channel.refPeriod; + //Paula::setChannelPeriod(channel.paulaChannel, channel.period); + } + continue; + + case 0x18: { // Sampleloop. Parameters: Offset from Samplestart(W) + // TODO: MI loads 24 bit, but thats useless? + const uint16 temp = /* ((int8)macroPtr[1] << 16) | */ READ_BE_UINT16(¯oPtr[2]); + if (macroPtr[1] || (temp & 1)) + warning("Tfmx: Problematic value for sampleloop: %06X", (macroPtr[1] << 16) | temp); + channel.sampleStart += temp & 0xFFFE; + channel.sampleLen -= (temp / 2) /* & 0x7FFF */; + Paula::setChannelSampleStart(channel.paulaChannel, getSamplePtr(channel.sampleStart)); + Paula::setChannelSampleLen(channel.paulaChannel, channel.sampleLen); + continue; + } + case 0x19: // Set One-Shot Sample + channel.addBeginLength = 0; + channel.sampleStart = 0; + channel.sampleLen = 1; + Paula::setChannelSampleStart(channel.paulaChannel, getSamplePtr(0)); + Paula::setChannelSampleLen(channel.paulaChannel, 1); + continue; + + case 0x1A: // Wait on DMA. Parameters: Cycles-1(W) to wait + channel.dmaIntCount = READ_BE_UINT16(¯oPtr[2]) + 1; + channel.macroRun = false; + Paula::setChannelDmaCount(channel.paulaChannel); + break; + +/* case 0x1B: // Random play. Parameters: macro/speed/mode + warnMacroUnimplemented(macroPtr, 0); + continue;*/ + + case 0x1C: // Branch on Note. Parameters: note/macrostep(W) + if (channel.note > macroPtr[1]) + channel.macroStep = READ_BE_UINT16(¯oPtr[2]); + continue; + + case 0x1D: // Branch on Volume. Parameters: volume/macrostep(W) + if (channel.volume > macroPtr[1]) + channel.macroStep = READ_BE_UINT16(¯oPtr[2]); + continue; + +/* case 0x1E: // Addvol+note. Parameters: note/CONST./volume + warnMacroUnimplemented(macroPtr, 0); + continue;*/ + + case 0x1F: // AddPrevNote. Parameters: Note, Finetune(W) + setNoteMacro(channel, channel.prevNote + macroPtr[1], READ_BE_UINT16(¯oPtr[2])); + break; + + case 0x20: // Signal. Parameters: signalnumber, value(W) + if (_playerCtx.numSignals > macroPtr[1]) + _playerCtx.signal[macroPtr[1]] = READ_BE_UINT16(¯oPtr[2]); + continue; + + case 0x21: // Play macro. Parameters: macro, chan, detune + noteCommand(channel.note, macroPtr[1], (channel.relVol << 4) | macroPtr[2], macroPtr[3]); + continue; + + // 0x22 - 0x29 are used by Gem`X + // 0x30 - 0x34 are used by Carribean Disaster + + default: + debug(3, "Tfmx: Macro %02X not supported", macroPtr[0]); + } + if (!deferWait) + return; + } +} + +void Tfmx::advancePatterns() { +startPatterns: + int runningPatterns = 0; + + for (int i = 0; i < kNumChannels; ++i) { + PatternContext &pattern = _patternCtx[i]; + const uint8 pattCmd = pattern.command; + if (pattCmd < 0x90) { // execute Patternstep + ++runningPatterns; + if (!pattern.wait) { + // issue all Steps for this tick + if (patternRun(pattern)) { + // we load the next Trackstep Command and then process all Channels again + if (trackRun(true)) + goto startPatterns; + else + break; + } + + } else + --pattern.wait; + + } else if (pattCmd == 0xFE) { // Stop voice in pattern.expose + pattern.command = 0xFF; + ChannelContext &channel = _channelCtx[pattern.expose & (kNumVoices - 1)]; + if (!channel.sfxLocked) { + haltMacroProgramm(channel); + Paula::disableChannel(channel.paulaChannel); + } + } // else this pattern-Channel is stopped + } + if (_playerCtx.stopWithLastPattern && !runningPatterns) { + stopPaula(); + } +} + +bool Tfmx::patternRun(PatternContext &pattern) { + for (;;) { + const byte *const patternPtr = (const byte *)(getPatternPtr(pattern.offset) + pattern.step); + ++pattern.step; + const byte pattCmd = patternPtr[0]; + + if (pattCmd < 0xF0) { // Playnote + bool doWait = false; + byte noteCmd = pattCmd + pattern.expose; + byte param3 = patternPtr[3]; + if (pattCmd < 0xC0) { // Note + if (pattCmd >= 0x80) { // Wait + pattern.wait = param3; + param3 = 0; + doWait = true; + } + noteCmd &= 0x3F; + } // else Portamento + noteCommand(noteCmd, patternPtr[1], patternPtr[2], param3); + if (doWait) + return false; + + } else { // Patterncommand + switch (pattCmd & 0xF) { + case 0: // End Pattern + Next Trackstep + pattern.command = 0xFF; + --pattern.step; + return true; + + case 1: // Loop Pattern. Parameters: Loopcount, PatternStep(W) + if (pattern.loopCount != 0) { + if (pattern.loopCount == 0xFF) + pattern.loopCount = patternPtr[1]; + pattern.step = READ_BE_UINT16(&patternPtr[2]); + } + --pattern.loopCount; + continue; + + case 2: // Jump. Parameters: PatternIndex, PatternStep(W) + pattern.offset = _resource->patternOffset[patternPtr[1] & (kMaxPatternOffsets - 1)]; + pattern.step = READ_BE_UINT16(&patternPtr[2]); + continue; + + case 3: // Wait. Paramters: ticks to wait + pattern.wait = patternPtr[1]; + return false; + + case 14: // Stop custompattern + // TODO apparently toggles on/off pattern channel 7 + debug(3, "Tfmx: Encountered 'Stop custompattern' command"); + // FT + case 4: // Stop this pattern + pattern.command = 0xFF; + --pattern.step; + // TODO: try figuring out if this was the last Channel? + return false; + + case 5: // Key Up Signal. Paramters: channel + if (!_channelCtx[patternPtr[2] & (kNumVoices - 1)].sfxLocked) + _channelCtx[patternPtr[2] & (kNumVoices - 1)].keyUp = true; + continue; + + case 6: // Vibrato. Parameters: length, channel, rate + case 7: // Envelope. Parameters: rate, tempo | channel, endVol + noteCommand(pattCmd, patternPtr[1], patternPtr[2], patternPtr[3]); + continue; + + case 8: // Subroutine. Parameters: pattern, patternstep(W) + pattern.savedOffset = pattern.offset; + pattern.savedStep = pattern.step; + + pattern.offset = _resource->patternOffset[patternPtr[1] & (kMaxPatternOffsets - 1)]; + pattern.step = READ_BE_UINT16(&patternPtr[2]); + continue; + + case 9: // Return from Subroutine + pattern.offset = pattern.savedOffset; + pattern.step = pattern.savedStep; + continue; + + case 10: // fade. Parameters: tempo, endVol + initFadeCommand((uint8)patternPtr[1], (int8)patternPtr[3]); + continue; + + case 11: // play pattern. Parameters: patternCmd, channel, expose + initPattern(_patternCtx[patternPtr[2] & (kNumChannels - 1)], patternPtr[1], patternPtr[3], _resource->patternOffset[patternPtr[1] & (kMaxPatternOffsets - 1)]); + continue; + + case 12: // Lock. Parameters: lockFlag, channel, lockTime + _channelCtx[patternPtr[2] & (kNumVoices - 1)].sfxLocked = (patternPtr[1] != 0); + _channelCtx[patternPtr[2] & (kNumVoices - 1)].sfxLockTime = patternPtr[3]; + continue; + + case 13: // Cue. Parameters: signalnumber, value(W) + if (_playerCtx.numSignals > patternPtr[1]) + _playerCtx.signal[patternPtr[1]] = READ_BE_UINT16(&patternPtr[2]); + continue; + + case 15: // NOP + continue; + } + } + } +} + +bool Tfmx::trackRun(const bool incStep) { + assert(_playerCtx.song >= 0); + if (incStep) { + // TODO Optionally disable looping + if (_trackCtx.posInd == _trackCtx.stopInd) + _trackCtx.posInd = _trackCtx.startInd; + else + ++_trackCtx.posInd; + } + for (;;) { + const uint16 *const trackData = getTrackPtr(_trackCtx.posInd); + + if (trackData[0] != FROM_BE_16(0xEFFE)) { + // 8 commands for Patterns + for (int i = 0; i < 8; ++i) { + const uint8 *patCmd = (const uint8 *)&trackData[i]; + // First byte is pattern number + const uint8 patNum = patCmd[0]; + // if highest bit is set then keep previous pattern + if (patNum < 0x80) { + initPattern(_patternCtx[i], patNum, patCmd[1], _resource->patternOffset[patNum]); + } else { + _patternCtx[i].command = patNum; + _patternCtx[i].expose = (int8)patCmd[1]; + } + } + return true; + + } else { + // 16 byte Trackstep Command + switch (READ_BE_UINT16(&trackData[1])) { + case 0: // Stop Player. No Parameters + stopPaula(); + return false; + + case 1: // Branch/Loop section of tracksteps. Parameters: branch target, loopcount + if (_trackCtx.loopCount != 0) { + if (_trackCtx.loopCount < 0) + _trackCtx.loopCount = READ_BE_UINT16(&trackData[3]); + _trackCtx.posInd = READ_BE_UINT16(&trackData[2]); + continue; + } + --_trackCtx.loopCount; + break; + + case 2: { // Set Tempo. Parameters: tempo, divisor + _playerCtx.patternCount = _playerCtx.patternSkip = READ_BE_UINT16(&trackData[2]); // tempo + const uint16 temp = READ_BE_UINT16(&trackData[3]); // divisor + + if (!(temp & 0x8000) && (temp & 0x1FF)) + setInterruptFreqUnscaled(temp & 0x1FF); + break; + } + case 4: // Fade. Parameters: tempo, endVol + // load the LSB of the 16bit words + initFadeCommand(((const uint8 *)&trackData[2])[1], ((const int8 *)&trackData[3])[1]); + break; + + case 3: // Unknown, stops player aswell + default: + debug(3, "Tfmx: Unknown Trackstep Command: %02X", READ_BE_UINT16(&trackData[1])); + // MI-Player handles this by stopping the player, we just continue + } + } + + if (_trackCtx.posInd == _trackCtx.stopInd) { + warning("Tfmx: Reached invalid Song-Position"); + return false; + } + ++_trackCtx.posInd; + } +} + +void Tfmx::noteCommand(const uint8 note, const uint8 param1, const uint8 param2, const uint8 param3) { + ChannelContext &channel = _channelCtx[param2 & (kNumVoices - 1)]; + + if (note == 0xFC) { // Lock command + channel.sfxLocked = (param1 != 0); + channel.sfxLockTime = param3; // only 1 byte read! + + } else if (channel.sfxLocked) { // Channel still locked, do nothing + + } else if (note < 0xC0) { // Play Note - Parameters: note, macro, relVol | channel, finetune + + channel.prevNote = channel.note; + channel.note = note; + // channel.macroIndex = param1 & (kMaxMacroOffsets - 1); + channel.macroOffset = _resource->macroOffset[param1 & (kMaxMacroOffsets - 1)]; + channel.relVol = param2 >> 4; + channel.fineTune = (int8)param3; + + // TODO: the point where the channel gets initialised varies with the games, needs more research. + initMacroProgramm(channel); + channel.keyUp = false; // key down = playing a Note + + } else if (note < 0xF0) { // Portamento - Parameters: note, tempo, channel, rate + channel.portaSkip = param1; + channel.portaCount = 1; + if (!channel.portaDelta) + channel.portaValue = channel.refPeriod; + channel.portaDelta = param3; + + channel.note = note & 0x3F; + channel.refPeriod = noteIntervalls[channel.note]; + + } else switch (note) { // Command + + case 0xF5: // Key Up Signal + channel.keyUp = true; + break; + + case 0xF6: // Vibratio - Parameters: length, channel, rate + channel.vibLength = param1 & 0xFE; + channel.vibCount = param1 / 2; + channel.vibDelta = param3; + channel.vibValue = 0; + break; + + case 0xF7: // Envelope - Parameters: rate, tempo | channel, endVol + channel.envDelta = param1; + channel.envCount = channel.envSkip = (param2 >> 4) + 1; + channel.envEndVolume = param3; + break; + } +} + +void Tfmx::initMacroProgramm(ChannelContext &channel) { + channel.macroStep = 0; + channel.macroWait = 0; + channel.macroRun = true; + channel.macroSfxRun = 0; + channel.macroLoopCount = 0xFF; + channel.dmaIntCount = 0; + channel.deferWait = false; + + channel.macroReturnOffset = 0; + channel.macroReturnStep = 0; +} + +void Tfmx::clearEffects(ChannelContext &channel) { + channel.addBeginLength = 0; + channel.envSkip = 0; + channel.vibLength = 0; + channel.portaDelta = 0; +} + +void Tfmx::haltMacroProgramm(ChannelContext &channel) { + channel.macroRun = false; + channel.dmaIntCount = 0; +} + +void Tfmx::unlockMacroChannel(ChannelContext &channel) { + channel.customMacro = 0; + channel.customMacroIndex = 0; + channel.customMacroPrio = 0; + channel.sfxLocked = false; + channel.sfxLockTime = -1; +} + +void Tfmx::initPattern(PatternContext &pattern, uint8 cmd, int8 expose, uint32 offset) { + pattern.command = cmd; + pattern.offset = offset; + pattern.expose = expose; + pattern.step = 0; + pattern.wait = 0; + pattern.loopCount = 0xFF; + + pattern.savedOffset = 0; + pattern.savedStep = 0; +} + +void Tfmx::stopSongImpl(bool stopAudio) { + _playerCtx.song = -1; + for (int i = 0; i < kNumChannels; ++i) { + _patternCtx[i].command = 0xFF; + _patternCtx[i].expose = 0; + } + if (stopAudio) { + stopPaula(); + for (int i = 0; i < kNumVoices; ++i) { + clearEffects(_channelCtx[i]); + unlockMacroChannel(_channelCtx[i]); + haltMacroProgramm(_channelCtx[i]); + _channelCtx[i].note = 0; + _channelCtx[i].volume = 0; + _channelCtx[i].macroSfxRun = -1; + _channelCtx[i].vibValue = 0; + + _channelCtx[i].sampleStart = 0; + _channelCtx[i].sampleLen = 2; + _channelCtx[i].refPeriod = 4; + _channelCtx[i].period = 4; + Paula::disableChannel(i); + } + } +} + +void Tfmx::setNoteMacro(ChannelContext &channel, uint note, int fineTune) { + const uint16 noteInt = noteIntervalls[note & 0x3F]; + const uint16 finetune = (uint16)(fineTune + channel.fineTune + (1 << 8)); + channel.refPeriod = ((uint32)noteInt * finetune >> 8); + if (!channel.portaDelta) + channel.period = channel.refPeriod; +} + +void Tfmx::initFadeCommand(const uint8 fadeTempo, const int8 endVol) { + _playerCtx.fadeCount = _playerCtx.fadeSkip = fadeTempo; + _playerCtx.fadeEndVolume = endVol; + + if (fadeTempo) { + const int diff = _playerCtx.fadeEndVolume - _playerCtx.volume; + _playerCtx.fadeDelta = (diff != 0) ? ((diff > 0) ? 1 : -1) : 0; + } else { + _playerCtx.volume = endVol; + _playerCtx.fadeDelta = 0; + } +} + +void Tfmx::setModuleData(Tfmx &otherPlayer) { + setModuleData(otherPlayer._resource, otherPlayer._resourceSample.sampleData, otherPlayer._resourceSample.sampleLen, false); +} + +bool Tfmx::load(Common::SeekableReadStream &musicData, Common::SeekableReadStream &sampleData, bool autoDelete) { + const MdatResource *mdat = loadMdatFile(musicData); + if (mdat) { + uint32 sampleLen = 0; + const int8 *sampleDat = loadSampleFile(sampleLen, sampleData); + if (sampleDat) { + setModuleData(mdat, sampleDat, sampleLen, autoDelete); + return true; + } + delete[] mdat->mdatAlloc; + delete mdat; + } + return false; +} + +void Tfmx::freeResourceDataImpl() { + if (_deleteResource) { + if (_resource) { + delete[] _resource->mdatAlloc; + delete _resource; + } + delete[] _resourceSample.sampleData; + } + _resource = 0; + _resourceSample.sampleData = 0; + _resourceSample.sampleLen = 0; + _deleteResource = false; +} + +void Tfmx::setModuleData(const MdatResource *resource, const int8 *sampleData, uint32 sampleLen, bool autoDelete) { + Common::StackLock lock(_mutex); + stopSongImpl(true); + freeResourceDataImpl(); + _resource = resource; + _resourceSample.sampleData = sampleData; + _resourceSample.sampleLen = sampleData ? sampleLen : 0; + _deleteResource = autoDelete; +} + +const int8 *Tfmx::loadSampleFile(uint32 &sampleLen, Common::SeekableReadStream &sampleStream) { + sampleLen = 0; + + const int32 sampleSize = sampleStream.size(); + if (sampleSize < 4) { + warning("Tfmx: Cant load Samplefile"); + return false; + } + + int8 *sampleAlloc = new int8[sampleSize]; + if (!sampleAlloc) { + warning("Tfmx: Could not allocate Memory: %dKB", sampleSize / 1024); + return 0; + } + + if (sampleStream.read(sampleAlloc, sampleSize) == (uint32)sampleSize) { + sampleAlloc[0] = sampleAlloc[1] = sampleAlloc[2] = sampleAlloc[3] = 0; + sampleLen = sampleSize; + } else { + delete[] sampleAlloc; + warning("Tfmx: Encountered IO-Error"); + return 0; + } + return sampleAlloc; +} + +const Tfmx::MdatResource *Tfmx::loadMdatFile(Common::SeekableReadStream &musicData) { + bool hasHeader = false; + const int32 mdatSize = musicData.size(); + if (mdatSize >= 0x200) { + byte buf[16] = { 0 }; + // 0x0000: 10 Bytes Header "TFMX-SONG " + musicData.read(buf, 10); + hasHeader = memcmp(buf, "TFMX-SONG ", 10) == 0; + } + + if (!hasHeader) { + warning("Tfmx: File is not a Tfmx Module"); + return 0; + } + + MdatResource *resource = new MdatResource; + + resource->mdatAlloc = 0; + resource->mdatData = 0; + resource->mdatLen = 0; + + // 0x000A: int16 flags + resource->headerFlags = musicData.readUint16BE(); + // 0x000C: int32 ? + // 0x0010: 6*40 Textfield + musicData.skip(4 + 6 * 40); + + /* 0x0100: Songstart x 32*/ + for (int i = 0; i < kNumSubsongs; ++i) + resource->subsong[i].songstart = musicData.readUint16BE(); + /* 0x0140: Songend x 32*/ + for (int i = 0; i < kNumSubsongs; ++i) + resource->subsong[i].songend = musicData.readUint16BE(); + /* 0x0180: Tempo x 32*/ + for (int i = 0; i < kNumSubsongs; ++i) + resource->subsong[i].tempo = musicData.readUint16BE(); + + /* 0x01c0: unused ? */ + musicData.skip(16); + + /* 0x01d0: trackstep, pattern data p, macro data p */ + const uint32 offTrackstep = musicData.readUint32BE(); + uint32 offPatternP, offMacroP; + + // This is how MI`s TFMX-Player tests for unpacked Modules. + if (offTrackstep == 0) { // unpacked File + resource->trackstepOffset = 0x600 + 0x200; + offPatternP = 0x200 + 0x200; + offMacroP = 0x400 + 0x200; + } else { // packed File + resource->trackstepOffset = offTrackstep; + offPatternP = musicData.readUint32BE(); + offMacroP = musicData.readUint32BE(); + } + + // End of basic header, check if everything worked ok + if (musicData.err()) { + warning("Tfmx: Encountered IO-Error"); + delete resource; + return 0; + } + + // TODO: if a File is packed it could have for Ex only 2 Patterns/Macros + // the following loops could then read beyond EOF. + // To correctly handle this it would be necessary to sort the pointers and + // figure out the number of Macros/Patterns + // We could also analyze pointers if they are correct offsets, + // so that accesses can be unchecked later + + // Read in pattern starting offsets + musicData.seek(offPatternP); + for (int i = 0; i < kMaxPatternOffsets; ++i) + resource->patternOffset[i] = musicData.readUint32BE(); + + // use last PatternOffset (stored at 0x5FC in mdat) if unpacked File + // or fixed offset 0x200 if packed + resource->sfxTableOffset = offTrackstep ? 0x200 : resource->patternOffset[127]; + + // Read in macro starting offsets + musicData.seek(offMacroP); + for (int i = 0; i < kMaxMacroOffsets; ++i) + resource->macroOffset[i] = musicData.readUint32BE(); + + // Read in mdat-file + // TODO: we can skip everything thats already stored in the resource-structure. + const int32 mdatOffset = offTrackstep ? 0x200 : 0x600; // 0x200 is very conservative + const uint32 allocSize = (uint32)mdatSize - mdatOffset; + + byte *mdatAlloc = new byte[allocSize]; + if (!mdatAlloc) { + warning("Tfmx: Could not allocate Memory: %dKB", allocSize / 1024); + delete resource; + return 0; + } + musicData.seek(mdatOffset); + if (musicData.read(mdatAlloc, allocSize) == allocSize) { + resource->mdatAlloc = mdatAlloc; + resource->mdatData = mdatAlloc - mdatOffset; + resource->mdatLen = mdatSize; + } else { + delete[] mdatAlloc; + warning("Tfmx: Encountered IO-Error"); + delete resource; + return 0; + } + + return resource; +} + +void Tfmx::doMacro(int note, int macro, int relVol, int finetune, int channelNo) { + assert(0 <= macro && macro < kMaxMacroOffsets); + assert(0 <= note && note < 0xC0); + Common::StackLock lock(_mutex); + + if (!hasResources()) + return; + channelNo &= (kNumVoices - 1); + ChannelContext &channel = _channelCtx[channelNo]; + unlockMacroChannel(channel); + + noteCommand((uint8)note, (uint8)macro, (uint8)((relVol << 4) | channelNo), (uint8)finetune); + startPaula(); +} + +void Tfmx::stopMacroEffect(int channel) { + assert(0 <= channel && channel < kNumVoices); + Common::StackLock lock(_mutex); + unlockMacroChannel(_channelCtx[channel]); + haltMacroProgramm(_channelCtx[channel]); + Paula::disableChannel(_channelCtx[channel].paulaChannel); +} + +void Tfmx::doSong(int songPos, bool stopAudio) { + assert(0 <= songPos && songPos < kNumSubsongs); + Common::StackLock lock(_mutex); + + stopSongImpl(stopAudio); + + if (!hasResources()) + return; + + _trackCtx.loopCount = -1; + _trackCtx.startInd = _trackCtx.posInd = _resource->subsong[songPos].songstart; + _trackCtx.stopInd = _resource->subsong[songPos].songend; + _playerCtx.song = (int8)songPos; + + const bool palFlag = (_resource->headerFlags & 2) != 0; + const uint16 tempo = _resource->subsong[songPos].tempo; + uint16 ciaIntervall; + if (tempo >= 0x10) { + ciaIntervall = (uint16)(kCiaBaseInterval / tempo); + _playerCtx.patternSkip = 0; + } else { + ciaIntervall = palFlag ? (uint16)kPalDefaultCiaVal : (uint16)kNtscDefaultCiaVal; + _playerCtx.patternSkip = tempo; + } + setInterruptFreqUnscaled(ciaIntervall); + Paula::setAudioFilter(true); + + _playerCtx.patternCount = 0; + if (trackRun()) + startPaula(); +} + +int Tfmx::doSfx(uint16 sfxIndex, bool unlockChannel) { + assert(sfxIndex < 128); + Common::StackLock lock(_mutex); + + if (!hasResources()) + return -1; + const byte *sfxEntry = getSfxPtr(sfxIndex); + if (sfxEntry[0] == 0xFB) { + warning("Tfmx: custom patterns are not supported"); + // custompattern + /* const uint8 patCmd = sfxEntry[2]; + const int8 patExp = (int8)sfxEntry[3]; */ + } else { + // custommacro + const byte channelNo = ((_playerCtx.song >= 0) ? sfxEntry[2] : sfxEntry[4]) & (kNumVoices - 1); + const byte priority = sfxEntry[5] & 0x7F; + + ChannelContext &channel = _channelCtx[channelNo]; + if (unlockChannel) + unlockMacroChannel(channel); + + const int16 sfxLocktime = channel.sfxLockTime; + if (priority >= channel.customMacroPrio || sfxLocktime < 0) { + if (sfxIndex != channel.customMacroIndex || sfxLocktime < 0 || (sfxEntry[5] < 0x80)) { + channel.customMacro = READ_UINT32(sfxEntry); // intentionally not "endian-correct" + channel.customMacroPrio = priority; + channel.customMacroIndex = (uint8)sfxIndex; + debug(3, "Tfmx: running Macro %08X on channel %i - priority: %02X", TO_BE_32(channel.customMacro), channelNo, priority); + return channelNo; + } + } + } + return -1; +} + +} // End of namespace Audio + +// some debugging functions +#if 0 +namespace { + +void displayMacroStep(const void * const vptr) { + static const char *tableMacros[] = { + "DMAoff+Resetxx/xx/xx flag/addset/vol ", + "DMAon (start sample at selected begin) ", + "SetBegin xxxxxx sample-startadress", + "SetLen ..xxxx sample-length ", + "Wait ..xxxx count (VBI''s) ", + "Loop xx/xxxx count/step ", + "Cont xx/xxxx macro-number/step ", + "-------------STOP----------------------", + "AddNote xx/xxxx note/detune ", + "SetNote xx/xxxx note/detune ", + "Reset Vibrato-Portamento-Envelope ", + "Portamento xx/../xx count/speed ", + "Vibrato xx/../xx speed/intensity ", + "AddVolume ....xx volume 00-3F ", + "SetVolume ....xx volume 00-3F ", + "Envelope xx/xx/xx speed/count/endvol", + "Loop key up xx/xxxx count/step ", + "AddBegin xx/xxxx count/add to start", + "AddLen ..xxxx add to sample-len ", + "DMAoff stop sample but no clear ", + "Wait key up ....xx count (VBI''s) ", + "Go submacro xx/xxxx macro-number/step ", + "--------Return to old macro------------", + "Setperiod ..xxxx DMA period ", + "Sampleloop ..xxxx relative adress ", + "-------Set one shot sample-------------", + "Wait on DMA ..xxxx count (Wavecycles)", + "Random play xx/xx/xx macro/speed/mode ", + "Splitkey xx/xxxx key/macrostep ", + "Splitvolume xx/xxxx volume/macrostep ", + "Addvol+note xx/fe/xx note/CONST./volume", + "SetPrevNote xx/xxxx note/detune ", + "Signal xx/xxxx signalnumber/value", + "Play macro xx/.x/xx macro/chan/detune ", + "SID setbeg xxxxxx sample-startadress", + "SID setlen xx/xxxx buflen/sourcelen ", + "SID op3 ofs xxxxxx offset ", + "SID op3 frq xx/xxxx speed/amplitude ", + "SID op2 ofs xxxxxx offset ", + "SID op2 frq xx/xxxx speed/amplitude ", + "SID op1 xx/xx/xx speed/amplitude/TC", + "SID stop xx.... flag (1=clear all)" + }; + + const byte *const macroData = (const byte * const)vptr; + if (macroData[0] < ARRAYSIZE(tableMacros)) + debug("%s %02X%02X%02X", tableMacros[macroData[0]], macroData[1], macroData[2], macroData[3]); + else + debug("Unknown Macro #%02X %02X%02X%02X", macroData[0], macroData[1], macroData[2], macroData[3]); +} + +void displayPatternstep(const void * const vptr) { + static const char *tablePatterns[] = { + "End --Next track step--", + "Loop[count / step.w]", + "Cont[patternno./ step.w]", + "Wait[count 00-FF--------", + "Stop--Stop this pattern-", + "Kup^-Set key up/channel]", + "Vibr[speed / rate.b]", + "Enve[speed /endvolume.b]", + "GsPt[patternno./ step.w]", + "RoPt-Return old pattern-", + "Fade[speed /endvolume.b]", + "PPat[patt./track+transp]", + "Lock---------ch./time.b]", + "Cue [number.b/ value.w]", + "Stop-Stop custompattern-", + "NOP!-no operation-------" + }; + + const byte * const patData = (const byte * const)vptr; + const byte command = patData[0]; + if (command < 0xF0) { // Playnote + const byte flags = command >> 6; // 0-1 means note+detune, 2 means wait, 3 means portamento? + const char *flagsSt[] = { "Note ", "Note ", "Wait ", "Porta" }; + debug("%s %02X%02X%02X%02X", flagsSt[flags], patData[0], patData[1], patData[2], patData[3]); + } else + debug("%s %02X%02X%02X",tablePatterns[command & 0xF], patData[1], patData[2], patData[3]); +} + +} // End of anonymous namespace +#endif + +#endif // #if defined(SOUND_MODS_TFMX_H) diff --git a/audio/mods/tfmx.h b/audio/mods/tfmx.h new file mode 100644 index 0000000000..1930487eb8 --- /dev/null +++ b/audio/mods/tfmx.h @@ -0,0 +1,284 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +// see if all engines using this class are DISABLED +#if !defined(ENABLE_SCUMM) + +// normal Header Guard +#elif !defined(SOUND_MODS_TFMX_H) +#define SOUND_MODS_TFMX_H + +#include "audio/mods/paula.h" + +namespace Audio { + +class Tfmx : public Paula { +public: + Tfmx(int rate, bool stereo); + virtual ~Tfmx(); + + /** + * Stops a playing Song (but leaves macros running) and optionally also stops the player + * + * @param stopAudio stops player and audio output + * @param dataSize number of bytes to be written + * @return the number of bytes which were actually written. + */ + void stopSong(bool stopAudio = true) { Common::StackLock lock(_mutex); stopSongImpl(stopAudio); } + /** + * Stops currently playing Song (if any) and cues up a new one. + * if stopAudio is specified, the player gets reset before starting the new song + * + * @param songPos index of Song to play + * @param stopAudio stops player and audio output + * @param dataSize number of bytes to be written + * @return the number of bytes which were actually written. + */ + void doSong(int songPos, bool stopAudio = false); + /** + * plays an effect from the sfx-table, does not start audio-playback. + * + * @param sfxIndex index of effect to play + * @param unlockChannel overwrite higher priority effects + * @return index of the channel which now queued up the effect. + * -1 in case the effect couldnt be queued up + */ + int doSfx(uint16 sfxIndex, bool unlockChannel = false); + /** + * stop a running macro channel + * + * @param channel index of effect to stop + */ + void stopMacroEffect(int channel); + + void doMacro(int note, int macro, int relVol = 0, int finetune = 0, int channelNo = 0); + int getTicks() const { return _playerCtx.tickCount; } + int getSongIndex() const { return _playerCtx.song; } + void setSignalPtr(uint16 *ptr, uint16 numSignals) { _playerCtx.signal = ptr; _playerCtx.numSignals = numSignals; } + void freeResources() { _deleteResource = true; freeResourceDataImpl(); } + bool load(Common::SeekableReadStream &musicData, Common::SeekableReadStream &sampleData, bool autoDelete = true); + void setModuleData(Tfmx &otherPlayer); + +protected: + void interrupt(); + +private: + enum { kPalDefaultCiaVal = 11822, kNtscDefaultCiaVal = 14320, kCiaBaseInterval = 0x1B51F8 }; + enum { kNumVoices = 4, kNumChannels = 8, kNumSubsongs = 32, kMaxPatternOffsets = 128, kMaxMacroOffsets = 128 }; + + struct MdatResource { + const byte *mdatAlloc; ///< allocated Block of Memory + const byte *mdatData; ///< Start of mdat-File, might point before mdatAlloc to correct Offset + uint32 mdatLen; + + uint16 headerFlags; +// uint32 headerUnknown; +// char textField[6 * 40]; + + struct Subsong { + uint16 songstart; ///< Index in Trackstep-Table + uint16 songend; ///< Last index in Trackstep-Table + uint16 tempo; + } subsong[kNumSubsongs]; + + uint32 trackstepOffset; ///< Offset in mdat + uint32 sfxTableOffset; + + uint32 patternOffset[kMaxPatternOffsets]; ///< Offset in mdat + uint32 macroOffset[kMaxMacroOffsets]; ///< Offset in mdat + + void boundaryCheck(const void *address, size_t accessLen = 1) const { + assert(mdatAlloc <= address && (const byte *)address + accessLen <= (const byte *)mdatData + mdatLen); + } + } const *_resource; + + struct SampleResource { + const int8 *sampleData; ///< The whole sample-File + uint32 sampleLen; + + void boundaryCheck(const void *address, size_t accessLen = 2) const { + assert(sampleData <= address && (const byte *)address + accessLen <= (const byte *)sampleData + sampleLen); + } + } _resourceSample; + + bool _deleteResource; + + bool hasResources() { + return _resource && _resource->mdatLen && _resourceSample.sampleLen; + } + + struct ChannelContext { + byte paulaChannel; + +// byte macroIndex; + uint16 macroWait; + uint32 macroOffset; + uint32 macroReturnOffset; + uint16 macroStep; + uint16 macroReturnStep; + uint8 macroLoopCount; + bool macroRun; + int8 macroSfxRun; ///< values are the folowing: -1 macro disabled, 0 macro init, 1 macro running + + uint32 customMacro; + uint8 customMacroIndex; + uint8 customMacroPrio; + + bool sfxLocked; + int16 sfxLockTime; + bool keyUp; + + bool deferWait; + uint16 dmaIntCount; + + uint32 sampleStart; + uint16 sampleLen; + uint16 refPeriod; + uint16 period; + + int8 volume; + uint8 relVol; + uint8 note; + uint8 prevNote; + int16 fineTune; // always a signextended byte + + uint8 portaSkip; + uint8 portaCount; + uint16 portaDelta; + uint16 portaValue; + + uint8 envSkip; + uint8 envCount; + uint8 envDelta; + int8 envEndVolume; + + uint8 vibLength; + uint8 vibCount; + int16 vibValue; + int8 vibDelta; + + uint8 addBeginLength; + uint8 addBeginCount; + int32 addBeginDelta; + } _channelCtx[kNumVoices]; + + struct PatternContext { + uint32 offset; // patternStart, Offset from mdat + uint32 savedOffset; // for subroutine calls + uint16 step; // distance from patternStart + uint16 savedStep; + + uint8 command; + int8 expose; + uint8 loopCount; + uint8 wait; ///< how many ticks to wait before next Command + } _patternCtx[kNumChannels]; + + struct TrackStepContext { + uint16 startInd; + uint16 stopInd; + uint16 posInd; + int16 loopCount; + } _trackCtx; + + struct PlayerContext { + int8 song; ///< >= 0 if Song is running (means process Patterns) + + uint16 patternCount; + uint16 patternSkip; ///< skip that amount of CIA-Interrupts + + int8 volume; ///< Master Volume + + uint8 fadeSkip; + uint8 fadeCount; + int8 fadeEndVolume; + int8 fadeDelta; + + int tickCount; + + uint16 *signal; + uint16 numSignals; + + bool stopWithLastPattern; ///< hack to automatically stop the whole player if no Pattern is running + } _playerCtx; + + const byte *getSfxPtr(uint16 index = 0) const { + const byte *sfxPtr = (const byte *)(_resource->mdatData + _resource->sfxTableOffset + index * 8); + + _resource->boundaryCheck(sfxPtr, 8); + return sfxPtr; + } + + const uint16 *getTrackPtr(uint16 trackstep = 0) const { + const uint16 *trackData = (const uint16 *)(_resource->mdatData + _resource->trackstepOffset + 16 * trackstep); + + _resource->boundaryCheck(trackData, 16); + return trackData; + } + + const uint32 *getPatternPtr(uint32 offset) const { + const uint32 *pattData = (const uint32 *)(_resource->mdatData + offset); + + _resource->boundaryCheck(pattData, 4); + return pattData; + } + + const uint32 *getMacroPtr(uint32 offset) const { + const uint32 *macroData = (const uint32 *)(_resource->mdatData + offset); + + _resource->boundaryCheck(macroData, 4); + return macroData; + } + + const int8 *getSamplePtr(const uint32 offset) const { + const int8 *sample = _resourceSample.sampleData + offset; + + _resourceSample.boundaryCheck(sample, 2); + return sample; + } + + static inline void initMacroProgramm(ChannelContext &channel); + static inline void clearEffects(ChannelContext &channel); + static inline void haltMacroProgramm(ChannelContext &channel); + static inline void unlockMacroChannel(ChannelContext &channel); + static inline void initPattern(PatternContext &pattern, uint8 cmd, int8 expose, uint32 offset); + void stopSongImpl(bool stopAudio = true); + static inline void setNoteMacro(ChannelContext &channel, uint note, int fineTune); + void initFadeCommand(const uint8 fadeTempo, const int8 endVol); + void setModuleData(const MdatResource *resource, const int8 *sampleData, uint32 sampleLen, bool autoDelete = true); + static const MdatResource *loadMdatFile(Common::SeekableReadStream &musicData); + static const int8 *loadSampleFile(uint32 &sampleLen, Common::SeekableReadStream &sampleStream); + void freeResourceDataImpl(); + void effects(ChannelContext &channel); + void macroRun(ChannelContext &channel); + void advancePatterns(); + bool patternRun(PatternContext &pattern); + bool trackRun(bool incStep = false); + void noteCommand(uint8 note, uint8 param1, uint8 param2, uint8 param3); +}; + +} // End of namespace Audio + +#endif // !defined(SOUND_MODS_TFMX_H) |