aboutsummaryrefslogtreecommitdiff
path: root/sound
diff options
context:
space:
mode:
authorMax Horn2003-08-07 23:54:39 +0000
committerMax Horn2003-08-07 23:54:39 +0000
commitac6bd10c59d08a906438978af47e538bb28cb20f (patch)
treee55bed8770d08d8d973eab7cad33d12fd08f1b88 /sound
parent93c90f46744feb5bc843f8b3fb8f8cf8970fcecc (diff)
downloadscummvm-rg350-ac6bd10c59d08a906438978af47e538bb28cb20f.tar.gz
scummvm-rg350-ac6bd10c59d08a906438978af47e538bb28cb20f.tar.bz2
scummvm-rg350-ac6bd10c59d08a906438978af47e538bb28cb20f.zip
my code was actually right. Rather the calling code is evil, it passes us odd buffer sizes for 16 bit audio data, which of course made no sense -> I added some asserts against this
svn-id: r9590
Diffstat (limited to 'sound')
-rw-r--r--sound/audiostream.cpp37
1 files changed, 21 insertions, 16 deletions
diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp
index d82aafbaab..c676e17b87 100644
--- a/sound/audiostream.cpp
+++ b/sound/audiostream.cpp
@@ -26,8 +26,6 @@
#include "common/file.h"
#include "common/util.h"
-//#define WHY_DOES_THIS_NOT_WORK 1
-
// This used to be an inline template function, but
// buggy template function handling in MSVC6 forced
@@ -64,6 +62,13 @@ protected:
public:
LinearMemoryStream(const byte *ptr, uint len, uint loopOffset, uint loopLen)
: _ptr(ptr), _end(ptr+len), _loopPtr(0), _loopEnd(0) {
+
+ // Verify the buffer sizes are sane
+ if (is16Bit && stereo)
+ assert((len & 3) == 0 && (loopLen & 3) == 0);
+ else if (is16Bit || stereo)
+ assert((len & 1) == 0 && (loopLen & 1) == 0);
+
if (loopLen) {
_loopPtr = _ptr + loopOffset;
_loopEnd = _loopPtr + loopLen;
@@ -82,12 +87,8 @@ template<bool stereo, bool is16Bit, bool isUnsigned>
int LinearMemoryStream<stereo, is16Bit, isUnsigned>::readBuffer(int16 *buffer, const int numSamples) {
int samples = 0;
while (samples < numSamples && !eosIntern()) {
-#ifdef WHY_DOES_THIS_NOT_WORK
const int len = MIN(numSamples, samples + (int)(_end - _ptr) / (is16Bit ? 2 : 1));
while (samples < len) {
-#else
- while (samples < numSamples && !eosIntern()) {
-#endif
*buffer++ = READSAMPLE(is16Bit, isUnsigned, _ptr);
_ptr += (is16Bit ? 2 : 1);
samples++;
@@ -133,8 +134,13 @@ public:
template<bool stereo, bool is16Bit, bool isUnsigned>
WrappedMemoryStream<stereo, is16Bit, isUnsigned>::WrappedMemoryStream(uint bufferSize) {
- if (stereo) // Stereo requires an even sized buffer
- assert(bufferSize % 2 == 0);
+
+ // Verify the buffer size is sane
+ if (is16Bit && stereo)
+ assert((bufferSize & 3) == 0);
+ else if (is16Bit || stereo)
+ assert((bufferSize & 1) == 0);
+
_bufferStart = (byte *)malloc(bufferSize);
_pos = _end = _bufferStart;
_bufferEnd = _bufferStart + bufferSize;
@@ -157,13 +163,9 @@ template<bool stereo, bool is16Bit, bool isUnsigned>
int WrappedMemoryStream<stereo, is16Bit, isUnsigned>::readBuffer(int16 *buffer, const int numSamples) {
int samples = 0;
while (samples < numSamples && !eosIntern()) {
-#ifdef WHY_DOES_THIS_NOT_WORK
const byte *endMarker = (_pos > _end) ? _bufferEnd : _end;
const int len = MIN(numSamples, samples + (int)(endMarker - _pos) / (is16Bit ? 2 : 1));
while (samples < len) {
-#else
- while (samples < numSamples && !eosIntern()) {
-#endif
*buffer++ = READSAMPLE(is16Bit, isUnsigned, _pos);
_pos += (is16Bit ? 2 : 1);
samples++;
@@ -177,6 +179,13 @@ int WrappedMemoryStream<stereo, is16Bit, isUnsigned>::readBuffer(int16 *buffer,
template<bool stereo, bool is16Bit, bool isUnsigned>
void WrappedMemoryStream<stereo, is16Bit, isUnsigned>::append(const byte *data, uint32 len) {
+
+ // Verify the buffer size is sane
+ if (is16Bit && stereo)
+ assert((len & 3) == 0);
+ else if (is16Bit || stereo)
+ assert((len & 1) == 0);
+
if (_end + len > _bufferEnd) {
// Wrap-around case
uint32 size_to_end_of_buffer = _bufferEnd - _end;
@@ -415,12 +424,8 @@ int MP3InputStream::readBuffer(int16 *buffer, const int numSamples) {
int samples = 0;
assert(_curChannel == 0); // Paranoia check
while (samples < numSamples && !eosIntern()) {
-#ifdef WHY_DOES_THIS_NOT_WORK
const int len = MIN(numSamples, samples + (int)(_synth.pcm.length - _posInFrame) * (_isStereo ? 2 : 1));
while (samples < len) {
-#else
- while (samples < numSamples && !eosIntern()) {
-#endif
*buffer++ = (int16)scale_sample(_synth.pcm.samples[0][_posInFrame]);
samples++;
if (_isStereo) {