aboutsummaryrefslogtreecommitdiff
path: root/sound/mods
diff options
context:
space:
mode:
Diffstat (limited to 'sound/mods')
-rw-r--r--sound/mods/infogrames.cpp458
-rw-r--r--sound/mods/infogrames.h150
-rw-r--r--sound/mods/paula.cpp167
-rw-r--r--sound/mods/paula.h94
4 files changed, 869 insertions, 0 deletions
diff --git a/sound/mods/infogrames.cpp b/sound/mods/infogrames.cpp
new file mode 100644
index 0000000000..e5eda6e838
--- /dev/null
+++ b/sound/mods/infogrames.cpp
@@ -0,0 +1,458 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2007 The ScummVM project
+ *
+ * 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 "sound/mods/infogrames.h"
+#include "common/endian.h"
+
+namespace Audio {
+
+Infogrames::Instruments::Instruments() {
+ init();
+}
+
+Infogrames::Instruments::~Instruments() {
+ if (_sampleData)
+ 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(Common::SeekableReadStream &ins) {
+ int i;
+ uint32 fsize;
+ uint32 offset[32];
+ uint32 offsetRepeat[32];
+ uint32 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(void) {
+ if (_sampleData)
+ 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) :
+ Paula(stereo, rate, rate/80) {
+ _instruments = &ins;
+ _data = 0;
+ _repCount = -1;
+
+ reset();
+}
+
+Infogrames::~Infogrames() {
+ if (_data)
+ 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(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;
+
+ _end = false;
+ _playing = true;
+ return true;
+}
+
+void Infogrames::unload(void) {
+ stopPlay();
+
+ if (_data)
+ delete[] _data;
+ _data = 0;
+
+ clearVoices();
+ reset();
+
+ _end = true;
+}
+
+void Infogrames::getNextSample(Channel &chn) {
+ byte *data;
+ byte cmdBlock;
+ 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]);
+ _voice[chn].volume = _volume;
+ _voice[chn].period = _period;
+ if ((_sample != 0xFF) && (_sample < _instruments->_count)) {
+ _voice[chn].data = _instruments->_samples[_sample].data;
+ _voice[chn].length = _instruments->_samples[_sample].length;
+ _voice[chn].dataRepeat = _instruments->_samples[_sample].dataRepeat;
+ _voice[chn].lengthRepeat =
+ _instruments->_samples[_sample].lengthRepeat;
+ _voice[chn].offset = 0;
+ _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) {
+ _end = true;
+ _playing = false;
+ }
+ else
+ init();
+ }
+}
+
+} // End of namespace Audio
diff --git a/sound/mods/infogrames.h b/sound/mods/infogrames.h
new file mode 100644
index 0000000000..78bc9efd82
--- /dev/null
+++ b/sound/mods/infogrames.h
@@ -0,0 +1,150 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2007 The ScummVM project
+ *
+ * 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_INFOGRAMES_H
+#define SOUND_MODS_INFOGRAMES_H
+
+#include "sound/mods/paula.h"
+#include "common/stream.h"
+#include "common/file.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) {
+ Common::File f;
+
+ if (f.open(ins))
+ return load(f);
+ return false;
+ }
+ void unload(void);
+
+ uint8 getCount(void) 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);
+ ~Infogrames();
+
+ Instruments *getInstruments(void) const { return _instruments; }
+ bool getRepeating(void) const { return _repCount != 0; }
+ void setRepeating (int32 repCount) { _repCount = repCount; }
+ virtual void startPlay(void) { _playing = true;}
+ virtual void stopPlay(void)
+ {
+ _mutex.lock();
+ _playing = false;
+ _mutex.unlock();
+ }
+ virtual void pausePlay(bool pause) { _playing = !pause; }
+
+ bool load(Common::SeekableReadStream &dum);
+ bool load(const char *dum) {
+ Common::File f;
+
+ if (f.open(dum))
+ return load(f);
+ return false;
+ }
+ void unload(void);
+ void restart(void) { if (_data) { stopPlay(); init(); startPlay(); } }
+
+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);
+ void reset(void);
+ void getNextSample(Channel &chn);
+ int16 tune(Slide &slide, int16 start) const;
+ virtual void interrupt(void);
+};
+
+} // End of namespace Audio
+
+#endif
diff --git a/sound/mods/paula.cpp b/sound/mods/paula.cpp
new file mode 100644
index 0000000000..90d247096c
--- /dev/null
+++ b/sound/mods/paula.cpp
@@ -0,0 +1,167 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2007 The ScummVM project
+ *
+ * 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 "sound/mods/paula.h"
+
+namespace Audio {
+
+Paula::Paula(bool stereo, int rate, int interruptFreq) :
+ _stereo(stereo), _rate(rate), _intFreq(interruptFreq) {
+ _playing = false;
+
+ clearVoices();
+ _voice[0].panning = 63;
+ _voice[1].panning = 191;
+ _voice[2].panning = 191;
+ _voice[3].panning = 63;
+
+ if (_intFreq <= 0)
+ _intFreq = _rate;
+
+ _curInt = _intFreq;
+ _end = true;
+}
+
+Paula::~Paula() {
+}
+
+void Paula::clearVoice(byte voice) {
+ if (voice >= 4)
+ return;
+
+ _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 = 0;
+}
+
+int Paula::readBuffer(int16 *buffer, const int numSamples) {
+ int voice;
+ int samples;
+ int nSamples;
+ int sLen;
+ double frequency;
+ double rate;
+ double offset;
+ int16 *p;
+ int8 *data;
+
+ _mutex.lock();
+
+ memset(buffer, 0, numSamples * 2);
+ if (!_playing)
+ {
+ _mutex.unlock();
+ return numSamples;
+ }
+
+ samples = _stereo ? numSamples / 2 : numSamples;
+ while (samples > 0) {
+ if (_curInt == _intFreq) {
+ interrupt();
+ _curInt = 0;
+ }
+ nSamples = MIN(samples, _intFreq - _curInt);
+ for (voice = 0; voice < 4; voice++) {
+ if (!_voice[voice].data || (_voice[voice].period <= 0))
+ continue;
+
+ frequency = (7093789.2 / 2.0) / _voice[voice].period;
+ rate = frequency / _rate;
+ offset = _voice[voice].offset;
+ sLen = _voice[voice].length;
+ data = _voice[voice].data;
+ p = buffer;
+
+ _voice[voice].volume &= 0x3F;
+ if ((_voice[voice].lengthRepeat > 2) &&
+ ((int)(offset + nSamples * rate) >= sLen)) {
+ int neededSamples = nSamples;
+
+ int end = (int)((sLen - offset) / rate);
+
+ for (int i = 0; i < end; i++)
+ mix(p, data[(int)(offset + rate * i)], voice);
+
+ _voice[voice].length = sLen = _voice[voice].lengthRepeat;
+ _voice[voice].data = data = _voice[voice].dataRepeat;
+ _voice[voice].offset = offset = 0;
+ neededSamples -= end;
+
+ while (neededSamples > 0) {
+ if (neededSamples >= (int) ((sLen - offset) / rate)) {
+ end = (int)((sLen - offset) / rate);
+
+ for (int i = 0; i < end; i++)
+ mix(p, data[(int)(offset + rate * i)], voice);
+
+ _voice[voice].data = data = _voice[voice].dataRepeat;
+ _voice[voice].length = sLen =
+ _voice[voice].lengthRepeat;
+ _voice[voice].offset = offset = 0;
+
+ neededSamples -= end;
+ } else {
+ for (int i = 0; i < neededSamples; i++)
+ mix(p, data[(int)(offset + rate * i)], voice);
+ _voice[voice].offset += rate * neededSamples;
+ if (ceil(_voice[voice].offset) >= sLen) {
+ _voice[voice].data = data = _voice[voice].dataRepeat;
+ _voice[voice].length = sLen =
+ _voice[voice].lengthRepeat;
+ _voice[voice].offset = offset = 0;
+ }
+ neededSamples = 0;
+ }
+ }
+ } else {
+ if (offset < sLen) {
+ if ((int)(offset + nSamples * rate) >= sLen) {
+ // The end of the sample is the limiting factor
+
+ int end = (int)((sLen - offset) / rate);
+ for (int i = 0; i < end; i++)
+ mix(p, data[(int)(offset + rate * i)], voice);
+ _voice[voice].offset = sLen;
+ } else {
+ // The requested number of samples is the limiting
+ // factor, not the sample
+
+ for (int i = 0; i < nSamples; i++)
+ mix(p, data[(int)(offset + rate * i)], voice);
+ _voice[voice].offset += rate * nSamples;
+ }
+ }
+ }
+ }
+ buffer += _stereo ? nSamples * 2 : nSamples;
+ _curInt += nSamples;
+ samples -= nSamples;
+ }
+ _mutex.unlock();
+ return numSamples;
+}
+
+} // End of namespace Audio
diff --git a/sound/mods/paula.h b/sound/mods/paula.h
new file mode 100644
index 0000000000..be85122635
--- /dev/null
+++ b/sound/mods/paula.h
@@ -0,0 +1,94 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2007 The ScummVM project
+ *
+ * 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 "sound/audiostream.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:
+ Paula(bool stereo = false, int rate = 44100, int interruptFreq = 0);
+ ~Paula();
+
+ bool playing() const { return _playing; }
+ void setInterruptFreq(int freq) { _intFreq = freq; }
+ void setPanning(byte voice, byte panning)
+ {
+ if (voice < 4)
+ _voice[voice].panning = panning;
+ }
+ void clearVoice(byte voice);
+ void clearVoices() { int i; for (i = 0; i < 4; i++) clearVoice(i); }
+ virtual void startPlay(void) {}
+ virtual void stopPlay(void) {}
+ virtual void pausePlay(bool 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 {
+ int8 *data;
+ int8 *dataRepeat;
+ uint32 length;
+ uint32 lengthRepeat;
+ int16 period;
+ byte volume;
+ double offset;
+ byte panning; // For stereo mixing: 0 = far left, 255 = far right
+ } _voice[4];
+
+ int _rate;
+ int _intFreq;
+ int _curInt;
+ bool _stereo;
+ bool _end;
+ bool _playing;
+ Common::Mutex _mutex;
+
+ void mix(int16 *&buf, int8 data, int voice) {
+ if (_stereo) {
+ *buf++ += (((int32) data) * _voice[voice].volume *
+ (255 - _voice[voice].panning)) >> 7;
+ *buf++ += (((int32) data) * _voice[voice].volume *
+ (_voice[voice].panning)) >> 7;
+ } else
+ *buf++ += _voice[voice].volume * data;
+ }
+ virtual void interrupt(void) {};
+};
+
+} // End of namespace Audio
+
+#endif