From 4af96bab470bf0116c25db0a243585dd92f0e0ab Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 18:23:15 +0200 Subject: [PATCH] - some cleanup on music code. * change the license of the main file because there is no more id-based code here licensed under the GPL. * moved VM interface definition out of the implementation. * moved idmus CCMD out of implementation because it is dependent on Doom level definitions. * moved s_music.cpp into the music folder with the rest of the music code. --- src/CMakeLists.txt | 2 +- src/console/c_cmds.cpp | 56 ++++++++++ src/console/c_console.cpp | 2 +- src/d_main.cpp | 1 + src/p_setup.cpp | 9 +- src/scripting/vmthunks.cpp | 16 +++ src/sound/music/i_music.cpp | 19 ++-- src/sound/music/i_soundfont.h | 4 +- src/sound/{s_music.cpp => music/music.cpp} | 120 +++------------------ src/sound/music/music_config.cpp | 10 +- src/sound/music/music_midi_base.cpp | 2 + src/sound/music/s_music.h | 72 +++++++++++++ src/sound/s_advsound.cpp | 14 +-- src/sound/s_doomsound.cpp | 1 - src/sound/s_music.h | 93 ---------------- 15 files changed, 196 insertions(+), 225 deletions(-) rename src/sound/{s_music.cpp => music/music.cpp} (86%) create mode 100644 src/sound/music/s_music.h delete mode 100644 src/sound/s_music.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4a2914c8d..5006ae0ce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -884,7 +884,6 @@ set (PCH_SOURCES sound/s_sndseq.cpp sound/s_doomsound.cpp sound/s_sound.cpp - sound/s_music.cpp serializer.cpp scriptutil.cpp st_stuff.cpp @@ -1097,6 +1096,7 @@ set (PCH_SOURCES scripting/zscript/ast.cpp scripting/zscript/zcc_compile.cpp scripting/zscript/zcc_parser.cpp + sound/music/music.cpp sound/music/i_music.cpp sound/music/i_soundfont.cpp sound/backend/i_sound.cpp diff --git a/src/console/c_cmds.cpp b/src/console/c_cmds.cpp index 4ad845338..6deec16b6 100644 --- a/src/console/c_cmds.cpp +++ b/src/console/c_cmds.cpp @@ -65,6 +65,8 @@ #include "v_video.h" #include "md5.h" #include "findfile.h" +#include "i_music.h" +#include "s_music.h" extern FILE *Logfile; extern bool insave; @@ -1084,3 +1086,57 @@ CCMD(r_showcaps) } +//========================================================================== +// +// CCMD idmus +// +//========================================================================== + +CCMD(idmus) +{ + level_info_t* info; + FString map; + int l; + + if (!nomusic) + { + if (argv.argc() > 1) + { + if (gameinfo.flags & GI_MAPxx) + { + l = atoi(argv[1]); + if (l <= 99) + { + map = CalcMapName(0, l); + } + else + { + Printf("%s\n", GStrings("STSTR_NOMUS")); + return; + } + } + else + { + map = CalcMapName(argv[1][0] - '0', argv[1][1] - '0'); + } + + if ((info = FindLevelInfo(map))) + { + if (info->Music.IsNotEmpty()) + { + S_ChangeMusic(info->Music, info->musicorder); + Printf("%s\n", GStrings("STSTR_MUS")); + } + } + else + { + Printf("%s\n", GStrings("STSTR_NOMUS")); + } + } + } + else + { + Printf("Music is disabled\n"); + } +} + diff --git a/src/console/c_console.cpp b/src/console/c_console.cpp index d000f71e3..b28eeb525 100644 --- a/src/console/c_console.cpp +++ b/src/console/c_console.cpp @@ -1280,7 +1280,7 @@ void C_FullConsole () gamestate = GS_FULLCONSOLE; primaryLevel->Music = ""; S_Start (); - S_StartMusic(); + S_StopMusic(true); P_FreeLevelData (); } else diff --git a/src/d_main.cpp b/src/d_main.cpp index b0fdde095..0cd780166 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -148,6 +148,7 @@ void DrawFullscreenSubtitle(const char *text); void D_Cleanup(); void FreeSBarInfoScript(); void I_UpdateWindowTitle(); +void S_ParseMusInfo(); // PRIVATE FUNCTION PROTOTYPES --------------------------------------------- diff --git a/src/p_setup.cpp b/src/p_setup.cpp index ccc37a267..7245f4cf0 100644 --- a/src/p_setup.cpp +++ b/src/p_setup.cpp @@ -415,7 +415,14 @@ void P_SetupLevel(FLevelLocals *Level, int position, bool newGame) // Make sure all sounds are stopped before Z_FreeTags. S_Start(); - S_StartMusic(); + S_ResetMusic(); + + // Don't start the music if loading a savegame, because the music is stored there. + // Don't start the music if revisiting a level in a hub for the same reason. + if (!primaryLevel->IsReentering()) + { + primaryLevel->SetMusic(); + } // [RH] clear out the mid-screen message C_MidPrint(nullptr, nullptr); diff --git a/src/scripting/vmthunks.cpp b/src/scripting/vmthunks.cpp index eb1709d35..c976f5186 100644 --- a/src/scripting/vmthunks.cpp +++ b/src/scripting/vmthunks.cpp @@ -56,6 +56,7 @@ #include "c_cvars.h" #include "c_bind.h" #include "c_dispatch.h" +#include "s_music.h" DVector2 AM_GetPosition(); int Net_GetLatency(int *ld, int *ad); @@ -3435,6 +3436,16 @@ DEFINE_ACTION_FUNCTION(_Console, Printf) } +DEFINE_ACTION_FUNCTION(DObject, S_ChangeMusic) +{ + PARAM_PROLOGUE; + PARAM_STRING(music); + PARAM_INT(order); + PARAM_BOOL(looping); + PARAM_BOOL(force); + ACTION_RETURN_BOOL(S_ChangeMusic(music, order, looping, force)); +} + //========================================================================== @@ -3619,3 +3630,8 @@ DEFINE_GLOBAL(StatusBar); DEFINE_GLOBAL(Bindings) DEFINE_GLOBAL(AutomapBindings) + +DEFINE_GLOBAL_NAMED(mus_playing, musplaying); +DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, name); +DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, baseorder); +DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, loop); diff --git a/src/sound/music/i_music.cpp b/src/sound/music/i_music.cpp index 33f6d05b4..d93498b1f 100644 --- a/src/sound/music/i_music.cpp +++ b/src/sound/music/i_music.cpp @@ -45,9 +45,9 @@ #include "c_dispatch.h" #include "templates.h" #include "stats.h" +#include "cmdlib.h" #include "c_cvars.h" #include "c_console.h" -#include "vm.h" #include "v_text.h" #include "i_sound.h" #include "i_soundfont.h" @@ -61,9 +61,7 @@ void I_InitSoundFonts(); EXTERN_CVAR (Int, snd_samplerate) EXTERN_CVAR (Int, snd_mididevice) - -static bool ungzip(uint8_t *data, int size, std::vector &newdata); - +EXTERN_CVAR(Float, snd_mastervolume) int nomusic = 0; //========================================================================== @@ -73,7 +71,7 @@ int nomusic = 0; // Maximum volume of MOD/stream music. //========================================================================== -CUSTOM_CVAR (Float, snd_musicvolume, 0.5f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +CUSTOM_CVARD(Float, snd_musicvolume, 0.5, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "controls music volume") { if (self < 0.f) self = 0.f; @@ -100,6 +98,12 @@ CUSTOM_CVAR (Float, snd_musicvolume, 0.5f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) } } +CUSTOM_CVARD(Bool, mus_enabled, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG, "enables/disables music") +{ + if (self) S_RestartMusic(); + else S_StopMusic(true); +} + //========================================================================== // // Callbacks for the music system. @@ -214,7 +218,7 @@ static void SetupDMXGUS() // //========================================================================== -void I_InitMusic (void) +void I_InitMusic(void) { I_InitSoundFonts(); @@ -360,7 +364,7 @@ UNSAFE_CCMD (writewave) if (source == nullptr) return; EMidiDevice dev = MDEV_DEFAULT; - +#ifndef ZMUSIC_LITE if (argv.argc() >= 6) { if (!stricmp(argv[5], "WildMidi")) dev = MDEV_WILDMIDI; @@ -376,6 +380,7 @@ UNSAFE_CCMD (writewave) return; } } +#endif // We must stop the currently playing music to avoid interference between two synths. auto savedsong = mus_playing; S_StopMusic(true); diff --git a/src/sound/music/i_soundfont.h b/src/sound/music/i_soundfont.h index 3a741a4d9..9f07b8cc4 100644 --- a/src/sound/music/i_soundfont.h +++ b/src/sound/music/i_soundfont.h @@ -1,7 +1,7 @@ #pragma once -#include -#include "doomtype.h" +#include "zstring.h" +#include "tarray.h" #include "filesystem.h" #include "files.h" #include "filereadermusicinterface.h" diff --git a/src/sound/s_music.cpp b/src/sound/music/music.cpp similarity index 86% rename from src/sound/s_music.cpp rename to src/sound/music/music.cpp index 85ab82cff..e93e5eb18 100644 --- a/src/sound/s_music.cpp +++ b/src/sound/music/music.cpp @@ -1,29 +1,11 @@ -//----------------------------------------------------------------------------- -// -// Copyright 1993-1996 id Software -// Copyright 1999-2016 Randy Heit -// Copyright 2002-2016 Christoph Oelckers -// -// 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 3 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. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/ -// -//----------------------------------------------------------------------------- -// -// DESCRIPTION: none -// -//----------------------------------------------------------------------------- - -/* For code that originates from ZDoom the following applies: +/* +** +** music.cpp +** +** music engine +** +** Copyright 1999-2016 Randy Heit +** Copyright 2002-2016 Christoph Oelckers ** **--------------------------------------------------------------------------- ** @@ -104,10 +86,6 @@ static FPlayList PlayList; float relative_volume = 1.f; float saved_relative_volume = 1.0f; // this could be used to implement an ACS FadeMusic function -DEFINE_GLOBAL_NAMED(mus_playing, musplaying); -DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, name); -DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, baseorder); -DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, loop); // PUBLIC DATA DEFINITIONS ------------------------------------------------- @@ -257,13 +235,11 @@ void S_UpdateMusic () //========================================================================== // -// S_Start +// Resets the music player if music playback was paused. // -// Per level startup code. Kills playing sounds at start of level -// and starts new music. //========================================================================== -void S_StartMusic () +void S_ResetMusic () { // stop the old music if it has been paused. // This ensures that the new music is started from the beginning @@ -272,13 +248,6 @@ void S_StartMusic () // start new music for the level MusicPaused = false; - - // Don't start the music if loading a savegame, because the music is stored there. - // Don't start the music if revisiting a level in a hub for the same reason. - if (!primaryLevel->IsReentering()) - { - primaryLevel->SetMusic(); - } } @@ -361,6 +330,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) mus_playing.LastSong = ""; return true; } + if (*musicname == '/') musicname++; FString DEH_Music; if (musicname[0] == '$') @@ -460,7 +430,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) // shutdown old music S_StopMusic (true); - // Just record it if volume is 0 + // Just record it if volume is 0 or music was disabled if (snd_musicvolume <= 0) { mus_playing.loop = looping; @@ -505,22 +475,10 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) return false; } -DEFINE_ACTION_FUNCTION(DObject, S_ChangeMusic) -{ - PARAM_PROLOGUE; - PARAM_STRING(music); - PARAM_INT(order); - PARAM_BOOL(looping); - PARAM_BOOL(force); - ACTION_RETURN_BOOL(S_ChangeMusic(music, order, looping, force)); -} - - //========================================================================== // // S_RestartMusic // -// Must only be called from snd_reset in i_sound.cpp! //========================================================================== void S_RestartMusic () @@ -613,60 +571,6 @@ void S_StopMusic (bool force) } } -//========================================================================== -// -// CCMD idmus -// -//========================================================================== - -CCMD (idmus) -{ - level_info_t *info; - FString map; - int l; - - if (!nomusic) - { - if (argv.argc() > 1) - { - if (gameinfo.flags & GI_MAPxx) - { - l = atoi (argv[1]); - if (l <= 99) - { - map = CalcMapName (0, l); - } - else - { - Printf ("%s\n", GStrings("STSTR_NOMUS")); - return; - } - } - else - { - map = CalcMapName (argv[1][0] - '0', argv[1][1] - '0'); - } - - if ( (info = FindLevelInfo (map)) ) - { - if (info->Music.IsNotEmpty()) - { - S_ChangeMusic (info->Music, info->musicorder); - Printf ("%s\n", GStrings("STSTR_MUS")); - } - } - else - { - Printf ("%s\n", GStrings("STSTR_NOMUS")); - } - } - } - else - { - Printf("Music is disabled\n"); - } -} - //========================================================================== // // CCMD changemus diff --git a/src/sound/music/music_config.cpp b/src/sound/music/music_config.cpp index bd71a8fc9..af3f7430a 100644 --- a/src/sound/music/music_config.cpp +++ b/src/sound/music/music_config.cpp @@ -63,7 +63,7 @@ auto ret = ChangeMusicSetting(zmusic_##key, mus_playing.handle,*self); \ if (ret) S_MIDIDeviceChanged(-1); - +#ifndef ZMUSIC_LITE CUSTOM_CVAR(Int, adl_chips_count, 6, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_CVAR(adl_chips_count); @@ -103,7 +103,7 @@ CUSTOM_CVAR(Int, adl_volume_model, 3/*ADLMIDI_VolumeModel_DMX*/, CVAR_ARCHIVE | { FORWARD_CVAR(adl_bank); } - +#endif //========================================================================== // // Fluidsynth MIDI device @@ -223,6 +223,7 @@ CUSTOM_CVAR(Bool, opl_fullpan, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIR FORWARD_BOOL_CVAR(opl_fullpan); } +#ifndef ZMUSIC_LITE //========================================================================== // // OPN MIDI device @@ -382,12 +383,13 @@ CUSTOM_CVAR(Float, timidity_min_sustain_time, 5000, CVAR_ARCHIVE | CVAR_GLOBALCO { FORWARD_CVAR(timidity_min_sustain_time); } +#endif CUSTOM_CVAR(String, timidity_config, GAMENAMELOWERCASE, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_VIRTUAL) { FORWARD_STRING_CVAR(timidity_config); } - +#ifndef ZMUSIC_LITE //========================================================================== // // WildMidi @@ -408,7 +410,7 @@ CUSTOM_CVAR(Bool, wildmidi_enhanced_resampling, true, CVAR_ARCHIVE | CVAR_GLOBAL { FORWARD_BOOL_CVAR(wildmidi_enhanced_resampling); } - +#endif //========================================================================== // diff --git a/src/sound/music/music_midi_base.cpp b/src/sound/music/music_midi_base.cpp index 3075af050..65c59c54b 100644 --- a/src/sound/music/music_midi_base.cpp +++ b/src/sound/music/music_midi_base.cpp @@ -38,6 +38,8 @@ #include "menu/menu.h" #include #include "s_music.h" +#include "c_cvars.h" +#include "printf.h" #define DEF_MIDIDEV -5 diff --git a/src/sound/music/s_music.h b/src/sound/music/s_music.h new file mode 100644 index 000000000..d58ca9ebd --- /dev/null +++ b/src/sound/music/s_music.h @@ -0,0 +1,72 @@ + +#ifndef __S_MUSIC__ +#define __S_MUSIC__ + +#include "zstring.h" +#include "tarray.h" +#include "name.h" +#include + +void S_CreateStream(); +void S_PauseStream(bool pause); +void S_StopStream(); +void S_SetStreamVolume(float vol); + + +// +void S_InitMusic (); +void S_ResetMusic (); + + +// Start music using +bool S_StartMusic (const char *music_name); + +// Start music using , and set whether looping +bool S_ChangeMusic (const char *music_name, int order=0, bool looping=true, bool force=false); + +void S_RestartMusic (); +void S_MIDIDeviceChanged(int newdev); + +int S_GetMusic (const char **name); + +// Stops the music for sure. +void S_StopMusic (bool force); + +// Stop and resume music, during game PAUSE. +void S_PauseMusic (); +void S_ResumeMusic (); + +// +// Updates music & sounds +// +void S_UpdateMusic (); + +struct MidiDeviceSetting +{ + int device; + FString args; +}; + +typedef TMap MusicAliasMap; +typedef TMap MidiDeviceMap; +typedef TMap MusicVolumeMap; + +extern MusicAliasMap MusicAliases; +extern MidiDeviceMap MidiDevices; +extern MusicVolumeMap MusicVolumes; + +struct MusPlayingInfo +{ + FString name; + ZMusic_MusicStream handle; + int baseorder; + bool loop; + FString LastSong; // last music that was played +}; + +extern MusPlayingInfo mus_playing; + +extern float relative_volume, saved_relative_volume; + + +#endif diff --git a/src/sound/s_advsound.cpp b/src/sound/s_advsound.cpp index 6c4b49269..5460563c2 100644 --- a/src/sound/s_advsound.cpp +++ b/src/sound/s_advsound.cpp @@ -230,7 +230,7 @@ static const char *SICommandStrings[] = NULL }; -static FMusicVolume *MusicVolumes; +static FMusicVolume *MusicVolumes_; static TArray SavedPlayerSounds; static int NumPlayerReserves; @@ -256,7 +256,7 @@ static uint8_t CurrentPitchMask; float S_GetMusicVolume (const char *music) { - FMusicVolume *musvol = MusicVolumes; + FMusicVolume *musvol = MusicVolumes_; while (musvol != NULL) { @@ -744,10 +744,10 @@ void S_ClearSoundData() soundEngine->Clear(); Ambients.Clear(); - while (MusicVolumes != NULL) + while (MusicVolumes_ != NULL) { - FMusicVolume *me = MusicVolumes; - MusicVolumes = me->Next; + FMusicVolume *me = MusicVolumes_; + MusicVolumes_ = me->Next; M_Free(me); } @@ -1184,8 +1184,8 @@ static void S_AddSNDINFO (int lump) FMusicVolume *mv = (FMusicVolume *)M_Malloc (sizeof(*mv) + musname.Len()); mv->Volume = (float)sc.Float; strcpy (mv->MusicName, musname); - mv->Next = MusicVolumes; - MusicVolumes = mv; + mv->Next = MusicVolumes_; + MusicVolumes_ = mv; } break; diff --git a/src/sound/s_doomsound.cpp b/src/sound/s_doomsound.cpp index 156f98f11..b3b256361 100644 --- a/src/sound/s_doomsound.cpp +++ b/src/sound/s_doomsound.cpp @@ -1412,7 +1412,6 @@ DEFINE_ACTION_FUNCTION(DObject, S_ResumeSound) } - CCMD (snd_status) { GSnd->PrintStatus (); diff --git a/src/sound/s_music.h b/src/sound/s_music.h deleted file mode 100644 index b11188226..000000000 --- a/src/sound/s_music.h +++ /dev/null @@ -1,93 +0,0 @@ -//----------------------------------------------------------------------------- -// -// Copyright 1993-1996 id Software -// Copyright 1999-2016 Randy Heit -// Copyright 2002-2016 Christoph Oelckers -// -// 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 3 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. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/ -// -//----------------------------------------------------------------------------- -// -// DESCRIPTION: -// The not so system specific sound interface. -// -//----------------------------------------------------------------------------- - - -#ifndef __S_MUSIC__ -#define __S_MUSIC__ - -#include -#include "doomtype.h" -#include "i_soundinternal.h" - - -void S_ParseMusInfo(); - - -// -void S_InitMusic (); -void S_StartMusic (); - - -// Start music using -bool S_StartMusic (const char *music_name); - -// Start music using , and set whether looping -bool S_ChangeMusic (const char *music_name, int order=0, bool looping=true, bool force=false); - -void S_RestartMusic (); -void S_MIDIDeviceChanged(int newdev); - -int S_GetMusic (const char **name); - -// Stops the music for sure. -void S_StopMusic (bool force); - -// Stop and resume music, during game PAUSE. -void S_PauseMusic (); -void S_ResumeMusic (); - -// -// Updates music & sounds -// -void S_UpdateMusic (); - -struct MidiDeviceSetting -{ - int device; - FString args; -}; - -typedef TMap MusicAliasMap; -typedef TMap MidiDeviceMap; - -extern MusicAliasMap MusicAliases; -extern MidiDeviceMap MidiDevices; - -struct MusPlayingInfo -{ - FString name; - ZMusic_MusicStream handle; - int baseorder; - bool loop; - FString LastSong; // last music that was played -}; - -extern MusPlayingInfo mus_playing; - -extern float relative_volume, saved_relative_volume; - - -#endif