+#define STREAM_BUFFER_DURATION 1.5f // 1.5 sec
+#define STREAM_BUFFER_SIZE(format_ptr) ((int)(ceil (STREAM_BUFFER_DURATION * ((format_ptr)->speed * (format_ptr)->width * (format_ptr)->channels))))
+
+// We work with 1 sec sequences, so this buffer must be able to contain
+// 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
+static unsigned char resampling_buffer [48000 * 2 * 2];
+
+
+// Per-sfx data structure
+typedef struct
+{
+ unsigned char *file;
+ size_t filesize;
+ snd_format_t format;
+ unsigned int total_length;
+ char name[128];
+} ogg_stream_persfx_t;
+
+// Per-channel data structure
+typedef struct
+{
+ OggVorbis_File vf;
+ ov_decode_t ov_decode;
+ unsigned int sb_offset;
+ int bs;
+ snd_buffer_t sb; // must be at the end due to its dynamically allocated size
+} ogg_stream_perchannel_t;
+
+
+static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
+
+/*
+====================
+OGG_FetchSound
+====================
+*/
+static const snd_buffer_t* OGG_FetchSound (void *sfxfetcher, void **chfetcherpointer, unsigned int *start, unsigned int nbsampleframes)
+{
+ ogg_stream_perchannel_t* per_ch = (ogg_stream_perchannel_t *)*chfetcherpointer;
+ ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfxfetcher;
+ snd_buffer_t* sb;
+ int newlength, done, ret, bigendian;
+ unsigned int real_start;
+ unsigned int factor;
+
+ // If there's no fetcher structure attached to the channel yet
+ if (per_ch == NULL)
+ {
+ size_t buff_len, memsize;
+ snd_format_t sb_format;
+
+ sb_format.speed = snd_renderbuffer->format.speed;
+ sb_format.width = per_sfx->format.width;
+ sb_format.channels = per_sfx->format.channels;
+
+ buff_len = STREAM_BUFFER_SIZE(&sb_format);
+ memsize = sizeof (*per_ch) - sizeof (per_ch->sb.samples) + buff_len;
+ per_ch = (ogg_stream_perchannel_t *)Mem_Alloc (snd_mempool, memsize);
+
+ // Open it with the VorbisFile API
+ per_ch->ov_decode.buffer = per_sfx->file;
+ per_ch->ov_decode.ind = 0;
+ per_ch->ov_decode.buffsize = per_sfx->filesize;
+ if (qov_open_callbacks (&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
+ {
+ Con_Printf("error while reading Ogg Vorbis stream \"%s\"\n", per_sfx->name);
+ Mem_Free (per_ch);
+ return NULL;
+ }
+ per_ch->bs = 0;
+
+ per_ch->sb_offset = 0;
+ per_ch->sb.format = sb_format;
+ per_ch->sb.nbframes = 0;
+ per_ch->sb.maxframes = buff_len / (per_ch->sb.format.channels * per_ch->sb.format.width);
+
+ *chfetcherpointer = per_ch;
+ }
+
+ real_start = *start;
+
+ sb = &per_ch->sb;
+ factor = per_sfx->format.width * per_sfx->format.channels;
+
+ // If the stream buffer can't contain that much samples anyway
+ if (nbsampleframes > sb->maxframes)
+ {
+ Con_Printf ("OGG_FetchSound: stream buffer too small (%u sample frames required)\n", nbsampleframes);
+ return NULL;
+ }
+
+ // If the data we need has already been decompressed in the sfxbuffer, just return it
+ if (per_ch->sb_offset <= real_start && per_ch->sb_offset + sb->nbframes >= real_start + nbsampleframes)
+ {
+ *start = per_ch->sb_offset;
+ return sb;
+ }
+
+ newlength = (int)(per_ch->sb_offset + sb->nbframes) - real_start;
+
+ // If we need to skip some data before decompressing the rest, or if the stream has looped
+ if (newlength < 0 || per_ch->sb_offset > real_start)
+ {
+ unsigned int time_start;
+ ogg_int64_t ogg_start;
+ int err;
+
+ if (real_start > (unsigned int)per_sfx->total_length)
+ {
+ Con_Printf ("OGG_FetchSound: asked for a start position after the end of the sfx! (%u > %u)\n",
+ real_start, per_sfx->total_length);
+ return NULL;
+ }
+
+ // We work with 200ms (1/5 sec) steps to avoid rounding errors
+ time_start = real_start * 5 / snd_renderbuffer->format.speed;
+ ogg_start = time_start * (per_sfx->format.speed / 5);
+ err = qov_pcm_seek (&per_ch->vf, ogg_start);
+ if (err != 0)
+ {
+ Con_Printf ("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n",
+ real_start, err);
+ return NULL;
+ }
+ sb->nbframes = 0;
+
+ real_start = (unsigned int) ((float)ogg_start / per_sfx->format.speed * snd_renderbuffer->format.speed);
+ if (*start - real_start + nbsampleframes > sb->maxframes)
+ {
+ Con_Printf ("OGG_FetchSound: stream buffer too small after seek (%u sample frames required)\n",
+ *start - real_start + nbsampleframes);
+ per_ch->sb_offset = real_start;
+ return NULL;
+ }
+ }
+ // Else, move forward the samples we need to keep in the sound buffer
+ else
+ {
+ memmove (sb->samples, sb->samples + (real_start - per_ch->sb_offset) * factor, newlength * factor);
+ sb->nbframes = newlength;
+ }
+
+ per_ch->sb_offset = real_start;
+
+ // We add exactly 1 sec of sound to the buffer:
+ // 1- to ensure we won't lose any sample during the resampling process
+ // 2- to force one call to OGG_FetchSound per second to regulate the workload
+ if (sb->format.speed + sb->nbframes > sb->maxframes)
+ {
+ Con_Printf ("OGG_FetchSound: stream buffer overflow (%u sample frames / %u)\n",
+ sb->format.speed + sb->nbframes, sb->maxframes);
+ return NULL;
+ }
+ newlength = per_sfx->format.speed * factor; // -> 1 sec of sound before resampling
+ if(newlength > (int)sizeof(resampling_buffer))
+ newlength = sizeof(resampling_buffer);
+
+ // Decompress in the resampling_buffer
+#if BYTE_ORDER == BIG_ENDIAN
+ bigendian = 1;
+#else
+ bigendian = 0;
+#endif
+ done = 0;
+ while ((ret = qov_read (&per_ch->vf, (char *)&resampling_buffer[done], (int)(newlength - done), bigendian, 2, 1, &per_ch->bs)) > 0)
+ done += ret;
+
+ Snd_AppendToSndBuffer (sb, resampling_buffer, (size_t)done / (size_t)factor, &per_sfx->format);
+
+ *start = per_ch->sb_offset;
+ return sb;
+}
+
+
+/*
+====================
+OGG_FetchEnd
+====================
+*/
+static void OGG_FetchEnd (void *chfetcherdata)
+{
+ ogg_stream_perchannel_t* per_ch = (ogg_stream_perchannel_t *)chfetcherdata;
+
+ if (per_ch != NULL)
+ {
+ // Free the ogg vorbis decoder
+ qov_clear (&per_ch->vf);
+
+ Mem_Free (per_ch);
+ }
+}
+
+
+/*
+====================
+OGG_FreeSfx
+====================
+*/
+static void OGG_FreeSfx (void *sfxfetcherdata)
+{
+ ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfxfetcherdata;
+
+ // Free the Ogg Vorbis file
+ Mem_Free(per_sfx->file);
+
+ // Free the stream structure
+ Mem_Free(per_sfx);
+}
+
+
+/*
+====================
+OGG_GetFormat
+====================
+*/
+static const snd_format_t* OGG_GetFormat (sfx_t* sfx)
+{
+ ogg_stream_persfx_t* per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
+ return &per_sfx->format;
+}
+
+static const snd_fetcher_t ogg_fetcher = { OGG_FetchSound, OGG_FetchEnd, OGG_FreeSfx, OGG_GetFormat };
+
+static void OGG_DecodeTags(vorbis_comment *vc, unsigned int *start, unsigned int *length, double samplesfactor, unsigned int numsamples, double *peak, double *gaindb)
+{
+ const char *startcomment = NULL, *lengthcomment = NULL, *endcomment = NULL, *thiscomment = NULL;
+
+ *start = numsamples;
+ *length = numsamples;
+ *peak = 0.0;
+ *gaindb = 0.0;
+
+ if(!vc)
+ return;
+
+ thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_PEAK", 0);
+ if(thiscomment)
+ *peak = atof(thiscomment);
+ thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_GAIN", 0);
+ if(thiscomment)
+ *gaindb = atof(thiscomment);
+
+ startcomment = qvorbis_comment_query(vc, "LOOP_START", 0); // DarkPlaces, and some Japanese app
+ if(startcomment)
+ {
+ endcomment = qvorbis_comment_query(vc, "LOOP_END", 0);
+ if(!endcomment)
+ lengthcomment = qvorbis_comment_query(vc, "LOOP_LENGTH", 0);
+ }
+ else
+ {
+ startcomment = qvorbis_comment_query(vc, "LOOPSTART", 0); // RPG Maker VX
+ if(startcomment)
+ {
+ lengthcomment = qvorbis_comment_query(vc, "LOOPLENGTH", 0);
+ if(!lengthcomment)
+ endcomment = qvorbis_comment_query(vc, "LOOPEND", 0);
+ }
+ else
+ {
+ startcomment = qvorbis_comment_query(vc, "LOOPPOINT", 0); // Sonic Robo Blast 2
+ }
+ }
+
+ if(startcomment)
+ {
+ *start = (unsigned int) bound(0, atof(startcomment) * samplesfactor, numsamples);
+ if(endcomment)
+ *length = (unsigned int) bound(0, atof(endcomment) * samplesfactor, numsamples);
+ else if(lengthcomment)
+ *length = (unsigned int) bound(0, *start + atof(lengthcomment) * samplesfactor, numsamples);
+ }
+}
+