📄 music.c
字号:
} music_volume = volume; SDL_LockAudio(); if ( music_playing ) { music_internal_volume(music_volume); } SDL_UnlockAudio(); return(prev_volume);}/* Halt playing of music */static void music_internal_halt(void){ switch (music_playing->type) {#ifdef CMD_MUSIC case MUS_CMD: MusicCMD_Stop(music_playing->data.cmd); break;#endif#ifdef WAV_MUSIC case MUS_WAV: WAVStream_Stop(); break;#endif#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC) case MUS_MOD: Player_Stop(); break;#endif#ifdef MID_MUSIC case MUS_MID:#ifdef USE_NATIVE_MIDI if ( native_midi_ok ) { native_midi_stop(); } MIDI_ELSE#endif#ifdef USE_TIMIDITY_MIDI if ( timidity_ok ) { Timidity_Stop(); }#endif break;#endif#ifdef OGG_MUSIC case MUS_OGG: OGG_stop(music_playing->data.ogg); break;#endif#ifdef MP3_MUSIC case MUS_MP3: smpeg.SMPEG_stop(music_playing->data.mp3); break;#endif default: /* Unknown music type?? */ return; } music_playing->fading = MIX_NO_FADING; music_playing = NULL;}int Mix_HaltMusic(void){ SDL_LockAudio(); if ( music_playing ) { music_internal_halt(); } SDL_UnlockAudio(); return(0);}/* Progressively stop the music */int Mix_FadeOutMusic(int ms){ int retval = 0; if (ms <= 0) { /* just halt immediately. */ Mix_HaltMusic(); return 1; } SDL_LockAudio(); if ( music_playing) { int fade_steps = (ms + ms_per_step - 1)/ms_per_step; if ( music_playing->fading == MIX_NO_FADING ) { music_playing->fade_step = 0; } else { int step; int old_fade_steps = music_playing->fade_steps; if ( music_playing->fading == MIX_FADING_OUT ) { step = music_playing->fade_step; } else { step = old_fade_steps - music_playing->fade_step + 1; } music_playing->fade_step = (step * fade_steps) / old_fade_steps; } music_playing->fading = MIX_FADING_OUT; music_playing->fade_steps = fade_steps; retval = 1; } SDL_UnlockAudio(); return(retval);}Mix_Fading Mix_FadingMusic(void){ Mix_Fading fading = MIX_NO_FADING; SDL_LockAudio(); if ( music_playing ) { fading = music_playing->fading; } SDL_UnlockAudio(); return(fading);}/* Pause/Resume the music stream */void Mix_PauseMusic(void){ music_active = 0;}void Mix_ResumeMusic(void){ music_active = 1;}void Mix_RewindMusic(void){ Mix_SetMusicPosition(0.0);}int Mix_PausedMusic(void){ return (music_active == 0);}/* Check the status of the music */static int music_internal_playing(){ int playing = 1; switch (music_playing->type) {#ifdef CMD_MUSIC case MUS_CMD: if (!MusicCMD_Active(music_playing->data.cmd)) { playing = 0; } break;#endif#ifdef WAV_MUSIC case MUS_WAV: if ( ! WAVStream_Active() ) { playing = 0; } break;#endif#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC) case MUS_MOD: if ( ! Player_Active() ) { playing = 0; } break;#endif#ifdef MID_MUSIC case MUS_MID:#ifdef USE_NATIVE_MIDI if ( native_midi_ok ) { if ( ! native_midi_active() ) playing = 0; } MIDI_ELSE#endif#ifdef USE_TIMIDITY_MIDI if ( timidity_ok ) { if ( ! Timidity_Active() ) playing = 0; }#endif break;#endif#ifdef OGG_MUSIC case MUS_OGG: if ( ! OGG_playing(music_playing->data.ogg) ) { playing = 0; } break;#endif#ifdef MP3_MUSIC case MUS_MP3: if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING ) playing = 0; break;#endif default: playing = 0; break; } return(playing);}int Mix_PlayingMusic(void){ int playing = 0; SDL_LockAudio(); if ( music_playing ) { playing = music_internal_playing(); } SDL_UnlockAudio(); return(playing);}/* Set the external music playback command */int Mix_SetMusicCMD(const char *command){ Mix_HaltMusic(); if ( music_cmd ) { free(music_cmd); music_cmd = NULL; } if ( command ) { music_cmd = (char *)malloc(strlen(command)+1); if ( music_cmd == NULL ) { return(-1); } strcpy(music_cmd, command); } return(0);}#ifdef LIBMIKMOD_MUSICstatic int _pl_synchro_value;void Player_SetSynchroValue(int i){ fprintf(stderr,"SDL_mixer: Player_SetSynchroValue is not supported.\n"); _pl_synchro_value = i;}int Player_GetSynchroValue(void){ fprintf(stderr,"SDL_mixer: Player_GetSynchroValue is not supported.\n"); return _pl_synchro_value;}#endifint Mix_SetSynchroValue(int i){ if ( music_playing && ! music_stopped ) { switch (music_playing->type) {#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC) case MUS_MOD: if ( ! Player_Active() ) { return(-1); } Player_SetSynchroValue(i); return 0; break;#endif default: return(-1); break; } return(-1); } return(-1);}int Mix_GetSynchroValue(void){ if ( music_playing && ! music_stopped ) { switch (music_playing->type) {#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC) case MUS_MOD: if ( ! Player_Active() ) { return(-1); } return Player_GetSynchroValue(); break;#endif default: return(-1); break; } return(-1); } return(-1);}/* Uninitialize the music players */void close_music(void){ Mix_HaltMusic();#ifdef CMD_MUSIC Mix_SetMusicCMD(NULL);#endif#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC) MikMod_Exit();# ifndef LIBMIKMOD_MUSIC MikMod_UnregisterAllLoaders(); MikMod_UnregisterAllDrivers();# endif#endif#ifdef MID_MUSIC# ifdef USE_TIMIDITY_MIDI Timidity_Close();# endif#endif}# ifdef LIBMIKMOD_MUSICtypedef struct{ MREADER mr; int offset; int eof; SDL_RWops *rw;} LMM_MREADER;BOOL LMM_Seek(struct MREADER *mr,long to,int dir){ int at; LMM_MREADER* lmmmr=(LMM_MREADER*)mr; if(dir==SEEK_SET) to+=lmmmr->offset; at=SDL_RWseek(lmmmr->rw, to, dir); return at<lmmmr->offset;}long LMM_Tell(struct MREADER *mr){ int at; LMM_MREADER* lmmmr=(LMM_MREADER*)mr; at=SDL_RWtell(lmmmr->rw)-lmmmr->offset; return at;}BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz){ int got; LMM_MREADER* lmmmr=(LMM_MREADER*)mr; got=SDL_RWread(lmmmr->rw, buf, sz, 1); return got;}int LMM_Get(struct MREADER *mr){ unsigned char c; int i=EOF; LMM_MREADER* lmmmr=(LMM_MREADER*)mr; if(SDL_RWread(lmmmr->rw,&c,1,1)) i=c; return i;}BOOL LMM_Eof(struct MREADER *mr){ int offset; LMM_MREADER* lmmmr=(LMM_MREADER*)mr; offset=LMM_Tell(mr); return offset>=lmmmr->eof;}MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan){ LMM_MREADER lmmmr={ LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof, 0, 0, rw }; MODULE *m; lmmmr.offset=SDL_RWtell(rw); SDL_RWseek(rw,0,SEEK_END); lmmmr.eof=SDL_RWtell(rw); SDL_RWseek(rw,lmmmr.offset,SEEK_SET); m=Player_LoadGeneric((MREADER*)&lmmmr,maxchan,0); return m;}# endifMix_Music *Mix_LoadMUS_RW(SDL_RWops *rw) { Uint8 magic[5]; /*Apparently there is no way to check if the file is really a MOD,*/ /* or there are too many formats supported by MikMod or MikMod does */ /* this check by itself. If someone implements other formats (e.g. MP3) */ /* the check can be uncommented */ Mix_Music *music; int start; /* Figure out what kind of file this is */ start = SDL_RWtell(rw); if (SDL_RWread(rw,magic,1,4)!=4) { Mix_SetError("Couldn't read from RWops"); return NULL; } SDL_RWseek(rw, start, SEEK_SET); magic[4]='\0'; /* Allocate memory for the music structure */ music=(Mix_Music *)malloc(sizeof(Mix_Music)); if (music==NULL ) { Mix_SetError("Out of memory"); return(NULL); } music->error = 0;#ifdef OGG_MUSIC /* Ogg Vorbis files have the magic four bytes "OggS" */ if ( strcmp((char *)magic, "OggS") == 0 ) { music->type = MUS_OGG; music->data.ogg = OGG_new_RW(rw); if ( music->data.ogg == NULL ) { music->error = 1; } } else#endif#ifdef MP3_MUSIC if ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0 ) { if ( Mix_InitMP3() == 0 ) { SMPEG_Info info; music->type = MUS_MP3; music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0); if ( !info.has_audio ) { Mix_SetError("MPEG file does not have any audio stream."); music->error = 1; } else { smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer); } } else { music->error = 1; } } else#endif#ifdef MID_MUSIC /* MIDI files have the magic four bytes "MThd" */ if ( strcmp((char *)magic, "MThd") == 0 ) { music->type = MUS_MID; music->error = 1;#ifdef USE_NATIVE_MIDI if ( native_midi_ok ) { music->data.nativemidi = native_midi_loadsong_RW(rw); if ( music->data.nativemidi ) { music->error = 0; } } MIDI_ELSE#endif#ifdef USE_TIMIDITY_MIDI if ( timidity_ok ) { music->data.midi = Timidity_LoadSong_RW(rw); if ( music->data.midi ) { music->error = 0; } }#endif } else#endif#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC) if (1) { music->type=MUS_MOD; music->data.module=MikMod_LoadSongRW(rw,64); if (music->data.module==NULL) { Mix_SetError("%s",MikMod_strerror(MikMod_errno)); music->error=1; } else { /* Stop implicit looping, fade out and other flags. */ music->data.module->extspd = 1; music->data.module->panflag = 1; music->data.module->wrap = 0; music->data.module->loop = 0;#if 0 /* Don't set fade out by default - unfortunately there's no real way to query the status of the song or set trigger actions. Hum. */ music->data.module->fadeout = 1;#endif } } else#endif { Mix_SetError("Unrecognized music format"); music->error=1; } if (music->error) { free(music); music=NULL; } return(music);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -