| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "client.h" |
| 25 |
#include "snd_codec.h" |
| 26 |
|
| 27 |
static snd_codec_t *codecs; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 54 |
for( codec = codecs; codec; codec = codec->next ) |
| 55 |
{ |
| 56 |
if( !Q_stricmp( ext, codec->ext ) ) |
| 57 |
{ |
| 58 |
|
| 59 |
if( info ) |
| 60 |
rtn = codec->load(localName, info); |
| 61 |
else |
| 62 |
rtn = codec->open(localName); |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
if( codec ) |
| 69 |
{ |
| 70 |
if( !rtn ) |
| 71 |
{ |
| 72 |
|
| 73 |
|
| 74 |
orgNameFailed = qtrue; |
| 75 |
orgCodec = codec; |
| 76 |
COM_StripExtension( filename, localName, MAX_QPATH ); |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
|
| 81 |
return rtn; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 136 |
S_CodecRegister(&wav_codec); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
void S_CodecShutdown(void) |
| 145 |
{ |
| 146 |
codecs = NULL; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
void S_CodecRegister(snd_codec_t *codec) |
| 155 |
{ |
| 156 |
codec->next = codecs; |
| 157 |
codecs = codec; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 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 |
|
| 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 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 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 |
|
| 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 |
|
| 213 |
stream = Z_Malloc(sizeof(snd_stream_t)); |
| 214 |
if(!stream) |
| 215 |
{ |
| 216 |
FS_FCloseFile(hnd); |
| 217 |
return NULL; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
stream->codec = codec; |
| 222 |
stream->file = hnd; |
| 223 |
stream->length = length; |
| 224 |
return stream; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
void S_CodecUtilClose(snd_stream_t **stream) |
| 233 |
{ |
| 234 |
FS_FCloseFile((*stream)->file); |
| 235 |
Z_Free(*stream); |
| 236 |
*stream = NULL; |
| 237 |
} |