| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* |
| 8 |
* Copyright (C) 2011 by |
| 9 |
* Nicholai Main |
| 10 |
* |
| 11 |
* This program is free software; you can redistribute it and/or |
| 12 |
* modify it under the terms of the GNU General Public License |
| 13 |
* as published by the Free Software Foundation; either version 2 |
| 14 |
* of the License, or (at your option) any later version. |
| 15 |
* |
| 16 |
* This program is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with this program; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 24 |
* 02111-1307, USA. |
| 25 |
* |
| 26 |
* DESCRIPTION: |
| 27 |
* |
| 28 |
*--------------------------------------------------------------------- |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include "config.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "musicplayer.h" |
| 38 |
|
| 39 |
#ifndef HAVE_LIBVORBISFILE |
| 40 |
|
| 41 |
#include <stddef.h> |
| 42 |
|
| 43 |
static const char *vorb_name (void) |
| 44 |
{ |
| 45 |
return "vorbis player (DISABLED)"; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
static int vorb_init (int samplerate) |
| 50 |
{ |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
const music_player_t vorb_player = |
| 55 |
{ |
| 56 |
vorb_name, |
| 57 |
vorb_init, |
| 58 |
NULL, |
| 59 |
NULL, |
| 60 |
NULL, |
| 61 |
NULL, |
| 62 |
NULL, |
| 63 |
NULL, |
| 64 |
NULL, |
| 65 |
NULL, |
| 66 |
NULL |
| 67 |
}; |
| 68 |
|
| 69 |
#else // HAVE_LIBVORBISFILE |
| 70 |
|
| 71 |
|
| 72 |
#include <stdlib.h> |
| 73 |
#include <string.h> |
| 74 |
#include "lprintf.h" |
| 75 |
|
| 76 |
#include <vorbis/vorbisfile.h> |
| 77 |
#include <vorbis/codec.h> |
| 78 |
|
| 79 |
#include "i_sound.h" |
| 80 |
|
| 81 |
// uncomment to allow (experiemntal) support for |
| 82 |
// zdoom-style audio loops |
| 83 |
#define ZDOOM_AUDIO_LOOP |
| 84 |
|
| 85 |
static int vorb_looping = 0; |
| 86 |
static int vorb_volume = 0; // 0-15 |
| 87 |
static int vorb_samplerate_target = 0; |
| 88 |
static int vorb_samplerate_in = 0; |
| 89 |
static int vorb_paused = 0; |
| 90 |
static int vorb_playing = 0; |
| 91 |
|
| 92 |
#ifdef ZDOOM_AUDIO_LOOP |
| 93 |
static unsigned vorb_loop_from; |
| 94 |
static unsigned vorb_loop_to; |
| 95 |
static unsigned vorb_total_pos; |
| 96 |
#endif // ZDOOM_AUDIO_LOOP |
| 97 |
|
| 98 |
static const char *vorb_data; |
| 99 |
static size_t vorb_len; |
| 100 |
static size_t vorb_pos; |
| 101 |
|
| 102 |
OggVorbis_File vf; |
| 103 |
|
| 104 |
// io callbacks |
| 105 |
|
| 106 |
static size_t vread (void *dst, size_t s, size_t n, void *src) |
| 107 |
{ |
| 108 |
size_t size = s * n; |
| 109 |
|
| 110 |
if (vorb_pos + size >= vorb_len) |
| 111 |
size = vorb_len - vorb_pos; |
| 112 |
|
| 113 |
memcpy (dst, vorb_data + vorb_pos, size); |
| 114 |
vorb_pos += size; |
| 115 |
return size; |
| 116 |
} |
| 117 |
|
| 118 |
static int vseek (void *src, ogg_int64_t offset, int whence) |
| 119 |
{ |
| 120 |
size_t desired_pos; |
| 121 |
|
| 122 |
switch (whence) |
| 123 |
{ |
| 124 |
case SEEK_SET: |
| 125 |
desired_pos = (size_t) offset; |
| 126 |
break; |
| 127 |
case SEEK_CUR: |
| 128 |
desired_pos = vorb_pos + (size_t) offset; |
| 129 |
break; |
| 130 |
case SEEK_END: |
| 131 |
default: |
| 132 |
desired_pos = vorb_len + (size_t) offset; |
| 133 |
break; |
| 134 |
} |
| 135 |
if (desired_pos > vorb_len) // placing exactly at the end is allowed) |
| 136 |
return -1; |
| 137 |
vorb_pos = desired_pos; |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
static long vtell (void *src) |
| 142 |
{ |
| 143 |
// correct to vorbisfile spec, this is a long, not 64 bit |
| 144 |
return (long) vorb_pos; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
ov_callbacks vcallback = |
| 149 |
{ |
| 150 |
vread, |
| 151 |
vseek, |
| 152 |
NULL, |
| 153 |
vtell |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
static const char *vorb_name (void) |
| 158 |
{ |
| 159 |
return "vorbis player"; |
| 160 |
} |
| 161 |
|
| 162 |
// http://zdoom.org/wiki/Audio_loop |
| 163 |
// it's hard to accurately implement a grammar with "etc" in the spec, |
| 164 |
// so weird edge cases are likely not the same |
| 165 |
#ifdef ZDOOM_AUDIO_LOOP |
| 166 |
static unsigned parsetag (const char *str, int samplerate) |
| 167 |
{ |
| 168 |
int ret = 0; |
| 169 |
int seendot = 0; // number of dots seen so far |
| 170 |
int mult = 1; // place value of next digit out |
| 171 |
int seencolon = 0; // number of colons seen so far |
| 172 |
int digincol = 0; // number of digits in current group |
| 173 |
// (needed to track switch between colon) |
| 174 |
|
| 175 |
const char *pos = str + strlen (str) - 1; |
| 176 |
|
| 177 |
for (; pos >= str; pos--) |
| 178 |
{ |
| 179 |
if (*pos >= '0' && *pos <= '9') |
| 180 |
{ |
| 181 |
ret += (*pos - '0') * mult; |
| 182 |
mult *= 10; |
| 183 |
digincol++; |
| 184 |
} |
| 185 |
else if (*pos == '.') |
| 186 |
{ |
| 187 |
if (seencolon || seendot) |
| 188 |
return 0; |
| 189 |
seendot = 1; |
| 190 |
// convert decimal to samplerate and move on |
| 191 |
ret *= samplerate; |
| 192 |
ret /= mult; |
| 193 |
mult = samplerate; |
| 194 |
digincol = 0; |
| 195 |
} |
| 196 |
else if (*pos == ':') |
| 197 |
{ |
| 198 |
if (seencolon == 2) // no mention of anything past hours in spec |
| 199 |
return 0; |
| 200 |
seencolon++; |
| 201 |
mult *= 6; |
| 202 |
|
| 203 |
// the spec is kind of vague and says lots of things can be left out, |
| 204 |
// so constructs like mmm:ss and hh::ss are allowed |
| 205 |
while (digincol > 1) |
| 206 |
{ |
| 207 |
digincol--; |
| 208 |
mult /= 10; |
| 209 |
} |
| 210 |
while (digincol < 1) |
| 211 |
{ |
| 212 |
digincol++; |
| 213 |
mult *= 10; |
| 214 |
} |
| 215 |
digincol = 0; |
| 216 |
} |
| 217 |
else |
| 218 |
return 0; |
| 219 |
} |
| 220 |
if (seencolon && !seendot) |
| 221 |
{ // HH:MM:SS, but we never converted to samples |
| 222 |
return ret * samplerate; |
| 223 |
} |
| 224 |
// either flat pcm or :. in which case everything was converted already |
| 225 |
return ret; |
| 226 |
} |
| 227 |
#endif // ZDOOM_AUDIO_LOOP |
| 228 |
|
| 229 |
#ifdef _MSC_VER |
| 230 |
#define WIN32_LEAN_AND_MEAN |
| 231 |
#include <windows.h> |
| 232 |
#include <delayimp.h> |
| 233 |
#endif |
| 234 |
|
| 235 |
static int vorb_init (int samplerate) |
| 236 |
{ |
| 237 |
vorb_samplerate_target = samplerate; |
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
static void vorb_shutdown (void) |
| 242 |
{ |
| 243 |
// nothing to do |
| 244 |
|
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
static const void *vorb_registersong (const void *data, unsigned len) |
| 249 |
{ |
| 250 |
int i; |
| 251 |
vorbis_info *vinfo; |
| 252 |
#ifdef ZDOOM_AUDIO_LOOP |
| 253 |
vorbis_comment *vcom; |
| 254 |
#endif // ZDOOM_AUDIO_LOOP |
| 255 |
|
| 256 |
vorb_data = (const char*)data; |
| 257 |
vorb_len = len; |
| 258 |
vorb_pos = 0; |
| 259 |
|
| 260 |
// This is required to interface with vorbis (I guess) |
| 261 |
#pragma GCC diagnostic push |
| 262 |
#pragma GCC diagnostic ignored "-Wcast-qual" |
| 263 |
i = ov_test_callbacks ((void *) data, &vf, NULL, 0, vcallback); |
| 264 |
#pragma GCC diagnostic pop |
| 265 |
|
| 266 |
if (i != 0) |
| 267 |
{ |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
i = ov_test_open (&vf); |
| 271 |
|
| 272 |
if (i != 0) |
| 273 |
{ |
| 274 |
ov_clear (&vf); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
vinfo = ov_info (&vf, -1); |
| 279 |
vorb_samplerate_in = vinfo->rate; |
| 280 |
|
| 281 |
#ifdef ZDOOM_AUDIO_LOOP |
| 282 |
// parse LOOP_START LOOP_END tags |
| 283 |
vorb_loop_from = 0; |
| 284 |
vorb_loop_to = 0; |
| 285 |
|
| 286 |
vcom = ov_comment (&vf, -1); |
| 287 |
for (i = 0; i < vcom->comments; i++) |
| 288 |
{ |
| 289 |
if (strncmp ("LOOP_START=", vcom->user_comments[i], 11) == 0) |
| 290 |
vorb_loop_to = parsetag (vcom->user_comments[i] + 11, vorb_samplerate_in); |
| 291 |
else if (strncmp ("LOOP_END=", vcom->user_comments[i], 9) == 0) |
| 292 |
vorb_loop_from = parsetag (vcom->user_comments[i] + 9, vorb_samplerate_in); |
| 293 |
} |
| 294 |
if (vorb_loop_from == 0) |
| 295 |
vorb_loop_from = 0xffffffff; |
| 296 |
else if (vorb_loop_to >= vorb_loop_from) |
| 297 |
vorb_loop_to = 0; |
| 298 |
#endif // ZDOOM_AUDIO_LOOP |
| 299 |
|
| 300 |
// handle not used |
| 301 |
return data; |
| 302 |
} |
| 303 |
|
| 304 |
static void vorb_setvolume (int v) |
| 305 |
{ |
| 306 |
vorb_volume = v; |
| 307 |
} |
| 308 |
|
| 309 |
static void vorb_pause (void) |
| 310 |
{ |
| 311 |
vorb_paused = 1; |
| 312 |
} |
| 313 |
|
| 314 |
static void vorb_resume (void) |
| 315 |
{ |
| 316 |
vorb_paused = 0; |
| 317 |
} |
| 318 |
|
| 319 |
static void vorb_unregistersong (const void *handle) |
| 320 |
{ |
| 321 |
vorb_data = NULL; |
| 322 |
ov_clear (&vf); |
| 323 |
vorb_playing = 0; |
| 324 |
} |
| 325 |
|
| 326 |
static void vorb_play (const void *handle, int looping) |
| 327 |
{ |
| 328 |
ov_raw_seek_lap (&vf, 0); |
| 329 |
|
| 330 |
|
| 331 |
vorb_playing = 1; |
| 332 |
vorb_looping = looping; |
| 333 |
#ifdef ZDOOM_AUDIO_LOOP |
| 334 |
vorb_total_pos = 0; |
| 335 |
#endif // ZDOOM_AUDIO_LOOP |
| 336 |
} |
| 337 |
|
| 338 |
static void vorb_stop (void) |
| 339 |
{ |
| 340 |
vorb_playing = 0; |
| 341 |
} |
| 342 |
|
| 343 |
static void vorb_render_ex (void *dest, unsigned nsamp) |
| 344 |
{ |
| 345 |
// no workie on files that dynamically change sampling rate |
| 346 |
|
| 347 |
short *sout = (short *) dest; |
| 348 |
|
| 349 |
int bitstreamnum; // not used |
| 350 |
|
| 351 |
float **pcmdata; |
| 352 |
|
| 353 |
float multiplier; |
| 354 |
|
| 355 |
int localerrors = 0; |
| 356 |
|
| 357 |
//int nchannel; |
| 358 |
//int freq; |
| 359 |
int numread; |
| 360 |
|
| 361 |
int i; |
| 362 |
|
| 363 |
// this call needs to be moved if support for changed number |
| 364 |
// of channels in middle of file is wanted |
| 365 |
vorbis_info *vinfo = ov_info (&vf, -1); |
| 366 |
|
| 367 |
|
| 368 |
if (!vorb_playing || vorb_paused) |
| 369 |
{ |
| 370 |
memset (dest, 0, nsamp * 4); |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
while (nsamp > 0) |
| 375 |
{ |
| 376 |
#ifdef ZDOOM_AUDIO_LOOP |
| 377 |
// don't use custom loop end point when not in looping mode |
| 378 |
if (vorb_looping && vorb_total_pos + nsamp > vorb_loop_from) |
| 379 |
numread = ov_read_float (&vf, &pcmdata, vorb_loop_from - vorb_total_pos, &bitstreamnum); |
| 380 |
else |
| 381 |
#endif // ZDOOM_AUDIO_LOOP |
| 382 |
numread = ov_read_float (&vf, &pcmdata, nsamp, &bitstreamnum); |
| 383 |
if (numread == OV_HOLE) |
| 384 |
{ // recoverable error, but discontinue if we get too many |
| 385 |
localerrors++; |
| 386 |
if (localerrors == 10) |
| 387 |
{ |
| 388 |
lprintf (LO_WARN, "vorb_render: many errors. aborting\n"); |
| 389 |
vorb_playing = 0; |
| 390 |
memset (sout, 0, nsamp * 4); |
| 391 |
return; |
| 392 |
} |
| 393 |
continue; |
| 394 |
} |
| 395 |
else if (numread == 0) |
| 396 |
{ // EOF |
| 397 |
if (vorb_looping) |
| 398 |
{ |
| 399 |
#ifdef ZDOOM_AUDIO_LOOP |
| 400 |
ov_pcm_seek_lap (&vf, vorb_loop_to); |
| 401 |
vorb_total_pos = vorb_loop_to; |
| 402 |
#else // ZDOOM_AUDIO_LOOP |
| 403 |
ov_raw_seek_lap (&vf, 0); |
| 404 |
#endif // ZDOOM_AUDIO_LOOP |
| 405 |
continue; |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
vorb_playing = 0; |
| 410 |
memset (sout, 0, nsamp * 4); |
| 411 |
return; |
| 412 |
} |
| 413 |
} |
| 414 |
else if (numread < 0) |
| 415 |
{ // unrecoverable errror |
| 416 |
lprintf (LO_WARN, "vorb_render: unrecoverable error\n"); |
| 417 |
vorb_playing = 0; |
| 418 |
memset (sout, 0, nsamp * 4); |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
multiplier = 16384.0f / 15.0f * vorb_volume; |
| 423 |
// volume and downmix |
| 424 |
if (vinfo->channels == 2) |
| 425 |
{ |
| 426 |
for (i = 0; i < numread; i++, sout += 2) |
| 427 |
{ // data is preclipped? |
| 428 |
sout[0] = (short) (pcmdata[0][i] * multiplier); |
| 429 |
sout[1] = (short) (pcmdata[1][i] * multiplier); |
| 430 |
} |
| 431 |
} |
| 432 |
else // mono |
| 433 |
{ |
| 434 |
for (i = 0; i < numread; i++, sout += 2) |
| 435 |
{ |
| 436 |
sout[0] = sout [1] = (short) (pcmdata[0][i] * multiplier); |
| 437 |
} |
| 438 |
} |
| 439 |
nsamp -= numread; |
| 440 |
#ifdef ZDOOM_AUDIO_LOOP |
| 441 |
vorb_total_pos += numread; |
| 442 |
#endif // ZDOOM_AUDIO_LOOP |
| 443 |
} |
| 444 |
|
| 445 |
} |
| 446 |
|
| 447 |
static void vorb_render (void *dest, unsigned nsamp) |
| 448 |
{ |
| 449 |
I_ResampleStream (dest, nsamp, vorb_render_ex, vorb_samplerate_in, vorb_samplerate_target); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
const music_player_t vorb_player = |
| 454 |
{ |
| 455 |
vorb_name, |
| 456 |
vorb_init, |
| 457 |
vorb_shutdown, |
| 458 |
vorb_setvolume, |
| 459 |
vorb_pause, |
| 460 |
vorb_resume, |
| 461 |
vorb_registersong, |
| 462 |
vorb_unregistersong, |
| 463 |
vorb_play, |
| 464 |
vorb_stop, |
| 465 |
vorb_render |
| 466 |
}; |
| 467 |
|
| 468 |
#endif // HAVE_LIBVORBISFILE |