1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Audio Codecs: Adapted from ioquake3 with changes.
* For now, only handles streaming music, not sound effects.
*
* Copyright (C) 1999-2005 Id Software, Inc.
* Copyright (C) 2005 Stuart Dalton <badcdev@gmail.com>
* Copyright (C) 2010-2012 O.Sezer <sezero@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "quakedef.h"
#include "snd_codec.h"
#include "snd_codeci.h"
/* headers for individual codecs */
#include "snd_mikmod.h"
#include "snd_xmp.h"
#include "snd_umx.h"
#include "snd_wave.h"
#include "snd_flac.h"
#include "snd_mp3.h"
#include "snd_vorbis.h"
#include "snd_opus.h"
static snd_codec_t *codecs;
/*
=================
S_CodecRegister
=================
*/
static void S_CodecRegister(snd_codec_t *codec)
{
codec->next = codecs;
codecs = codec;
}
/*
=================
S_CodecInit
=================
*/
void S_CodecInit (void)
{
snd_codec_t *codec;
codecs = NULL;
/* Register in the inverse order
* of codec choice preference: */
#ifdef USE_CODEC_UMX
S_CodecRegister(&umx_codec);
#endif
#ifdef USE_CODEC_MIKMOD
S_CodecRegister(&mikmod_codec);
#endif
#ifdef USE_CODEC_XMP
S_CodecRegister(&xmp_codec);
#endif
#ifdef USE_CODEC_WAVE
S_CodecRegister(&wav_codec);
#endif
#ifdef USE_CODEC_FLAC
S_CodecRegister(&flac_codec);
#endif
#ifdef USE_CODEC_MP3
S_CodecRegister(&mp3_codec);
#endif
#ifdef USE_CODEC_VORBIS
S_CodecRegister(&vorbis_codec);
#endif
#ifdef USE_CODEC_OPUS
S_CodecRegister(&opus_codec);
#endif
codec = codecs;
while (codec)
{
codec->initialize();
codec = codec->next;
}
}
/*
=================
S_CodecShutdown
=================
*/
void S_CodecShutdown (void)
{
snd_codec_t *codec = codecs;
while (codec)
{
codec->shutdown();
codec = codec->next;
}
codecs = NULL;
}
/*
=================
S_CodecOpenStream
=================
*/
snd_stream_t *S_CodecOpenStreamType (const char *filename, unsigned int type)
{
snd_codec_t *codec;
snd_stream_t *stream;
if (type == CODECTYPE_NONE)
{
Con_Printf("Bad type for %s\n", filename);
return NULL;
}
codec = codecs;
while (codec)
{
if (type == codec->type)
break;
codec = codec->next;
}
if (!codec)
{
Con_Printf("Unknown type for %s\n", filename);
return NULL;
}
stream = S_CodecUtilOpen(filename, codec);
if (stream) {
if (codec->codec_open(stream))
stream->status = STREAM_PLAY;
else S_CodecUtilClose(&stream);
}
return stream;
}
snd_stream_t *S_CodecOpenStreamExt (const char *filename)
{
snd_codec_t *codec;
snd_stream_t *stream;
const char *ext;
ext = COM_FileGetExtension(filename);
if (! *ext)
{
Con_Printf("No extension for %s\n", filename);
return NULL;
}
codec = codecs;
while (codec)
{
if (!q_strcasecmp(ext, codec->ext))
break;
codec = codec->next;
}
if (!codec)
{
Con_Printf("Unknown extension for %s\n", filename);
return NULL;
}
stream = S_CodecUtilOpen(filename, codec);
if (stream) {
if (codec->codec_open(stream))
stream->status = STREAM_PLAY;
else S_CodecUtilClose(&stream);
}
return stream;
}
snd_stream_t *S_CodecOpenStreamAny (const char *filename)
{
snd_codec_t *codec;
snd_stream_t *stream;
const char *ext;
ext = COM_FileGetExtension(filename);
if (! *ext) /* try all available */
{
char tmp[MAX_QPATH];
codec = codecs;
while (codec)
{
q_snprintf(tmp, sizeof(tmp), "%s.%s", filename, codec->ext);
stream = S_CodecUtilOpen(tmp, codec);
if (stream) {
if (codec->codec_open(stream)) {
stream->status = STREAM_PLAY;
return stream;
}
S_CodecUtilClose(&stream);
}
codec = codec->next;
}
return NULL;
}
else /* use the name as is */
{
codec = codecs;
while (codec)
{
if (!q_strcasecmp(ext, codec->ext))
break;
codec = codec->next;
}
if (!codec)
{
Con_Printf("Unknown extension for %s\n", filename);
return NULL;
}
stream = S_CodecUtilOpen(filename, codec);
if (stream) {
if (codec->codec_open(stream))
stream->status = STREAM_PLAY;
else S_CodecUtilClose(&stream);
}
return stream;
}
}
qboolean S_CodecForwardStream (snd_stream_t *stream, unsigned int type)
{
snd_codec_t *codec = codecs;
while (codec)
{
if (type == codec->type)
break;
codec = codec->next;
}
if (!codec) return false;
stream->codec = codec;
return codec->codec_open(stream);
}
void S_CodecCloseStream (snd_stream_t *stream)
{
stream->status = STREAM_NONE;
stream->codec->codec_close(stream);
}
int S_CodecRewindStream (snd_stream_t *stream)
{
return stream->codec->codec_rewind(stream);
}
int S_CodecReadStream (snd_stream_t *stream, int bytes, void *buffer)
{
return stream->codec->codec_read(stream, bytes, buffer);
}
/* Util functions (used by codecs) */
snd_stream_t *S_CodecUtilOpen(const char *filename, snd_codec_t *codec)
{
snd_stream_t *stream;
FILE *handle;
qboolean pak;
long length;
/* Try to open the file */
length = (long) COM_FOpenFile(filename, &handle, NULL);
pak = file_from_pak;
if (length == -1)
{
Con_DPrintf("Couldn't open %s\n", filename);
return NULL;
}
/* Allocate a stream, Z_Malloc zeroes its content */
stream = (snd_stream_t *) Z_Malloc(sizeof(snd_stream_t));
stream->codec = codec;
stream->fh.file = handle;
stream->fh.start = ftell(handle);
stream->fh.pos = 0;
stream->fh.length = length;
stream->fh.pak = stream->pak = pak;
q_strlcpy(stream->name, filename, MAX_QPATH);
return stream;
}
void S_CodecUtilClose(snd_stream_t **stream)
{
fclose((*stream)->fh.file);
Z_Free(*stream);
*stream = NULL;
}
int S_CodecIsAvailable (unsigned int type)
{
snd_codec_t *codec = codecs;
while (codec)
{
if (type == codec->type)
return codec->initialized;
codec = codec->next;
}
return -1;
}
|