ref: 59dae316bed420c4b4b682f27373a5b71efca21e
dir: /src/i_sdlmusic.c/
// // Copyright(C) 1993-1996 Id Software, Inc. // Copyright(C) 2005-2014 Simon Howard // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // DESCRIPTION: // System interface for music. // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include "SDL.h" #include "SDL_mixer.h" #include "i_midipipe.h" #include "config.h" #include "doomtype.h" #include "memio.h" #include "mus2mid.h" #include "deh_str.h" #include "gusconf.h" #include "i_sound.h" #include "i_system.h" #include "i_swap.h" #include "m_argv.h" #include "m_config.h" #include "m_misc.h" #include "sha1.h" #include "w_wad.h" #include "z_zone.h" #define MAXMIDLENGTH (96 * 1024) #define MID_HEADER_MAGIC "MThd" #define MUS_HEADER_MAGIC "MUS\x1a" #define FLAC_HEADER "fLaC" #define OGG_HEADER "OggS" // Looping Vorbis metadata tag names. These have been defined by ZDoom // for specifying the start and end positions for looping music tracks // in .ogg and .flac files. // More information is here: http://zdoom.org/wiki/Audio_loop #define LOOP_START_TAG "LOOP_START" #define LOOP_END_TAG "LOOP_END" // FLAC metadata headers that we care about. #define FLAC_STREAMINFO 0 #define FLAC_VORBIS_COMMENT 4 // Ogg metadata headers that we care about. #define OGG_ID_HEADER 1 #define OGG_COMMENT_HEADER 3 // Structure for music substitution. // We store a mapping based on SHA1 checksum -> filename of substitute music // file to play, so that substitution occurs based on content rather than // lump name. This has some inherent advantages: // * Music for Plutonia (reused from Doom 1) works automatically. // * If a PWAD replaces music, the replacement music is used rather than // the substitute music for the IWAD. // * If a PWAD reuses music from an IWAD (even from a different game), we get // the high quality version of the music automatically (neat!) typedef struct { const char *hash_prefix; const char *filename; } subst_music_t; // Structure containing parsed metadata read from a digital music track: typedef struct { boolean valid; unsigned int samplerate_hz; int start_time, end_time; } file_metadata_t; static subst_music_t *subst_music = NULL; static unsigned int subst_music_len = 0; static const char *subst_config_filenames[] = { "doom1-music.cfg", "doom2-music.cfg", "tnt-music.cfg", "heretic-music.cfg", "hexen-music.cfg", "strife-music.cfg", }; static boolean music_initialized = false; // If this is true, this module initialized SDL sound and has the // responsibility to shut it down static boolean sdl_was_initialized = false; static boolean musicpaused = false; static int current_music_volume; char *music_pack_path = ""; char *timidity_cfg_path = ""; static char *temp_timidity_cfg = NULL; // If true, we are playing a substitute digital track rather than in-WAD // MIDI/MUS track, and file_metadata contains loop metadata. static boolean playing_substitute = false; static file_metadata_t file_metadata; // Position (in samples) that we have reached in the current track. // This is updated by the TrackPositionCallback function. static unsigned int current_track_pos; // Currently playing music track. static Mix_Music *current_track_music = NULL; // If true, the currently playing track is being played on loop. static boolean current_track_loop; // Table of known hashes and filenames to look up for them. This allows // users to drop in a set of files without having to also provide a // configuration file. static const subst_music_t known_filenames[] = { // Doom 1 music files. {"b2e05b4e8dff8d76f8f4", "d_inter.ogg"}, {"0c0acce45130bab935d2", "d_intro.ogg"}, {"fca4086939a68ae4ed84", "d_victor.ogg"}, {"5971e5e20554f47ca065", "d_intro.ogg"}, {"99767e32769229897f77", "d_e1m1.ogg"}, {"b5e7dfb4efe9e688bf2a", "d_e1m2.ogg"}, {"fda8fa73e4d30a6b961c", "d_e1m3.ogg"}, {"3805f9bf3f1702f7e7f5", "d_e1m4.ogg"}, {"f546ed823b234fe39165", "d_e1m5.ogg"}, {"4450811b5a6748cfd83e", "d_e1m6.ogg"}, {"73edb50d96b0ac03be34", "d_e1m7.ogg"}, {"47d711a6fd32f5047879", "d_e1m8.ogg"}, {"62c631c2fdaa5ecd9a8d", "d_e1m9.ogg"}, {"7702a6449585428e7185", "d_e2m1.ogg"}, {"1cb1810989cbfae2b29b", "d_e2m2.ogg"}, {"7d740f3c881a22945e47", "d_e2m4.ogg"}, {"ae9c3dc2f9aeea002327", "d_e2m6.ogg"}, {"b26aad3caa420e9a2c76", "d_e2m7.ogg"}, {"90f06251a2a90bfaefd4", "d_e2m8.ogg"}, {"b2fb439f23c08c8e2577", "d_e3m1.ogg"}, {"b6c07bb249526b864208", "d_e3m2.ogg"}, {"ce3587ee503ffe707b2d", "d_e3m3.ogg"}, {"d746ea2aa16b3237422c", "d_e3m8.ogg"}, {"3da3b1335560a92912e6", "d_bunny.ogg"}, // Duplicates that don't have identical hashes: {"4a5badc4f10a7d4ed021", "d_inter.ogg"}, // E2M3 {"36b14bf165b3fdd3958e", "d_e1m7.ogg"}, // E3M5 {"e77c3d42f2ea87f04607", "d_e1m6.ogg"}, // E3M6 {"3d85ec9c10b5ea465568", "d_e2m7.ogg"}, // E3M7 {"4d42e2ce1c1ff192500e", "d_e1m9.ogg"}, // E3M9 // These tracks are reused in Alien Vendetta, but are MIDs: {"a05e45f67e1b64733fe3", "d_e2m1.ogg"}, // MAP02 {"8024ae1616ddd97ce330", "d_e1m4.ogg"}, // MAP03 {"3af8d79ddba49edaf9eb", "d_victor.ogg"}, // MAP05 {"a55352c96c025b6bd08a", "d_inter.ogg"}, // MAP07 {"76d1fc25ab7b1b4a58d6", "d_e1m8.ogg"}, // MAP11 {"497777f0863eca7cea87", "d_e1m2.ogg"}, // MAP12 {"0228fd87f8762f112fb6", "d_e2m2.ogg"}, // MAP13 {"db94e8e1d7c02092eab5", "d_e1m6.ogg"}, // MAP14 {"5a8d7a307eebc952795c", "d_e2m7.ogg"}, // MAP16 {"1a36b692bf26d94a72cc", "d_e1m7.ogg"}, // MAP23 {"37c6cefa351b06995152", "d_e1m5.ogg"}, // MAP27 {"36b97b87fe98348d44b6", "d_e2m6.ogg"}, // MAP28 // Doom II music files. {"79080e9681a2d7bec3fb", "d_runnin.ogg"}, // MAP01,15 {"868b3aae73c7b12e92c0", "d_stalks.ogg"}, // MAP02,11,17 {"19237754d2eb85f41d84", "d_countd.ogg"}, // MAP03,21 {"00abff3b61b25a6855d2", "d_betwee.ogg"}, // MAP04 {"954636c7ee09edf5d98f", "d_doom.ogg"}, // MAP05,13 {"8d32b2b7aa3b806474c1", "d_the_da.ogg"}, // MAP06,12,24 {"41efc3c84bb321af2b6b", "d_shawn.ogg"}, // MAP07,19,29 // Assuming single D_DDTBLU: http://doomwiki.org/wiki/Doom_II_music#Trivia {"51c0872fec9f43259318", "d_ddtblu.ogg"}, // MAP08 {"acb7ad85494d18235df8", "d_ddtblu.ogg"}, // MAP14,22 {"4b7ceccbf47e78e2fa0b", "d_in_cit.ogg"}, // MAP09 {"1d1f4a9edba174584e11", "d_dead.ogg"}, // MAP10,16 {"1736c81aac77f9bffd3d", "d_romero.ogg"}, // MAP18,27 {"a55d400570ad255a576b", "d_messag.ogg"}, // MAP20,26 {"29d30c3fbd712016f2e5", "d_ampie.ogg"}, // MAP23 {"bcfe9786afdcfb704afa", "d_adrian.ogg"}, // MAP25 {"e05c10389e71836834ae", "d_tense.ogg"}, // MAP28 {"b779022b1d0f0010b8f0", "d_openin.ogg"}, // MAP30 {"a9a5f7b0ab3be0f4fc24", "d_evil.ogg"}, // MAP31 {"4503d155aafec0296689", "d_ultima.ogg"}, // MAP32 {"56f2363f01df38908c77", "d_dm2ttl.ogg"}, {"71e58baf9e9dea4dd24a", "d_dm2int.ogg"}, {"e632318629869811f7dc", "d_read_m.ogg"}, // Duplicate filenames: the above filenames are the "canonical" files // for the given SHA1 hashes, but we can also look for these filenames // corresponding to the duplicated music tracks too. {"868b3aae73c7b12e92c0", "d_stlks2.ogg"}, {"868b3aae73c7b12e92c0", "d_stlks3.ogg"}, {"8d32b2b7aa3b806474c1", "d_theda2.ogg"}, {"8d32b2b7aa3b806474c1", "d_theda3.ogg"}, {"954636c7ee09edf5d98f", "d_doom2.ogg"}, {"acb7ad85494d18235df8", "d_ddtbl2.ogg"}, {"acb7ad85494d18235df8", "d_ddtbl3.ogg"}, {"79080e9681a2d7bec3fb", "d_runni2.ogg"}, {"1d1f4a9edba174584e11", "d_dead2.ogg"}, {"41efc3c84bb321af2b6b", "d_shawn2.ogg"}, {"41efc3c84bb321af2b6b", "d_shawn3.ogg"}, {"19237754d2eb85f41d84", "d_count2.ogg"}, {"a55d400570ad255a576b", "d_messg2.ogg"}, {"1736c81aac77f9bffd3d", "d_romer2.ogg"}, // These tracks are reused in Alien Vendetta, but are MIDs: {"9433604c098b7b1119a4", "d_in_cit.ogg"}, // MAP26 // Heretic tracks. {"12818ca0d3c957e7d57e", "mus_titl.ogg"}, {"5cb988538ce1b1857349", "mus_intr.ogg"}, {"6f126abe35a78b61b930", "mus_cptd.ogg"}, {"62557250f0427c067dc9", "mus_e1m1.ogg"}, {"1e8d5fd814490b9ae166", "mus_e1m2.ogg"}, {"f0f31e8834e85035d434", "mus_e1m3.ogg"}, {"054d6997405cc5a32b46", "mus_e1m4.ogg"}, {"31950ab062cc1e5ca49d", "mus_e1m5.ogg"}, {"7389024fbab0dff47211", "mus_e1m6.ogg"}, {"f2aa312dddd0a294a095", "mus_e1m7.ogg"}, {"cd6856731d1ae1f3aa4e", "mus_e1m8.ogg"}, {"d7fe793f266733d92e61", "mus_e1m9.ogg"}, {"933545b48fad8c66f042", "mus_e2m1.ogg"}, {"bf88ecd4ae1621222592", "mus_e2m2.ogg"}, {"4f619f87a828c2ca4801", "mus_e2m3.ogg"}, {"13033a83c49424b2f2ab", "mus_e2m4.ogg"}, {"b3851f9351ae411d9de3", "mus_e2m6.ogg"}, {"82539791159fbbc02a23", "mus_e2m7.ogg"}, {"fd9e53a49cfa62c463a0", "mus_e2m8.ogg"}, {"29503959324d2ca67958", "mus_e2m9.ogg"}, {"3aa632257c5be375b97b", "mus_e3m2.ogg"}, {"69ba0dce7913d53b67a8", "mus_e3m3.ogg"}, // These Heretic tracks are reused in Alien Vendetta, but are MIDs: {"51344131e8d260753ce7", "mus_e2m3.ogg"}, // MAP15 {"78b570b2397570440aff", "mus_e1m1.ogg"}, // MAP19 {"ee21ba9fad4de3dfaef0", "mus_e1m4.ogg"}, // MAP29 {"d2bb643a60696ccbca03", "mus_e1m9.ogg"}, // MAP32 // Hexen tracks: {"fbf55fc1ee26bd01266b", "winnowr.ogg"}, {"71776e2da2b7ba607d81", "jachr.ogg"}, {"c5c8630608b8132b33cd", "simonr.ogg"}, {"43683b3f55a031de88d4", "wutzitr.ogg"}, {"a6062883f29436ef73db", "falconr.ogg"}, {"512cb6cc9b558d5f0fef", "levelr.ogg"}, {"d31226ae75fce6a24208", "chartr.ogg"}, {"bf1f1e561bbdba4e699f", "swampr.ogg"}, {"b303193f756ca0e2de0f", "deepr.ogg"}, {"f0635f0386d883b00186", "fubasr.ogg"}, {"18f2a01f83df6e3abedc", "grover.ogg"}, {"b2527eb0522f08b2cf5f", "fortr.ogg"}, {"343addba8ba53a20a160", "foojar.ogg"}, {"c13109045b06b5a63386", "sixater.ogg"}, {"693525aaf69eac5429ab", "wobabyr.ogg"}, {"8f884223811c2bb8311d", "cryptr.ogg"}, {"de540e6826e62b32c01c", "fantar.ogg"}, {"efdff548df918934f71f", "blechr.ogg"}, {"de91f150f6a127e72e35", "voidr.ogg"}, {"e0497fe27289fe18515b", "chap_1r.ogg"}, {"f2ef1abdc3f672a3519a", "chap_2r.ogg"}, {"78cd9882f61cc441bef4", "chap_3r.ogg"}, {"97b2b575d9d096c1f89f", "chap_4r.ogg"}, {"ad0197a0f6c52ac30915", "chippyr.ogg"}, {"30506c62e9f0989ffe09", "percr.ogg"}, {"3542803beaa43bf1de1a", "secretr.ogg"}, {"81067721f40c611d09fb", "bonesr.ogg"}, {"4822af2e1a2eb7faf660", "octor.ogg"}, {"26bb3cec902ed8008fc2", "rithmr.ogg"}, {"94ab641c7aa93caac77a", "stalkr.ogg"}, {"d0a3f337c54b0703b4d3", "borkr.ogg"}, {"79e7781ec7eb9b9434b5", "crucibr.ogg"}, {"c2786e5581a7f8801969", "hexen.ogg"}, {"97fae9a084c0efda5151", "hub.ogg"}, {"c5da52d5c2ec4803ef8f", "hall.ogg"}, {"1e71bc0e2feafb06214e", "orb.ogg"}, {"bc9dcfa6632e847e03af", "chess.ogg"}, // Hexen CD tracks: alternate filenames for a ripped copy of // the CD soundtrack. {"71776e2da2b7ba607d81", "hexen02.ogg"}, // level 2 (jachr) {"efdff548df918934f71f", "hexen03.ogg"}, // level 26 (blechr) {"c2786e5581a7f8801969", "hexen04.ogg"}, // (hexen) {"1e71bc0e2feafb06214e", "hexen05.ogg"}, // (orb) {"f0635f0386d883b00186", "hexen06.ogg"}, // level 10 (fubasr) {"bc9dcfa6632e847e03af", "hexen07.ogg"}, // (chess) {"8f884223811c2bb8311d", "hexen08.ogg"}, // level 24 (cryptr) {"a6062883f29436ef73db", "hexen09.ogg"}, // level 5 (falconr) {"4822af2e1a2eb7faf660", "hexen10.ogg"}, // level 36 (octor) {"26bb3cec902ed8008fc2", "hexen11.ogg"}, // level 37 (rithmr) {"c13109045b06b5a63386", "hexen12.ogg"}, // level 22 (sixater) {"fbf55fc1ee26bd01266b", "hexen13.ogg"}, // level 1 (winnowr) {"bf1f1e561bbdba4e699f", "hexen14.ogg"}, // level 8 (swampr) {"43683b3f55a031de88d4", "hexen15.ogg"}, // level 4 (wutzitr) {"81067721f40c611d09fb", "hexen16.ogg"}, // level 35 (bonesr) {"e0497fe27289fe18515b", "hexen17.ogg"}, // level 28 (chap_1r) {"97b2b575d9d096c1f89f", "hexen18.ogg"}, // level 31 (chap_4r) {"de540e6826e62b32c01c", "hexen19.ogg"}, // level 25 (fantar) {"343addba8ba53a20a160", "hexen20.ogg"}, // level 21 (foojar) {"512cb6cc9b558d5f0fef", "hexen21.ogg"}, // level 6 (levelr) {"c5c8630608b8132b33cd", "hexen22.ogg"}, // level 3 (simonr) // Strife: {"8ac2b2b47707f0fdf8f6", "d_logo.ogg"}, // Title {"62e1c58054a1f1bc39b2", "d_action.ogg"}, // 1,15,28 {"12fa000f3fa1edac5c4f", "d_tavern.ogg"}, // 2 {"695e56ab3251792d20e5", "d_danger.ogg"}, // 3,11 {"96fe30e8712217b60dd7", "d_fast.ogg"}, // 4 {"ec8fa484c4e85adbf700", "d_intro.ogg"}, // 5 {"61345598a3de04aad508", "d_darker.ogg"}, // 6,14 {"52353e9a435b7b1cb268", "d_strike.ogg"}, // 7,19 {"061164504907bffc9c22", "d_slide.ogg"}, // 8,18,22 {"3dbb4b703ce69aafcdd5", "d_tribal.ogg"}, // 9 {"393773688eba050c3548", "d_march.ogg"}, // 10 {"3cba3c627de065a667dd", "d_mood.ogg"}, // 12 {"b1f65a333e5c70255784", "d_castle.ogg"}, // 13 {"e1455a83a04c9ac4a09f", "d_fight.ogg"}, // 16,31 {"17f822b7374b1f069b89", "d_spense.ogg"}, // 17 {"e66c5a1a7d05f021f4ae", "d_dark.ogg"}, // 20 {"1c92bd0625026af30dad", "d_tech.ogg"}, // 21,27 {"7ae280713d078de7933a", "d_drone.ogg"}, // 23,30 {"4a664afd0d7eae79c97a", "d_panthr.ogg"}, // 24 {"4a7d62beeac5601ccf21", "d_sad.ogg"}, // 25 {"e60e109779400f2855d7", "d_instry.ogg"}, // 26,29 {"b7d36878faeb291d6df5", "d_happy.ogg"}, // Better ending {"ff4a342c8c5ec51b06c3", "d_end.ogg"}, // Worse ending }; // Given a time string (for LOOP_START/LOOP_END), parse it and return // the time (in # samples since start of track) it represents. static unsigned int ParseVorbisTime(unsigned int samplerate_hz, char *value) { char *num_start, *p; unsigned int result = 0; char c; if (strchr(value, ':') == NULL) { return atoi(value); } result = 0; num_start = value; for (p = value; *p != '\0'; ++p) { if (*p == '.' || *p == ':') { c = *p; *p = '\0'; result = result * 60 + atoi(num_start); num_start = p + 1; *p = c; } if (*p == '.') { return result * samplerate_hz + (unsigned int) (atof(p) * samplerate_hz); } } return (result * 60 + atoi(num_start)) * samplerate_hz; } // Given a vorbis comment string (eg. "LOOP_START=12345"), set fields // in the metadata structure as appropriate. static void ParseVorbisComment(file_metadata_t *metadata, char *comment) { char *eq, *key, *value; eq = strchr(comment, '='); if (eq == NULL) { return; } key = comment; *eq = '\0'; value = eq + 1; if (!strcmp(key, LOOP_START_TAG)) { metadata->start_time = ParseVorbisTime(metadata->samplerate_hz, value); } else if (!strcmp(key, LOOP_END_TAG)) { metadata->end_time = ParseVorbisTime(metadata->samplerate_hz, value); } } // Parse a vorbis comments structure, reading from the given file. static void ParseVorbisComments(file_metadata_t *metadata, FILE *fs) { uint32_t buf; unsigned int num_comments, i, comment_len; char *comment; // We must have read the sample rate already from an earlier header. if (metadata->samplerate_hz == 0) { return; } // Skip the starting part we don't care about. if (fread(&buf, 4, 1, fs) < 1) { return; } if (fseek(fs, LONG(buf), SEEK_CUR) != 0) { return; } // Read count field for number of comments. if (fread(&buf, 4, 1, fs) < 1) { return; } num_comments = LONG(buf); // Read each individual comment. for (i = 0; i < num_comments; ++i) { // Read length of comment. if (fread(&buf, 4, 1, fs) < 1) { return; } comment_len = LONG(buf); // Read actual comment data into string buffer. comment = calloc(1, comment_len + 1); if (comment == NULL || fread(comment, 1, comment_len, fs) < comment_len) { free(comment); break; } // Parse comment string. ParseVorbisComment(metadata, comment); free(comment); } } static void ParseFlacStreaminfo(file_metadata_t *metadata, FILE *fs) { byte buf[34]; // Read block data. if (fread(buf, sizeof(buf), 1, fs) < 1) { return; } // We only care about sample rate and song length. metadata->samplerate_hz = (buf[10] << 12) | (buf[11] << 4) | (buf[12] >> 4); // Song length is actually a 36 bit field, but 32 bits should be // enough for everybody. //metadata->song_length = (buf[14] << 24) | (buf[15] << 16) // | (buf[16] << 8) | buf[17]; } static void ParseFlacFile(file_metadata_t *metadata, FILE *fs) { byte header[4]; unsigned int block_type; size_t block_len; boolean last_block; for (;;) { long pos = -1; // Read METADATA_BLOCK_HEADER: if (fread(header, 4, 1, fs) < 1) { return; } block_type = header[0] & ~0x80; last_block = (header[0] & 0x80) != 0; block_len = (header[1] << 16) | (header[2] << 8) | header[3]; pos = ftell(fs); if (pos < 0) { return; } if (block_type == FLAC_STREAMINFO) { ParseFlacStreaminfo(metadata, fs); } else if (block_type == FLAC_VORBIS_COMMENT) { ParseVorbisComments(metadata, fs); } if (last_block) { break; } // Seek to start of next block. if (fseek(fs, pos + block_len, SEEK_SET) != 0) { return; } } } static void ParseOggIdHeader(file_metadata_t *metadata, FILE *fs) { byte buf[21]; if (fread(buf, sizeof(buf), 1, fs) < 1) { return; } metadata->samplerate_hz = (buf[8] << 24) | (buf[7] << 16) | (buf[6] << 8) | buf[5]; } static void ParseOggFile(file_metadata_t *metadata, FILE *fs) { byte buf[7]; unsigned int offset; // Scan through the start of the file looking for headers. They // begin '[byte]vorbis' where the byte value indicates header type. memset(buf, 0, sizeof(buf)); for (offset = 0; offset < 100 * 1024; ++offset) { // buf[] is used as a sliding window. Each iteration, we // move the buffer one byte to the left and read an extra // byte onto the end. memmove(buf, buf + 1, sizeof(buf) - 1); if (fread(&buf[6], 1, 1, fs) < 1) { return; } if (!memcmp(buf + 1, "vorbis", 6)) { switch (buf[0]) { case OGG_ID_HEADER: ParseOggIdHeader(metadata, fs); break; case OGG_COMMENT_HEADER: ParseVorbisComments(metadata, fs); break; default: break; } } } } static void ReadLoopPoints(const char *filename, file_metadata_t *metadata) { FILE *fs; char header[4]; metadata->valid = false; metadata->samplerate_hz = 0; metadata->start_time = 0; metadata->end_time = -1; fs = fopen(filename, "rb"); if (fs == NULL) { return; } // Check for a recognized file format; use the first four bytes // of the file. if (fread(header, 4, 1, fs) < 1) { fclose(fs); return; } if (memcmp(header, FLAC_HEADER, 4) == 0) { ParseFlacFile(metadata, fs); } else if (memcmp(header, OGG_HEADER, 4) == 0) { ParseOggFile(metadata, fs); } fclose(fs); // Only valid if at the very least we read the sample rate. metadata->valid = metadata->samplerate_hz > 0; // If start and end time are both zero, ignore the loop tags. // This is consistent with other source ports. if (metadata->start_time == 0 && metadata->end_time == 0) { metadata->valid = false; } } // Given a MUS lump, look up a substitute MUS file to play instead // (or NULL to just use normal MIDI playback). static const char *GetSubstituteMusicFile(void *data, size_t data_len) { sha1_context_t context; sha1_digest_t hash; const char *filename; char hash_str[sizeof(sha1_digest_t) * 2 + 1]; unsigned int i; // Don't bother doing a hash if we're never going to find anything. if (subst_music_len == 0) { return NULL; } SHA1_Init(&context); SHA1_Update(&context, data, data_len); SHA1_Final(hash, &context); // Build a string representation of the hash. for (i = 0; i < sizeof(sha1_digest_t); ++i) { M_snprintf(hash_str + i * 2, sizeof(hash_str) - i * 2, "%02x", hash[i]); } // Look for a hash that matches. // The substitute mapping list can (intentionally) contain multiple // filename mappings for the same hash. This allows us to try // different files and fall back if our first choice isn't found. filename = NULL; for (i = 0; i < subst_music_len; ++i) { if (M_StringStartsWith(hash_str, subst_music[i].hash_prefix)) { filename = subst_music[i].filename; // If the file exists, then use this file in preference to // any fallbacks. But we always return a filename if it's // in the list, even if it's just so we can print an error // message to the user saying it doesn't exist. if (M_FileExists(filename)) { break; } } } return filename; } static char *GetFullPath(const char *musicdir, const char *path) { char *result; char *systemized_path; // Starting with directory separator means we have an absolute path, // so just return it. if (path[0] == DIR_SEPARATOR) { return M_StringDuplicate(path); } #ifdef _WIN32 // d:\path\... if (isalpha(path[0]) && path[1] == ':' && path[2] == DIR_SEPARATOR) { return M_StringDuplicate(path); } #endif // Paths in the substitute filenames can contain Unix-style / // path separators, but we should convert this to the separator // for the native platform. systemized_path = M_StringReplace(path, "/", DIR_SEPARATOR_S); // Copy config filename and cut off the filename to just get the // parent dir. result = M_StringJoin(musicdir, systemized_path, NULL); free(systemized_path); return result; } // Add a substitute music file to the lookup list. static void AddSubstituteMusic(const char *musicdir, const subst_music_t *subst) { subst_music_t *s; ++subst_music_len; subst_music = I_Realloc(subst_music, sizeof(subst_music_t) * subst_music_len); s = &subst_music[subst_music_len - 1]; *s = *subst; s->filename = GetFullPath(musicdir, s->filename); } static const char *ReadHashPrefix(char *line) { char *result; char *p; int i, len; for (p = line; *p != '\0' && !isspace(*p) && *p != '='; ++p) { if (!isxdigit(*p)) { return NULL; } } len = p - line; if (len == 0 || len > sizeof(sha1_digest_t) * 2) { return NULL; } result = malloc(len + 1); if (result == NULL) { return NULL; } for (i = 0; i < len; ++i) { result[i] = tolower(line[i]); } result[len] = '\0'; return result; } // Parse a line from substitute music configuration file; returns error // message or NULL for no error. static char *ParseSubstituteLine(char *musicdir, char *line) { subst_music_t subst; char *p; // Strip out comments if present. p = strchr(line, '#'); if (p != NULL) { while (p > line && isspace(*(p - 1))) { --p; } *p = '\0'; } // Skip leading spaces. for (p = line; *p != '\0' && isspace(*p); ++p); // Empty line? This includes comment lines now that comments have // been stripped. if (*p == '\0') { return NULL; } subst.hash_prefix = ReadHashPrefix(p); if (subst.hash_prefix == NULL) { return "Invalid hash prefix"; } p += strlen(subst.hash_prefix); // Skip spaces. for (; *p != '\0' && isspace(*p); ++p); if (*p != '=') { return "Expected '='"; } ++p; // Skip spaces. for (; *p != '\0' && isspace(*p); ++p); subst.filename = p; // We're now at the filename. Cut off trailing space characters. while (strlen(p) > 0 && isspace(p[strlen(p) - 1])) { p[strlen(p) - 1] = '\0'; } if (strlen(p) == 0) { return "No filename specified for music substitution"; } // Expand full path and add to our database of substitutes. AddSubstituteMusic(musicdir, &subst); return NULL; } // Read a substitute music configuration file. static boolean ReadSubstituteConfig(char *musicdir, char *filename) { char *buffer; char *line; int linenum = 1; // This unnecessarily opens the file twice... if (!M_FileExists(filename)) { return false; } M_ReadFile(filename, (byte **) &buffer); line = buffer; while (line != NULL) { char *error; char *next; // find end of line char *eol = strchr(line, '\n'); if (eol != NULL) { // change the newline into NUL *eol = '\0'; next = eol + 1; } else { // end of buffer next = NULL; } error = ParseSubstituteLine(musicdir, line); if (error != NULL) { fprintf(stderr, "%s:%i: Error: %s\n", filename, linenum, error); } ++linenum; line = next; } Z_Free(buffer); return true; } // Find substitute configs and try to load them. static void LoadSubstituteConfigs(void) { char *musicdir; char *path; unsigned int i; // We can configure the path to music packs using the music_pack_path // configuration variable. Otherwise we use the current directory, or // $configdir/music to look for .cfg files. if (strcmp(music_pack_path, "") != 0) { musicdir = M_StringJoin(music_pack_path, DIR_SEPARATOR_S, NULL); } else if (!strcmp(configdir, "")) { musicdir = M_StringDuplicate(""); } else { musicdir = M_StringJoin(configdir, "music", DIR_SEPARATOR_S, NULL); } // Load all music packs. We always load all music substitution packs for // all games. Why? Suppose we have a Doom PWAD that reuses some music from // Heretic. If we have the Heretic music pack loaded, then we get an // automatic substitution. for (i = 0; i < arrlen(subst_config_filenames); ++i) { path = M_StringJoin(musicdir, subst_config_filenames[i], NULL); ReadSubstituteConfig(musicdir, path); free(path); } if (subst_music_len > 0) { printf("Loaded %i music substitutions from config files.\n", subst_music_len); } // Add entries from known filenames list. We add this after those from the // configuration files, so that the entries here can be overridden. for (i = 0; i < arrlen(known_filenames); ++i) { AddSubstituteMusic(musicdir, &known_filenames[i]); } free(musicdir); } // Returns true if the given lump number is a music lump that should // be included in substitute configs. // Identifying music lumps by name is not feasible; some games (eg. // Heretic, Hexen) don't have a common naming pattern for music lumps. static boolean IsMusicLump(int lumpnum) { byte *data; boolean result; if (W_LumpLength(lumpnum) < 4) { return false; } data = W_CacheLumpNum(lumpnum, PU_STATIC); result = memcmp(data, MUS_HEADER_MAGIC, 4) == 0 || memcmp(data, MID_HEADER_MAGIC, 4) == 0; W_ReleaseLumpNum(lumpnum); return result; } // Dump an example config file containing checksums for all MIDI music // found in the WAD directory. static void DumpSubstituteConfig(char *filename) { sha1_context_t context; sha1_digest_t digest; char name[9]; byte *data; FILE *fs; unsigned int lumpnum; size_t h; fs = fopen(filename, "w"); if (fs == NULL) { I_Error("Failed to open %s for writing", filename); return; } fprintf(fs, "# Example %s substitute MIDI file.\n\n", PACKAGE_NAME); fprintf(fs, "# SHA1 hash = filename\n"); for (lumpnum = 0; lumpnum < numlumps; ++lumpnum) { strncpy(name, lumpinfo[lumpnum]->name, 8); name[8] = '\0'; if (!IsMusicLump(lumpnum)) { continue; } // Calculate hash. data = W_CacheLumpNum(lumpnum, PU_STATIC); SHA1_Init(&context); SHA1_Update(&context, data, W_LumpLength(lumpnum)); SHA1_Final(digest, &context); W_ReleaseLumpNum(lumpnum); // Print line. for (h = 0; h < sizeof(sha1_digest_t); ++h) { fprintf(fs, "%02x", digest[h]); } fprintf(fs, " = %s.ogg\n", name); } fprintf(fs, "\n"); fclose(fs); printf("Substitute MIDI config file written to %s.\n", filename); I_Quit(); } // 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 *path; FILE *fstream; if (!strcmp(timidity_cfg_path, "")) { return false; } fstream = fopen(write_path, "w"); if (fstream == NULL) { return false; } path = M_DirName(timidity_cfg_path); 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 = M_StringJoin("TIMIDITY_CFG=", temp_timidity_cfg, NULL); putenv(env_string); } else { 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); free(temp_timidity_cfg); } } // Shutdown music static void I_SDL_ShutdownMusic(void) { if (music_initialized) { #if defined(_WIN32) I_MidiPipe_ShutdownServer(); #endif Mix_HaltMusic(); music_initialized = false; if (sdl_was_initialized) { Mix_CloseAudio(); SDL_QuitSubSystem(SDL_INIT_AUDIO); sdl_was_initialized = false; } } } static boolean SDLIsInitialized(void) { int freq, channels; Uint16 format; return Mix_QuerySpec(&freq, &format, &channels) != 0; } // Callback function that is invoked to track current track position. void TrackPositionCallback(int chan, void *stream, int len, void *udata) { // Position is doubled up twice: for 16-bit samples and for stereo. current_track_pos += len / 4; } // Initialize music subsystem static boolean I_SDL_InitMusic(void) { int i; //! // @category obscure // @arg <filename> // // Read all MIDI files from loaded WAD files, dump an example substitution // music config file to the specified filename and quit. // i = M_CheckParmWithArgs("-dumpsubstconfig", 1); if (i > 0) { DumpSubstituteConfig(myargv[i + 1]); } // If SDL_mixer is not initialized, we have to initialize it // and have the responsibility to shut it down later on. if (SDLIsInitialized()) { music_initialized = true; } else { if (SDL_Init(SDL_INIT_AUDIO) < 0) { fprintf(stderr, "Unable to set up sound.\n"); } else if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, 1024) < 0) { fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError()); SDL_QuitSubSystem(SDL_INIT_AUDIO); } else { SDL_PauseAudio(0); sdl_was_initialized = true; music_initialized = true; } } // Once initialization is complete, the temporary Timidity config // file can be removed. RemoveTimidityConfig(); // If snd_musiccmd is set, we need to call Mix_SetMusicCMD to // configure an external music playback program. if (strlen(snd_musiccmd) > 0) { Mix_SetMusicCMD(snd_musiccmd); } // Register an effect function to track the music position. Mix_RegisterEffect(MIX_CHANNEL_POST, TrackPositionCallback, NULL, NULL); // If we're in GENMIDI mode, try to load sound packs. if (snd_musicdevice == SNDDEVICE_GENMIDI) { LoadSubstituteConfigs(); } #if defined(_WIN32) // [AM] Start up midiproc to handle playing MIDI music. I_MidiPipe_InitServer(); #endif return music_initialized; } // // SDL_mixer's native MIDI music playing does not pause properly. // As a workaround, set the volume to 0 when paused. // static void UpdateMusicVolume(void) { int vol; if (musicpaused) { vol = 0; } else { vol = (current_music_volume * MIX_MAX_VOLUME) / 127; } #if defined(_WIN32) I_MidiPipe_SetVolume(vol); #endif Mix_VolumeMusic(vol); } // Set music volume (0 - 127) static void I_SDL_SetMusicVolume(int volume) { // Internal state variable. current_music_volume = volume; UpdateMusicVolume(); } // Start playing a mid static void I_SDL_PlaySong(void *handle, boolean looping) { int loops; if (!music_initialized) { return; } if (handle == NULL && !midi_server_registered) { return; } current_track_music = (Mix_Music *) handle; current_track_loop = looping; if (looping) { loops = -1; } else { loops = 1; } // Don't loop when playing substitute music, as we do it // ourselves instead. if (playing_substitute && file_metadata.valid) { loops = 1; SDL_LockAudio(); current_track_pos = 0; // start of track SDL_UnlockAudio(); } #if defined(_WIN32) if (midi_server_registered) { I_MidiPipe_PlaySong(loops); } else #endif { Mix_PlayMusic(current_track_music, loops); } } static void I_SDL_PauseSong(void) { if (!music_initialized) { return; } musicpaused = true; UpdateMusicVolume(); } static void I_SDL_ResumeSong(void) { if (!music_initialized) { return; } musicpaused = false; UpdateMusicVolume(); } static void I_SDL_StopSong(void) { if (!music_initialized) { return; } #if defined(_WIN32) if (midi_server_registered) { I_MidiPipe_StopSong(); } else #endif { Mix_HaltMusic(); } playing_substitute = false; current_track_music = NULL; } static void I_SDL_UnRegisterSong(void *handle) { Mix_Music *music = (Mix_Music *) handle; if (!music_initialized) { return; } if (handle == NULL) { return; } Mix_FreeMusic(music); } // Determine whether memory block is a .mid file static boolean IsMid(byte *mem, int len) { return len > 4 && !memcmp(mem, "MThd", 4); } static boolean ConvertMus(byte *musdata, int len, const char *filename) { MEMFILE *instream; MEMFILE *outstream; void *outbuf; size_t outbuf_len; int result; instream = mem_fopen_read(musdata, len); outstream = mem_fopen_write(); result = mus2mid(instream, outstream); if (result == 0) { mem_get_buf(outstream, &outbuf, &outbuf_len); M_WriteFile(filename, outbuf, outbuf_len); } mem_fclose(instream); mem_fclose(outstream); return result; } static void *I_SDL_RegisterSong(void *data, int len) { const char *filename; char *tmp_filename; Mix_Music *music; if (!music_initialized) { return NULL; } playing_substitute = false; // See if we're substituting this MUS for a high-quality replacement. filename = GetSubstituteMusicFile(data, len); if (filename != NULL) { music = Mix_LoadMUS(filename); if (music == NULL) { // Fall through and play MIDI normally, but print an error // message. fprintf(stderr, "Failed to load substitute music file: %s: %s\n", filename, Mix_GetError()); } else { // Read loop point metadata from the file so that we know where // to loop the music. playing_substitute = true; ReadLoopPoints(filename, &file_metadata); return music; } } // MUS files begin with "MUS" // Reject anything which doesnt have this signature tmp_filename = M_TempFile("doom.mid"); if (IsMid(data, len) && len < MAXMIDLENGTH) { M_WriteFile(tmp_filename, data, len); } else { // Assume a MUS file and try to convert ConvertMus(data, len, tmp_filename); } // Load the MIDI. In an ideal world we'd be using Mix_LoadMUS_RW() // by now, but Mix_SetMusicCMD() only works with Mix_LoadMUS(), so // we have to generate a temporary file. #if defined(_WIN32) // [AM] If we do not have an external music command defined, play // music with the MIDI server. if (midi_server_initialized) { music = NULL; if (!I_MidiPipe_RegisterSong(tmp_filename)) { fprintf(stderr, "Error loading midi: %s\n", "Could not communicate with midiproc."); } } else #endif { music = Mix_LoadMUS(tmp_filename); if (music == NULL) { // Failed to load fprintf(stderr, "Error loading midi: %s\n", Mix_GetError()); } // Remove the temporary MIDI file; however, when using an external // MIDI program we can't delete the file. Otherwise, the program // won't find the file to play. This means we leave a mess on // disk :( if (strlen(snd_musiccmd) == 0) { remove(tmp_filename); } } free(tmp_filename); return music; } // Is the song playing? static boolean I_SDL_MusicIsPlaying(void) { if (!music_initialized) { return false; } return Mix_PlayingMusic(); } // Get position in substitute music track, in seconds since start of track. static double GetMusicPosition(void) { unsigned int music_pos; int freq; Mix_QuerySpec(&freq, NULL, NULL); SDL_LockAudio(); music_pos = current_track_pos; SDL_UnlockAudio(); return (double) music_pos / freq; } static void RestartCurrentTrack(void) { double start = (double) file_metadata.start_time / file_metadata.samplerate_hz; // If the track finished we need to restart it. if (current_track_music != NULL) { Mix_PlayMusic(current_track_music, 1); } Mix_SetMusicPosition(start); SDL_LockAudio(); current_track_pos = file_metadata.start_time; SDL_UnlockAudio(); } // Poll music position; if we have passed the loop point end position // then we need to go back. static void I_SDL_PollMusic(void) { // When playing substitute tracks, loop tags only apply if we're playing // a looping track. Tracks like the title screen music have the loop // tags ignored. if (current_track_loop && playing_substitute && file_metadata.valid) { double end = (double) file_metadata.end_time / file_metadata.samplerate_hz; // If we have reached the loop end point then we have to take action. if (file_metadata.end_time >= 0 && GetMusicPosition() >= end) { RestartCurrentTrack(); } // Have we reached the actual end of track (not loop end)? if (!Mix_PlayingMusic()) { RestartCurrentTrack(); } } } static snddevice_t music_sdl_devices[] = { SNDDEVICE_PAS, SNDDEVICE_GUS, SNDDEVICE_WAVEBLASTER, SNDDEVICE_SOUNDCANVAS, SNDDEVICE_GENMIDI, SNDDEVICE_AWE32, }; music_module_t music_sdl_module = { music_sdl_devices, arrlen(music_sdl_devices), I_SDL_InitMusic, I_SDL_ShutdownMusic, I_SDL_SetMusicVolume, I_SDL_PauseSong, I_SDL_ResumeSong, I_SDL_RegisterSong, I_SDL_UnRegisterSong, I_SDL_PlaySong, I_SDL_StopSong, I_SDL_MusicIsPlaying, I_SDL_PollMusic, };