2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
34 (if "sampleframes" is 0, the function chooses the size).
37 snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer)
39 snd_ringbuffer_t *ringbuffer;
41 // If the caller provides a buffer, it must give us its size
42 if (sampleframes == 0 && buffer != NULL)
45 ringbuffer = (snd_ringbuffer_t*)Mem_Alloc(snd_mempool, sizeof (*ringbuffer));
46 memset(ringbuffer, 0, sizeof(*ringbuffer));
47 memcpy(&ringbuffer->format, format, sizeof(ringbuffer->format));
49 // If we haven't been given a buffer
52 unsigned int maxframes;
55 if (sampleframes == 0)
56 maxframes = (format->speed + 1) / 2; // Make the sound buffer large enough for containing 0.5 sec of sound
58 maxframes = sampleframes;
60 memsize = maxframes * format->width * format->channels;
61 ringbuffer->ring = (unsigned char *) Mem_Alloc(snd_mempool, memsize);
62 ringbuffer->maxframes = maxframes;
66 ringbuffer->ring = (unsigned char *) buffer;
67 ringbuffer->maxframes = sampleframes;
74 //=============================================================================
81 qboolean S_LoadSound (sfx_t *sfx, qboolean complain)
83 char namebuffer[MAX_QPATH + 16];
86 // See if already loaded
87 if (sfx->fetcher != NULL)
90 // If we weren't able to load it previously, no need to retry
91 // Note: S_PrecacheSound clears this flag to cause a retry
92 if (sfx->flags & SFXFLAG_FILEMISSING)
96 if (snd_renderbuffer == NULL)
99 // Initialize volume peak to 0; if ReplayGain is supported, the loader will change this away
100 sfx->volume_peak = 0.0;
102 if (developer_loading.integer)
103 Con_Printf("loading sound %s\n", sfx->name);
105 SCR_PushLoadingScreen(sfx->name, 1);
107 // LadyHavoc: if the sound filename does not begin with sound/, try adding it
108 if (strncasecmp(sfx->name, "sound/", 6))
110 dpsnprintf (namebuffer, sizeof(namebuffer), "sound/%s", sfx->name);
111 len = strlen(namebuffer);
112 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".wav"))
114 if (S_LoadWavFile (namebuffer, sfx))
116 memcpy (namebuffer + len - 3, "ogg", 4);
118 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".ogg"))
120 if (OGG_LoadVorbisFile (namebuffer, sfx))
125 // LadyHavoc: then try without the added sound/ as wav and ogg
126 dpsnprintf (namebuffer, sizeof(namebuffer), "%s", sfx->name);
127 len = strlen(namebuffer);
128 // request foo.wav: tries foo.wav, then foo.ogg
129 // request foo.ogg: tries foo.ogg only
130 // request foo.mod: tries foo.mod only
131 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".wav"))
133 if (S_LoadWavFile (namebuffer, sfx))
135 memcpy (namebuffer + len - 3, "ogg", 4);
137 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".ogg"))
139 if (OGG_LoadVorbisFile (namebuffer, sfx))
143 // Can't load the sound!
144 sfx->flags |= SFXFLAG_FILEMISSING;
146 Con_DPrintf("failed to load sound \"%s\"\n", sfx->name);
148 SCR_PopLoadingScreen(false);
152 SCR_PopLoadingScreen(false);