Skip to content

Commit

Permalink
add XM music support
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte committed Nov 1, 2023
1 parent d154ce6 commit 93cb97a
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 28 deletions.
Binary file modified roboden_data/music/crush.ogg
Binary file not shown.
Binary file added roboden_data/music/crush.xm
Binary file not shown.
Binary file added roboden_data/music/deadly_windmills.xm
Binary file not shown.
Binary file modified roboden_data/music/track4.ogg
Binary file not shown.
Binary file added roboden_data/music/track4.xm
Binary file not shown.
Binary file added roboden_data/music/war_path.xm
Binary file not shown.
4 changes: 4 additions & 0 deletions src/assets/_data/raw/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ You can change this decision later.
##menu.options.extra : Extra
##menu.options.controls : Controls

##menu.options.music_player : Music player
##menu.option.music_player.xm : XM
##menu.option.music_player.ogg : OGG

##menu.terminal : Terminal
##menu.terminal.command_output_label : Command output
##menu.terminal.placeholder : type commands in here
Expand Down
4 changes: 4 additions & 0 deletions src/assets/_data/raw/ru.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
##menu.options.extra : Дополнительно
##menu.options.controls : Управление

##menu.options.music_player : Формат музыки
##menu.option.music_player.xm : XM
##menu.option.music_player.ogg : OGG

##menu.terminal : Терминал
##menu.terminal.command_output_label : Вывод команды
##menu.terminal.placeholder : введите команду
Expand Down
2 changes: 1 addition & 1 deletion src/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Config struct {
ExtraMusic bool
XM bool
}

const (
Expand Down
53 changes: 42 additions & 11 deletions src/assets/audio.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
package assets

import (
"fmt"
"io"
"runtime"
"strings"

resource "github.com/quasilyte/ebitengine-resource"
"github.com/quasilyte/ge"
"github.com/quasilyte/xm"
"github.com/quasilyte/xm/xmfile"

_ "image/png"
)

func RegisterMusicResource(ctx *ge.Context, config *Config, progress *float64) {
audioResources := map[resource.AudioID]resource.AudioInfo{
AudioMusicTrack1: {Path: "$music/deadly_windmills.ogg", Volume: -0.3, Group: SoundGroupMusic},
AudioMusicTrack2: {Path: "$music/war_path.ogg", Volume: -0.3, Group: SoundGroupMusic},
AudioMusicTrack3: {Path: "$music/crush.ogg", Volume: -0.3, Group: SoundGroupMusic},
}

if config.ExtraMusic {
audioResources[AudioMusicTrack4] = resource.AudioInfo{
Path: "$music/track4.ogg",
Volume: -0.3,
Group: SoundGroupMusic,
var audioResources map[resource.AudioID]resource.AudioInfo
if !config.XM {
audioResources = map[resource.AudioID]resource.AudioInfo{
AudioMusicTrack1: {Path: "$music/deadly_windmills.ogg", Volume: -0.3, Group: SoundGroupMusic},
AudioMusicTrack2: {Path: "$music/war_path.ogg", Volume: -0.3, Group: SoundGroupMusic, StreamDecorator: resource.LoopOGG},
AudioMusicTrack3: {Path: "$music/crush.ogg", Volume: -0.3, Group: SoundGroupMusic, StreamDecorator: resource.LoopOGG},
AudioMusicTrack4: {Path: "$music/track4.ogg", Volume: -0.3, Group: SoundGroupMusic},
}
} else {
audioResources = map[resource.AudioID]resource.AudioInfo{
AudioMusicTrack1: {Path: "$music/deadly_windmills.xm", Volume: -0.1, Group: SoundGroupMusic},
AudioMusicTrack2: {Path: "$music/war_path.xm", Volume: -0.1, Group: SoundGroupMusic},
AudioMusicTrack3: {Path: "$music/crush.xm", Volume: -0.1, Group: SoundGroupMusic},
AudioMusicTrack4: {Path: "$music/track4.xm", Volume: -0.1, Group: SoundGroupMusic},
}
xmParser := xmfile.NewParser(xmfile.ParserConfig{})
ctx.Loader.CustomAudioLoader = func(r io.Reader, info resource.AudioInfo) io.ReadSeeker {
if !strings.HasSuffix(info.Path, ".xm") {
return nil
}
m, err := xmParser.Parse(r)
if err != nil {
panic(fmt.Sprintf("parse %q module: %v", info.Path, err))
}
s := xm.NewStream()
s.SetLooping(true)
config := xm.LoadModuleConfig{
LinearInterpolation: true,
}
if err := s.LoadModule(m, config); err != nil {
panic(fmt.Sprintf("load %q module: %v", info.Path, err))
}
return s
}
}

Expand All @@ -36,6 +63,10 @@ func RegisterMusicResource(ctx *ge.Context, config *Config, progress *float64) {
runtime.Gosched()
}
}

// The custom music is still available via LoadAudio,
// but the XM parser should be garbage collected after we set this field to nil.
ctx.Loader.CustomAudioLoader = nil
}

func RegisterAudioResource(ctx *ge.Context, config *Config, progress *float64) {
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/game/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func main() {
state.ServerProtocol = "https"
state.ServerHost = "quasilyte.tech"
state.ServerPath = "/roboden/api"
// Also, force XM tracks when running a web build.
// We don't bundle OGG tracks for it.
state.Persistent.Settings.XM = true
}

ctx := ge.NewContext(ge.ContextConfig{
Expand Down Expand Up @@ -217,7 +220,6 @@ func newLevelConfig(options *gamedata.LevelConfig) *gamedata.LevelConfig {

func getDefaultSessionState() *session.State {
state := &session.State{
ExtraMusic: runtime.GOARCH != "wasm",
ReverseLevelConfig: newLevelConfig(&gamedata.LevelConfig{
ReplayLevelConfig: serverapi.ReplayLevelConfig{
Teleporters: 1,
Expand Down
5 changes: 5 additions & 0 deletions src/contentlock/contentlock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package contentlock

import (
"runtime"

"github.com/quasilyte/ge"
"github.com/quasilyte/ge/xslices"
"github.com/quasilyte/roboden-game/gamedata"
Expand All @@ -18,6 +20,9 @@ func GetDefaultData() session.PersistentData {
// The default settings.
FirstLaunch: true,
Settings: session.GameSettings{
// Web platforms have XM music set as default.
XM: runtime.GOARCH == "wasm",

WheelScrollingMode: int(gameinput.WheelScrollDrag),
Player1InputMethod: int(gameinput.InputMethodCombined),
Player2InputMethod: int(gameinput.InputMethodGamepad2),
Expand Down
5 changes: 4 additions & 1 deletion src/gamedata/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,7 @@ const SeasonNumber = 1
// - Added Megaroomba relict
// - Fixed a memory leak issue (solves a performance problem)
// - Added screen filters
const BuildNumber int = 21
//
// # Version 22
// - Added XM music support (optional for desktops, mandatory for wasm builds)
const BuildNumber int = 22
3 changes: 2 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hajimehoshi/go-steamworks v0.0.0-20230809071343-ee1a81910acc
github.com/mattn/go-sqlite3 v1.14.16
github.com/quasilyte/ebitengine-resource v0.5.1-0.20230412072401-66c02806357e
github.com/quasilyte/ge v0.0.0-20230925153958-e5b2a565e636
github.com/quasilyte/ge v0.0.0-20231101095822-da2497518aca
github.com/quasilyte/gmath v0.0.0-20230626195558-43d7cdc669ef
github.com/quasilyte/gsignal v0.0.0-20231010082051-3c00e9ebb4e5
golang.org/x/image v0.12.0
Expand All @@ -24,6 +24,7 @@ require (
github.com/jezek/xgb v1.1.0 // indirect
github.com/jfreymuth/oggvorbis v1.0.5 // indirect
github.com/jfreymuth/vorbis v1.0.2 // indirect
github.com/quasilyte/xm v0.0.0-20231101102547-462698b5183e // indirect
golang.org/x/exp v0.0.0-20221023144134-a1e5550cf13e // indirect
golang.org/x/exp/shiny v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mobile v0.0.0-20230922142353-e2f452493d57 // indirect
Expand Down
8 changes: 8 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ github.com/quasilyte/ebitenui v0.0.0-20230726093423-e2a7a066d711 h1:jYfAMq+T8Sbk
github.com/quasilyte/ebitenui v0.0.0-20230726093423-e2a7a066d711/go.mod h1:6Ty4FIhkLzzwO5kOpRQq+Ja4yP2eTE0ASg6y0eo5cvA=
github.com/quasilyte/ge v0.0.0-20230925153958-e5b2a565e636 h1:SXdyww/bTMf99bA7IJBkiE4DDhxj2G4Xk9YY7nYVtRM=
github.com/quasilyte/ge v0.0.0-20230925153958-e5b2a565e636/go.mod h1:Ybw5zUJaepnaxpYwjdwFeiqx0gHf7Um+Om62bR9oUbE=
github.com/quasilyte/ge v0.0.0-20231101092205-0ac6a94c4f0a h1:8tlj2eIvj74dV+Cve1GcwADQHNwFnKmhMMxXPhT9JG0=
github.com/quasilyte/ge v0.0.0-20231101092205-0ac6a94c4f0a/go.mod h1:Ybw5zUJaepnaxpYwjdwFeiqx0gHf7Um+Om62bR9oUbE=
github.com/quasilyte/ge v0.0.0-20231101095822-da2497518aca h1:9t3sBiejFYi+2ms4/IOu2ab86yi/mvyIHCvtjBnZV4s=
github.com/quasilyte/ge v0.0.0-20231101095822-da2497518aca/go.mod h1:Ybw5zUJaepnaxpYwjdwFeiqx0gHf7Um+Om62bR9oUbE=
github.com/quasilyte/gmath v0.0.0-20230626195558-43d7cdc669ef h1:ylROAnCA+a6Ry6M9pkRXzL+bhPIT5ctHND5kcdxbwe4=
github.com/quasilyte/gmath v0.0.0-20230626195558-43d7cdc669ef/go.mod h1:EbI+KMbALSVE2s0YFOQpR4uj66zBh9ter5P4CBMSuvA=
github.com/quasilyte/gsignal v0.0.0-20230130114532-ff67b4bb9e15 h1:7JgEJ6KA+VQtt+Bbx2MpV4LttBOJTLH4ehB/R3DfijM=
github.com/quasilyte/gsignal v0.0.0-20230130114532-ff67b4bb9e15/go.mod h1:qAMDDsI56AFnSrBZYG8Tj/Ys+sRsakd8Ho2be2S8Vfc=
github.com/quasilyte/gsignal v0.0.0-20231010082051-3c00e9ebb4e5 h1:G6D9dfcBvveLmNi8B7de96/347Xk8rvTyhQb6uNwWPQ=
github.com/quasilyte/gsignal v0.0.0-20231010082051-3c00e9ebb4e5/go.mod h1:qAMDDsI56AFnSrBZYG8Tj/Ys+sRsakd8Ho2be2S8Vfc=
github.com/quasilyte/xm v0.0.0-20231101074402-c7810c49909b h1:EVDWLRRy7/IC/1OsaR7m/Co/wsCF4z7Hv3Q6rDGP4nA=
github.com/quasilyte/xm v0.0.0-20231101074402-c7810c49909b/go.mod h1:NfR3uYC5yXgsGuSDHSXep+EiAPnNsa7/0jpWyGvRxng=
github.com/quasilyte/xm v0.0.0-20231101102547-462698b5183e h1:dT0WchDsDNCWaqln28OHIdi2RQn83wgthr7Q9usrrp4=
github.com/quasilyte/xm v0.0.0-20231101102547-462698b5183e/go.mod h1:NfR3uYC5yXgsGuSDHSXep+EiAPnNsa7/0jpWyGvRxng=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
2 changes: 1 addition & 1 deletion src/runsim/runsim.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewState(ctx *ge.Context) *session.State {

func PrepareAssets(ctx *ge.Context) {
assetsConfig := &assets.Config{
ExtraMusic: false,
XM: true,
}
var progress float64
assets.RegisterImageResources(ctx, assetsConfig, &progress)
Expand Down
10 changes: 9 additions & 1 deletion src/scenes/menus/bootload.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ func (c *BootloadController) Init(scene *ge.Scene) {
name string
f func(*ge.Context, *assets.Config, *float64)
}

config := &assets.Config{
ExtraMusic: c.state.ExtraMusic,
XM: c.state.Persistent.Settings.XM,
}

if c.state.Persistent.Settings.XM {
c.state.Logf("loading XM music")
} else {
c.state.Logf("loading OGG music")
}

steps := []initializationStep{
{name: "load_images", f: assets.RegisterImageResources},
{name: "load_audio", f: assets.RegisterAudioResource},
Expand Down
18 changes: 18 additions & 0 deletions src/scenes/menus/options_soundmenu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package menus

import (
"runtime"

"github.com/quasilyte/ge"

"github.com/quasilyte/roboden-game/assets"
Expand Down Expand Up @@ -80,6 +82,22 @@ func (c *OptionsSoundMenuController) initUI() {
}))
}

{
b := eui.NewBoolSelectButton(eui.BoolSelectButtonConfig{
Scene: c.scene,
Resources: uiResources,
Value: &options.XM,
Label: d.Get("menu.options.music_player"),
ValueNames: []string{
d.Get("menu.option.music_player.ogg"),
d.Get("menu.option.music_player.xm"),
},
})
rowContainer.AddChild(b)
// Don't allow web platforms to change the music player.
b.GetWidget().Disabled = runtime.GOARCH == "wasm"
}

rowContainer.AddChild(eui.NewTransparentSeparator())

rowContainer.AddChild(eui.NewButton(uiResources, c.scene, d.Get("menu.back"), func() {
Expand Down
13 changes: 5 additions & 8 deletions src/scenes/staging/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type musicTrack struct {
}

var musicTrackList = []musicTrack{
{audio: assets.AudioMusicTrack1, length: 5*time.Minute + 12*time.Second},
{audio: assets.AudioMusicTrack2, length: 2*time.Minute + 44*time.Second},
{audio: assets.AudioMusicTrack3, length: 2*time.Minute + 3*time.Second},
{audio: assets.AudioMusicTrack4, length: 2*time.Minute + 37*time.Second},
{audio: assets.AudioMusicTrack1, length: 5*time.Minute + 13*time.Second},
{audio: assets.AudioMusicTrack2, length: 2*time.Minute + 45*time.Second},
{audio: assets.AudioMusicTrack3, length: 2*time.Minute + 5*time.Second},
{audio: assets.AudioMusicTrack4, length: 2*time.Minute + 38*time.Second},
}

type musicPlayer struct {
Expand All @@ -31,13 +31,10 @@ type musicPlayer struct {
timeCheckDelay float64
}

func newMusicPlayer(scene *ge.Scene, extraTrack bool) *musicPlayer {
func newMusicPlayer(scene *ge.Scene) *musicPlayer {
p := &musicPlayer{scene: scene}

numTracks := len(musicTrackList) - 1
if !extraTrack {
numTracks--
}
p.musicTrackSlider.SetBounds(0, numTracks)

return p
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/staging/staging_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (c *Controller) doInit(scene *ge.Scene) {
scene.Context().Rand.SetSeed((c.config.Seed + 42) * 21917)
c.scene = scene

c.musicPlayer = newMusicPlayer(scene, c.state.ExtraMusic)
c.musicPlayer = newMusicPlayer(scene)
c.musicPlayer.Start()

if c.state.CPUProfile != "" {
Expand Down
3 changes: 1 addition & 2 deletions src/session/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ type State struct {
MemProfile string
MemProfileWriter io.WriteCloser

ExtraMusic bool

ServerProtocol string
ServerHost string
ServerPath string
Expand Down Expand Up @@ -159,6 +157,7 @@ type GameSettings struct {
EdgeScrollRange int
HintMode int
WheelScrollingMode int
XM bool
ShowFPS bool
ShowTimer bool
DebugLogs bool
Expand Down

0 comments on commit 93cb97a

Please sign in to comment.