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.
22 #include "darkplaces.h"
31 void SCR_PushLoadingScreen (const char *, float);
32 void SCR_PopLoadingScreen (qbool);
38 If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
39 (if "sampleframes" is 0, the function chooses the size).
42 snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer)
44 snd_ringbuffer_t *ringbuffer;
46 // If the caller provides a buffer, it must give us its size
47 if (sampleframes == 0 && buffer != NULL)
50 ringbuffer = (snd_ringbuffer_t*)Mem_Alloc(snd_mempool, sizeof (*ringbuffer));
51 memset(ringbuffer, 0, sizeof(*ringbuffer));
52 memcpy(&ringbuffer->format, format, sizeof(ringbuffer->format));
54 // If we haven't been given a buffer
57 unsigned int maxframes;
60 if (sampleframes == 0)
61 maxframes = (format->speed + 1) / 2; // Make the sound buffer large enough for containing 0.5 sec of sound
63 maxframes = sampleframes;
65 memsize = maxframes * format->width * format->channels;
66 ringbuffer->ring = (unsigned char *) Mem_Alloc(snd_mempool, memsize);
67 ringbuffer->maxframes = maxframes;
71 ringbuffer->ring = (unsigned char *) buffer;
72 ringbuffer->maxframes = sampleframes;
79 //=============================================================================
86 qbool S_LoadSound (sfx_t *sfx, qbool complain)
88 char namebuffer[MAX_QPATH + 16];
91 // See if already loaded
92 if (sfx->fetcher != NULL)
95 // If we weren't able to load it previously, no need to retry
96 // Note: S_PrecacheSound clears this flag to cause a retry
97 if (sfx->flags & SFXFLAG_FILEMISSING)
101 if (snd_renderbuffer == NULL)
104 // Initialize volume peak to 0; if ReplayGain is supported, the loader will change this away
105 sfx->volume_peak = 0.0;
107 if (developer_loading.integer)
108 Con_Printf("loading sound %s\n", sfx->name);
110 SCR_PushLoadingScreen(sfx->name, 1);
112 // LadyHavoc: if the sound filename does not begin with sound/, try adding it
113 if (strncasecmp(sfx->name, "sound/", 6))
115 dpsnprintf (namebuffer, sizeof(namebuffer), "sound/%s", sfx->name);
116 len = strlen(namebuffer);
117 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".wav"))
119 if (S_LoadWavFile (namebuffer, sfx))
121 memcpy (namebuffer + len - 3, "ogg", 4);
123 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".ogg"))
125 if (OGG_LoadVorbisFile (namebuffer, sfx))
131 if (XMP_LoadModFile (namebuffer, sfx))
137 // LadyHavoc: then try without the added sound/ as wav and ogg
138 dpsnprintf (namebuffer, sizeof(namebuffer), "%s", sfx->name);
139 len = strlen(namebuffer);
140 // request foo.wav: tries foo.wav, then foo.ogg
141 // request foo.ogg: tries foo.ogg only
142 // request foo.mod: tries foo.mod only
143 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".wav"))
145 if (S_LoadWavFile (namebuffer, sfx))
147 memcpy (namebuffer + len - 3, "ogg", 4);
149 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".ogg"))
151 if (OGG_LoadVorbisFile (namebuffer, sfx))
157 if (XMP_LoadModFile (namebuffer, sfx))
162 // Can't load the sound!
163 sfx->flags |= SFXFLAG_FILEMISSING;
165 Con_Printf(CON_ERROR "Failed to load sound \"%s\"\n", sfx->name);
167 SCR_PopLoadingScreen(false);
171 SCR_PopLoadingScreen(false);