SDL sample converters

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

SDL sample converters

Post by J.F. »

SDL has the ability to convert the format and rate of samples to the mixer format and rate, but there's a slight catch - it only converts the rate by powers of two. Look at this in SDL_audiocvt.c:

Code: Select all

		/* We may need a slow conversion here to finish up */
		if ( (lo_rate/100) != (hi_rate/100) ) {
#if 1
			/* The problem with this is that if the input buffer is
			   say 1K, and the conversion rate is say 1.1, then the
			   output buffer is 1.1K, which may not be an acceptable
			   buffer size for the audio driver (not a power of 2)
			*/
			/* For now, punt and hope the rate distortion isn't great.
			*/
#else
			if &#40; src_rate < dst_rate &#41; &#123;
				cvt->rate_incr = &#40;double&#41;lo_rate/hi_rate;
				cvt->len_mult *= 2;
				cvt->len_ratio /= cvt->rate_incr;
			&#125; else &#123;
				cvt->rate_incr = &#40;double&#41;hi_rate/lo_rate;
				cvt->len_ratio *= cvt->rate_incr;
			&#125;
			cvt->filters&#91;cvt->filter_index++&#93; = SDL_RateSLOW;
#endif
		&#125;
The "slow" rate conversion isn't enabled because on some audio cards, it can result in the mixed sample being a length that the card can't handle. The PSP wants sample lengths to be a multiple of 64, but the only result from not doing so is the possibility of a slight pop or crackle at the end of the sample. Because of that, I changed the "#if 1" to "#if 0" and recompiled SDL. This fixes the pitch problem in Duke Nukem 3D. Duke3D uses samples at 5 and 8 kHz, and relies on SDL to resample them to 11025 Hz when mixing the sounds. Without the change, the samples were being resampled to the closest power of two between 5/8 kHz and 44100 Hz (the mixer rate).

Any thoughts on enabling the slow conversion in the repo copy of SDL?
Post Reply