Changeset - r12679:1015c7a9aebf
[Not reviewed]
master
0 2 0
rubidium - 15 years ago 2009-08-10 17:46:44
rubidium@openttd.org
(svn r17146) -Codechange: improve the sample rate conversion a bit
2 files changed with 24 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/mixer.cpp
Show inline comments
 
@@ -33,12 +33,25 @@ static uint32 _play_rate = 11025;
 
 * samples should not exceed this limit as it will sound too loud. It also
 
 * stops overflowing when too many sounds are played at the same time, which
 
 * causes an even worse sound quality.
 
 */
 
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;
 
	uint32 frac_pos;
 
	uint32 frac_speed;
 
	int volume_left;
 
@@ -61,14 +74,15 @@ static void mix_int16(MixerChannel *sc, 
 
			buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
 
			b++;
 
			buffer += 2;
 
		} 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;
 
			frac_pos &= 0xffff;
 
		} while (--samples > 0);
 
	}
 
@@ -102,14 +116,15 @@ static void mix_int8_to_int16(MixerChann
 
			buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
 
			b++;
 
			buffer += 2;
 
		} 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;
 
			frac_pos &= 0xffff;
 
		} while (--samples > 0);
 
	}
src/sound.cpp
Show inline comments
 
@@ -106,13 +106,17 @@ static void OpenBankFile(const char *fil
 
static bool SetBankSource(MixerChannel *mc, const SoundEntry *sound)
 
{
 
	assert(sound != NULL);
 

	
 
	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);
 

	
 
	/* 16-bit PCM WAV files should be signed by default */
 
	if (sound->bits_per_sample == 8) {
0 comments (0 inline, 0 general)