DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
snd_mem.c
Go to the documentation of this file.
1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19*/
20
21
22#include "darkplaces.h"
23
24#include "snd_main.h"
25#include "snd_ogg.h"
26#include "snd_wav.h"
27#ifdef USEXMP
28#include "snd_xmp.h"
29#endif
30#include "sound.h"
31
32void SCR_PushLoadingScreen (const char *, float);
34
35/*
36====================
37Snd_CreateRingBuffer
38
39If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
40(if "sampleframes" is 0, the function chooses the size).
41====================
42*/
43snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer)
44{
45 snd_ringbuffer_t *ringbuffer;
46
47 // If the caller provides a buffer, it must give us its size
48 if (sampleframes == 0 && buffer != NULL)
49 return NULL;
50
51 ringbuffer = (snd_ringbuffer_t*)Mem_Alloc(snd_mempool, sizeof (*ringbuffer));
52 memset(ringbuffer, 0, sizeof(*ringbuffer));
53 memcpy(&ringbuffer->format, format, sizeof(ringbuffer->format));
54
55 // If we haven't been given a buffer
56 if (buffer == NULL)
57 {
58 unsigned int maxframes;
59 size_t memsize;
60
61 if (sampleframes == 0)
62 maxframes = (format->speed + 1) / 2; // Make the sound buffer large enough for containing 0.5 sec of sound
63 else
64 maxframes = sampleframes;
65
66 memsize = maxframes * format->width * format->channels;
67 ringbuffer->ring = (unsigned char *) Mem_Alloc(snd_mempool, memsize);
68 ringbuffer->maxframes = maxframes;
69 }
70 else
71 {
72 ringbuffer->ring = (unsigned char *) buffer;
73 ringbuffer->maxframes = sampleframes;
74 }
75
76 return ringbuffer;
77}
78
79
80//=============================================================================
81
82/*
83==============
84S_LoadSound
85==============
86*/
87qbool S_LoadSound (sfx_t *sfx, qbool complain)
88{
89 char namebuffer[MAX_QPATH + 16];
90 size_t len;
91
92 // See if already loaded
93 if (sfx->fetcher != NULL)
94 return true;
95
96 // If we weren't able to load it previously, no need to retry
97 // Note: S_PrecacheSound clears this flag to cause a retry
98 if (sfx->flags & SFXFLAG_FILEMISSING)
99 return false;
100
101 // No sound?
102 if (snd_renderbuffer == NULL)
103 return false;
104
105 // Initialize volume peak to 0; if ReplayGain is supported, the loader will change this away
106 sfx->volume_peak = 0.0;
107
109 Con_Printf("loading sound %s\n", sfx->name);
110
111 SCR_PushLoadingScreen(sfx->name, 1);
112
113 // LadyHavoc: if the sound filename does not begin with sound/, try adding it
114 if (strncasecmp(sfx->name, "sound/", 6))
115 {
116 dpsnprintf (namebuffer, sizeof(namebuffer), "sound/%s", sfx->name);
117 len = strlen(namebuffer);
118 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".wav"))
119 {
120 if (S_LoadWavFile (namebuffer, sfx))
121 goto loaded;
122 memcpy (namebuffer + len - 3, "ogg", 4);
123 }
124 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".ogg"))
125 {
126 if (OGG_LoadVorbisFile (namebuffer, sfx))
127 goto loaded;
128 }
129#ifdef USEXMP
130 else if (len >= 1)
131 {
132 if (XMP_LoadModFile (namebuffer, sfx))
133 goto loaded;
134 }
135#endif
136 }
137
138 // LadyHavoc: then try without the added sound/ as wav and ogg
139 dpsnprintf (namebuffer, sizeof(namebuffer), "%s", sfx->name);
140 len = strlen(namebuffer);
141 // request foo.wav: tries foo.wav, then foo.ogg
142 // request foo.ogg: tries foo.ogg only
143 // request foo.mod: tries foo.mod only
144 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".wav"))
145 {
146 if (S_LoadWavFile (namebuffer, sfx))
147 goto loaded;
148 memcpy (namebuffer + len - 3, "ogg", 4);
149 }
150 if (len >= 4 && !strcasecmp (namebuffer + len - 4, ".ogg"))
151 {
152 if (OGG_LoadVorbisFile (namebuffer, sfx))
153 goto loaded;
154 }
155#ifdef USEXMP
156 else if (len >= 1)
157 {
158 if (XMP_LoadModFile (namebuffer, sfx))
159 goto loaded;
160 }
161#endif
162
163 // Can't load the sound!
164 sfx->flags |= SFXFLAG_FILEMISSING;
165 if (complain)
166 Con_Printf(CON_ERROR "Failed to load sound \"%s\"\n", sfx->name);
167
169 return false;
170
171loaded:
173 return true;
174}
int dpsnprintf(char *buffer, size_t buffersize, const char *format,...)
Returns the number of printed characters, excluding the final '\0' or returns -1 if the buffer isn't ...
Definition common.c:997
void Con_Printf(const char *fmt,...)
Prints to all appropriate console targets.
Definition console.c:1514
#define CON_ERROR
Definition console.h:102
GLuint buffer
Definition glquake.h:630
GLint GLint GLint GLsizei GLsizei GLenum format
Definition glquake.h:649
cvar_t developer_loading
Definition host.c:52
float strlen(string s)
#define MAX_QPATH
max length of a quake game pathname
Definition qdefs.h:169
#define NULL
Definition qtypes.h:12
bool qbool
Definition qtypes.h:9
mempool_t * snd_mempool
Definition snd_main.c:144
snd_ringbuffer_t * snd_renderbuffer
Definition snd_main.c:131
#define SFXFLAG_FILEMISSING
wasn't able to load the associated sound file
Definition snd_main.h:60
snd_ringbuffer_t * Snd_CreateRingBuffer(const snd_format_t *format, unsigned int sampleframes, void *buffer)
If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself (if "sa...
Definition snd_mem.c:43
void SCR_PopLoadingScreen(qbool)
Definition cl_screen.c:1899
qbool S_LoadSound(sfx_t *sfx, qbool complain)
Definition snd_mem.c:87
void SCR_PushLoadingScreen(const char *, float)
Definition cl_screen.c:1873
qbool OGG_LoadVorbisFile(const char *filename, sfx_t *sfx)
Definition snd_ogg.c:598
qbool S_LoadWavFile(const char *filename, sfx_t *sfx)
Definition snd_wav.c:268
qbool XMP_LoadModFile(const char *filename, sfx_t *sfx)
Definition snd_xmp.c:596
int integer
Definition cvar.h:73
snd_format_t format
Definition snd_main.h:47
unsigned char * ring
Definition snd_main.h:48
unsigned int maxframes
max size (buffer size), in sample frames
Definition snd_main.h:49
#define Mem_Alloc(pool, size)
Definition zone.h:92