aboutsummaryrefslogtreecommitdiff
path: root/sound
diff options
context:
space:
mode:
authorMax Horn2004-01-03 02:29:48 +0000
committerMax Horn2004-01-03 02:29:48 +0000
commitd212b2c2e1bc6aba370fa5e0ae4d122cd19a2466 (patch)
tree4abfba8de3f818e3495c9a652677eec063b96059 /sound
parent1b706d35649cac533d9899edc172855fc88bafd9 (diff)
downloadscummvm-rg350-d212b2c2e1bc6aba370fa5e0ae4d122cd19a2466.tar.gz
scummvm-rg350-d212b2c2e1bc6aba370fa5e0ae4d122cd19a2466.tar.bz2
scummvm-rg350-d212b2c2e1bc6aba370fa5e0ae4d122cd19a2466.zip
replace read() by readBuffer() in CopyRateConverter
svn-id: r12102
Diffstat (limited to 'sound')
-rw-r--r--sound/rate.cpp48
1 files changed, 38 insertions, 10 deletions
diff --git a/sound/rate.cpp b/sound/rate.cpp
index 45046cb80b..352243bc79 100644
--- a/sound/rate.cpp
+++ b/sound/rate.cpp
@@ -192,26 +192,54 @@ the_end:
/**
* Simple audio rate converter for the case that the inrate equals the outrate.
- * @todo This is inefficient, it would be better if this used readBuffer()
- * instead of read().
*/
template<bool stereo, bool reverseStereo>
class CopyRateConverter : public RateConverter {
+ st_sample_t *_buffer;
+ st_size_t _bufferSize;
public:
+ CopyRateConverter() : _buffer(0), _bufferSize(0) {}
+ ~CopyRateConverter() {
+ free(_buffer);
+ }
+
virtual int flow(AudioInputStream &input, st_sample_t *obuf, st_size_t osamp, st_volume_t vol_l, st_volume_t vol_r) {
- int16 tmp[2];
- st_size_t len = osamp;
assert(input.isStereo() == stereo);
- while (!input.endOfData() && len--) {
- tmp[0] = tmp[1] = input.read();
- if (stereo)
- tmp[reverseStereo ? 0 : 1] = input.read();
+
+ st_sample_t *ptr;
+ st_size_t len;
+
+ if (stereo)
+ osamp *= 2;
+
+ // Reallocate temp buffer, if necessary
+ if (osamp > _bufferSize) {
+ free(_buffer);
+ _buffer = (st_sample_t *)malloc(osamp * 2);
+ _bufferSize = osamp;
+ }
+
+ // Read up to 'osamp' samples into our temporary buffer
+ len = input.readBuffer(_buffer, osamp);
+
+ // Mix the data into the output buffer
+ ptr = _buffer;
+ while (len--) {
+ st_sample_t tmp0, tmp1;
+ tmp0 = tmp1 = *ptr++;
+ if (stereo) {
+ if (reverseStereo)
+ tmp0 = *ptr++;
+ else
+ tmp1 = *ptr++;
+ len--;
+ }
// output left channel
- clampedAdd(*obuf++, (tmp[0] * (int)vol_l) >> 8);
+ clampedAdd(*obuf++, (tmp0 * (int)vol_l) >> 8);
// output right channel
- clampedAdd(*obuf++, (tmp[1] * (int)vol_r) >> 8);
+ clampedAdd(*obuf++, (tmp1 * (int)vol_r) >> 8);
}
return (ST_SUCCESS);
}