/project/jni/sdl_mixer/music.c

https://github.com/aichunyu/FFPlayer · C · 1356 lines · 1172 code · 90 blank · 94 comment · 251 complexity · 243ee66f52d4c023955717610f8fb149 MD5 · raw file

  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2009 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /* $Id: music.c 5247 2009-11-14 20:44:30Z slouken $ */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <assert.h>
  23. #include "SDL_endian.h"
  24. #include "SDL_audio.h"
  25. #include "SDL_timer.h"
  26. #include "SDL_mixer.h"
  27. #ifdef CMD_MUSIC
  28. #include "music_cmd.h"
  29. #endif
  30. #ifdef WAV_MUSIC
  31. #include "wavestream.h"
  32. #endif
  33. #ifdef MOD_MUSIC
  34. #include "music_mod.h"
  35. #endif
  36. #ifdef MID_MUSIC
  37. # ifdef USE_TIMIDITY_MIDI
  38. # include "timidity.h"
  39. # endif
  40. # ifdef USE_NATIVE_MIDI
  41. # include "native_midi.h"
  42. # endif
  43. # if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
  44. # define MIDI_ELSE else
  45. # else
  46. # define MIDI_ELSE
  47. # endif
  48. #endif
  49. #ifdef OGG_MUSIC
  50. #include "music_ogg.h"
  51. #endif
  52. #ifdef MP3_MUSIC
  53. #include "dynamic_mp3.h"
  54. #endif
  55. #ifdef MP3_MAD_MUSIC
  56. #include "music_mad.h"
  57. #endif
  58. #ifdef FLAC_MUSIC
  59. #include "music_flac.h"
  60. #endif
  61. #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
  62. static SDL_AudioSpec used_mixer;
  63. #endif
  64. int volatile music_active = 1;
  65. static int volatile music_stopped = 0;
  66. static int music_loops = 0;
  67. static char *music_cmd = NULL;
  68. static Mix_Music * volatile music_playing = NULL;
  69. static int music_volume = MIX_MAX_VOLUME;
  70. struct _Mix_Music {
  71. Mix_MusicType type;
  72. union {
  73. #ifdef CMD_MUSIC
  74. MusicCMD *cmd;
  75. #endif
  76. #ifdef WAV_MUSIC
  77. WAVStream *wave;
  78. #endif
  79. #ifdef MOD_MUSIC
  80. struct MODULE *module;
  81. #endif
  82. #ifdef MID_MUSIC
  83. #ifdef USE_TIMIDITY_MIDI
  84. MidiSong *midi;
  85. #endif
  86. #ifdef USE_NATIVE_MIDI
  87. NativeMidiSong *nativemidi;
  88. #endif
  89. #endif
  90. #ifdef OGG_MUSIC
  91. OGG_music *ogg;
  92. #endif
  93. #ifdef MP3_MUSIC
  94. SMPEG *mp3;
  95. #endif
  96. #ifdef MP3_MAD_MUSIC
  97. mad_data *mp3_mad;
  98. #endif
  99. #ifdef FLAC_MUSIC
  100. FLAC_music *flac;
  101. #endif
  102. } data;
  103. Mix_Fading fading;
  104. int fade_step;
  105. int fade_steps;
  106. int error;
  107. };
  108. #ifdef MID_MUSIC
  109. #ifdef USE_TIMIDITY_MIDI
  110. static int timidity_ok;
  111. static int samplesize;
  112. #endif
  113. #ifdef USE_NATIVE_MIDI
  114. static int native_midi_ok;
  115. #endif
  116. #endif
  117. /* Used to calculate fading steps */
  118. static int ms_per_step;
  119. /* rcg06042009 report available decoders at runtime. */
  120. static const char **music_decoders = NULL;
  121. static int num_decoders = 0;
  122. int Mix_GetNumMusicDecoders(void)
  123. {
  124. return(num_decoders);
  125. }
  126. const char *Mix_GetMusicDecoder(int index)
  127. {
  128. if ((index < 0) || (index >= num_decoders)) {
  129. return NULL;
  130. }
  131. return(music_decoders[index]);
  132. }
  133. static void add_music_decoder(const char *decoder)
  134. {
  135. void *ptr = realloc(music_decoders, (num_decoders + 1) * sizeof (const char **));
  136. if (ptr == NULL) {
  137. return; /* oh well, go on without it. */
  138. }
  139. music_decoders = (const char **) ptr;
  140. music_decoders[num_decoders++] = decoder;
  141. }
  142. /* Local low-level functions prototypes */
  143. static void music_internal_initialize_volume(void);
  144. static void music_internal_volume(int volume);
  145. static int music_internal_play(Mix_Music *music, double position);
  146. static int music_internal_position(double position);
  147. static int music_internal_playing();
  148. static void music_internal_halt(void);
  149. /* Support for hooking when the music has finished */
  150. static void (*music_finished_hook)(void) = NULL;
  151. void Mix_HookMusicFinished(void (*music_finished)(void))
  152. {
  153. SDL_LockAudio();
  154. music_finished_hook = music_finished;
  155. SDL_UnlockAudio();
  156. }
  157. /* If music isn't playing, halt it if no looping is required, restart it */
  158. /* otherwhise. NOP if the music is playing */
  159. static int music_halt_or_loop (void)
  160. {
  161. /* Restart music if it has to loop */
  162. if (!music_internal_playing())
  163. {
  164. /* Restart music if it has to loop at a high level */
  165. if (music_loops && --music_loops)
  166. {
  167. Mix_Fading current_fade = music_playing->fading;
  168. music_internal_play(music_playing, 0.0);
  169. music_playing->fading = current_fade;
  170. }
  171. else
  172. {
  173. music_internal_halt();
  174. if (music_finished_hook)
  175. music_finished_hook();
  176. return 0;
  177. }
  178. }
  179. return 1;
  180. }
  181. /* Mixing function */
  182. void music_mixer(void *udata, Uint8 *stream, int len)
  183. {
  184. int left = 0;
  185. if ( music_playing && music_active ) {
  186. /* Handle fading */
  187. if ( music_playing->fading != MIX_NO_FADING ) {
  188. if ( music_playing->fade_step++ < music_playing->fade_steps ) {
  189. int volume;
  190. int fade_step = music_playing->fade_step;
  191. int fade_steps = music_playing->fade_steps;
  192. if ( music_playing->fading == MIX_FADING_OUT ) {
  193. volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
  194. } else { /* Fading in */
  195. volume = (music_volume * fade_step) / fade_steps;
  196. }
  197. music_internal_volume(volume);
  198. } else {
  199. if ( music_playing->fading == MIX_FADING_OUT ) {
  200. music_internal_halt();
  201. if ( music_finished_hook ) {
  202. music_finished_hook();
  203. }
  204. return;
  205. }
  206. music_playing->fading = MIX_NO_FADING;
  207. }
  208. }
  209. if (music_halt_or_loop() == 0)
  210. return;
  211. switch (music_playing->type) {
  212. #ifdef CMD_MUSIC
  213. case MUS_CMD:
  214. /* The playing is done externally */
  215. break;
  216. #endif
  217. #ifdef WAV_MUSIC
  218. case MUS_WAV:
  219. left = WAVStream_PlaySome(stream, len);
  220. break;
  221. #endif
  222. #ifdef MOD_MUSIC
  223. case MUS_MOD:
  224. left = MOD_playAudio(music_playing->data.module, stream, len);
  225. break;
  226. #endif
  227. #ifdef MID_MUSIC
  228. #ifdef USE_TIMIDITY_MIDI
  229. case MUS_MID:
  230. if ( timidity_ok ) {
  231. int samples = len / samplesize;
  232. Timidity_PlaySome(stream, samples);
  233. }
  234. break;
  235. #endif
  236. #endif
  237. #ifdef OGG_MUSIC
  238. case MUS_OGG:
  239. left = OGG_playAudio(music_playing->data.ogg, stream, len);
  240. break;
  241. #endif
  242. #ifdef FLAC_MUSIC
  243. case MUS_FLAC:
  244. left = FLAC_playAudio(music_playing->data.flac, stream, len);
  245. break;
  246. #endif
  247. #ifdef MP3_MUSIC
  248. case MUS_MP3:
  249. left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
  250. break;
  251. #endif
  252. #ifdef MP3_MAD_MUSIC
  253. case MUS_MP3_MAD:
  254. left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
  255. break;
  256. #endif
  257. default:
  258. /* Unknown music type?? */
  259. break;
  260. }
  261. }
  262. /* Handle seamless music looping */
  263. if (left > 0 && left < len && music_halt_or_loop()) {
  264. music_mixer(udata, stream+(len-left), left);
  265. }
  266. }
  267. /* Initialize the music players with a certain desired audio format */
  268. int open_music(SDL_AudioSpec *mixer)
  269. {
  270. #ifdef WAV_MUSIC
  271. if ( WAVStream_Init(mixer) == 0 ) {
  272. add_music_decoder("WAVE");
  273. }
  274. #endif
  275. #ifdef MOD_MUSIC
  276. if ( MOD_init(mixer) == 0 ) {
  277. add_music_decoder("MIKMOD");
  278. }
  279. #endif
  280. #ifdef MID_MUSIC
  281. #ifdef USE_TIMIDITY_MIDI
  282. samplesize = mixer->size / mixer->samples;
  283. if ( Timidity_Init(mixer->freq, mixer->format,
  284. mixer->channels, mixer->samples) == 0 ) {
  285. timidity_ok = 1;
  286. add_music_decoder("TIMIDITY");
  287. } else {
  288. timidity_ok = 0;
  289. }
  290. #endif
  291. #ifdef USE_NATIVE_MIDI
  292. #ifdef USE_TIMIDITY_MIDI
  293. native_midi_ok = !timidity_ok;
  294. if ( !native_midi_ok ) {
  295. native_midi_ok = (getenv("SDL_NATIVE_MUSIC") != NULL);
  296. }
  297. if ( native_midi_ok )
  298. #endif
  299. native_midi_ok = native_midi_detect();
  300. if ( native_midi_ok )
  301. add_music_decoder("NATIVEMIDI");
  302. #endif
  303. #endif
  304. #ifdef OGG_MUSIC
  305. if ( OGG_init(mixer) == 0 ) {
  306. add_music_decoder("OGG");
  307. }
  308. #endif
  309. #ifdef FLAC_MUSIC
  310. if ( FLAC_init(mixer) == 0 ) {
  311. add_music_decoder("FLAC");
  312. }
  313. #endif
  314. #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
  315. /* Keep a copy of the mixer */
  316. used_mixer = *mixer;
  317. add_music_decoder("MP3");
  318. #endif
  319. music_playing = NULL;
  320. music_stopped = 0;
  321. Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
  322. /* Calculate the number of ms for each callback */
  323. ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
  324. return(0);
  325. }
  326. /* Portable case-insensitive string compare function */
  327. int MIX_string_equals(const char *str1, const char *str2)
  328. {
  329. while ( *str1 && *str2 ) {
  330. if ( toupper((unsigned char)*str1) !=
  331. toupper((unsigned char)*str2) )
  332. break;
  333. ++str1;
  334. ++str2;
  335. }
  336. return (!*str1 && !*str2);
  337. }
  338. /* Load a music file */
  339. Mix_Music *Mix_LoadMUS(const char *file)
  340. {
  341. FILE *fp;
  342. char *ext;
  343. Uint8 magic[5], moremagic[9];
  344. Mix_Music *music;
  345. /* Figure out what kind of file this is */
  346. fp = fopen(file, "rb");
  347. if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
  348. if ( fp != NULL ) {
  349. fclose(fp);
  350. }
  351. Mix_SetError("Couldn't read from '%s'", file);
  352. return(NULL);
  353. }
  354. if (!fread(moremagic, 8, 1, fp)) {
  355. Mix_SetError("Couldn't read from '%s'", file);
  356. return(NULL);
  357. }
  358. magic[4] = '\0';
  359. moremagic[8] = '\0';
  360. fclose(fp);
  361. /* Figure out the file extension, so we can determine the type */
  362. ext = strrchr(file, '.');
  363. if ( ext ) ++ext; /* skip the dot in the extension */
  364. /* Allocate memory for the music structure */
  365. music = (Mix_Music *)malloc(sizeof(Mix_Music));
  366. if ( music == NULL ) {
  367. Mix_SetError("Out of memory");
  368. return(NULL);
  369. }
  370. music->error = 0;
  371. #ifdef CMD_MUSIC
  372. if ( music_cmd ) {
  373. music->type = MUS_CMD;
  374. music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
  375. if ( music->data.cmd == NULL ) {
  376. music->error = 1;
  377. }
  378. } else
  379. #endif
  380. #ifdef WAV_MUSIC
  381. /* WAVE files have the magic four bytes "RIFF"
  382. AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
  383. */
  384. if ( (ext && MIX_string_equals(ext, "WAV")) ||
  385. ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
  386. (strcmp((char *)magic, "FORM") == 0) ) {
  387. music->type = MUS_WAV;
  388. music->data.wave = WAVStream_LoadSong(file, (char *)magic);
  389. if ( music->data.wave == NULL ) {
  390. Mix_SetError("Unable to load WAV file");
  391. music->error = 1;
  392. }
  393. } else
  394. #endif
  395. #ifdef MID_MUSIC
  396. /* MIDI files have the magic four bytes "MThd" */
  397. if ( (ext && MIX_string_equals(ext, "MID")) ||
  398. (ext && MIX_string_equals(ext, "MIDI")) ||
  399. strcmp((char *)magic, "MThd") == 0 ||
  400. ( strcmp((char *)magic, "RIFF") == 0 &&
  401. strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
  402. music->type = MUS_MID;
  403. #ifdef USE_NATIVE_MIDI
  404. if ( native_midi_ok ) {
  405. music->data.nativemidi = native_midi_loadsong(file);
  406. if ( music->data.nativemidi == NULL ) {
  407. Mix_SetError("%s", native_midi_error());
  408. music->error = 1;
  409. }
  410. } MIDI_ELSE
  411. #endif
  412. #ifdef USE_TIMIDITY_MIDI
  413. if ( timidity_ok ) {
  414. music->data.midi = Timidity_LoadSong(file);
  415. if ( music->data.midi == NULL ) {
  416. Mix_SetError("%s", Timidity_Error());
  417. music->error = 1;
  418. }
  419. } else {
  420. Mix_SetError("%s", Timidity_Error());
  421. music->error = 1;
  422. }
  423. #endif
  424. } else
  425. #endif
  426. #ifdef OGG_MUSIC
  427. /* Ogg Vorbis files have the magic four bytes "OggS" */
  428. if ( (ext && MIX_string_equals(ext, "OGG")) ||
  429. strcmp((char *)magic, "OggS") == 0 ) {
  430. music->type = MUS_OGG;
  431. music->data.ogg = OGG_new(file);
  432. if ( music->data.ogg == NULL ) {
  433. music->error = 1;
  434. }
  435. } else
  436. #endif
  437. #ifdef FLAC_MUSIC
  438. /* FLAC files have the magic four bytes "fLaC" */
  439. if ( (ext && MIX_string_equals(ext, "FLAC")) ||
  440. strcmp((char *)magic, "fLaC") == 0 ) {
  441. music->type = MUS_FLAC;
  442. music->data.flac = FLAC_new(file);
  443. if ( music->data.flac == NULL ) {
  444. music->error = 1;
  445. }
  446. } else
  447. #endif
  448. #ifdef MP3_MUSIC
  449. if ( (ext && MIX_string_equals(ext, "MPG")) ||
  450. (ext && MIX_string_equals(ext, "MP3")) ||
  451. (ext && MIX_string_equals(ext, "MPEG")) ||
  452. (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
  453. (strncmp((char *)magic, "ID3", 3) == 0) ) {
  454. if ( Mix_Init(MIX_INIT_MP3) ) {
  455. SMPEG_Info info;
  456. music->type = MUS_MP3;
  457. music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
  458. if ( !info.has_audio ) {
  459. Mix_SetError("MPEG file does not have any audio stream.");
  460. music->error = 1;
  461. } else {
  462. smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
  463. }
  464. } else {
  465. music->error = 1;
  466. }
  467. } else
  468. #endif
  469. #ifdef MP3_MAD_MUSIC
  470. if ( (ext && MIX_string_equals(ext, "MPG")) ||
  471. (ext && MIX_string_equals(ext, "MP3")) ||
  472. (ext && MIX_string_equals(ext, "MPEG")) ||
  473. (ext && MIX_string_equals(ext, "MAD")) ||
  474. (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
  475. (strncmp((char *)magic, "ID3", 3) == 0) ) {
  476. music->type = MUS_MP3_MAD;
  477. music->data.mp3_mad = mad_openFile(file, &used_mixer);
  478. if (music->data.mp3_mad == 0) {
  479. Mix_SetError("Could not initialize MPEG stream.");
  480. music->error = 1;
  481. }
  482. } else
  483. #endif
  484. #ifdef MOD_MUSIC
  485. if ( 1 ) {
  486. music->type = MUS_MOD;
  487. music->data.module = MOD_new(file);
  488. if ( music->data.module == NULL ) {
  489. music->error = 1;
  490. }
  491. } else
  492. #endif
  493. {
  494. Mix_SetError("Unrecognized music format");
  495. music->error = 1;
  496. }
  497. if ( music->error ) {
  498. free(music);
  499. music = NULL;
  500. }
  501. return(music);
  502. }
  503. /* Free a music chunk previously loaded */
  504. void Mix_FreeMusic(Mix_Music *music)
  505. {
  506. if ( music ) {
  507. /* Stop the music if it's currently playing */
  508. SDL_LockAudio();
  509. if ( music == music_playing ) {
  510. /* Wait for any fade out to finish */
  511. while ( music->fading == MIX_FADING_OUT ) {
  512. SDL_UnlockAudio();
  513. SDL_Delay(100);
  514. SDL_LockAudio();
  515. }
  516. if ( music == music_playing ) {
  517. music_internal_halt();
  518. }
  519. }
  520. SDL_UnlockAudio();
  521. switch (music->type) {
  522. #ifdef CMD_MUSIC
  523. case MUS_CMD:
  524. MusicCMD_FreeSong(music->data.cmd);
  525. break;
  526. #endif
  527. #ifdef WAV_MUSIC
  528. case MUS_WAV:
  529. WAVStream_FreeSong(music->data.wave);
  530. break;
  531. #endif
  532. #ifdef MOD_MUSIC
  533. case MUS_MOD:
  534. MOD_delete(music->data.module);
  535. break;
  536. #endif
  537. #ifdef MID_MUSIC
  538. case MUS_MID:
  539. #ifdef USE_NATIVE_MIDI
  540. if ( native_midi_ok ) {
  541. native_midi_freesong(music->data.nativemidi);
  542. } MIDI_ELSE
  543. #endif
  544. #ifdef USE_TIMIDITY_MIDI
  545. if ( timidity_ok ) {
  546. Timidity_FreeSong(music->data.midi);
  547. }
  548. #endif
  549. break;
  550. #endif
  551. #ifdef OGG_MUSIC
  552. case MUS_OGG:
  553. OGG_delete(music->data.ogg);
  554. break;
  555. #endif
  556. #ifdef FLAC_MUSIC
  557. case MUS_FLAC:
  558. FLAC_delete(music->data.flac);
  559. break;
  560. #endif
  561. #ifdef MP3_MUSIC
  562. case MUS_MP3:
  563. smpeg.SMPEG_delete(music->data.mp3);
  564. break;
  565. #endif
  566. #ifdef MP3_MAD_MUSIC
  567. case MUS_MP3_MAD:
  568. mad_closeFile(music->data.mp3_mad);
  569. break;
  570. #endif
  571. default:
  572. /* Unknown music type?? */
  573. break;
  574. }
  575. free(music);
  576. }
  577. }
  578. /* Find out the music format of a mixer music, or the currently playing
  579. music, if 'music' is NULL.
  580. */
  581. Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
  582. {
  583. Mix_MusicType type = MUS_NONE;
  584. if ( music ) {
  585. type = music->type;
  586. } else {
  587. SDL_LockAudio();
  588. if ( music_playing ) {
  589. type = music_playing->type;
  590. }
  591. SDL_UnlockAudio();
  592. }
  593. return(type);
  594. }
  595. /* Play a music chunk. Returns 0, or -1 if there was an error.
  596. */
  597. static int music_internal_play(Mix_Music *music, double position)
  598. {
  599. int retval = 0;
  600. /* Note the music we're playing */
  601. if ( music_playing ) {
  602. music_internal_halt();
  603. }
  604. music_playing = music;
  605. /* Set the initial volume */
  606. if ( music->type != MUS_MOD ) {
  607. music_internal_initialize_volume();
  608. }
  609. /* Set up for playback */
  610. switch (music->type) {
  611. #ifdef CMD_MUSIC
  612. case MUS_CMD:
  613. MusicCMD_Start(music->data.cmd);
  614. break;
  615. #endif
  616. #ifdef WAV_MUSIC
  617. case MUS_WAV:
  618. WAVStream_Start(music->data.wave);
  619. break;
  620. #endif
  621. #ifdef MOD_MUSIC
  622. case MUS_MOD:
  623. MOD_play(music->data.module);
  624. /* Player_SetVolume() does nothing before Player_Start() */
  625. music_internal_initialize_volume();
  626. break;
  627. #endif
  628. #ifdef MID_MUSIC
  629. case MUS_MID:
  630. #ifdef USE_NATIVE_MIDI
  631. if ( native_midi_ok ) {
  632. native_midi_start(music->data.nativemidi);
  633. } MIDI_ELSE
  634. #endif
  635. #ifdef USE_TIMIDITY_MIDI
  636. if ( timidity_ok ) {
  637. Timidity_Start(music->data.midi);
  638. }
  639. #endif
  640. break;
  641. #endif
  642. #ifdef OGG_MUSIC
  643. case MUS_OGG:
  644. OGG_play(music->data.ogg);
  645. break;
  646. #endif
  647. #ifdef FLAC_MUSIC
  648. case MUS_FLAC:
  649. FLAC_play(music->data.flac);
  650. break;
  651. #endif
  652. #ifdef MP3_MUSIC
  653. case MUS_MP3:
  654. smpeg.SMPEG_enableaudio(music->data.mp3,1);
  655. smpeg.SMPEG_enablevideo(music->data.mp3,0);
  656. smpeg.SMPEG_play(music_playing->data.mp3);
  657. break;
  658. #endif
  659. #ifdef MP3_MAD_MUSIC
  660. case MUS_MP3_MAD:
  661. mad_start(music->data.mp3_mad);
  662. break;
  663. #endif
  664. default:
  665. Mix_SetError("Can't play unknown music type");
  666. retval = -1;
  667. break;
  668. }
  669. /* Set the playback position, note any errors if an offset is used */
  670. if ( retval == 0 ) {
  671. if ( position > 0.0 ) {
  672. if ( music_internal_position(position) < 0 ) {
  673. Mix_SetError("Position not implemented for music type");
  674. retval = -1;
  675. }
  676. } else {
  677. music_internal_position(0.0);
  678. }
  679. }
  680. /* If the setup failed, we're not playing any music anymore */
  681. if ( retval < 0 ) {
  682. music_playing = NULL;
  683. }
  684. return(retval);
  685. }
  686. int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
  687. {
  688. int retval;
  689. /* Don't play null pointers :-) */
  690. if ( music == NULL ) {
  691. Mix_SetError("music parameter was NULL");
  692. return(-1);
  693. }
  694. /* Setup the data */
  695. if ( ms ) {
  696. music->fading = MIX_FADING_IN;
  697. } else {
  698. music->fading = MIX_NO_FADING;
  699. }
  700. music->fade_step = 0;
  701. music->fade_steps = ms/ms_per_step;
  702. /* Play the puppy */
  703. SDL_LockAudio();
  704. /* If the current music is fading out, wait for the fade to complete */
  705. while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
  706. SDL_UnlockAudio();
  707. SDL_Delay(100);
  708. SDL_LockAudio();
  709. }
  710. music_active = 1;
  711. music_loops = loops;
  712. retval = music_internal_play(music, position);
  713. SDL_UnlockAudio();
  714. return(retval);
  715. }
  716. int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
  717. {
  718. return Mix_FadeInMusicPos(music, loops, ms, 0.0);
  719. }
  720. int Mix_PlayMusic(Mix_Music *music, int loops)
  721. {
  722. return Mix_FadeInMusicPos(music, loops, 0, 0.0);
  723. }
  724. /* Set the playing music position */
  725. int music_internal_position(double position)
  726. {
  727. int retval = 0;
  728. switch (music_playing->type) {
  729. #ifdef MOD_MUSIC
  730. case MUS_MOD:
  731. MOD_jump_to_time(music_playing->data.module, position);
  732. break;
  733. #endif
  734. #ifdef OGG_MUSIC
  735. case MUS_OGG:
  736. OGG_jump_to_time(music_playing->data.ogg, position);
  737. break;
  738. #endif
  739. #ifdef FLAC_MUSIC
  740. case MUS_FLAC:
  741. FLAC_jump_to_time(music_playing->data.flac, position);
  742. break;
  743. #endif
  744. #ifdef MP3_MUSIC
  745. case MUS_MP3:
  746. if ( position > 0.0 ) {
  747. smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
  748. } else {
  749. smpeg.SMPEG_rewind(music_playing->data.mp3);
  750. smpeg.SMPEG_play(music_playing->data.mp3);
  751. }
  752. break;
  753. #endif
  754. #ifdef MP3_MAD_MUSIC
  755. case MUS_MP3_MAD:
  756. mad_seek(music_playing->data.mp3_mad, position);
  757. break;
  758. #endif
  759. default:
  760. /* TODO: Implement this for other music backends */
  761. retval = -1;
  762. break;
  763. }
  764. return(retval);
  765. }
  766. int Mix_SetMusicPosition(double position)
  767. {
  768. int retval;
  769. SDL_LockAudio();
  770. if ( music_playing ) {
  771. retval = music_internal_position(position);
  772. if ( retval < 0 ) {
  773. Mix_SetError("Position not implemented for music type");
  774. }
  775. } else {
  776. Mix_SetError("Music isn't playing");
  777. retval = -1;
  778. }
  779. SDL_UnlockAudio();
  780. return(retval);
  781. }
  782. /* Set the music's initial volume */
  783. static void music_internal_initialize_volume(void)
  784. {
  785. if ( music_playing->fading == MIX_FADING_IN ) {
  786. music_internal_volume(0);
  787. } else {
  788. music_internal_volume(music_volume);
  789. }
  790. }
  791. /* Set the music volume */
  792. static void music_internal_volume(int volume)
  793. {
  794. switch (music_playing->type) {
  795. #ifdef CMD_MUSIC
  796. case MUS_CMD:
  797. MusicCMD_SetVolume(volume);
  798. break;
  799. #endif
  800. #ifdef WAV_MUSIC
  801. case MUS_WAV:
  802. WAVStream_SetVolume(volume);
  803. break;
  804. #endif
  805. #ifdef MOD_MUSIC
  806. case MUS_MOD:
  807. MOD_setvolume(music_playing->data.module, volume);
  808. break;
  809. #endif
  810. #ifdef MID_MUSIC
  811. case MUS_MID:
  812. #ifdef USE_NATIVE_MIDI
  813. if ( native_midi_ok ) {
  814. native_midi_setvolume(volume);
  815. } MIDI_ELSE
  816. #endif
  817. #ifdef USE_TIMIDITY_MIDI
  818. if ( timidity_ok ) {
  819. Timidity_SetVolume(volume);
  820. }
  821. #endif
  822. break;
  823. #endif
  824. #ifdef OGG_MUSIC
  825. case MUS_OGG:
  826. OGG_setvolume(music_playing->data.ogg, volume);
  827. break;
  828. #endif
  829. #ifdef FLAC_MUSIC
  830. case MUS_FLAC:
  831. FLAC_setvolume(music_playing->data.flac, volume);
  832. break;
  833. #endif
  834. #ifdef MP3_MUSIC
  835. case MUS_MP3:
  836. smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
  837. break;
  838. #endif
  839. #ifdef MP3_MAD_MUSIC
  840. case MUS_MP3_MAD:
  841. mad_setVolume(music_playing->data.mp3_mad, volume);
  842. break;
  843. #endif
  844. default:
  845. /* Unknown music type?? */
  846. break;
  847. }
  848. }
  849. int Mix_VolumeMusic(int volume)
  850. {
  851. int prev_volume;
  852. prev_volume = music_volume;
  853. if ( volume < 0 ) {
  854. return prev_volume;
  855. }
  856. if ( volume > SDL_MIX_MAXVOLUME ) {
  857. volume = SDL_MIX_MAXVOLUME;
  858. }
  859. music_volume = volume;
  860. SDL_LockAudio();
  861. if ( music_playing ) {
  862. music_internal_volume(music_volume);
  863. }
  864. SDL_UnlockAudio();
  865. return(prev_volume);
  866. }
  867. /* Halt playing of music */
  868. static void music_internal_halt(void)
  869. {
  870. switch (music_playing->type) {
  871. #ifdef CMD_MUSIC
  872. case MUS_CMD:
  873. MusicCMD_Stop(music_playing->data.cmd);
  874. break;
  875. #endif
  876. #ifdef WAV_MUSIC
  877. case MUS_WAV:
  878. WAVStream_Stop();
  879. break;
  880. #endif
  881. #ifdef MOD_MUSIC
  882. case MUS_MOD:
  883. MOD_stop(music_playing->data.module);
  884. break;
  885. #endif
  886. #ifdef MID_MUSIC
  887. case MUS_MID:
  888. #ifdef USE_NATIVE_MIDI
  889. if ( native_midi_ok ) {
  890. native_midi_stop();
  891. } MIDI_ELSE
  892. #endif
  893. #ifdef USE_TIMIDITY_MIDI
  894. if ( timidity_ok ) {
  895. Timidity_Stop();
  896. }
  897. #endif
  898. break;
  899. #endif
  900. #ifdef OGG_MUSIC
  901. case MUS_OGG:
  902. OGG_stop(music_playing->data.ogg);
  903. break;
  904. #endif
  905. #ifdef FLAC_MUSIC
  906. case MUS_FLAC:
  907. FLAC_stop(music_playing->data.flac);
  908. break;
  909. #endif
  910. #ifdef MP3_MUSIC
  911. case MUS_MP3:
  912. smpeg.SMPEG_stop(music_playing->data.mp3);
  913. break;
  914. #endif
  915. #ifdef MP3_MAD_MUSIC
  916. case MUS_MP3_MAD:
  917. mad_stop(music_playing->data.mp3_mad);
  918. break;
  919. #endif
  920. default:
  921. /* Unknown music type?? */
  922. return;
  923. }
  924. music_playing->fading = MIX_NO_FADING;
  925. music_playing = NULL;
  926. }
  927. int Mix_HaltMusic(void)
  928. {
  929. SDL_LockAudio();
  930. if ( music_playing ) {
  931. music_internal_halt();
  932. }
  933. SDL_UnlockAudio();
  934. return(0);
  935. }
  936. /* Progressively stop the music */
  937. int Mix_FadeOutMusic(int ms)
  938. {
  939. int retval = 0;
  940. if (ms <= 0) { /* just halt immediately. */
  941. Mix_HaltMusic();
  942. return 1;
  943. }
  944. SDL_LockAudio();
  945. if ( music_playing) {
  946. int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
  947. if ( music_playing->fading == MIX_NO_FADING ) {
  948. music_playing->fade_step = 0;
  949. } else {
  950. int step;
  951. int old_fade_steps = music_playing->fade_steps;
  952. if ( music_playing->fading == MIX_FADING_OUT ) {
  953. step = music_playing->fade_step;
  954. } else {
  955. step = old_fade_steps
  956. - music_playing->fade_step + 1;
  957. }
  958. music_playing->fade_step = (step * fade_steps)
  959. / old_fade_steps;
  960. }
  961. music_playing->fading = MIX_FADING_OUT;
  962. music_playing->fade_steps = fade_steps;
  963. retval = 1;
  964. }
  965. SDL_UnlockAudio();
  966. return(retval);
  967. }
  968. Mix_Fading Mix_FadingMusic(void)
  969. {
  970. Mix_Fading fading = MIX_NO_FADING;
  971. SDL_LockAudio();
  972. if ( music_playing ) {
  973. fading = music_playing->fading;
  974. }
  975. SDL_UnlockAudio();
  976. return(fading);
  977. }
  978. /* Pause/Resume the music stream */
  979. void Mix_PauseMusic(void)
  980. {
  981. music_active = 0;
  982. }
  983. void Mix_ResumeMusic(void)
  984. {
  985. music_active = 1;
  986. }
  987. void Mix_RewindMusic(void)
  988. {
  989. Mix_SetMusicPosition(0.0);
  990. }
  991. int Mix_PausedMusic(void)
  992. {
  993. return (music_active == 0);
  994. }
  995. /* Check the status of the music */
  996. static int music_internal_playing()
  997. {
  998. int playing = 1;
  999. switch (music_playing->type) {
  1000. #ifdef CMD_MUSIC
  1001. case MUS_CMD:
  1002. if (!MusicCMD_Active(music_playing->data.cmd)) {
  1003. playing = 0;
  1004. }
  1005. break;
  1006. #endif
  1007. #ifdef WAV_MUSIC
  1008. case MUS_WAV:
  1009. if ( ! WAVStream_Active() ) {
  1010. playing = 0;
  1011. }
  1012. break;
  1013. #endif
  1014. #ifdef MOD_MUSIC
  1015. case MUS_MOD:
  1016. if ( ! MOD_playing(music_playing->data.module) ) {
  1017. playing = 0;
  1018. }
  1019. break;
  1020. #endif
  1021. #ifdef MID_MUSIC
  1022. case MUS_MID:
  1023. #ifdef USE_NATIVE_MIDI
  1024. if ( native_midi_ok ) {
  1025. if ( ! native_midi_active() )
  1026. playing = 0;
  1027. } MIDI_ELSE
  1028. #endif
  1029. #ifdef USE_TIMIDITY_MIDI
  1030. if ( timidity_ok ) {
  1031. if ( ! Timidity_Active() )
  1032. playing = 0;
  1033. }
  1034. #endif
  1035. break;
  1036. #endif
  1037. #ifdef OGG_MUSIC
  1038. case MUS_OGG:
  1039. if ( ! OGG_playing(music_playing->data.ogg) ) {
  1040. playing = 0;
  1041. }
  1042. break;
  1043. #endif
  1044. #ifdef FLAC_MUSIC
  1045. case MUS_FLAC:
  1046. if ( ! FLAC_playing(music_playing->data.flac) ) {
  1047. playing = 0;
  1048. }
  1049. break;
  1050. #endif
  1051. #ifdef MP3_MUSIC
  1052. case MUS_MP3:
  1053. if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
  1054. playing = 0;
  1055. break;
  1056. #endif
  1057. #ifdef MP3_MAD_MUSIC
  1058. case MUS_MP3_MAD:
  1059. if (!mad_isPlaying(music_playing->data.mp3_mad)) {
  1060. playing = 0;
  1061. }
  1062. break;
  1063. #endif
  1064. default:
  1065. playing = 0;
  1066. break;
  1067. }
  1068. return(playing);
  1069. }
  1070. int Mix_PlayingMusic(void)
  1071. {
  1072. int playing = 0;
  1073. SDL_LockAudio();
  1074. if ( music_playing ) {
  1075. playing = music_internal_playing();
  1076. }
  1077. SDL_UnlockAudio();
  1078. return(playing);
  1079. }
  1080. /* Set the external music playback command */
  1081. int Mix_SetMusicCMD(const char *command)
  1082. {
  1083. Mix_HaltMusic();
  1084. if ( music_cmd ) {
  1085. free(music_cmd);
  1086. music_cmd = NULL;
  1087. }
  1088. if ( command ) {
  1089. music_cmd = (char *)malloc(strlen(command)+1);
  1090. if ( music_cmd == NULL ) {
  1091. return(-1);
  1092. }
  1093. strcpy(music_cmd, command);
  1094. }
  1095. return(0);
  1096. }
  1097. int Mix_SetSynchroValue(int i)
  1098. {
  1099. /* Not supported by any players at this time */
  1100. return(-1);
  1101. }
  1102. int Mix_GetSynchroValue(void)
  1103. {
  1104. /* Not supported by any players at this time */
  1105. return(-1);
  1106. }
  1107. /* Uninitialize the music players */
  1108. void close_music(void)
  1109. {
  1110. Mix_HaltMusic();
  1111. #ifdef CMD_MUSIC
  1112. Mix_SetMusicCMD(NULL);
  1113. #endif
  1114. #ifdef MOD_MUSIC
  1115. MOD_exit();
  1116. #endif
  1117. #ifdef MID_MUSIC
  1118. # ifdef USE_TIMIDITY_MIDI
  1119. Timidity_Close();
  1120. # endif
  1121. #endif
  1122. /* rcg06042009 report available decoders at runtime. */
  1123. free(music_decoders);
  1124. music_decoders = NULL;
  1125. num_decoders = 0;
  1126. }
  1127. Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
  1128. {
  1129. Uint8 magic[5]; /*Apparently there is no way to check if the file is really a MOD,*/
  1130. /* or there are too many formats supported by MikMod or MikMod does */
  1131. /* this check by itself. If someone implements other formats (e.g. MP3) */
  1132. /* the check can be uncommented */
  1133. Uint8 moremagic[9];
  1134. Mix_Music *music;
  1135. int start;
  1136. if (!rw) {
  1137. Mix_SetError("RWops pointer is NULL");
  1138. return NULL;
  1139. }
  1140. /* Figure out what kind of file this is */
  1141. start = SDL_RWtell(rw);
  1142. if ( SDL_RWread(rw,magic,1,4) != 4 ||
  1143. SDL_RWread(rw,moremagic,1,8) != 8 ) {
  1144. Mix_SetError("Couldn't read from RWops");
  1145. return NULL;
  1146. }
  1147. SDL_RWseek(rw, start, RW_SEEK_SET);
  1148. magic[4]='\0';
  1149. moremagic[8] = '\0';
  1150. /* Allocate memory for the music structure */
  1151. music=(Mix_Music *)malloc(sizeof(Mix_Music));
  1152. if (music==NULL ) {
  1153. Mix_SetError("Out of memory");
  1154. return(NULL);
  1155. }
  1156. music->error = 0;
  1157. #ifdef WAV_MUSIC
  1158. /* WAVE files have the magic four bytes "RIFF"
  1159. AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
  1160. */
  1161. if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
  1162. (strcmp((char *)magic, "FORM") == 0) ) {
  1163. music->type = MUS_WAV;
  1164. music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic);
  1165. if ( music->data.wave == NULL ) {
  1166. music->error = 1;
  1167. }
  1168. } else
  1169. #endif
  1170. #ifdef OGG_MUSIC
  1171. /* Ogg Vorbis files have the magic four bytes "OggS" */
  1172. if ( strcmp((char *)magic, "OggS") == 0 ) {
  1173. music->type = MUS_OGG;
  1174. music->data.ogg = OGG_new_RW(rw);
  1175. if ( music->data.ogg == NULL ) {
  1176. music->error = 1;
  1177. }
  1178. } else
  1179. #endif
  1180. #ifdef FLAC_MUSIC
  1181. /* FLAC files have the magic four bytes "fLaC" */
  1182. if ( strcmp((char *)magic, "fLaC") == 0 ) {
  1183. music->type = MUS_FLAC;
  1184. music->data.flac = FLAC_new_RW(rw);
  1185. if ( music->data.flac == NULL ) {
  1186. music->error = 1;
  1187. }
  1188. } else
  1189. #endif
  1190. #ifdef MP3_MUSIC
  1191. if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
  1192. if ( Mix_Init(MIX_INIT_MP3) ) {
  1193. SMPEG_Info info;
  1194. music->type = MUS_MP3;
  1195. music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
  1196. if ( !info.has_audio ) {
  1197. Mix_SetError("MPEG file does not have any audio stream.");
  1198. music->error = 1;
  1199. } else {
  1200. smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
  1201. }
  1202. } else {
  1203. music->error = 1;
  1204. }
  1205. } else
  1206. #endif
  1207. #ifdef MP3_MAD_MUSIC
  1208. if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
  1209. music->type = MUS_MP3_MAD;
  1210. music->data.mp3_mad = mad_openFileRW(rw, &used_mixer);
  1211. if (music->data.mp3_mad == 0) {
  1212. Mix_SetError("Could not initialize MPEG stream.");
  1213. music->error = 1;
  1214. }
  1215. } else
  1216. #endif
  1217. #ifdef MID_MUSIC
  1218. /* MIDI files have the magic four bytes "MThd" */
  1219. if ( strcmp((char *)magic, "MThd") == 0 ) {
  1220. music->type = MUS_MID;
  1221. #ifdef USE_NATIVE_MIDI
  1222. if ( native_midi_ok ) {
  1223. music->data.nativemidi = native_midi_loadsong_RW(rw);
  1224. if ( music->data.nativemidi == NULL ) {
  1225. Mix_SetError("%s", native_midi_error());
  1226. music->error = 1;
  1227. }
  1228. } MIDI_ELSE
  1229. #endif
  1230. #ifdef USE_TIMIDITY_MIDI
  1231. if ( timidity_ok ) {
  1232. music->data.midi = Timidity_LoadSong_RW(rw);
  1233. if ( music->data.midi == NULL ) {
  1234. Mix_SetError("%s", Timidity_Error());
  1235. music->error = 1;
  1236. }
  1237. } else {
  1238. Mix_SetError("%s", Timidity_Error());
  1239. music->error = 1;
  1240. }
  1241. #endif
  1242. } else
  1243. #endif
  1244. #ifdef MOD_MUSIC
  1245. if (1) {
  1246. music->type=MUS_MOD;
  1247. music->data.module = MOD_new_RW(rw);
  1248. if ( music->data.module == NULL ) {
  1249. music->error = 1;
  1250. }
  1251. } else
  1252. #endif
  1253. {
  1254. Mix_SetError("Unrecognized music format");
  1255. music->error=1;
  1256. }
  1257. if (music->error) {
  1258. free(music);
  1259. music=NULL;
  1260. }
  1261. return(music);
  1262. }