| 1 |
/** |
| 2 |
* Background music (currently .midi's and audio CDs) |
| 3 |
|
| 4 |
* Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson |
| 5 |
* Copyright (C) 2005, 2007, 2008, 2009, 2010, 2014, 2015 Sylvain Beucler |
| 6 |
|
| 7 |
* This file is part of GNU FreeDink |
| 8 |
|
| 9 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation; either version 3 of the |
| 12 |
* License, or (at your option) any later version. |
| 13 |
|
| 14 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* General Public License for more details. |
| 18 |
|
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see |
| 21 |
* <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
/* CD functions */ |
| 29 |
#include "SDL.h" |
| 30 |
/* MIDI functions */ |
| 31 |
#include "SDL_mixer.h" |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <strings.h> /* strcasecmp */ |
| 36 |
|
| 37 |
#include "bgm.h" |
| 38 |
#include "io_util.h" |
| 39 |
#include "str_util.h" |
| 40 |
#include "paths.h" |
| 41 |
#include "log.h" |
| 42 |
#include "sfx.h" /* sound_on */ |
| 43 |
|
| 44 |
/* Current background music (not cd) */ |
| 45 |
static Mix_Music *music_data = NULL; |
| 46 |
static char* last_midi = NULL; |
| 47 |
static int loop_midi = 0; |
| 48 |
int midi_active = 1; |
| 49 |
|
| 50 |
/* |
| 51 |
* MIDI functions |
| 52 |
*/ |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns whether the background music is currently playing |
| 56 |
*/ |
| 57 |
int something_playing(void) |
| 58 |
{ |
| 59 |
return Mix_PlayingMusic(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Clean-up music when it's finished or manually halted |
| 65 |
*/ |
| 66 |
static void callback_HookMusicFinished() |
| 67 |
{ |
| 68 |
if (music_data != NULL) |
| 69 |
Mix_FreeMusic (music_data); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Thing to play the midi |
| 74 |
*/ |
| 75 |
int PlayMidi(char *midi_filename) |
| 76 |
{ |
| 77 |
char relpath[256]; |
| 78 |
char *fullpath = NULL; |
| 79 |
|
| 80 |
/* no midi stuff right now */ |
| 81 |
if (sound_on == /*false*/0) |
| 82 |
return 1; |
| 83 |
|
| 84 |
/* Do nothing if the same midi is already playing */ |
| 85 |
/* TODO: Does not differentiate midi and ./midi, qsf\\midi and |
| 86 |
qsf/midi... Ok, midi is supposed to be just a number, but..*/ |
| 87 |
if (last_midi != NULL && compare(last_midi, midi_filename) |
| 88 |
&& something_playing()) |
| 89 |
{ |
| 90 |
log_info("I think %s is already playing, I should skip it...", midi_filename); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
// Attempt to play .ogg in addition to .mid, if playing a ".*\.mid$" |
| 96 |
char* oggv_filename = NULL; |
| 97 |
int pos = strlen(midi_filename) - strlen(".mid"); |
| 98 |
if (strcasecmp(midi_filename + pos, ".mid") == 0) |
| 99 |
{ |
| 100 |
oggv_filename = strdup(midi_filename); |
| 101 |
strcpy(oggv_filename + pos, ".ogg"); |
| 102 |
} |
| 103 |
|
| 104 |
/* Try to load the ogg vorbis or midi in the DMod or the main game */ |
| 105 |
int exists = 0; |
| 106 |
fullpath = (char*)malloc(1); |
| 107 |
if (!exists && oggv_filename != NULL) |
| 108 |
{ |
| 109 |
free(fullpath); |
| 110 |
sprintf(relpath, "sound/%s", oggv_filename); |
| 111 |
fullpath = paths_dmodfile(relpath); |
| 112 |
exists = exist(fullpath); |
| 113 |
} |
| 114 |
if (!exists) |
| 115 |
{ |
| 116 |
free(fullpath); |
| 117 |
sprintf(relpath, "sound/%s", midi_filename); |
| 118 |
fullpath = paths_dmodfile(relpath); |
| 119 |
exists = exist(fullpath); |
| 120 |
} |
| 121 |
if (!exists && oggv_filename != NULL) |
| 122 |
{ |
| 123 |
free(fullpath); |
| 124 |
sprintf(relpath, "sound/%s", oggv_filename); |
| 125 |
fullpath = paths_fallbackfile(relpath); |
| 126 |
exists = exist(fullpath); |
| 127 |
} |
| 128 |
if (!exist(fullpath)) |
| 129 |
{ |
| 130 |
free(fullpath); |
| 131 |
sprintf(relpath, "sound/%s", midi_filename); |
| 132 |
fullpath = paths_fallbackfile(relpath); |
| 133 |
exists = exist(fullpath); |
| 134 |
} |
| 135 |
free(oggv_filename); |
| 136 |
|
| 137 |
if (!exist(fullpath)) |
| 138 |
{ |
| 139 |
free(fullpath); |
| 140 |
log_warn("Error playing midi %s, doesn't exist in any dir.", midi_filename); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/* Save the midi currently playing */ |
| 146 |
if (last_midi != NULL) |
| 147 |
free(last_midi); |
| 148 |
last_midi = strdup(midi_filename); |
| 149 |
|
| 150 |
|
| 151 |
/* Stop whatever is playing before we play something else. */ |
| 152 |
Mix_HaltMusic (); |
| 153 |
|
| 154 |
/* Load the file */ |
| 155 |
if ((music_data = Mix_LoadMUS(fullpath)) == NULL) |
| 156 |
{ |
| 157 |
log_warn("Unable to play '%s': %s", fullpath, Mix_GetError()); |
| 158 |
free(fullpath); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
/* Play it */ |
| 163 |
Mix_HookMusicFinished(callback_HookMusicFinished); |
| 164 |
Mix_PlayMusic (music_data, (loop_midi == 1) ? -1 : 1); |
| 165 |
|
| 166 |
free(fullpath); |
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Pause midi file if we're not already paused |
| 174 |
*/ |
| 175 |
/* TODO: should be used when player hits 'n' or alt+'n' - but I never |
| 176 |
got it to work in the original game */ |
| 177 |
int PauseMidi() |
| 178 |
{ |
| 179 |
Mix_PauseMusic(); |
| 180 |
return 1; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Resumes playing of a midi file |
| 185 |
*/ |
| 186 |
/* TODO: should be used when player hits 'b' or alt+'b' - but I never |
| 187 |
got it to work in the original game */ |
| 188 |
int |
| 189 |
ResumeMidi() |
| 190 |
{ |
| 191 |
Mix_ResumeMusic(); |
| 192 |
return 1; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Stops a midi file playing |
| 198 |
*/ |
| 199 |
// TODO: rename *Midi to *BGM (BackGround Music) - we may support |
| 200 |
// background .ogg's in the future |
| 201 |
// DinkC binding: stopmidi() |
| 202 |
int StopMidi() |
| 203 |
{ |
| 204 |
Mix_HaltMusic(); // return always 0 |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Initialize BackGround Music (currently nothing to do, MIDI init is |
| 210 |
* done with SDL_INIT_AUDIO in sfx.c). |
| 211 |
*/ |
| 212 |
void bgm_init(void) |
| 213 |
{ |
| 214 |
} |
| 215 |
|
| 216 |
void bgm_quit(void) |
| 217 |
{ |
| 218 |
Mix_HaltMusic(); |
| 219 |
if (last_midi != NULL) |
| 220 |
free(last_midi); |
| 221 |
last_midi = NULL; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
/** DinkC procedures **/ |
| 227 |
void loopmidi(int arg_loop_midi) |
| 228 |
{ |
| 229 |
if (arg_loop_midi > 0) |
| 230 |
loop_midi = 1; |
| 231 |
else |
| 232 |
loop_midi = 0; |
| 233 |
} |