summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-08-10 17:46:44 +0000
committerrubidium <rubidium@openttd.org>2009-08-10 17:46:44 +0000
commit238b0e43bbaa997acb7e53f1de118a963bb330fa (patch)
treeda4694db44320da55a69268177a8abe47f1069c7
parent6800db65a7a16ae02e521dc34ac1ad3d646dee0b (diff)
downloadopenttd-238b0e43bbaa997acb7e53f1de118a963bb330fa.tar.xz
(svn r17146) -Codechange: improve the sample rate conversion a bit
-rw-r--r--src/mixer.cpp23
-rw-r--r--src/sound.cpp6
2 files changed, 24 insertions, 5 deletions
diff --git a/src/mixer.cpp b/src/mixer.cpp
index 5a8370df8..a2b296792 100644
--- a/src/mixer.cpp
+++ b/src/mixer.cpp
@@ -36,6 +36,19 @@ static uint32 _play_rate = 11025;
*/
static const int MAX_VOLUME = 128 * 128;
+/**
+ * Perform the rate conversion between the input and output.
+ * @param b the buffer to read the data from
+ * @param frac_pos the position from the begin of the buffer till the next element
+ * @tparam T the size of the buffer (8 or 16 bits)
+ * @return the converted value.
+ */
+template <typename T>
+static int RateConversion(T *b, int frac_pos)
+{
+ return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
+}
+
static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples)
{
int16 *b;
@@ -64,8 +77,9 @@ static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples)
} while (--samples > 0);
} else {
do {
- buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
- buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
+ int data = RateConversion(b, frac_pos);
+ buffer[0] = Clamp(buffer[0] + (data * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
+ buffer[1] = Clamp(buffer[1] + (data * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
buffer += 2;
frac_pos += frac_speed;
b += frac_pos >> 16;
@@ -105,8 +119,9 @@ static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
} while (--samples > 0);
} else {
do {
- buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
- buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
+ int data = RateConversion(b, frac_pos);
+ buffer[0] = Clamp(buffer[0] + (data * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
+ buffer[1] = Clamp(buffer[1] + (data * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
buffer += 2;
frac_pos += frac_speed;
b += frac_pos >> 16;
diff --git a/src/sound.cpp b/src/sound.cpp
index e314c7ade..130841154 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -109,7 +109,11 @@ static bool SetBankSource(MixerChannel *mc, const SoundEntry *sound)
if (sound->file_size == 0) return false;
- int8 *mem = MallocT<int8>(sound->file_size);
+ int8 *mem = MallocT<int8>(sound->file_size + 2);
+ /* Add two extra bytes so rate conversion can read these
+ * without reading out of it's input buffer. */
+ mem[sound->file_size ] = 0;
+ mem[sound->file_size + 1] = 0;
FioSeekToFile(sound->file_slot, sound->file_offset);
FioReadBlock(mem, sound->file_size);