+#include "snd_modplug.h"
+
+unsigned char resampling_buffer [48000 * 2 * 2];
+
+
+/*
+====================
+Snd_CreateRingBuffer
+
+If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
+(if "sampleframes" is 0, the function chooses the size).
+====================
+*/
+snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer)
+{
+ snd_ringbuffer_t *ringbuffer;
+
+ // If the caller provides a buffer, it must give us its size
+ if (sampleframes == 0 && buffer != NULL)
+ return NULL;
+
+ ringbuffer = (snd_ringbuffer_t*)Mem_Alloc(snd_mempool, sizeof (*ringbuffer));
+ memset(ringbuffer, 0, sizeof(*ringbuffer));
+ memcpy(&ringbuffer->format, format, sizeof(ringbuffer->format));
+
+ // If we haven't been given a buffer
+ if (buffer == NULL)
+ {
+ unsigned int maxframes;
+ size_t memsize;
+
+ if (sampleframes == 0)
+ maxframes = (format->speed + 1) / 2; // Make the sound buffer large enough for containing 0.5 sec of sound
+ else
+ maxframes = sampleframes;
+
+ memsize = maxframes * format->width * format->channels;
+ ringbuffer->ring = (unsigned char *) Mem_Alloc(snd_mempool, memsize);
+ ringbuffer->maxframes = maxframes;
+ }
+ else
+ {
+ ringbuffer->ring = (unsigned char *) buffer;
+ ringbuffer->maxframes = sampleframes;
+ }
+
+ return ringbuffer;
+}
+
+
+/*
+====================
+Snd_CreateSndBuffer
+====================
+*/
+snd_buffer_t *Snd_CreateSndBuffer (const unsigned char *samples, unsigned int sampleframes, const snd_format_t* in_format, unsigned int sb_speed)
+{
+ size_t newsampleframes, memsize;
+ snd_buffer_t* sb;
+
+ newsampleframes = (size_t) ceil((double)sampleframes * (double)sb_speed / (double)in_format->speed);
+
+ memsize = newsampleframes * in_format->channels * in_format->width;
+ memsize += sizeof (*sb) - sizeof (sb->samples);
+
+ sb = (snd_buffer_t*)Mem_Alloc (snd_mempool, memsize);
+ sb->format.channels = in_format->channels;
+ sb->format.width = in_format->width;
+ sb->format.speed = sb_speed;
+ sb->maxframes = newsampleframes;
+ sb->nbframes = 0;
+
+ if (!Snd_AppendToSndBuffer (sb, samples, sampleframes, in_format))
+ {
+ Mem_Free (sb);
+ return NULL;
+ }
+
+ return sb;
+}