music.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* $Id$ */
  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 MODPLUG_MUSIC
  34. #include "music_modplug.h"
  35. #endif
  36. #ifdef MOD_MUSIC
  37. #include "music_mod.h"
  38. #endif
  39. #ifdef MID_MUSIC
  40. # ifdef USE_TIMIDITY_MIDI
  41. # include "timidity.h"
  42. # endif
  43. # ifdef USE_FLUIDSYNTH_MIDI
  44. # include "fluidsynth.h"
  45. # endif
  46. # ifdef USE_NATIVE_MIDI
  47. # include "native_midi.h"
  48. # endif
  49. #endif
  50. #ifdef OGG_MUSIC
  51. #include "music_ogg.h"
  52. #endif
  53. #ifdef MP3_MUSIC
  54. #include "dynamic_mp3.h"
  55. #endif
  56. #ifdef MP3_MAD_MUSIC
  57. #include "music_mad.h"
  58. #endif
  59. #ifdef FLAC_MUSIC
  60. #include "music_flac.h"
  61. #endif
  62. #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
  63. static SDL_AudioSpec used_mixer;
  64. #endif
  65. int volatile music_active = 1;
  66. static int volatile music_stopped = 0;
  67. static int music_loops = 0;
  68. static char *music_cmd = NULL;
  69. static Mix_Music * volatile music_playing = NULL;
  70. static int music_volume = MIX_MAX_VOLUME;
  71. struct _Mix_Music {
  72. Mix_MusicType type;
  73. union {
  74. #ifdef CMD_MUSIC
  75. MusicCMD *cmd;
  76. #endif
  77. #ifdef WAV_MUSIC
  78. WAVStream *wave;
  79. #endif
  80. #ifdef MODPLUG_MUSIC
  81. modplug_data *modplug;
  82. #endif
  83. #ifdef MOD_MUSIC
  84. struct MODULE *module;
  85. #endif
  86. #ifdef MID_MUSIC
  87. #ifdef USE_TIMIDITY_MIDI
  88. MidiSong *midi;
  89. #endif
  90. #ifdef USE_FLUIDSYNTH_MIDI
  91. FluidSynthMidiSong *fluidsynthmidi;
  92. #endif
  93. #ifdef USE_NATIVE_MIDI
  94. NativeMidiSong *nativemidi;
  95. #endif
  96. #endif
  97. #ifdef OGG_MUSIC
  98. OGG_music *ogg;
  99. #endif
  100. #ifdef MP3_MUSIC
  101. SMPEG *mp3;
  102. #endif
  103. #ifdef MP3_MAD_MUSIC
  104. mad_data *mp3_mad;
  105. #endif
  106. #ifdef FLAC_MUSIC
  107. FLAC_music *flac;
  108. #endif
  109. void *mark_sibly_dummy; //MSVC complains if union has no members...
  110. } data;
  111. Mix_Fading fading;
  112. int fade_step;
  113. int fade_steps;
  114. int error;
  115. };
  116. #ifdef MID_MUSIC
  117. #ifdef USE_TIMIDITY_MIDI
  118. static int timidity_ok;
  119. static int samplesize;
  120. #endif
  121. #ifdef USE_FLUIDSYNTH_MIDI
  122. static int fluidsynth_ok;
  123. #endif
  124. #ifdef USE_NATIVE_MIDI
  125. static int native_midi_ok;
  126. #endif
  127. #endif
  128. /* Used to calculate fading steps */
  129. static int ms_per_step;
  130. /* rcg06042009 report available decoders at runtime. */
  131. static const char **music_decoders = NULL;
  132. static int num_decoders = 0;
  133. /* Semicolon-separated SoundFont paths */
  134. #ifdef MID_MUSIC
  135. char* soundfont_paths = NULL;
  136. #endif
  137. int Mix_GetNumMusicDecoders(void)
  138. {
  139. return(num_decoders);
  140. }
  141. const char *Mix_GetMusicDecoder(int index)
  142. {
  143. if ((index < 0) || (index >= num_decoders)) {
  144. return NULL;
  145. }
  146. return(music_decoders[index]);
  147. }
  148. static void add_music_decoder(const char *decoder)
  149. {
  150. void *ptr = SDL_realloc((void *)music_decoders, (num_decoders + 1) * sizeof (const char *));
  151. if (ptr == NULL) {
  152. return; /* oh well, go on without it. */
  153. }
  154. music_decoders = (const char **) ptr;
  155. music_decoders[num_decoders++] = decoder;
  156. }
  157. /* Local low-level functions prototypes */
  158. static void music_internal_initialize_volume(void);
  159. static void music_internal_volume(int volume);
  160. static int music_internal_play(Mix_Music *music, double position);
  161. static int music_internal_position(double position);
  162. static int music_internal_playing();
  163. static void music_internal_halt(void);
  164. /* Support for hooking when the music has finished */
  165. static void (*music_finished_hook)(void) = NULL;
  166. void Mix_HookMusicFinished(void (*music_finished)(void))
  167. {
  168. SDL_LockAudio();
  169. music_finished_hook = music_finished;
  170. SDL_UnlockAudio();
  171. }
  172. /* If music isn't playing, halt it if no looping is required, restart it */
  173. /* othesrchise. NOP if the music is playing */
  174. static int music_halt_or_loop (void)
  175. {
  176. /* Restart music if it has to loop */
  177. if (!music_internal_playing())
  178. {
  179. #ifdef USE_NATIVE_MIDI
  180. /* Native MIDI handles looping internally */
  181. if (music_playing->type == MUS_MID && native_midi_ok) {
  182. music_loops = 0;
  183. }
  184. #endif
  185. /* Restart music if it has to loop at a high level */
  186. if (music_loops)
  187. {
  188. Mix_Fading current_fade;
  189. if (music_loops > 0) {
  190. --music_loops;
  191. }
  192. current_fade = music_playing->fading;
  193. music_internal_play(music_playing, 0.0);
  194. music_playing->fading = current_fade;
  195. }
  196. else
  197. {
  198. music_internal_halt();
  199. if (music_finished_hook)
  200. music_finished_hook();
  201. return 0;
  202. }
  203. }
  204. return 1;
  205. }
  206. /* Mixing function */
  207. void music_mixer(void *udata, Uint8 *stream, int len)
  208. {
  209. int left = 0;
  210. if ( music_playing && music_active ) {
  211. /* Handle fading */
  212. if ( music_playing->fading != MIX_NO_FADING ) {
  213. if ( music_playing->fade_step++ < music_playing->fade_steps ) {
  214. int volume;
  215. int fade_step = music_playing->fade_step;
  216. int fade_steps = music_playing->fade_steps;
  217. if ( music_playing->fading == MIX_FADING_OUT ) {
  218. volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
  219. } else { /* Fading in */
  220. volume = (music_volume * fade_step) / fade_steps;
  221. }
  222. music_internal_volume(volume);
  223. } else {
  224. if ( music_playing->fading == MIX_FADING_OUT ) {
  225. music_internal_halt();
  226. if ( music_finished_hook ) {
  227. music_finished_hook();
  228. }
  229. return;
  230. }
  231. music_playing->fading = MIX_NO_FADING;
  232. }
  233. }
  234. music_halt_or_loop();
  235. if (!music_internal_playing())
  236. return;
  237. switch (music_playing->type) {
  238. #ifdef CMD_MUSIC
  239. case MUS_CMD:
  240. /* The playing is done externally */
  241. break;
  242. #endif
  243. #ifdef WAV_MUSIC
  244. case MUS_WAV:
  245. left = WAVStream_PlaySome(stream, len);
  246. break;
  247. #endif
  248. #ifdef MODPLUG_MUSIC
  249. case MUS_MODPLUG:
  250. left = modplug_playAudio(music_playing->data.modplug, stream, len);
  251. break;
  252. #endif
  253. #ifdef MOD_MUSIC
  254. case MUS_MOD:
  255. left = MOD_playAudio(music_playing->data.module, stream, len);
  256. break;
  257. #endif
  258. #ifdef MID_MUSIC
  259. case MUS_MID:
  260. #ifdef USE_NATIVE_MIDI
  261. if ( native_midi_ok ) {
  262. /* Native midi is handled asynchronously */
  263. goto skip;
  264. }
  265. #endif
  266. #ifdef USE_FLUIDSYNTH_MIDI
  267. if ( fluidsynth_ok ) {
  268. fluidsynth_playsome(music_playing->data.fluidsynthmidi, stream, len);
  269. goto skip;
  270. }
  271. #endif
  272. #ifdef USE_TIMIDITY_MIDI
  273. if ( timidity_ok ) {
  274. int samples = len / samplesize;
  275. Timidity_PlaySome(stream, samples);
  276. goto skip;
  277. }
  278. #endif
  279. break;
  280. #endif
  281. #ifdef OGG_MUSIC
  282. case MUS_OGG:
  283. left = OGG_playAudio(music_playing->data.ogg, stream, len);
  284. break;
  285. #endif
  286. #ifdef FLAC_MUSIC
  287. case MUS_FLAC:
  288. left = FLAC_playAudio(music_playing->data.flac, stream, len);
  289. break;
  290. #endif
  291. #ifdef MP3_MUSIC
  292. case MUS_MP3:
  293. left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
  294. break;
  295. #endif
  296. #ifdef MP3_MAD_MUSIC
  297. case MUS_MP3_MAD:
  298. left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
  299. break;
  300. #endif
  301. default:
  302. /* Unknown music type?? */
  303. break;
  304. }
  305. }
  306. skip:
  307. /* Handle seamless music looping */
  308. if (left > 0 && left < len) {
  309. music_halt_or_loop();
  310. if (music_internal_playing())
  311. music_mixer(udata, stream+(len-left), left);
  312. }
  313. }
  314. /* Initialize the music players with a certain desired audio format */
  315. int open_music(SDL_AudioSpec *mixer)
  316. {
  317. #ifdef WAV_MUSIC
  318. if ( WAVStream_Init(mixer) == 0 ) {
  319. add_music_decoder("WAVE");
  320. }
  321. #endif
  322. #ifdef MODPLUG_MUSIC
  323. if ( modplug_init(mixer) == 0 ) {
  324. add_music_decoder("MODPLUG");
  325. }
  326. #endif
  327. #ifdef MOD_MUSIC
  328. if ( MOD_init(mixer) == 0 ) {
  329. add_music_decoder("MIKMOD");
  330. }
  331. #endif
  332. #ifdef MID_MUSIC
  333. #ifdef USE_TIMIDITY_MIDI
  334. samplesize = mixer->size / mixer->samples;
  335. if ( Timidity_Init(mixer->freq, mixer->format,
  336. mixer->channels, mixer->samples) == 0 ) {
  337. timidity_ok = 1;
  338. add_music_decoder("TIMIDITY");
  339. } else {
  340. timidity_ok = 0;
  341. }
  342. #endif
  343. #ifdef USE_FLUIDSYNTH_MIDI
  344. if ( fluidsynth_init(mixer) == 0 ) {
  345. fluidsynth_ok = 1;
  346. add_music_decoder("FLUIDSYNTH");
  347. } else {
  348. fluidsynth_ok = 0;
  349. }
  350. #endif
  351. #ifdef USE_NATIVE_MIDI
  352. #ifdef USE_FLUIDSYNTH_MIDI
  353. native_midi_ok = !fluidsynth_ok;
  354. if ( native_midi_ok )
  355. #endif
  356. #ifdef USE_TIMIDITY_MIDI
  357. native_midi_ok = !timidity_ok;
  358. if ( !native_midi_ok ) {
  359. native_midi_ok = (getenv("SDL_NATIVE_MUSIC") != NULL);
  360. }
  361. if ( native_midi_ok )
  362. #endif
  363. native_midi_ok = native_midi_detect();
  364. if ( native_midi_ok )
  365. add_music_decoder("NATIVEMIDI");
  366. #endif
  367. #endif
  368. #ifdef OGG_MUSIC
  369. if ( OGG_init(mixer) == 0 ) {
  370. add_music_decoder("OGG");
  371. }
  372. #endif
  373. #ifdef FLAC_MUSIC
  374. if ( FLAC_init(mixer) == 0 ) {
  375. add_music_decoder("FLAC");
  376. }
  377. #endif
  378. #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
  379. /* Keep a copy of the mixer */
  380. used_mixer = *mixer;
  381. add_music_decoder("MP3");
  382. #endif
  383. music_playing = NULL;
  384. music_stopped = 0;
  385. Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
  386. /* Calculate the number of ms for each callback */
  387. ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
  388. return(0);
  389. }
  390. /* Portable case-insensitive string compare function */
  391. int MIX_string_equals(const char *str1, const char *str2)
  392. {
  393. while ( *str1 && *str2 ) {
  394. if ( toupper((unsigned char)*str1) !=
  395. toupper((unsigned char)*str2) )
  396. break;
  397. ++str1;
  398. ++str2;
  399. }
  400. return (!*str1 && !*str2);
  401. }
  402. static int detect_mp3(Uint8 *magic)
  403. {
  404. if ( strncmp((char *)magic, "ID3", 3) == 0 ) {
  405. return 1;
  406. }
  407. /* Detection code lifted from SMPEG */
  408. if(((magic[0] & 0xff) != 0xff) || // No sync bits
  409. ((magic[1] & 0xf0) != 0xf0) || //
  410. ((magic[2] & 0xf0) == 0x00) || // Bitrate is 0
  411. ((magic[2] & 0xf0) == 0xf0) || // Bitrate is 15
  412. ((magic[2] & 0x0c) == 0x0c) || // Frequency is 3
  413. ((magic[1] & 0x06) == 0x00)) { // Layer is 4
  414. return(0);
  415. }
  416. return 1;
  417. }
  418. /* MUS_MOD can't be auto-detected. If no other format was detected, MOD is
  419. * assumed and MUS_MOD will be returned, meaning that the format might not
  420. * actually be MOD-based.
  421. *
  422. * Returns MUS_NONE in case of errors. */
  423. static Mix_MusicType detect_music_type(SDL_RWops *src)
  424. {
  425. Uint8 magic[5];
  426. Uint8 moremagic[9];
  427. Sint64 start = SDL_RWtell(src);
  428. if (SDL_RWread(src, magic, 1, 4) != 4 || SDL_RWread(src, moremagic, 1, 8) != 8 ) {
  429. Mix_SetError("Couldn't read from RWops");
  430. return MUS_NONE;
  431. }
  432. SDL_RWseek(src, start, RW_SEEK_SET);
  433. magic[4]='\0';
  434. moremagic[8] = '\0';
  435. /* WAVE files have the magic four bytes "RIFF"
  436. AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
  437. if (((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
  438. (strcmp((char *)magic, "FORM") == 0)) {
  439. return MUS_WAV;
  440. }
  441. /* Ogg Vorbis files have the magic four bytes "OggS" */
  442. if (strcmp((char *)magic, "OggS") == 0) {
  443. return MUS_OGG;
  444. }
  445. /* FLAC files have the magic four bytes "fLaC" */
  446. if (strcmp((char *)magic, "fLaC") == 0) {
  447. return MUS_FLAC;
  448. }
  449. /* MIDI files have the magic four bytes "MThd" */
  450. if (strcmp((char *)magic, "MThd") == 0) {
  451. return MUS_MID;
  452. }
  453. if (detect_mp3(magic)) {
  454. return MUS_MP3;
  455. }
  456. /* Assume MOD format.
  457. *
  458. * Apparently there is no way to check if the file is really a MOD,
  459. * or there are too many formats supported by MikMod/ModPlug, or
  460. * MikMod/ModPlug does this check by itself. */
  461. return MUS_MOD;
  462. }
  463. /* Load a music file */
  464. Mix_Music *Mix_LoadMUS(const char *file)
  465. {
  466. SDL_RWops *src;
  467. Mix_Music *music;
  468. Mix_MusicType type;
  469. char *ext = strrchr(file, '.');
  470. #ifdef CMD_MUSIC
  471. if ( music_cmd ) {
  472. /* Allocate memory for the music structure */
  473. music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
  474. if ( music == NULL ) {
  475. Mix_SetError("Out of memory");
  476. return(NULL);
  477. }
  478. music->error = 0;
  479. music->type = MUS_CMD;
  480. music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
  481. if ( music->data.cmd == NULL ) {
  482. SDL_free(music);
  483. music = NULL;
  484. }
  485. return music;
  486. }
  487. #endif
  488. src = SDL_RWFromFile(file, "rb");
  489. if ( src == NULL ) {
  490. Mix_SetError("Couldn't open '%s'", file);
  491. return NULL;
  492. }
  493. /* Use the extension as a first guess on the file type */
  494. type = MUS_NONE;
  495. ext = strrchr(file, '.');
  496. /* No need to guard these with #ifdef *_MUSIC stuff,
  497. * since we simply call Mix_LoadMUSType_RW() later */
  498. if ( ext ) {
  499. ++ext; /* skip the dot in the extension */
  500. if ( MIX_string_equals(ext, "WAV") ) {
  501. type = MUS_WAV;
  502. } else if ( MIX_string_equals(ext, "MID") ||
  503. MIX_string_equals(ext, "MIDI") ||
  504. MIX_string_equals(ext, "KAR") ) {
  505. type = MUS_MID;
  506. } else if ( MIX_string_equals(ext, "OGG") ) {
  507. type = MUS_OGG;
  508. } else if ( MIX_string_equals(ext, "FLAC") ) {
  509. type = MUS_FLAC;
  510. } else if ( MIX_string_equals(ext, "MPG") ||
  511. MIX_string_equals(ext, "MPEG") ||
  512. MIX_string_equals(ext, "MP3") ||
  513. MIX_string_equals(ext, "MAD") ) {
  514. type = MUS_MP3;
  515. }
  516. }
  517. if ( type == MUS_NONE ) {
  518. type = detect_music_type(src);
  519. }
  520. /* We need to know if a specific error occurs; if not, we'll set a
  521. * generic one, so we clear the current one. */
  522. SDL_ClearError();
  523. music = Mix_LoadMUSType_RW(src, type, SDL_TRUE);
  524. if ( music == NULL && Mix_GetError()[0] == '\0' ) {
  525. Mix_SetError("Unrecognized music format");
  526. }
  527. return music;
  528. }
  529. Mix_Music *Mix_LoadMUS_RW(SDL_RWops *src, int freesrc)
  530. {
  531. return Mix_LoadMUSType_RW(src, MUS_NONE, freesrc);
  532. }
  533. Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
  534. {
  535. Mix_Music *music;
  536. Sint64 start;
  537. if (!src) {
  538. Mix_SetError("RWops pointer is NULL");
  539. return NULL;
  540. }
  541. start = SDL_RWtell(src);
  542. /* If the caller wants auto-detection, figure out what kind of file
  543. * this is. */
  544. if (type == MUS_NONE) {
  545. if ((type = detect_music_type(src)) == MUS_NONE) {
  546. /* Don't call Mix_SetError() here since detect_music_type()
  547. * does that. */
  548. if (freesrc) {
  549. SDL_RWclose(src);
  550. }
  551. return NULL;
  552. }
  553. }
  554. /* Allocate memory for the music structure */
  555. music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
  556. if (music == NULL ) {
  557. Mix_SetError("Out of memory");
  558. if (freesrc) {
  559. SDL_RWclose(src);
  560. }
  561. return NULL;
  562. }
  563. music->error = 1;
  564. switch (type) {
  565. #ifdef WAV_MUSIC
  566. case MUS_WAV:
  567. music->type = MUS_WAV;
  568. music->data.wave = WAVStream_LoadSong_RW(src, freesrc);
  569. if (music->data.wave) {
  570. music->error = 0;
  571. }
  572. break;
  573. #endif
  574. #ifdef OGG_MUSIC
  575. case MUS_OGG:
  576. music->type = MUS_OGG;
  577. music->data.ogg = OGG_new_RW(src, freesrc);
  578. if (music->data.ogg) {
  579. music->error = 0;
  580. }
  581. break;
  582. #endif
  583. #ifdef FLAC_MUSIC
  584. case MUS_FLAC:
  585. music->type = MUS_FLAC;
  586. music->data.flac = FLAC_new_RW(src, freesrc);
  587. if (music->data.flac) {
  588. music->error = 0;
  589. }
  590. break;
  591. #endif
  592. #ifdef MP3_MUSIC
  593. case MUS_MP3:
  594. if (Mix_Init(MIX_INIT_MP3)) {
  595. SMPEG_Info info;
  596. music->type = MUS_MP3;
  597. music->data.mp3 = smpeg.SMPEG_new_rwops(src, &info, freesrc, 0);
  598. if (!info.has_audio) {
  599. Mix_SetError("MPEG file does not have any audio stream.");
  600. smpeg.SMPEG_delete(music->data.mp3);
  601. /* Deleting the MP3 closed the source if desired */
  602. freesrc = SDL_FALSE;
  603. } else {
  604. smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
  605. music->error = 0;
  606. }
  607. }
  608. break;
  609. #elif defined(MP3_MAD_MUSIC)
  610. case MUS_MP3:
  611. music->type = MUS_MP3_MAD;
  612. music->data.mp3_mad = mad_openFileRW(src, &used_mixer, freesrc);
  613. if (music->data.mp3_mad) {
  614. music->error = 0;
  615. } else {
  616. Mix_SetError("Could not initialize MPEG stream.");
  617. }
  618. break;
  619. #endif
  620. #ifdef MID_MUSIC
  621. case MUS_MID:
  622. music->type = MUS_MID;
  623. #ifdef USE_NATIVE_MIDI
  624. if (native_midi_ok) {
  625. SDL_RWseek(src, start, RW_SEEK_SET);
  626. music->data.nativemidi = native_midi_loadsong_RW(src, freesrc);
  627. if (music->data.nativemidi) {
  628. music->error = 0;
  629. } else {
  630. Mix_SetError("%s", native_midi_error());
  631. }
  632. break;
  633. }
  634. #endif
  635. #ifdef USE_FLUIDSYNTH_MIDI
  636. if (fluidsynth_ok) {
  637. SDL_RWseek(src, start, RW_SEEK_SET);
  638. music->data.fluidsynthmidi = fluidsynth_loadsong_RW(src, freesrc);
  639. if (music->data.fluidsynthmidi) {
  640. music->error = 0;
  641. }
  642. break;
  643. }
  644. #endif
  645. #ifdef USE_TIMIDITY_MIDI
  646. if (timidity_ok) {
  647. SDL_RWseek(src, start, RW_SEEK_SET);
  648. music->data.midi = Timidity_LoadSong_RW(src, freesrc);
  649. if (music->data.midi) {
  650. music->error = 0;
  651. } else {
  652. Mix_SetError("%s", Timidity_Error());
  653. }
  654. } else {
  655. Mix_SetError("%s", Timidity_Error());
  656. }
  657. #endif
  658. break;
  659. #endif
  660. #if defined(MODPLUG_MUSIC) || defined(MOD_MUSIC)
  661. case MUS_MOD:
  662. #ifdef MODPLUG_MUSIC
  663. if (music->error) {
  664. SDL_RWseek(src, start, RW_SEEK_SET);
  665. music->type = MUS_MODPLUG;
  666. music->data.modplug = modplug_new_RW(src, freesrc);
  667. if (music->data.modplug) {
  668. music->error = 0;
  669. }
  670. }
  671. #endif
  672. #ifdef MOD_MUSIC
  673. if (music->error) {
  674. SDL_RWseek(src, start, RW_SEEK_SET);
  675. music->type = MUS_MOD;
  676. music->data.module = MOD_new_RW(src, freesrc);
  677. if (music->data.module) {
  678. music->error = 0;
  679. }
  680. }
  681. #endif
  682. break;
  683. #endif
  684. default:
  685. Mix_SetError("Unrecognized music format");
  686. break;
  687. } /* switch (want) */
  688. if (music->error) {
  689. SDL_free(music);
  690. if (freesrc) {
  691. SDL_RWclose(src);
  692. } else {
  693. SDL_RWseek(src, start, RW_SEEK_SET);
  694. }
  695. music = NULL;
  696. }
  697. return music;
  698. }
  699. /* Free a music chunk previously loaded */
  700. void Mix_FreeMusic(Mix_Music *music)
  701. {
  702. if ( music ) {
  703. /* Stop the music if it's currently playing */
  704. SDL_LockAudio();
  705. if ( music == music_playing ) {
  706. /* Wait for any fade out to finish */
  707. while ( music->fading == MIX_FADING_OUT ) {
  708. SDL_UnlockAudio();
  709. SDL_Delay(100);
  710. SDL_LockAudio();
  711. }
  712. if ( music == music_playing ) {
  713. music_internal_halt();
  714. }
  715. }
  716. SDL_UnlockAudio();
  717. switch (music->type) {
  718. #ifdef CMD_MUSIC
  719. case MUS_CMD:
  720. MusicCMD_FreeSong(music->data.cmd);
  721. break;
  722. #endif
  723. #ifdef WAV_MUSIC
  724. case MUS_WAV:
  725. WAVStream_FreeSong(music->data.wave);
  726. break;
  727. #endif
  728. #ifdef MODPLUG_MUSIC
  729. case MUS_MODPLUG:
  730. modplug_delete(music->data.modplug);
  731. break;
  732. #endif
  733. #ifdef MOD_MUSIC
  734. case MUS_MOD:
  735. MOD_delete(music->data.module);
  736. break;
  737. #endif
  738. #ifdef MID_MUSIC
  739. case MUS_MID:
  740. #ifdef USE_NATIVE_MIDI
  741. if ( native_midi_ok ) {
  742. native_midi_freesong(music->data.nativemidi);
  743. goto skip;
  744. }
  745. #endif
  746. #ifdef USE_FLUIDSYNTH_MIDI
  747. if ( fluidsynth_ok ) {
  748. fluidsynth_freesong(music->data.fluidsynthmidi);
  749. goto skip;
  750. }
  751. #endif
  752. #ifdef USE_TIMIDITY_MIDI
  753. if ( timidity_ok ) {
  754. Timidity_FreeSong(music->data.midi);
  755. goto skip;
  756. }
  757. #endif
  758. break;
  759. #endif
  760. #ifdef OGG_MUSIC
  761. case MUS_OGG:
  762. OGG_delete(music->data.ogg);
  763. break;
  764. #endif
  765. #ifdef FLAC_MUSIC
  766. case MUS_FLAC:
  767. FLAC_delete(music->data.flac);
  768. break;
  769. #endif
  770. #ifdef MP3_MUSIC
  771. case MUS_MP3:
  772. smpeg.SMPEG_delete(music->data.mp3);
  773. break;
  774. #endif
  775. #ifdef MP3_MAD_MUSIC
  776. case MUS_MP3_MAD:
  777. mad_closeFile(music->data.mp3_mad);
  778. break;
  779. #endif
  780. default:
  781. /* Unknown music type?? */
  782. break;
  783. }
  784. skip:
  785. SDL_free(music);
  786. }
  787. }
  788. /* Find out the music format of a mixer music, or the currently playing
  789. music, if 'music' is NULL.
  790. */
  791. Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
  792. {
  793. Mix_MusicType type = MUS_NONE;
  794. if ( music ) {
  795. type = music->type;
  796. } else {
  797. SDL_LockAudio();
  798. if ( music_playing ) {
  799. type = music_playing->type;
  800. }
  801. SDL_UnlockAudio();
  802. }
  803. return(type);
  804. }
  805. /* Play a music chunk. Returns 0, or -1 if there was an error.
  806. */
  807. static int music_internal_play(Mix_Music *music, double position)
  808. {
  809. int retval = 0;
  810. #if defined(__MACOSX__) && defined(USE_NATIVE_MIDI)
  811. /* This fixes a bug with native MIDI on Mac OS X, where you
  812. can't really stop and restart MIDI from the audio callback.
  813. */
  814. if ( music == music_playing && music->type == MUS_MID && native_midi_ok ) {
  815. /* Just a seek suffices to restart playing */
  816. music_internal_position(position);
  817. return 0;
  818. }
  819. #endif
  820. /* Note the music we're playing */
  821. if ( music_playing ) {
  822. music_internal_halt();
  823. }
  824. music_playing = music;
  825. /* Set the initial volume */
  826. if ( music->type != MUS_MOD ) {
  827. music_internal_initialize_volume();
  828. }
  829. /* Set up for playback */
  830. switch (music->type) {
  831. #ifdef CMD_MUSIC
  832. case MUS_CMD:
  833. MusicCMD_Start(music->data.cmd);
  834. break;
  835. #endif
  836. #ifdef WAV_MUSIC
  837. case MUS_WAV:
  838. WAVStream_Start(music->data.wave);
  839. break;
  840. #endif
  841. #ifdef MODPLUG_MUSIC
  842. case MUS_MODPLUG:
  843. /* can't set volume until file is loaded, so finally set it now */
  844. music_internal_initialize_volume();
  845. modplug_play(music->data.modplug);
  846. break;
  847. #endif
  848. #ifdef MOD_MUSIC
  849. case MUS_MOD:
  850. MOD_play(music->data.module);
  851. /* Player_SetVolume() does nothing before Player_Start() */
  852. music_internal_initialize_volume();
  853. break;
  854. #endif
  855. #ifdef MID_MUSIC
  856. case MUS_MID:
  857. #ifdef USE_NATIVE_MIDI
  858. if ( native_midi_ok ) {
  859. native_midi_start(music->data.nativemidi, music_loops);
  860. goto skip;
  861. }
  862. #endif
  863. #ifdef USE_FLUIDSYNTH_MIDI
  864. if (fluidsynth_ok ) {
  865. fluidsynth_start(music->data.fluidsynthmidi);
  866. goto skip;
  867. }
  868. #endif
  869. #ifdef USE_TIMIDITY_MIDI
  870. if ( timidity_ok ) {
  871. Timidity_Start(music->data.midi);
  872. goto skip;
  873. }
  874. #endif
  875. break;
  876. #endif
  877. #ifdef OGG_MUSIC
  878. case MUS_OGG:
  879. OGG_play(music->data.ogg);
  880. break;
  881. #endif
  882. #ifdef FLAC_MUSIC
  883. case MUS_FLAC:
  884. FLAC_play(music->data.flac);
  885. break;
  886. #endif
  887. #ifdef MP3_MUSIC
  888. case MUS_MP3:
  889. smpeg.SMPEG_enableaudio(music->data.mp3,1);
  890. smpeg.SMPEG_enablevideo(music->data.mp3,0);
  891. smpeg.SMPEG_play(music_playing->data.mp3);
  892. break;
  893. #endif
  894. #ifdef MP3_MAD_MUSIC
  895. case MUS_MP3_MAD:
  896. mad_start(music->data.mp3_mad);
  897. break;
  898. #endif
  899. default:
  900. Mix_SetError("Can't play unknown music type");
  901. retval = -1;
  902. break;
  903. }
  904. skip:
  905. /* Set the playback position, note any errors if an offset is used */
  906. if ( retval == 0 ) {
  907. if ( position > 0.0 ) {
  908. if ( music_internal_position(position) < 0 ) {
  909. Mix_SetError("Position not implemented for music type");
  910. retval = -1;
  911. }
  912. } else {
  913. music_internal_position(0.0);
  914. }
  915. }
  916. /* If the setup failed, we're not playing any music anymore */
  917. if ( retval < 0 ) {
  918. music_playing = NULL;
  919. }
  920. return(retval);
  921. }
  922. int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
  923. {
  924. int retval;
  925. if ( ms_per_step == 0 ) {
  926. SDL_SetError("Audio device hasn't been opened");
  927. return(-1);
  928. }
  929. /* Don't play null pointers :-) */
  930. if ( music == NULL ) {
  931. Mix_SetError("music parameter was NULL");
  932. return(-1);
  933. }
  934. /* Setup the data */
  935. if ( ms ) {
  936. music->fading = MIX_FADING_IN;
  937. } else {
  938. music->fading = MIX_NO_FADING;
  939. }
  940. music->fade_step = 0;
  941. music->fade_steps = ms/ms_per_step;
  942. /* Play the puppy */
  943. SDL_LockAudio();
  944. /* If the current music is fading out, wait for the fade to complete */
  945. while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
  946. SDL_UnlockAudio();
  947. SDL_Delay(100);
  948. SDL_LockAudio();
  949. }
  950. music_active = 1;
  951. if (loops == 1) {
  952. /* Loop is the number of times to play the audio */
  953. loops = 0;
  954. }
  955. music_loops = loops;
  956. retval = music_internal_play(music, position);
  957. SDL_UnlockAudio();
  958. return(retval);
  959. }
  960. int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
  961. {
  962. return Mix_FadeInMusicPos(music, loops, ms, 0.0);
  963. }
  964. int Mix_PlayMusic(Mix_Music *music, int loops)
  965. {
  966. return Mix_FadeInMusicPos(music, loops, 0, 0.0);
  967. }
  968. /* Set the playing music position */
  969. int music_internal_position(double position)
  970. {
  971. int retval = 0;
  972. switch (music_playing->type) {
  973. #ifdef MODPLUG_MUSIC
  974. case MUS_MODPLUG:
  975. modplug_jump_to_time(music_playing->data.modplug, position);
  976. break;
  977. #endif
  978. #ifdef MOD_MUSIC
  979. case MUS_MOD:
  980. MOD_jump_to_time(music_playing->data.module, position);
  981. break;
  982. #endif
  983. #ifdef OGG_MUSIC
  984. case MUS_OGG:
  985. OGG_jump_to_time(music_playing->data.ogg, position);
  986. break;
  987. #endif
  988. #ifdef FLAC_MUSIC
  989. case MUS_FLAC:
  990. FLAC_jump_to_time(music_playing->data.flac, position);
  991. break;
  992. #endif
  993. #ifdef MP3_MUSIC
  994. case MUS_MP3:
  995. smpeg.SMPEG_rewind(music_playing->data.mp3);
  996. smpeg.SMPEG_play(music_playing->data.mp3);
  997. if ( position > 0.0 ) {
  998. smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
  999. }
  1000. break;
  1001. #endif
  1002. #ifdef MP3_MAD_MUSIC
  1003. case MUS_MP3_MAD:
  1004. mad_seek(music_playing->data.mp3_mad, position);
  1005. break;
  1006. #endif
  1007. default:
  1008. /* TODO: Implement this for other music backends */
  1009. retval = -1;
  1010. break;
  1011. }
  1012. return(retval);
  1013. }
  1014. int Mix_SetMusicPosition(double position)
  1015. {
  1016. int retval;
  1017. SDL_LockAudio();
  1018. if ( music_playing ) {
  1019. retval = music_internal_position(position);
  1020. if ( retval < 0 ) {
  1021. Mix_SetError("Position not implemented for music type");
  1022. }
  1023. } else {
  1024. Mix_SetError("Music isn't playing");
  1025. retval = -1;
  1026. }
  1027. SDL_UnlockAudio();
  1028. return(retval);
  1029. }
  1030. /* Set the music's initial volume */
  1031. static void music_internal_initialize_volume(void)
  1032. {
  1033. if ( music_playing->fading == MIX_FADING_IN ) {
  1034. music_internal_volume(0);
  1035. } else {
  1036. music_internal_volume(music_volume);
  1037. }
  1038. }
  1039. /* Set the music volume */
  1040. static void music_internal_volume(int volume)
  1041. {
  1042. switch (music_playing->type) {
  1043. #ifdef CMD_MUSIC
  1044. case MUS_CMD:
  1045. MusicCMD_SetVolume(volume);
  1046. break;
  1047. #endif
  1048. #ifdef WAV_MUSIC
  1049. case MUS_WAV:
  1050. WAVStream_SetVolume(volume);
  1051. break;
  1052. #endif
  1053. #ifdef MODPLUG_MUSIC
  1054. case MUS_MODPLUG:
  1055. modplug_setvolume(music_playing->data.modplug, volume);
  1056. break;
  1057. #endif
  1058. #ifdef MOD_MUSIC
  1059. case MUS_MOD:
  1060. MOD_setvolume(music_playing->data.module, volume);
  1061. break;
  1062. #endif
  1063. #ifdef MID_MUSIC
  1064. case MUS_MID:
  1065. #ifdef USE_NATIVE_MIDI
  1066. if ( native_midi_ok ) {
  1067. native_midi_setvolume(volume);
  1068. return;
  1069. }
  1070. #endif
  1071. #ifdef USE_FLUIDSYNTH_MIDI
  1072. if ( fluidsynth_ok ) {
  1073. fluidsynth_setvolume(music_playing->data.fluidsynthmidi, volume);
  1074. return;
  1075. }
  1076. #endif
  1077. #ifdef USE_TIMIDITY_MIDI
  1078. if ( timidity_ok ) {
  1079. Timidity_SetVolume(volume);
  1080. return;
  1081. }
  1082. #endif
  1083. break;
  1084. #endif
  1085. #ifdef OGG_MUSIC
  1086. case MUS_OGG:
  1087. OGG_setvolume(music_playing->data.ogg, volume);
  1088. break;
  1089. #endif
  1090. #ifdef FLAC_MUSIC
  1091. case MUS_FLAC:
  1092. FLAC_setvolume(music_playing->data.flac, volume);
  1093. break;
  1094. #endif
  1095. #ifdef MP3_MUSIC
  1096. case MUS_MP3:
  1097. smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
  1098. break;
  1099. #endif
  1100. #ifdef MP3_MAD_MUSIC
  1101. case MUS_MP3_MAD:
  1102. mad_setVolume(music_playing->data.mp3_mad, volume);
  1103. break;
  1104. #endif
  1105. default:
  1106. /* Unknown music type?? */
  1107. break;
  1108. }
  1109. }
  1110. int Mix_VolumeMusic(int volume)
  1111. {
  1112. int prev_volume;
  1113. prev_volume = music_volume;
  1114. if ( volume < 0 ) {
  1115. return prev_volume;
  1116. }
  1117. if ( volume > SDL_MIX_MAXVOLUME ) {
  1118. volume = SDL_MIX_MAXVOLUME;
  1119. }
  1120. music_volume = volume;
  1121. SDL_LockAudio();
  1122. if ( music_playing ) {
  1123. music_internal_volume(music_volume);
  1124. }
  1125. SDL_UnlockAudio();
  1126. return(prev_volume);
  1127. }
  1128. /* Halt playing of music */
  1129. static void music_internal_halt(void)
  1130. {
  1131. switch (music_playing->type) {
  1132. #ifdef CMD_MUSIC
  1133. case MUS_CMD:
  1134. MusicCMD_Stop(music_playing->data.cmd);
  1135. break;
  1136. #endif
  1137. #ifdef WAV_MUSIC
  1138. case MUS_WAV:
  1139. WAVStream_Stop();
  1140. break;
  1141. #endif
  1142. #ifdef MODPLUG_MUSIC
  1143. case MUS_MODPLUG:
  1144. modplug_stop(music_playing->data.modplug);
  1145. break;
  1146. #endif
  1147. #ifdef MOD_MUSIC
  1148. case MUS_MOD:
  1149. MOD_stop(music_playing->data.module);
  1150. break;
  1151. #endif
  1152. #ifdef MID_MUSIC
  1153. case MUS_MID:
  1154. #ifdef USE_NATIVE_MIDI
  1155. if ( native_midi_ok ) {
  1156. native_midi_stop();
  1157. goto skip;
  1158. }
  1159. #endif
  1160. #ifdef USE_FLUIDSYNTH_MIDI
  1161. if ( fluidsynth_ok ) {
  1162. fluidsynth_stop(music_playing->data.fluidsynthmidi);
  1163. goto skip;
  1164. }
  1165. #endif
  1166. #ifdef USE_TIMIDITY_MIDI
  1167. if ( timidity_ok ) {
  1168. Timidity_Stop();
  1169. goto skip;
  1170. }
  1171. #endif
  1172. break;
  1173. #endif
  1174. #ifdef OGG_MUSIC
  1175. case MUS_OGG:
  1176. OGG_stop(music_playing->data.ogg);
  1177. break;
  1178. #endif
  1179. #ifdef FLAC_MUSIC
  1180. case MUS_FLAC:
  1181. FLAC_stop(music_playing->data.flac);
  1182. break;
  1183. #endif
  1184. #ifdef MP3_MUSIC
  1185. case MUS_MP3:
  1186. smpeg.SMPEG_stop(music_playing->data.mp3);
  1187. break;
  1188. #endif
  1189. #ifdef MP3_MAD_MUSIC
  1190. case MUS_MP3_MAD:
  1191. mad_stop(music_playing->data.mp3_mad);
  1192. break;
  1193. #endif
  1194. default:
  1195. /* Unknown music type?? */
  1196. return;
  1197. }
  1198. skip:
  1199. music_playing->fading = MIX_NO_FADING;
  1200. music_playing = NULL;
  1201. }
  1202. int Mix_HaltMusic(void)
  1203. {
  1204. SDL_LockAudio();
  1205. if ( music_playing ) {
  1206. music_internal_halt();
  1207. if ( music_finished_hook ) {
  1208. music_finished_hook();
  1209. }
  1210. }
  1211. SDL_UnlockAudio();
  1212. return(0);
  1213. }
  1214. /* Progressively stop the music */
  1215. int Mix_FadeOutMusic(int ms)
  1216. {
  1217. int retval = 0;
  1218. if ( ms_per_step == 0 ) {
  1219. SDL_SetError("Audio device hasn't been opened");
  1220. return 0;
  1221. }
  1222. if (ms <= 0) { /* just halt immediately. */
  1223. Mix_HaltMusic();
  1224. return 1;
  1225. }
  1226. SDL_LockAudio();
  1227. if ( music_playing) {
  1228. int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
  1229. if ( music_playing->fading == MIX_NO_FADING ) {
  1230. music_playing->fade_step = 0;
  1231. } else {
  1232. int step;
  1233. int old_fade_steps = music_playing->fade_steps;
  1234. if ( music_playing->fading == MIX_FADING_OUT ) {
  1235. step = music_playing->fade_step;
  1236. } else {
  1237. step = old_fade_steps
  1238. - music_playing->fade_step + 1;
  1239. }
  1240. music_playing->fade_step = (step * fade_steps)
  1241. / old_fade_steps;
  1242. }
  1243. music_playing->fading = MIX_FADING_OUT;
  1244. music_playing->fade_steps = fade_steps;
  1245. retval = 1;
  1246. }
  1247. SDL_UnlockAudio();
  1248. return(retval);
  1249. }
  1250. Mix_Fading Mix_FadingMusic(void)
  1251. {
  1252. Mix_Fading fading = MIX_NO_FADING;
  1253. SDL_LockAudio();
  1254. if ( music_playing ) {
  1255. fading = music_playing->fading;
  1256. }
  1257. SDL_UnlockAudio();
  1258. return(fading);
  1259. }
  1260. /* Pause/Resume the music stream */
  1261. void Mix_PauseMusic(void)
  1262. {
  1263. music_active = 0;
  1264. }
  1265. void Mix_ResumeMusic(void)
  1266. {
  1267. music_active = 1;
  1268. }
  1269. void Mix_RewindMusic(void)
  1270. {
  1271. Mix_SetMusicPosition(0.0);
  1272. }
  1273. int Mix_PausedMusic(void)
  1274. {
  1275. return (music_active == 0);
  1276. }
  1277. /* Check the status of the music */
  1278. static int music_internal_playing()
  1279. {
  1280. int playing = 1;
  1281. if (music_playing == NULL) {
  1282. return 0;
  1283. }
  1284. switch (music_playing->type) {
  1285. #ifdef CMD_MUSIC
  1286. case MUS_CMD:
  1287. if (!MusicCMD_Active(music_playing->data.cmd)) {
  1288. playing = 0;
  1289. }
  1290. break;
  1291. #endif
  1292. #ifdef WAV_MUSIC
  1293. case MUS_WAV:
  1294. if ( ! WAVStream_Active() ) {
  1295. playing = 0;
  1296. }
  1297. break;
  1298. #endif
  1299. #ifdef MODPLUG_MUSIC
  1300. case MUS_MODPLUG:
  1301. if ( ! modplug_playing(music_playing->data.modplug) ) {
  1302. playing = 0;
  1303. }
  1304. break;
  1305. #endif
  1306. #ifdef MOD_MUSIC
  1307. case MUS_MOD:
  1308. if ( ! MOD_playing(music_playing->data.module) ) {
  1309. playing = 0;
  1310. }
  1311. break;
  1312. #endif
  1313. #ifdef MID_MUSIC
  1314. case MUS_MID:
  1315. #ifdef USE_NATIVE_MIDI
  1316. if ( native_midi_ok ) {
  1317. if ( ! native_midi_active() )
  1318. playing = 0;
  1319. goto skip;
  1320. }
  1321. #endif
  1322. #ifdef USE_FLUIDSYNTH_MIDI
  1323. if ( fluidsynth_ok ) {
  1324. if ( ! fluidsynth_active(music_playing->data.fluidsynthmidi) )
  1325. playing = 0;
  1326. goto skip;
  1327. }
  1328. #endif
  1329. #ifdef USE_TIMIDITY_MIDI
  1330. if ( timidity_ok ) {
  1331. if ( ! Timidity_Active() )
  1332. playing = 0;
  1333. goto skip;
  1334. }
  1335. #endif
  1336. break;
  1337. #endif
  1338. #ifdef OGG_MUSIC
  1339. case MUS_OGG:
  1340. if ( ! OGG_playing(music_playing->data.ogg) ) {
  1341. playing = 0;
  1342. }
  1343. break;
  1344. #endif
  1345. #ifdef FLAC_MUSIC
  1346. case MUS_FLAC:
  1347. if ( ! FLAC_playing(music_playing->data.flac) ) {
  1348. playing = 0;
  1349. }
  1350. break;
  1351. #endif
  1352. #ifdef MP3_MUSIC
  1353. case MUS_MP3:
  1354. if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
  1355. playing = 0;
  1356. break;
  1357. #endif
  1358. #ifdef MP3_MAD_MUSIC
  1359. case MUS_MP3_MAD:
  1360. if (!mad_isPlaying(music_playing->data.mp3_mad)) {
  1361. playing = 0;
  1362. }
  1363. break;
  1364. #endif
  1365. default:
  1366. playing = 0;
  1367. break;
  1368. }
  1369. skip:
  1370. return(playing);
  1371. }
  1372. int Mix_PlayingMusic(void)
  1373. {
  1374. int playing = 0;
  1375. SDL_LockAudio();
  1376. if ( music_playing ) {
  1377. playing = music_loops || music_internal_playing();
  1378. }
  1379. SDL_UnlockAudio();
  1380. return(playing);
  1381. }
  1382. /* Set the external music playback command */
  1383. int Mix_SetMusicCMD(const char *command)
  1384. {
  1385. Mix_HaltMusic();
  1386. if ( music_cmd ) {
  1387. SDL_free(music_cmd);
  1388. music_cmd = NULL;
  1389. }
  1390. if ( command ) {
  1391. music_cmd = (char *)SDL_malloc(strlen(command)+1);
  1392. if ( music_cmd == NULL ) {
  1393. return(-1);
  1394. }
  1395. strcpy(music_cmd, command);
  1396. }
  1397. return(0);
  1398. }
  1399. int Mix_SetSynchroValue(int i)
  1400. {
  1401. /* Not supported by any players at this time */
  1402. return(-1);
  1403. }
  1404. int Mix_GetSynchroValue(void)
  1405. {
  1406. /* Not supported by any players at this time */
  1407. return(-1);
  1408. }
  1409. /* Uninitialize the music players */
  1410. void close_music(void)
  1411. {
  1412. Mix_HaltMusic();
  1413. #ifdef CMD_MUSIC
  1414. Mix_SetMusicCMD(NULL);
  1415. #endif
  1416. #ifdef MODPLUG_MUSIC
  1417. modplug_exit();
  1418. #endif
  1419. #ifdef MOD_MUSIC
  1420. MOD_exit();
  1421. #endif
  1422. #ifdef MID_MUSIC
  1423. # ifdef USE_TIMIDITY_MIDI
  1424. Timidity_Close();
  1425. # endif
  1426. #endif
  1427. /* rcg06042009 report available decoders at runtime. */
  1428. SDL_free((void *)music_decoders);
  1429. music_decoders = NULL;
  1430. num_decoders = 0;
  1431. ms_per_step = 0;
  1432. }
  1433. int Mix_SetSoundFonts(const char *paths)
  1434. {
  1435. #ifdef MID_MUSIC
  1436. if (soundfont_paths) {
  1437. SDL_free(soundfont_paths);
  1438. soundfont_paths = NULL;
  1439. }
  1440. if (paths) {
  1441. if (!(soundfont_paths = SDL_strdup(paths))) {
  1442. Mix_SetError("Insufficient memory to set SoundFonts");
  1443. return 0;
  1444. }
  1445. }
  1446. #endif
  1447. return 1;
  1448. }
  1449. #ifdef MID_MUSIC
  1450. const char* Mix_GetSoundFonts(void)
  1451. {
  1452. const char* force = getenv("SDL_FORCE_SOUNDFONTS");
  1453. if (!soundfont_paths || (force && force[0] == '1')) {
  1454. return getenv("SDL_SOUNDFONTS");
  1455. } else {
  1456. return soundfont_paths;
  1457. }
  1458. }
  1459. int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
  1460. {
  1461. char *context, *path, *paths;
  1462. const char* cpaths = Mix_GetSoundFonts();
  1463. if (!cpaths) {
  1464. Mix_SetError("No SoundFonts have been requested");
  1465. return 0;
  1466. }
  1467. if (!(paths = SDL_strdup(cpaths))) {
  1468. Mix_SetError("Insufficient memory to iterate over SoundFonts");
  1469. return 0;
  1470. }
  1471. #if defined(__MINGW32__) || defined(__MINGW64__)
  1472. for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
  1473. #elif defined(_WIN32)
  1474. for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
  1475. #else
  1476. for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
  1477. #endif
  1478. if (!function(path, data)) {
  1479. SDL_free(paths);
  1480. return 0;
  1481. }
  1482. }
  1483. SDL_free(paths);
  1484. return 1;
  1485. }
  1486. #endif