1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "audio/softsynth/fmtowns_pc98/pcm_common.h"
#include "audio/mixer.h"
PCMChannel_Base::PCMChannel_Base() : _vol(0), _data(nullptr), _dataEnd(0), _loopLen(0), _pos(0), _loopStart(0), _step(0), _panLeft(7), _panRight(7), _activeOutput(false) {
}
PCMChannel_Base::~PCMChannel_Base() {
clear();
}
void PCMChannel_Base::clear() {
_vol = 0;
_data = nullptr;
_dataEnd = 0;
_loopLen = 0;
_pos = 0;
_loopStart = 0;
_step = 0;
_panLeft = _panRight = 7;
_activeOutput = false;
}
void PCMChannel_Base::updateOutput() {
if (!isPlaying())
return;
_pos += _step;
if (_pos >= _dataEnd) {
if (_loopStart != _dataEnd) {
_pos = _loopStart;
_dataEnd = _loopStart + _loopLen;
} else {
_pos = 0;
stopInternal();
}
}
}
int32 PCMChannel_Base::currentSampleLeft() {
return (isActive() && _panLeft) ? (((_data[_pos >> 11] * _vol) * _panLeft) >> 3) : 0;
}
int32 PCMChannel_Base::currentSampleRight() {
return (isActive() && _panRight) ? (((_data[_pos >> 11] * _vol) * _panRight) >> 3) : 0;
}
bool PCMChannel_Base::isActive() const {
return _activeOutput;
}
void PCMChannel_Base::activate() {
_activeOutput = true;
}
void PCMChannel_Base::deactivate() {
_activeOutput = false;
}
void PCMChannel_Base::setData(const int8 *data, uint32 dataEnd, uint32 dataStart) {
_data = data;
_dataEnd = dataEnd;
_pos = dataStart;
}
void PCMChannel_Base::setVolume(uint8 vol) {
_vol = vol;
}
void PCMChannel_Base::setPanPos(uint8 pan) {
_panLeft = pan & 0x0f;
_panRight = pan >> 4;
}
void PCMChannel_Base::setupLoop(uint32 loopStart, uint32 loopLen) {
_loopStart = loopStart << 11;
_loopLen = loopLen << 11;
}
void PCMChannel_Base::setRate(uint16 rate) {
_step = rate;
}
PCMDevice_Base::PCMDevice_Base(int samplingRate, int deviceVolume, int numChannels) : _numChannels(numChannels), _deviceVolume(deviceVolume), _intRate((7670454 << 8) / samplingRate),
_extRate(72 << 8), _timer(0), _musicVolume(Audio::Mixer::kMaxMixerVolume), _sfxVolume(Audio::Mixer::kMaxMixerVolume), _pcmSfxChanMask(0) {
_channels = new PCMChannel_Base*[numChannels];
}
PCMDevice_Base::~PCMDevice_Base() {
delete[] _channels;
}
void PCMDevice_Base::assignChannel(uint8 id, PCMChannel_Base *const chan) {
assert(id < _numChannels);
_channels[id] = chan;
}
void PCMDevice_Base::setMusicVolume(uint16 vol) {
_musicVolume = vol;
}
void PCMDevice_Base::setSfxVolume(uint16 vol) {
_sfxVolume = vol;
}
void PCMDevice_Base::setSfxChanMask(int mask) {
_pcmSfxChanMask = mask;
}
void PCMDevice_Base::readBuffer(int32 *buffer, uint32 bufferSize) {
for (uint32 i = 0; i < bufferSize; i++) {
_timer += _extRate;
while (_timer >= _intRate) {
_timer -= _intRate;
for (int ii = 0; ii < 8; ii++)
_channels[ii]->updateOutput();
}
int32 finOutL = 0;
int32 finOutR = 0;
for (int ii = 0; ii < _numChannels; ii++) {
if (_channels[ii]->isActive()) {
int32 oL = _channels[ii]->currentSampleLeft();
int32 oR = _channels[ii]->currentSampleRight();
if ((1 << ii) & (~_pcmSfxChanMask)) {
oL = (oL * _musicVolume) / Audio::Mixer::kMaxMixerVolume;
oR = (oR * _musicVolume) / Audio::Mixer::kMaxMixerVolume;
}
if ((1 << ii) & _pcmSfxChanMask) {
oL = (oL * _sfxVolume) / Audio::Mixer::kMaxMixerVolume;
oR = (oR * _sfxVolume) / Audio::Mixer::kMaxMixerVolume;
}
finOutL += oL;
finOutR += oR;
if (!_channels[ii]->isPlaying())
_channels[ii]->deactivate();
}
}
buffer[i << 1] += ((finOutL * _deviceVolume) >> 7);
buffer[(i << 1) + 1] += ((finOutR * _deviceVolume) >> 7);
}
}
|