diff options
Diffstat (limited to 'src/i_sdlmusic.c')
-rw-r--r-- | src/i_sdlmusic.c | 126 |
1 files changed, 110 insertions, 16 deletions
diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c index 4aa89add..79755890 100644 --- a/src/i_sdlmusic.c +++ b/src/i_sdlmusic.c @@ -27,16 +27,18 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "SDL.h" #include "SDL_mixer.h" -#include "doomdef.h" +#include "doomtype.h" #include "memio.h" #include "mus2mid.h" -#include "deh_main.h" +#include "deh_str.h" +#include "gusconf.h" +#include "i_sound.h" #include "m_misc.h" -#include "s_sound.h" #include "w_wad.h" #include "z_zone.h" @@ -52,10 +54,94 @@ static boolean sdl_was_initialized = false; static boolean musicpaused = false; static int current_music_volume; +char *timidity_cfg_path = ""; + +static char *temp_timidity_cfg = NULL; + +// If the temp_timidity_cfg config variable is set, generate a "wrapper" +// config file for Timidity to point to the actual config file. This +// is needed to inject a "dir" command so that the patches are read +// relative to the actual config file. + +static boolean WriteWrapperTimidityConfig(char *write_path) +{ + char *p, *path; + FILE *fstream; + + if (!strcmp(timidity_cfg_path, "")) + { + return false; + } + + fstream = fopen(write_path, "w"); + + if (fstream == NULL) + { + return false; + } + + p = strrchr(timidity_cfg_path, DIR_SEPARATOR); + if (p != NULL) + { + path = strdup(timidity_cfg_path); + path[p - timidity_cfg_path] = '\0'; + fprintf(fstream, "dir %s\n", path); + free(path); + } + + fprintf(fstream, "source %s\n", timidity_cfg_path); + fclose(fstream); + + return true; +} + +void I_InitTimidityConfig(void) +{ + char *env_string; + boolean success; + + temp_timidity_cfg = M_TempFile("timidity.cfg"); + + if (snd_musicdevice == SNDDEVICE_GUS) + { + success = GUS_WriteConfig(temp_timidity_cfg); + } + else + { + success = WriteWrapperTimidityConfig(temp_timidity_cfg); + } + + // Set the TIMIDITY_CFG environment variable to point to the temporary + // config file. + + if (success) + { + env_string = malloc(strlen(temp_timidity_cfg) + 15); + sprintf(env_string, "TIMIDITY_CFG=%s", temp_timidity_cfg); + putenv(env_string); + } + else + { + Z_Free(temp_timidity_cfg); + temp_timidity_cfg = NULL; + } +} + +// Remove the temporary config file generated by I_InitTimidityConfig(). + +static void RemoveTimidityConfig(void) +{ + if (temp_timidity_cfg != NULL) + { + remove(temp_timidity_cfg); + Z_Free(temp_timidity_cfg); + } +} + // Shutdown music static void I_SDL_ShutdownMusic(void) -{ +{ if (music_initialized) { Mix_HaltMusic(); @@ -106,29 +192,37 @@ static boolean I_SDL_InitMusic(void) // If SDL_mixer is not initialized, we have to initialize it // and have the responsibility to shut it down later on. - if (!SDLIsInitialized()) + if (SDLIsInitialized()) + { + music_initialized = true; + } + else { if (SDL_Init(SDL_INIT_AUDIO) < 0) { fprintf(stderr, "Unable to set up sound.\n"); - return false; } - - if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, 1024) < 0) + else if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, 1024) < 0) { - fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError()); + fprintf(stderr, "Error initializing SDL_mixer: %s\n", + Mix_GetError()); SDL_QuitSubSystem(SDL_INIT_AUDIO); - return false; } + else + { + SDL_PauseAudio(0); - SDL_PauseAudio(0); - - sdl_was_initialized = true; + sdl_was_initialized = true; + music_initialized = true; + } } - music_initialized = true; + // Once initialization is complete, the temporary Timidity config + // file can be removed. - return true; + RemoveTimidityConfig(); + + return music_initialized; } // @@ -164,7 +258,7 @@ static void I_SDL_SetMusicVolume(int volume) // Start playing a mid -static void I_SDL_PlaySong(void *handle, int looping) +static void I_SDL_PlaySong(void *handle, boolean looping) { Mix_Music *music = (Mix_Music *) handle; int loops; |