/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
#ifdef ENABLE_EOB
#include "kyra/kyra_v1.h"
#include "kyra/sound/drivers/audiomaster2.h"
#include "audio/mods/paula.h"
#include "common/formats/iff_container.h"
namespace Kyra {
class AudioMaster2ResourceManager;
class AudioMaster2Internal;
class SoundResource;
class AudioMaster2IOManager {
public:
AudioMaster2IOManager();
~AudioMaster2IOManager();
struct IOUnit {
IOUnit() : _next(0), _sampleData(0), _sampleDataRepeat(0), _lenOnce(0), _lenRepeat(0), _startTick(0), _endTick(0), _transposeData(0), _rate(0), _period(0), _transposePara(0), _transposeDuration(0),
_levelAdjustData(0), _volumeSetting(0), _outputVolume(0), _levelAdjustPara(0), _levelAdjustDuration(0), _fadeOutState(-1), _flags(0) {}
IOUnit *_next;
const int8 *_sampleData;
const int8 *_sampleDataRepeat;
uint32 _lenOnce;
uint32 _lenRepeat;
uint32 _startTick;
uint32 _endTick;
const uint8 *_transposeData;
uint16 _rate;
uint16 _period;
uint16 _transposePara;
uint8 _transposeDuration;
const uint8 *_levelAdjustData;
uint16 _volumeSetting;
uint16 _outputVolume;
int16 _levelAdjustPara;
uint8 _levelAdjustDuration;
int16 _fadeOutState;
uint8 _flags;
};
void clearChain();
void deployChannels(IOUnit **dest);
IOUnit *requestFreeUnit();
void fadeOut();
bool isFading();
uint32 _sync;
uint32 _tempo;
private:
IOUnit *_units[8];
IOUnit *_ioChain;
};
class AudioMaster2Internal : public Audio::Paula {
friend class AudioMaster2IOManager;
private:
AudioMaster2Internal(Audio::Mixer *mixer);
public:
~AudioMaster2Internal() override;
static AudioMaster2Internal *open(Audio::Mixer *mixer);
static void close();
bool init();
bool loadRessourceFile(Common::SeekableReadStream *data);
bool startSound(const Common::String &name);
bool stopSound(const Common::String &name);
void flushResource(const Common::String &name);
void flushAllResources();
void fadeOut(int delay);
bool isFading();
void setMusicVolume(int volume);
void setSoundEffectVolume(int volume);
void interrupt() override;
void resetCounter();
int getPlayDuration();
void sync(SoundResource *res);
void stopChannels();
private:
void updateDevice();
AudioMaster2IOManager::IOUnit *_channels[4];
AudioMaster2IOManager *_io;
AudioMaster2ResourceManager *_res;
Audio::Mixer *_mixer;
uint32 _durationCounter;
uint8 _fadeOutSteps;
static AudioMaster2Internal *_refInstance;
static int _refCount;
Audio::SoundHandle _soundHandle;
bool _ready;
};
class SoundResource {
protected:
SoundResource(AudioMaster2ResourceManager *res, int type) : _res(res), _type(type), _playing(false), _next(0), _flags(0), _masterVolume(64), _refCnt(1) {}
virtual ~SoundResource() {}
public:
void loadName(Common::ReadStream *stream, uint32 size);
void open();
void close();
virtual void prepare() {}
virtual void setSync(uint32 sync) {}
const Common::String &getName() const;
uint8 getType() const;
bool getPlayStatus() const;
void setPlayStatus(bool playing);
void setMasterVolume(int volume);
virtual void interrupt(AudioMaster2IOManager *io);
virtual void setupMusicNote(AudioMaster2IOManager::IOUnit *unit, uint8 note, uint16 volume) {}
virtual void setupSoundEffect(AudioMaster2IOManager::IOUnit *unit, uint32 sync, uint32 tempo) {}
enum Mode {
kIdle = 0,
kRestart = 1
};
SoundResource *_next;
protected:
Common::String _name;
const uint8 _type;
uint8 _flags;
uint16 _masterVolume;
AudioMaster2ResourceManager *_res;
private:
virtual void release() = 0;
virtual void setupEnvelopes(AudioMaster2IOManager::IOUnit *unit) {}
int _refCnt;
bool _playing;
};
class SoundResource8SVX : public SoundResource {
public:
SoundResource8SVX(AudioMaster2ResourceManager *res);
private:
~SoundResource8SVX() override;
public:
void loadHeader(Common::ReadStream *stream, uint32 size);
void loadData(Common::ReadStream *stream, uint32 size);
void setupMusicNote(AudioMaster2IOManager::IOUnit *unit, uint8 note, uint16 volume) override;
void setupSoundEffect(AudioMaster2IOManager::IOUnit *unit, uint32 sync, uint32 tempo) override;
private:
void release() override;
void setupEnvelopes(AudioMaster2IOManager::IOUnit *unit) override;
uint32 _numSamplesOnce;
uint32 _numSamplesRepeat;
uint32 _numSamplesPerCycle;
uint16 _rate;
uint8 _numBlocks;
uint8 _format;
uint32 _trackVolume;
const int8 *_data;
uint32 _dataSize;
static const uint32 _periods[128];
};
class SoundResourceINST : public SoundResource {
public:
SoundResourceINST(AudioMaster2ResourceManager *res) : SoundResource(res, 2), _samplesResource(0), _transpose(0), _levelAdjust(0) {}
private:
~SoundResourceINST() override;
public:
void loadPitchData(Common::ReadStream *stream, uint32 size);
void loadSamples(Common::ReadStream *stream, uint32 size);
void loadVolumeData(Common::ReadStream *stream, uint32 size);
void setupMusicNote(AudioMaster2IOManager::IOUnit *unit, uint8 note, uint16 volume) override;
void setupSoundEffect(AudioMaster2IOManager::IOUnit *unit, uint32 sync, uint32 rate) override;
struct EnvelopeData {
EnvelopeData(const uint8 *data, uint32 size) : volume(0x40), _data(data), _dataSize(size) {}
~EnvelopeData() { delete[] _data; }
uint8 volume;
const uint8 *_data;
uint32 _dataSize;
};
private:
void release() override;
void setupEnvelopes(AudioMaster2IOManager::IOUnit *unit) override;
EnvelopeData *_transpose;
EnvelopeData *_levelAdjust;
SoundResource *_samplesResource;
};
class SoundResourceSMUS : public SoundResource {
public:
SoundResourceSMUS(AudioMaster2ResourceManager *res) : SoundResource(res, 1), _tempo(0), _songVolume(0), _playFlags(0) {}
private:
~SoundResourceSMUS() override;
public:
void loadHeader(Common::ReadStream *stream, uint32 size);
void loadInstrument(Common::ReadStream *stream, uint32 size);
void loadTrack(Common::ReadStream *stream, uint32 size);
void prepare() override;
uint16 getTempo() const;
void setSync(uint32 sync) override;
void interrupt(AudioMaster2IOManager *io) override;
private:
void release() override;
struct Track {
Track() : _dataStart(0), _dataEnd(0), _dataCur(0), _instrument(0), _volume(0), _sync(0) {}
~Track() {
if (_instrument)
_instrument->close();
delete[] _dataStart;
}
void setInstrument(SoundResource *instr) {
if (_instrument)
_instrument->close();
_instrument = instr;
_instrument->open();
}
uint32 _sync;
SoundResource *_instrument;
uint8 _volume;
const uint8 *_dataStart;
const uint8 *_dataEnd;
const uint8 *_dataCur;
};
bool parse(AudioMaster2IOManager *io, Track *track);
uint16 _tempo;
uint8 _songVolume;
static const uint16 _durationTable[64];
Common::Array