- sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
- if (!sc)
- return NULL;
-
- sc->length = info.samples;
- sc->loopstart = info.loopstart;
- sc->speed = info.rate;
- sc->width = info.width;
- sc->stereo = info.channels == 2;
+ for (i = 0; i < srclength; i++)
+ ((signed char*)out_data)[i] = samples[i] - 128;
+ }
+ else // if (format->width == 2)
+ memcpy (out_data, samples, srclength * format->width);
+ }
+
+ // General case (linear interpolation with a fixed-point fractional
+ // step, 18-bit integer part and 14-bit fractional part)
+ // Can handle up to 2^18 (262144) samples per second (> 96KHz stereo)
+# define FRACTIONAL_BITS 14
+# define FRACTIONAL_MASK ((1 << FRACTIONAL_BITS) - 1)
+# define INTEGER_BITS (sizeof(samplefrac)*8 - FRACTIONAL_BITS)
+ else
+ {
+ const unsigned int fracstep = (unsigned int)((double)format->speed / sb->format.speed * (1 << FRACTIONAL_BITS));
+ size_t remain_in = srclength, total_out = 0;
+ unsigned int samplefrac;
+ const unsigned char *in_ptr = samples;
+ unsigned char *out_ptr = out_data;
+
+ // Check that we can handle one second of that sound
+ if (format->speed * format->channels > (1 << INTEGER_BITS))
+ {
+ Con_Printf ("ResampleSfx: sound quality too high for resampling (%uHz, %u channel(s))\n",
+ format->speed, format->channels);
+ return 0;
+ }