Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for LOOPSTART and LOOPLENGTH tags #1695

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 107 additions & 8 deletions Monika After Story/game/zz_music_selector.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ init -1 python in songs:
return ""

if _ext == EXT_OGG:
return _getOggLoop(_audio_file)
return _getOggLoop(_audio_file, _ext)

elif _ext == EXT_OPUS:
return _getOggLoop(_audio_file)
return _getOggLoop(_audio_file, _ext)

return ""

Expand Down Expand Up @@ -414,23 +414,50 @@ init -1 python in songs:
return sel_name


def _getOggLoop(_audio_file):
def _getOggLoop(_audio_file, _ext):
"""
Attempts to retreive loop data from Ogg tags

IN:
_audio_file - audio object
_ext - extension of the audio file

RETURNS:
the loop string we should use, or "" if no loop
"""
# first, try MAS tags
loopstart = _audio_file.tags.get(MT_LSTART, [])
loopend = _audio_file.tags.get(MT_LEND, [])

# pre-check that we even have values
if not loopstart and not loopend:
if loopstart or loopend:
return _getOggLoopMAS(loopstart, loopend, _audio_file)

# if not found, double check that we are ogg before continuing
if _ext != EXT_OGG:
return ""

# if ogg, we can try the RPGMaker sample tags
loopstart = _audio_file.tags.get(MT_LSSTART, [])
looplen = _audio_file.tags.get(MT_LSEND, [])

if loopstart:
return _getOggLoopRPG(loopstart, looplen, _audio_file)

return ""


def _getOggLoopMAS(loopstart, loopend, _audio_file):
"""
Attempts to retrieve MAS-based loop data from Ogg tags

IN:
loopstart - list of loopstart tags
loopend - list of loopend tags
_audio_file - audio object

RETURNS:
the loop string we should use or "" if no loop
"""
# now try to float these values
try:
if loopstart:
Expand All @@ -455,7 +482,7 @@ init -1 python in songs:
loopstart = 0

if loopend is not None and loopend > _audio_file.info.length:
loopend = _audio_file.info.length
loopend = None

# NOTE: we shoudl for sure have at least one of these tags by now
# now we can build the tag
Expand All @@ -474,6 +501,71 @@ init -1 python in songs:
return " ".join(_tag_elems)


def _getOggLoopRPG(loopstart, looplen, _audio_file):
"""
Attempts to retrieve RPGMaker-based loop data form Ogg tags

NOTE: unlike the MAS tags, loopstart is REQUIRED

IN:
loopstart - list of loopstart tags
looplen - list of loop length tags
_audio_file - audio object

RETURNS:
the loop string we should use or "" if no loop
"""
# int these values
try:
loopstart = int(loopstart[0])

if looplen:
looplen = int(looplen[0])

else:
looplen = None

except:
# error in parsing tags.
return ""

# now we have ints
# convert these into seconds
_sample_rate = float(_audio_file.info.sample_rate)
loopstart = loopstart / _sample_rate

if looplen is not None:
looplen = looplen / _sample_rate

# validations
if loopstart < 0:
loopstart = 0

loopend = None
if looplen is not None:

# calculate endpoint
loopend = loopstart + looplen

if loopend > _audio_file.info.length:
loopend = None

# now we can bulid the tag
_tag_elems = [
RPY_START,
RPY_FROM,
str(loopstart)
]

if loopend is not None:
_tag_elems.append(RPY_TO)
_tag_elems.append(str(loopend))

_tag_elems.append(RPY_END)

return " ".join(_tag_elems)


def _getOpus(filepath):
"""
Attempts to retrieve the Opus object from the given audio file
Expand Down Expand Up @@ -581,10 +673,17 @@ init -1 python in songs:

# NOTE: we default looping, so think of this as loop start and loop end
# seconds to start playback
MT_LSTART = "loopstart"
MT_LSTART = "masloopstart"

# seconds to end playback
MT_LEND = "loopend"
MT_LEND = "masloopend"

# for RPGMaker support
# samples to start playback
MT_LSSTART = "loopstart"

# length of loop
MT_LSEND = "looplength"

# renpy audio tags
RPY_START = "<"
Expand Down