Skip to content

Commit

Permalink
New PlayBGM param: StartPosition
Browse files Browse the repository at this point in the history
This parameter lets you choose the sample point where the music should start playing. It can be used in PlayBGM SCTRL and lua function.
  • Loading branch information
Lazin3ss committed Dec 2, 2022
1 parent 9f71572 commit 890dce9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7538,14 +7538,15 @@ const (
playBgm_loop
playBgm_loopstart
playBgm_loopend
playBgm_startposition
playBgm_redirectid
)

func (sc playBgm) Run(c *Char, _ []int32) bool {
crun := c
var b bool
var bgm string
var loop, volume, loopstart, loopend int = 1, 100, 0, 0
var loop, volume, loopstart, loopend, startposition int = 1, 100, 0, 0, 0
StateControllerBase(sc).run(c, func(id byte, exp []BytecodeExp) bool {
switch id {
case playBgm_bgm:
Expand All @@ -7565,6 +7566,8 @@ func (sc playBgm) Run(c *Char, _ []int32) bool {
loopstart = int(exp[0].evalI(c))
case playBgm_loopend:
loopend = int(exp[0].evalI(c))
case playBgm_startposition:
startposition = int(exp[0].evalI(c))
case playBgm_redirectid:
if rid := sys.playerID(exp[0].evalI(c)); rid != nil {
crun = rid
Expand All @@ -7575,7 +7578,7 @@ func (sc playBgm) Run(c *Char, _ []int32) bool {
return true
})
if b {
sys.bgm.Open(bgm, loop, volume, loopstart, loopend)
sys.bgm.Open(bgm, loop, volume, loopstart, loopend, startposition)
sys.playBgmFlg = true
}
return false
Expand Down
4 changes: 4 additions & 0 deletions src/compiler_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4147,6 +4147,10 @@ func (c *Compiler) playBgm(is IniSection, sc *StateControllerBase, _ int8) (Stat
playBgm_loopend, VT_Int, 1, false); err != nil {
return err
}
if err := c.paramValue(is, sc, "startposition",
playBgm_startposition, VT_Int, 1, false); err != nil {
return err
}
return nil
})
return *ret, err
Expand Down
9 changes: 6 additions & 3 deletions src/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ func systemScriptInit(l *lua.LState) {
l.Push(lua.LNumber(winp))
l.Push(tbl)
if sys.playBgmFlg {
sys.bgm.Open("", 1, 100, 0, 0)
sys.bgm.Open("", 1, 100, 0, 0, 0)
sys.playBgmFlg = false
}
sys.clearAllSound()
Expand Down Expand Up @@ -1536,7 +1536,7 @@ func systemScriptInit(l *lua.LState) {
return 0
})
luaRegister(l, "playBGM", func(l *lua.LState) int {
var loop, volume, loopstart, loopend int = 1, 100, 0, 0
var loop, volume, loopstart, loopend, startposition int = 1, 100, 0, 0, 0
if l.GetTop() >= 2 {
loop = int(numArg(l, 2))
}
Expand All @@ -1549,7 +1549,10 @@ func systemScriptInit(l *lua.LState) {
if l.GetTop() >= 5 && numArg(l, 5) > 1 {
loopend = int(numArg(l, 5))
}
sys.bgm.Open(strArg(l, 1), loop, volume, loopstart, loopend)
if l.GetTop() >= 6 && numArg(l, 6) > 1 {
startposition = int(numArg(l, 6))
}
sys.bgm.Open(strArg(l, 1), loop, volume, loopstart, loopend, startposition)
return 0
})
luaRegister(l, "playerBufReset", func(*lua.LState) int {
Expand Down
3 changes: 2 additions & 1 deletion src/sound.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func newBgm() *Bgm {
return &Bgm{}
}

func (bgm *Bgm) Open(filename string, loop, bgmVolume, bgmLoopStart, bgmLoopEnd int) {
func (bgm *Bgm) Open(filename string, loop, bgmVolume, bgmLoopStart, bgmLoopEnd, startPosition int) {
bgm.filename = filename
bgm.loop = loop
bgm.bgmVolume = bgmVolume
Expand Down Expand Up @@ -211,6 +211,7 @@ func (bgm *Bgm) Open(filename string, loop, bgmVolume, bgmLoopStart, bgmLoopEnd
resampler := beep.Resample(audioResampleQuality, format.SampleRate, audioFrequency, bgm.volctrl)
bgm.ctrl = &beep.Ctrl{Streamer: resampler}
bgm.UpdateVolume()
bgm.streamer.Seek(startPosition)
speaker.Play(bgm.ctrl)
}

Expand Down
2 changes: 1 addition & 1 deletion src/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ func (s *System) fight() (reload bool) {

//default bgm playback, used only in Quick VS or if externalized Lua implementaion is disabled
if s.round == 1 && (s.gameMode == "" || len(sys.commonLua) == 0) {
s.bgm.Open(s.stage.bgmusic, 1, int(s.stage.bgmvolume), int(s.stage.bgmloopstart), int(s.stage.bgmloopend))
s.bgm.Open(s.stage.bgmusic, 1, int(s.stage.bgmvolume), int(s.stage.bgmloopstart), int(s.stage.bgmloopend), 0)
}

oldWins, oldDraws := s.wins, s.draws
Expand Down

0 comments on commit 890dce9

Please sign in to comment.