Skip to content

Commit

Permalink
Add menu music courtesy of franzopow
Browse files Browse the repository at this point in the history
- Credit: Francesco Corrado for menu_master.ogg and lost_space_explorer.ogg
- Refactor MusicPlayer.lua to expose an API for other modules to consume.
- Remove SoundMusic's dependency on Lua, decouple through a signal
  • Loading branch information
Web-eWorks committed Nov 28, 2020
1 parent a62655c commit 6f1893a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 31 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The Pioneer Developers are:
* Enric Morales
* Florian Schmidt
* Fran Morton
* Francesco Corrado
* Frank Klippenberg
* Gareth Allnutt
* Gaz Davidson
Expand Down Expand Up @@ -135,7 +136,7 @@ Pioneer includes the following third-party software:

Lua 5.2.1 by R. Ierusalimschy, L. H. de Figueiredo & W. Celes,
Copyright (C) 1994-2012 Lua.org, PUC-Rio
Licensed under the MIT licence (see contrib/lua/lua.h)
Licensed under the MIT licence (see contrib/lua/src/lua.h)

GLEW: The OpenGL Extension Wrangler
Copyright (C) 2002-2008, Milan Ikits <milan ikits[]ieee org>
Expand Down
61 changes: 34 additions & 27 deletions data/modules/MusicPlayer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ local Music = require 'Music'
local Event = require 'Event'
local SystemPath = require 'SystemPath'

local MusicPlayer = {}

local music = {}

local getCategoryForSong = function (name)
function MusicPlayer.getCategoryForSong(name)
if not name then return "" end
local _, _, category = string.find(name, "^music/core/([%l-]+)/")
return category
end

local playRandomSongFromCategory = function (category)
function MusicPlayer.playRandomSongFromCategory(category, loop)
-- if there's no song in the wanted category then do nothing
if not music[category] then return end

local current_song = Music.GetSongName()
local current_category = getCategoryForSong(current_song)
local current_category = MusicPlayer.getCategoryForSong(current_song)

if Music.IsPlaying() then
-- no category means some other script is playing something and we
Expand Down Expand Up @@ -48,9 +50,9 @@ local playRandomSongFromCategory = function (category)
end

if Music.IsPlaying() then
Music.FadeIn(song, 0.5, false)
Music.FadeIn(song, 0.5, loop)
else
Music.Play(song, false)
Music.Play(song, loop)
end
end

Expand Down Expand Up @@ -79,45 +81,48 @@ local playAmbient = function ()
if not category then
if Game.system then
if Game.system:DistanceTo(sol) > unexplored_distance then
playRandomSongFromCategory("unexplored")
MusicPlayer.playRandomSongFromCategory("unexplored")
else
playRandomSongFromCategory("space")
MusicPlayer.playRandomSongFromCategory("space")
end
else
playRandomSongFromCategory("unexplored")
MusicPlayer.playRandomSongFromCategory("unexplored")
end
return
end

-- switch to the specific music. if the music doesn't start (ie we don't
-- have any specific music) then fall back to normal space music
playRandomSongFromCategory(category)
MusicPlayer.playRandomSongFromCategory(category)
if not Music.IsPlaying() then
if Game.system then
if Game.system:DistanceTo(sol) > unexplored_distance then
playRandomSongFromCategory("unexplored")
MusicPlayer.playRandomSongFromCategory("unexplored")
else
playRandomSongFromCategory("space")
MusicPlayer.playRandomSongFromCategory("space")
end
else
playRandomSongFromCategory("unexplored")
MusicPlayer.playRandomSongFromCategory("unexplored")
end
end
end

Event.Register("onGameStart", function ()
function MusicPlayer.rebuildSongList()
music = {}

-- get all the interesting songs by category
local songs = Music.GetSongList()
for n,key in pairs(songs) do
local category = getCategoryForSong(key)
local category = MusicPlayer.getCategoryForSong(key)
if category then
if not music[category] then music[category] = {} end
table.insert(music[category], key)
end
end
end

Event.Register("onGameStart", function ()
MusicPlayer.rebuildSongList()
playAmbient()
end)

Expand All @@ -131,18 +136,18 @@ end)
Event.Register("onEnterSystem", function ()
local player_body = Game.player.frameBody
if player_body and not Game.system.explored then
if player_body.type == 'STAR_SM_BH' then
playRandomSongFromCategory("discovery")
if player_body.type == 'STAR_SM_BH' then
MusicPlayer.playRandomSongFromCategory("discovery")
elseif player_body.type == 'STAR_IM_BH' then
playRandomSongFromCategory("discovery")
MusicPlayer.playRandomSongFromCategory("discovery")
elseif player_body.type == 'STAR_S_BH' then
playRandomSongFromCategory("discovery")
MusicPlayer.playRandomSongFromCategory("discovery")
elseif player_body.type == 'STAR_O_WF' then
playRandomSongFromCategory("discovery")
MusicPlayer.playRandomSongFromCategory("discovery")
elseif player_body.type == 'STAR_B_WF' then
playRandomSongFromCategory("discovery")
MusicPlayer.playRandomSongFromCategory("discovery")
elseif player_body.type == 'STAR_M_WF' then
playRandomSongFromCategory("discovery")
MusicPlayer.playRandomSongFromCategory("discovery")
else
playAmbient()
end
Expand All @@ -154,31 +159,31 @@ end)
-- ship or player destruction (aka game over)
Event.Register("onShipDestroyed", function (ship, attacker)
if ship:IsPlayer() then
playRandomSongFromCategory("player-destroyed")
MusicPlayer.playRandomSongFromCategory("player-destroyed")
elseif attacker:isa("Ship") and attacker:IsPlayer() then
playRandomSongFromCategory("ship-destroyed")
MusicPlayer.playRandomSongFromCategory("ship-destroyed")
end
end)

-- player docked
Event.Register("onShipDocked", function (ship, station)
if not ship:IsPlayer() then return end
playRandomSongFromCategory("docked")
MusicPlayer.playRandomSongFromCategory("docked")
end)

-- player undocked
Event.Register("onShipUndocked", function (ship, station)
if not ship:IsPlayer() then return end
playRandomSongFromCategory("undocked")
MusicPlayer.playRandomSongFromCategory("undocked")
end)

-- ship near the player
Event.Register("onShipAlertChanged", function (ship, alert)
if not ship:IsPlayer() then return end
if alert == "SHIP_NEARBY" and not ship:IsDocked() then
playRandomSongFromCategory("ship-nearby")
MusicPlayer.playRandomSongFromCategory("ship-nearby")
elseif alert == "SHIP_FIRING" then
playRandomSongFromCategory("ship-firing")
MusicPlayer.playRandomSongFromCategory("ship-firing")
end
end)

Expand All @@ -189,3 +194,5 @@ Event.Register("onFrameChanged", function (body)

playAmbient()
end)

return MusicPlayer
Binary file added data/music/core/menu/menu_master.ogg
Binary file not shown.
Binary file not shown.
10 changes: 9 additions & 1 deletion data/pigui/views/mainmenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local ShipDef = require 'ShipDef'
local SystemPath = require 'SystemPath'
local Equipment = require 'Equipment'
local Format = require 'Format'
local Event = require 'Event'
local MusicPlayer = require 'modules.MusicPlayer'
local Lang = require 'Lang'
local FlightLog = require ("FlightLog")

Expand Down Expand Up @@ -173,9 +173,17 @@ local function callModules(mode)
end
end

local hasMusicList = false

local overlayWindowFlags = ui.WindowFlags {"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus", "AlwaysAutoResize"}
local mainMenuFlags = ui.WindowFlags{"NoTitleBar", "NoResize", "NoFocusOnAppearing", "NoBringToFrontOnFocus"}
local function showMainMenu()
if not hasMusicList then
hasMusicList = true
MusicPlayer.rebuildSongList()
MusicPlayer.playRandomSongFromCategory("menu", true)
end

local showContinue = canContinue()
local buttons = 4

Expand Down
5 changes: 5 additions & 0 deletions src/lua/LuaMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#include "LuaMusic.h"
#include "LuaEvent.h"
#include "LuaObject.h"
#include "LuaUtils.h"
#include "Pi.h"
Expand Down Expand Up @@ -255,5 +256,9 @@ void LuaMusic::Register()
lua_setfield(l, -2, "Music");
lua_pop(l, 1);

Pi::GetMusicPlayer().onSongFinished.connect([]() {
LuaEvent::Queue("onSongFinished");
});

LUA_DEBUG_END(l, 0);
}
3 changes: 1 addition & 2 deletions src/sound/SoundMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "SoundMusic.h"
#include "Pi.h"
#include "libs.h" //for clamp
#include "lua/LuaEvent.h"
#include <map>

namespace Sound {
Expand Down Expand Up @@ -98,7 +97,7 @@ namespace Sound {
if (m_playing) { //expecting report
if ((m_eventOnePlaying && !m_eventOne.IsPlaying()) || (!m_eventOnePlaying && !m_eventTwo.IsPlaying())) {
m_playing = false;
LuaEvent::Queue("onSongFinished");
onSongFinished.emit();
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/sound/SoundMusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Sound.h"
#include <SDL.h>
#include <sigc++/sigc++.h>
#include <string>
#include <vector>

Expand Down Expand Up @@ -33,6 +34,8 @@ namespace Sound {
bool IsPlaying() const;
void SetEnabled(bool);

sigc::signal<void> onSongFinished;

private:
float m_volume;
//two streams for crossfade
Expand Down

0 comments on commit 6f1893a

Please sign in to comment.