Debian Codegraph @

code/client/snd_codec.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2005 Stuart Dalton ([email protected])
5
6 This file is part of Quake III Arena source code.
7
8 Quake III Arena source code is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 Quake III Arena source code is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Quake III Arena source code; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ===========================================================================
22 */
23
24 #include "client.h"
25 #include "snd_codec.h"
26
27 static snd_codec_t *codecs;
28
29 /*
30 =================
31 S_CodecGetSound
32
33 Opens/loads a sound, tries codec based on the sound's file extension
34 then tries all supported codecs.
35 =================
36 */
37 static void *S_CodecGetSound(const char *filename, snd_info_t *info)
38 {
39 snd_codec_t *codec;
40 snd_codec_t *orgCodec = NULL;
41 qboolean orgNameFailed = qfalse;
42 char localName[ MAX_QPATH ];
43 const char *ext;
44 char altName[ MAX_QPATH ];
45 void *rtn = NULL;
46
47 Q_strncpyz(localName, filename, MAX_QPATH);
48
49 ext = COM_GetExtension(localName);
50
51 if( *ext )
52 {
53 // Look for the correct loader and use it
54 for( codec = codecs; codec; codec = codec->next )
55 {
56 if( !Q_stricmp( ext, codec->ext ) )
57 {
58 // Load
59 if( info )
60 rtn = codec->load(localName, info);
61 else
62 rtn = codec->open(localName);
63 break;
64 }
65 }
66
67 // A loader was found
68 if( codec )
69 {
70 if( !rtn )
71 {
72 // Loader failed, most likely because the file isn't there;
73 // try again without the extension
74 orgNameFailed = qtrue;
75 orgCodec = codec;
76 COM_StripExtension( filename, localName, MAX_QPATH );
77 }
78 else
79 {
80 // Something loaded
81 return rtn;
82 }
83 }
84 }
85
86 // Try and find a suitable match using all
87 // the sound codecs supported
88 for( codec = codecs; codec; codec = codec->next )
89 {
90 if( codec == orgCodec )
91 continue;
92
93 Com_sprintf( altName, sizeof (altName), "%s.%s", localName, codec->ext );
94
95 // Load
96 if( info )
97 rtn = codec->load(altName, info);
98 else
99 rtn = codec->open(altName);
100
101 if( rtn )
102 {
103 if( orgNameFailed )
104 {
105 Com_DPrintf(S_COLOR_YELLOW "WARNING: %s not present, using %s instead\n",
106 filename, altName );
107 }
108
109 return rtn;
110 }
111 }
112
113 Com_Printf(S_COLOR_YELLOW "WARNING: Failed to %s sound %s!\n", info ? "load" : "open", filename);
114
115 return NULL;
116 }
117
118 /*
119 =================
120 S_CodecInit
121 =================
122 */
123 void S_CodecInit(void)
124 {
125 codecs = NULL;
126
127 #ifdef USE_CODEC_OPUS
128 S_CodecRegister(&opus_codec);
129 #endif
130
131 #ifdef USE_CODEC_VORBIS
132 S_CodecRegister(&ogg_codec);
133 #endif
134
135 // Register wav codec last so that it is always tried first when a file extension was not found
136 S_CodecRegister(&wav_codec);
137 }
138
139 /*
140 =================
141 S_CodecShutdown
142 =================
143 */
144 void S_CodecShutdown(void)
145 {
146 codecs = NULL;
147 }
148
149 /*
150 =================
151 S_CodecRegister
152 =================
153 */
154 void S_CodecRegister(snd_codec_t *codec)
155 {
156 codec->next = codecs;
157 codecs = codec;
158 }
159
160 /*
161 =================
162 S_CodecLoad
163 =================
164 */
165 void *S_CodecLoad(const char *filename, snd_info_t *info)
166 {
167 return S_CodecGetSound(filename, info);
168 }
169
170 /*
171 =================
172 S_CodecOpenStream
173 =================
174 */
175 snd_stream_t *S_CodecOpenStream(const char *filename)
176 {
177 return S_CodecGetSound(filename, NULL);
178 }
179
180 void S_CodecCloseStream(snd_stream_t *stream)
181 {
182 stream->codec->close(stream);
183 }
184
185 int S_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer)
186 {
187 return stream->codec->read(stream, bytes, buffer);
188 }
189
190 //=======================================================================
191 // Util functions (used by codecs)
192
193 /*
194 =================
195 S_CodecUtilOpen
196 =================
197 */
198 snd_stream_t *S_CodecUtilOpen(const char *filename, snd_codec_t *codec)
199 {
200 snd_stream_t *stream;
201 fileHandle_t hnd;
202 int length;
203
204 // Try to open the file
205 length = FS_FOpenFileRead(filename, &hnd, qtrue);
206 if(!hnd)
207 {
208 Com_DPrintf("Can't read sound file %s\n", filename);
209 return NULL;
210 }
211
212 // Allocate a stream
213 stream = Z_Malloc(sizeof(snd_stream_t));
214 if(!stream)
215 {
216 FS_FCloseFile(hnd);
217 return NULL;
218 }
219
220 // Copy over, return
221 stream->codec = codec;
222 stream->file = hnd;
223 stream->length = length;
224 return stream;
225 }
226
227 /*
228 =================
229 S_CodecUtilClose
230 =================
231 */
232 void S_CodecUtilClose(snd_stream_t **stream)
233 {
234 FS_FCloseFile((*stream)->file);
235 Z_Free(*stream);
236 *stream = NULL;
237 }