music.c
changeset 718 fb0562cc1559
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
equal deleted inserted replaced
717:27eb690edafc 718:fb0562cc1559
    28 #include "SDL_endian.h"
    28 #include "SDL_endian.h"
    29 #include "SDL_audio.h"
    29 #include "SDL_audio.h"
    30 #include "SDL_timer.h"
    30 #include "SDL_timer.h"
    31 
    31 
    32 #include "SDL_mixer.h"
    32 #include "SDL_mixer.h"
       
    33 #include "mixer.h"
    33 
    34 
    34 #ifdef CMD_MUSIC
    35 #ifdef CMD_MUSIC
    35 #include "music_cmd.h"
    36 #include "music_cmd.h"
    36 #endif
    37 #endif
    37 #ifdef WAV_MUSIC
    38 #ifdef WAV_MUSIC
   183 /* Support for hooking when the music has finished */
   184 /* Support for hooking when the music has finished */
   184 static void (*music_finished_hook)(void) = NULL;
   185 static void (*music_finished_hook)(void) = NULL;
   185 
   186 
   186 void Mix_HookMusicFinished(void (*music_finished)(void))
   187 void Mix_HookMusicFinished(void (*music_finished)(void))
   187 {
   188 {
   188     SDL_LockAudio();
   189     Mix_LockAudio();
   189     music_finished_hook = music_finished;
   190     music_finished_hook = music_finished;
   190     SDL_UnlockAudio();
   191     Mix_UnlockAudio();
   191 }
   192 }
   192 
   193 
   193 
   194 
   194 /* If music isn't playing, halt it if no looping is required, restart it */
   195 /* If music isn't playing, halt it if no looping is required, restart it */
   195 /* othesrchise. NOP if the music is playing */
   196 /* othesrchise. NOP if the music is playing */
   763 /* Free a music chunk previously loaded */
   764 /* Free a music chunk previously loaded */
   764 void Mix_FreeMusic(Mix_Music *music)
   765 void Mix_FreeMusic(Mix_Music *music)
   765 {
   766 {
   766     if ( music ) {
   767     if ( music ) {
   767         /* Stop the music if it's currently playing */
   768         /* Stop the music if it's currently playing */
   768         SDL_LockAudio();
   769         Mix_LockAudio();
   769         if ( music == music_playing ) {
   770         if ( music == music_playing ) {
   770             /* Wait for any fade out to finish */
   771             /* Wait for any fade out to finish */
   771             while ( music->fading == MIX_FADING_OUT ) {
   772             while ( music->fading == MIX_FADING_OUT ) {
   772                 SDL_UnlockAudio();
   773                 Mix_UnlockAudio();
   773                 SDL_Delay(100);
   774                 SDL_Delay(100);
   774                 SDL_LockAudio();
   775                 Mix_LockAudio();
   775             }
   776             }
   776             if ( music == music_playing ) {
   777             if ( music == music_playing ) {
   777                 music_internal_halt();
   778                 music_internal_halt();
   778             }
   779             }
   779         }
   780         }
   780         SDL_UnlockAudio();
   781         Mix_UnlockAudio();
   781         switch (music->type) {
   782         switch (music->type) {
   782 #ifdef CMD_MUSIC
   783 #ifdef CMD_MUSIC
   783             case MUS_CMD:
   784             case MUS_CMD:
   784                 MusicCMD_FreeSong(music->data.cmd);
   785                 MusicCMD_FreeSong(music->data.cmd);
   785                 break;
   786                 break;
   859     Mix_MusicType type = MUS_NONE;
   860     Mix_MusicType type = MUS_NONE;
   860 
   861 
   861     if ( music ) {
   862     if ( music ) {
   862         type = music->type;
   863         type = music->type;
   863     } else {
   864     } else {
   864         SDL_LockAudio();
   865         Mix_LockAudio();
   865         if ( music_playing ) {
   866         if ( music_playing ) {
   866             type = music_playing->type;
   867             type = music_playing->type;
   867         }
   868         }
   868         SDL_UnlockAudio();
   869         Mix_UnlockAudio();
   869     }
   870     }
   870     return(type);
   871     return(type);
   871 }
   872 }
   872 
   873 
   873 /* Play a music chunk.  Returns 0, or -1 if there was an error.
   874 /* Play a music chunk.  Returns 0, or -1 if there was an error.
  1016     }
  1017     }
  1017     music->fade_step = 0;
  1018     music->fade_step = 0;
  1018     music->fade_steps = ms/ms_per_step;
  1019     music->fade_steps = ms/ms_per_step;
  1019 
  1020 
  1020     /* Play the puppy */
  1021     /* Play the puppy */
  1021     SDL_LockAudio();
  1022     Mix_LockAudio();
  1022     /* If the current music is fading out, wait for the fade to complete */
  1023     /* If the current music is fading out, wait for the fade to complete */
  1023     while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
  1024     while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
  1024         SDL_UnlockAudio();
  1025         Mix_UnlockAudio();
  1025         SDL_Delay(100);
  1026         SDL_Delay(100);
  1026         SDL_LockAudio();
  1027         Mix_LockAudio();
  1027     }
  1028     }
  1028     music_active = 1;
  1029     music_active = 1;
  1029     if (loops == 1) {
  1030     if (loops == 1) {
  1030         /* Loop is the number of times to play the audio */
  1031         /* Loop is the number of times to play the audio */
  1031         loops = 0;
  1032         loops = 0;
  1032     }
  1033     }
  1033     music_loops = loops;
  1034     music_loops = loops;
  1034     retval = music_internal_play(music, position);
  1035     retval = music_internal_play(music, position);
  1035     SDL_UnlockAudio();
  1036     Mix_UnlockAudio();
  1036 
  1037 
  1037     return(retval);
  1038     return(retval);
  1038 }
  1039 }
  1039 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
  1040 int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
  1040 {
  1041 {
  1094 }
  1095 }
  1095 int Mix_SetMusicPosition(double position)
  1096 int Mix_SetMusicPosition(double position)
  1096 {
  1097 {
  1097     int retval;
  1098     int retval;
  1098 
  1099 
  1099     SDL_LockAudio();
  1100     Mix_LockAudio();
  1100     if ( music_playing ) {
  1101     if ( music_playing ) {
  1101         retval = music_internal_position(position);
  1102         retval = music_internal_position(position);
  1102         if ( retval < 0 ) {
  1103         if ( retval < 0 ) {
  1103             Mix_SetError("Position not implemented for music type");
  1104             Mix_SetError("Position not implemented for music type");
  1104         }
  1105         }
  1105     } else {
  1106     } else {
  1106         Mix_SetError("Music isn't playing");
  1107         Mix_SetError("Music isn't playing");
  1107         retval = -1;
  1108         retval = -1;
  1108     }
  1109     }
  1109     SDL_UnlockAudio();
  1110     Mix_UnlockAudio();
  1110 
  1111 
  1111     return(retval);
  1112     return(retval);
  1112 }
  1113 }
  1113 
  1114 
  1114 /* Set the music's initial volume */
  1115 /* Set the music's initial volume */
  1202     }
  1203     }
  1203     if ( volume > SDL_MIX_MAXVOLUME ) {
  1204     if ( volume > SDL_MIX_MAXVOLUME ) {
  1204         volume = SDL_MIX_MAXVOLUME;
  1205         volume = SDL_MIX_MAXVOLUME;
  1205     }
  1206     }
  1206     music_volume = volume;
  1207     music_volume = volume;
  1207     SDL_LockAudio();
  1208     Mix_LockAudio();
  1208     if ( music_playing ) {
  1209     if ( music_playing ) {
  1209         music_internal_volume(music_volume);
  1210         music_internal_volume(music_volume);
  1210     }
  1211     }
  1211     SDL_UnlockAudio();
  1212     Mix_UnlockAudio();
  1212     return(prev_volume);
  1213     return(prev_volume);
  1213 }
  1214 }
  1214 
  1215 
  1215 /* Halt playing of music */
  1216 /* Halt playing of music */
  1216 static void music_internal_halt(void)
  1217 static void music_internal_halt(void)
  1287     music_playing->fading = MIX_NO_FADING;
  1288     music_playing->fading = MIX_NO_FADING;
  1288     music_playing = NULL;
  1289     music_playing = NULL;
  1289 }
  1290 }
  1290 int Mix_HaltMusic(void)
  1291 int Mix_HaltMusic(void)
  1291 {
  1292 {
  1292     SDL_LockAudio();
  1293     Mix_LockAudio();
  1293     if ( music_playing ) {
  1294     if ( music_playing ) {
  1294         music_internal_halt();
  1295         music_internal_halt();
  1295         if ( music_finished_hook ) {
  1296         if ( music_finished_hook ) {
  1296             music_finished_hook();
  1297             music_finished_hook();
  1297         }
  1298         }
  1298     }
  1299     }
  1299     SDL_UnlockAudio();
  1300     Mix_UnlockAudio();
  1300 
  1301 
  1301     return(0);
  1302     return(0);
  1302 }
  1303 }
  1303 
  1304 
  1304 /* Progressively stop the music */
  1305 /* Progressively stop the music */
  1314     if (ms <= 0) {  /* just halt immediately. */
  1315     if (ms <= 0) {  /* just halt immediately. */
  1315         Mix_HaltMusic();
  1316         Mix_HaltMusic();
  1316         return 1;
  1317         return 1;
  1317     }
  1318     }
  1318 
  1319 
  1319     SDL_LockAudio();
  1320     Mix_LockAudio();
  1320     if ( music_playing) {
  1321     if ( music_playing) {
  1321                 int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
  1322                 int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
  1322                 if ( music_playing->fading == MIX_NO_FADING ) {
  1323                 if ( music_playing->fading == MIX_NO_FADING ) {
  1323                 music_playing->fade_step = 0;
  1324                 music_playing->fade_step = 0;
  1324                 } else {
  1325                 } else {
  1335                 }
  1336                 }
  1336         music_playing->fading = MIX_FADING_OUT;
  1337         music_playing->fading = MIX_FADING_OUT;
  1337         music_playing->fade_steps = fade_steps;
  1338         music_playing->fade_steps = fade_steps;
  1338         retval = 1;
  1339         retval = 1;
  1339     }
  1340     }
  1340     SDL_UnlockAudio();
  1341     Mix_UnlockAudio();
  1341 
  1342 
  1342     return(retval);
  1343     return(retval);
  1343 }
  1344 }
  1344 
  1345 
  1345 Mix_Fading Mix_FadingMusic(void)
  1346 Mix_Fading Mix_FadingMusic(void)
  1346 {
  1347 {
  1347     Mix_Fading fading = MIX_NO_FADING;
  1348     Mix_Fading fading = MIX_NO_FADING;
  1348 
  1349 
  1349     SDL_LockAudio();
  1350     Mix_LockAudio();
  1350     if ( music_playing ) {
  1351     if ( music_playing ) {
  1351         fading = music_playing->fading;
  1352         fading = music_playing->fading;
  1352     }
  1353     }
  1353     SDL_UnlockAudio();
  1354     Mix_UnlockAudio();
  1354 
  1355 
  1355     return(fading);
  1356     return(fading);
  1356 }
  1357 }
  1357 
  1358 
  1358 /* Pause/Resume the music stream */
  1359 /* Pause/Resume the music stream */
  1476 }
  1477 }
  1477 int Mix_PlayingMusic(void)
  1478 int Mix_PlayingMusic(void)
  1478 {
  1479 {
  1479     int playing = 0;
  1480     int playing = 0;
  1480 
  1481 
  1481     SDL_LockAudio();
  1482     Mix_LockAudio();
  1482     if ( music_playing ) {
  1483     if ( music_playing ) {
  1483         playing = music_loops || music_internal_playing();
  1484         playing = music_loops || music_internal_playing();
  1484     }
  1485     }
  1485     SDL_UnlockAudio();
  1486     Mix_UnlockAudio();
  1486 
  1487 
  1487     return(playing);
  1488     return(playing);
  1488 }
  1489 }
  1489 
  1490 
  1490 /* Set the external music playback command */
  1491 /* Set the external music playback command */