In-game music player

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

In-game music player

Postby vedan77 » Fri Sep 10, 2021 5:30 am

Hi,

there are many awesome music collections for Doom, yet no possibility to change a song while playing.

Latest code after forum suggestions (thanks, Enjay) and some updates (like inc/dec volume):

Code: Select allExpand view
// MUSIC PLAYER ============================================================================

bool muted = FALSE;
bool fading = FALSE;
int song = -1;
int volume = 1.0;
int percent = 100;

script "SetTrack" (void) { // SetTrack
   if (song < 0) {song = 32;}
   if (song >= 33) {song = 0;}
   str songs[33][2] = {
      {"d_dm2ttl", "Doom Title"},
      {"d_dead", "The Demon's Dead"},
      {"d_doom2", "Doom 2"},
      {"d_read_m", "Text Screen"},
      {"d_stalks", "The Healer Stalks 1"},
      {"d_stlks2", "The Healer Stalks 2"},
      {"d_doom", "Doom 1"},
      {"d_countd", "Countdown to Death"},
      {"d_theda2", "In the Dark 2"},
      {"d_openin", "Opening to Hell"},
      {"d_shawn", "Shawn's got the Shotgun 1"},
      {"d_shawn2", "Shawn's got the Shotgun 2"},
      {"d_the_da", "In the Dark 1"},
      {"d_ultima", "The Ultimate Challenge"},
      {"d_count2", "Countdown to Death 2"},
      {"d_in_cit", "Into Sandy's City"},
      {"d_messag", "Message for the Archvile 1"},
      {"d_runni2", "Running From Evil 2"},
      {"d_messg2", "Message for the Archvile 2"},
      {"d_dead2", "The Demon's Dead 2"},
      {"d_tense", "Getting too Tense"},
      {"d_theda3", "In the Dark 3"},
      {"d_adrian", "Adrian's Asleep"},
      {"d_betwee", "Between Levels"},
      {"d_ddtblu", "The Dave D. Taylor Blues 1"},
      {"d_ddtbl3", "The Dave D. Taylor Blues 3"},
      {"d_stlks3", "The Healer Stalks 3"},
      {"d_ddtbl2", "The Dave D. Taylor Blues 2"},
      {"d_shawn3", "Shawn's got the Shotgun 3"},
      {"d_ampie", "Bye bye American Pie"},
      {"d_romer2", "Waiting for Romero to Play 2"},
      {"d_romero", "Waiting for Romero to Play 1"},
      {"d_evil", "Evil Incarnate"}      
   };
   print(s:"Track ", d: song + 1, s:"\n", s:songs[song][1] , s:", (", s:songs[song][0], s:")");
   SetMusic(songs[song][0]);
}

script "NextTrack" (void) { // NextTrack
   if (!fading) {
      song += 1;
      muted = FALSE;
      ACS_NamedSuspend("FadeOut", 0);
      SetMusicVolume(volume);
      ACS_NamedExecute("SetTrack", 0, 0);
   }
}

script "UnMute" (void) { // UnMute
   if (muted == FALSE) {
      fading = TRUE;
      print(s:"Music muted");
      ACS_NamedExecute("FadeOut", 0, 0);
      muted = TRUE;
   } else {
      fading = false;
      SetMusicVolume(volume);
      print(s:"Music unmuted");
      muted = FALSE;
   }
}

script "PrevTrack" (void) { // PrevTrack
   if (!fading) {
      song -= 1;
      muted = FALSE;
      ACS_NamedSuspend("FadeOut", 0);
      SetMusicVolume(volume);
      ACS_NamedExecute("SetTrack", 0, 0);
   }
}

script "FadeOut" (void) { // Fade out
   int fadevolume = volume;
   while (fading) {
      if (fadevolume > 0.0) {
         fadevolume -= 0.1;
         SetMusicVolume(fadevolume);
         Delay(1);
      } else {
         fading = false;
         break;
      }
   }
}

script "IncVolume" (void) { // Increase volume
   muted = FALSE;
   if (volume <= 1.0) {
      volume += 0.1;
      percent += 10;
      if (volume > 1.0) {
         volume = 1.0;
         percent = 100;
      }
      SetMusicVolume(volume);
      print(s:"Volume now at ", i:percent, s:"%");
   }
}

script "DecVolume" (void) { // Decrease volume
   muted = FALSE;
   if (volume > 0.0) {
      volume -= 0.1;
      percent -= 10;
      if (volume < 0.0) {
         volume = 0.0;
         percent = 0;
      }
      SetMusicVolume(volume);
      print(s:"Volume now at ", i:percent, s:"%");
   }
}

// =========================================================================================


Create a KEYCONF.txt for your custom pk3 resource file and add this:

Code: Select allExpand view
addmenukey "PrevTrack" PrevTrack
alias PrevTrack "pukename PrevTrack"
defaultbind KP1 "PrevTrack"

addmenukey "UnMute" UnMute
alias UnMute "pukename UnMute"
defaultbind KP2 "UnMute"

addmenukey "NextTrack" NextTrack
alias NextTrack "pukename NextTrack"
defaultbind KP3 "NextTrack"

addmenukey "IncVolume" Incvolume
alias Incvolume "pukename Incvolume"
defaultbind KP+ "Incvolume"

addmenukey "DecVolume" DecVolume
alias DecVolume "pukename DecVolume"
defaultbind KP- "DecVolume"


Now I'll just have to figure out how to "doom menu" this thing for key bindings...

Credits go to Naniyue (according to viewtopic.php?t=34433) for the inspiration.

...and OF COURSE, I just found what I was originally looking for: Jimmy's Jukebox viewtopic.php?f=46&t=29117
Last edited by vedan77 on Fri Sep 10, 2021 11:09 am, edited 5 times in total.
vedan77
 
Joined: 20 Aug 2021

Re: In-game music player

Postby Enjay » Fri Sep 10, 2021 7:06 am

If you use names for your scripts, rather than numbers, and ACS_NamedExecute in your ACS code, and also use pukename instead of puke in your KEYCONF, you are even less likely to run into conflicts with other peoples' scrips from any WADs you might be playing with.
User avatar
Enjay
Everyone is a moon, and has a dark side which he never shows to anybody. Twain
 
 
 
Joined: 15 Jul 2003
Location: Scotland

Re: In-game music player

Postby vedan77 » Fri Sep 10, 2021 7:11 am

Enjay wrote:If you use names for your scripts,


Nice, I'll probably do that. It also took me a while that even numbered scripts can be given a name via comment.

I'm already thinking about a shot fade out when changing track and adding a hud element... but menu options & better KEYCONF come first.

I just oriented myself on the random music player that I mentioned before and wanted to implement a quick and dirty solution for my trilogy.
vedan77
 
Joined: 20 Aug 2021


Return to Scripting

Who is online

Users browsing this forum: No registered users and 2 guests