Some optimizations to the Paula sound chip emu code (removing common sub expressions and stuff)
svn-id: r27761
This commit is contained in:
parent
6dfa44dbd5
commit
21aa642e7a
2 changed files with 63 additions and 69 deletions
|
@ -59,26 +59,7 @@ void Paula::clearVoice(byte voice) {
|
||||||
_voice[voice].offset = 0;
|
_voice[voice].offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void mix(int16 *&buf, int8 data, byte volume, byte panning, bool stereo) {
|
|
||||||
const int32 tmp = ((int32) data) * volume;
|
|
||||||
if (stereo) {
|
|
||||||
*buf++ += (tmp * (255 - panning)) >> 7;
|
|
||||||
*buf++ += (tmp * panning) >> 7;
|
|
||||||
} else
|
|
||||||
*buf++ += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Paula::readBuffer(int16 *buffer, const int numSamples) {
|
int Paula::readBuffer(int16 *buffer, const int numSamples) {
|
||||||
int voice;
|
|
||||||
int samples;
|
|
||||||
int nSamples;
|
|
||||||
int sLen;
|
|
||||||
double frequency;
|
|
||||||
double rate;
|
|
||||||
double offset;
|
|
||||||
int16 *p;
|
|
||||||
const int8 *data;
|
|
||||||
|
|
||||||
Common::StackLock lock(_mutex);
|
Common::StackLock lock(_mutex);
|
||||||
|
|
||||||
memset(buffer, 0, numSamples * 2);
|
memset(buffer, 0, numSamples * 2);
|
||||||
|
@ -86,6 +67,38 @@ int Paula::readBuffer(int16 *buffer, const int numSamples) {
|
||||||
return numSamples;
|
return numSamples;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_stereo)
|
||||||
|
return readBufferIntern<true>(buffer, numSamples);
|
||||||
|
else
|
||||||
|
return readBufferIntern<false>(buffer, numSamples);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<bool stereo>
|
||||||
|
inline void mixBuffer(int16 *&buf, const int8 *data, double &offset, double rate, int end, byte volume, byte panning) {
|
||||||
|
for (int i = 0; i < end; i++) {
|
||||||
|
// FIXME: We should avoid using floating point arithmetic here, since
|
||||||
|
// FP calculations and int<->FP conversions are very expensive on many
|
||||||
|
// architectures.
|
||||||
|
// So consider replacing offset and rate with fixed point values...
|
||||||
|
|
||||||
|
const int32 tmp = ((int32) data[(int)offset]) * volume;
|
||||||
|
if (stereo) {
|
||||||
|
*buf++ += (tmp * (255 - panning)) >> 7;
|
||||||
|
*buf++ += (tmp * (panning)) >> 7;
|
||||||
|
} else
|
||||||
|
*buf++ += tmp;
|
||||||
|
|
||||||
|
offset += rate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<bool stereo>
|
||||||
|
int Paula::readBufferIntern(int16 *buffer, const int numSamples) {
|
||||||
|
int voice;
|
||||||
|
int samples;
|
||||||
|
int nSamples;
|
||||||
|
|
||||||
samples = _stereo ? numSamples / 2 : numSamples;
|
samples = _stereo ? numSamples / 2 : numSamples;
|
||||||
while (samples > 0) {
|
while (samples > 0) {
|
||||||
if (_curInt == _intFreq) {
|
if (_curInt == _intFreq) {
|
||||||
|
@ -97,74 +110,52 @@ int Paula::readBuffer(int16 *buffer, const int numSamples) {
|
||||||
if (!_voice[voice].data || (_voice[voice].period <= 0))
|
if (!_voice[voice].data || (_voice[voice].period <= 0))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
frequency = (7093789.2 / 2.0) / _voice[voice].period;
|
double frequency = (7093789.2 / 2.0) / _voice[voice].period;
|
||||||
rate = frequency / _rate;
|
double rate = frequency / _rate;
|
||||||
offset = _voice[voice].offset;
|
double offset = _voice[voice].offset;
|
||||||
sLen = _voice[voice].length;
|
|
||||||
data = _voice[voice].data;
|
int sLen = _voice[voice].length;
|
||||||
p = buffer;
|
const int8 *data = _voice[voice].data;
|
||||||
|
int16 *p = buffer;
|
||||||
|
int end = 0;
|
||||||
|
|
||||||
|
|
||||||
_voice[voice].volume = MIN((byte) 0x40, _voice[voice].volume);
|
_voice[voice].volume = MIN((byte) 0x40, _voice[voice].volume);
|
||||||
|
// If looping has been enabled and we see that we will have to loop
|
||||||
|
// to generate enough samples, then use the "loop" branch.
|
||||||
if ((_voice[voice].lengthRepeat > 2) &&
|
if ((_voice[voice].lengthRepeat > 2) &&
|
||||||
((int)(offset + nSamples * rate) >= sLen)) {
|
((int)(offset + nSamples * rate) >= sLen)) {
|
||||||
int neededSamples = nSamples;
|
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].volume, _voice[voice].panning, _stereo);
|
|
||||||
|
|
||||||
_voice[voice].length = sLen = _voice[voice].lengthRepeat;
|
|
||||||
_voice[voice].data = data = _voice[voice].dataRepeat;
|
|
||||||
_voice[voice].offset = offset = 0;
|
|
||||||
neededSamples -= end;
|
|
||||||
|
|
||||||
while (neededSamples > 0) {
|
while (neededSamples > 0) {
|
||||||
if (neededSamples >= (int) ((sLen - offset) / rate)) {
|
end = MIN(neededSamples, (int)((sLen - offset) / rate));
|
||||||
|
|
||||||
|
if (end == 0) {
|
||||||
|
// This means that "rate" is too high, bigger than the sample size.
|
||||||
|
// So we scale it down according to the euclidean algorithm.
|
||||||
while (rate > (sLen - offset))
|
while (rate > (sLen - offset))
|
||||||
rate -= (sLen - offset);
|
rate -= (sLen - offset);
|
||||||
|
|
||||||
end = (int)((sLen - offset) / rate);
|
end = MIN(neededSamples, (int)((sLen - offset) / rate));
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < end; i++)
|
mixBuffer<stereo>(p, data, offset, rate, end, _voice[voice].volume, _voice[voice].panning);
|
||||||
mix(p, data[(int)(offset + rate * i)], _voice[voice].volume, _voice[voice].panning, _stereo);
|
_voice[voice].offset = offset;
|
||||||
|
neededSamples -= end;
|
||||||
|
|
||||||
|
// If we read beyond the sample end, loop back to the start.
|
||||||
|
if (ceil(_voice[voice].offset) >= sLen) {
|
||||||
_voice[voice].data = data = _voice[voice].dataRepeat;
|
_voice[voice].data = data = _voice[voice].dataRepeat;
|
||||||
_voice[voice].length = sLen =
|
_voice[voice].length = sLen = _voice[voice].lengthRepeat;
|
||||||
_voice[voice].lengthRepeat;
|
|
||||||
_voice[voice].offset = offset = 0;
|
_voice[voice].offset = offset = 0;
|
||||||
|
|
||||||
neededSamples -= end;
|
|
||||||
} else {
|
|
||||||
for (int i = 0; i < neededSamples; i++)
|
|
||||||
mix(p, data[(int)(offset + rate * i)], _voice[voice].volume, _voice[voice].panning, _stereo);
|
|
||||||
_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 {
|
} else {
|
||||||
if (offset < sLen) {
|
if (offset < sLen) { // Sample data left?
|
||||||
if ((int)(offset + nSamples * rate) >= sLen) {
|
end = MIN(nSamples, (int)((sLen - offset) / rate));
|
||||||
// The end of the sample is the limiting factor
|
|
||||||
|
|
||||||
int end = (int)((sLen - offset) / rate);
|
mixBuffer<stereo>(p, data, offset, rate, end, _voice[voice].volume, _voice[voice].panning);
|
||||||
for (int i = 0; i < end; i++)
|
_voice[voice].offset = offset;
|
||||||
mix(p, data[(int)(offset + rate * i)], _voice[voice].volume, _voice[voice].panning, _stereo);
|
|
||||||
_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].volume, _voice[voice].panning, _stereo);
|
|
||||||
_voice[voice].offset += rate * nSamples;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,9 @@ private:
|
||||||
int _intFreq;
|
int _intFreq;
|
||||||
int _curInt;
|
int _curInt;
|
||||||
bool _playing;
|
bool _playing;
|
||||||
|
|
||||||
|
template<bool stereo>
|
||||||
|
int readBufferIntern(int16 *buffer, const int numSamples);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // End of namespace Audio
|
} // End of namespace Audio
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue