diff --git a/src/tja2fumen/converters.py b/src/tja2fumen/converters.py index 71a513a..ea65dc3 100644 --- a/src/tja2fumen/converters.py +++ b/src/tja2fumen/converters.py @@ -51,6 +51,12 @@ def processTJACommands(tja): measureTJAProcessed.delay = data.value * 1000 # ms -> s elif data.name == 'branchStart': measureTJAProcessed.branchStart = data.value + # If the measure immediately preceding a #BRANCHSTART has a #SECTION command, then remove it. + # From TJA spec: "Placing [a #SECTION command] near #BRANCHSTART or a measure before does not reset + # the accuracy for that branch. The value is calculated before it and a measure + # has not started yet at that point." + if tjaBranchesProcessed[branchName][-1].branchStart == ["#SECTION", -1, -1]: + tjaBranchesProcessed[branchName][-1].branchStart = None elif data.name == 'barline': currentBarline = bool(int(data.value)) measureTJAProcessed.barline = currentBarline @@ -208,6 +214,10 @@ def convertTJAToFumen(tja): vals.append(int(percent * 100)) elif measureTJAProcessed.branchStart[0] == 'r': vals = measureTJAProcessed.branchStart[1:] + # If it's a #SECTION command, use the branch condition values as-is AND reset the accuracy + elif measureTJAProcessed.branchStart[0] == '#SECTION': + vals = measureTJAProcessed.branchStart[1:] + note_counter_branch = 0 # Determine which bytes to assign the values to if currentBranch == 'normal': idx_b1, idx_b2 = 0, 1 diff --git a/src/tja2fumen/parsers.py b/src/tja2fumen/parsers.py index 01e6a32..b186d88 100644 --- a/src/tja2fumen/parsers.py +++ b/src/tja2fumen/parsers.py @@ -111,6 +111,7 @@ def parseCourseMeasures(course): # Check if the course has branches or not hasBranches = True if [l for l in course.data if l.name == 'BRANCHSTART'] else False currentBranch = 'all' if hasBranches else 'normal' + branch_condition = None flagLevelhold = False # Process course lines @@ -133,7 +134,7 @@ def parseCourseMeasures(course): # 2. Parse measure commands that produce an "event" elif line.name in ['GOGOSTART', 'GOGOEND', 'BARLINEON', 'BARLINEOFF', 'DELAY', - 'SCROLL', 'BPMCHANGE', 'MEASURE', 'BRANCHSTART']: + 'SCROLL', 'BPMCHANGE', 'MEASURE', 'SECTION', 'BRANCHSTART']: # Get position of the event for branch in course.branches.keys() if currentBranch == 'all' else [currentBranch]: pos = len(course.branches[branch][idx_m].notes) @@ -155,6 +156,14 @@ def parseCourseMeasures(course): currentEvent = TJAData('bpm', float(line.value), pos) elif line.name == 'MEASURE': currentEvent = TJAData('measure', line.value, pos) + elif line.name == 'SECTION': + # If #SECTION occurs before the first #BRANCHSTART condition, then we have no percentage/drumroll values + # to use for the branchInfo bytes when writing to the fumen. So, we just use default values (-1, -1). + if branch_condition is None: + branch_condition = ['#SECTION', -1, -1] + # Otherwise, if #SECTION occurs after a #BRANCHSTART condition, then we just repeat the previous + # condition (to set the correct branchInfo bytes for this measure.) + currentEvent = TJAData('branchStart', branch_condition, pos) elif line.name == 'BRANCHSTART': if flagLevelhold: continue @@ -172,10 +181,6 @@ def parseCourseMeasures(course): # Append event to the current measure's events for branch in course.branches.keys() if currentBranch == 'all' else [currentBranch]: course.branches[branch][idx_m].events.append(currentEvent) - elif line.name == 'SECTION': - # Simply repeat the same #BRANCHSTART condition that happened previously - # The purpose of #SECTION is to "Reset accuracy values for notes and drumrolls on the next measure." - course.branches[currentBranch][idx_m].events.append(TJAData('branchStart', branch_condition, pos)) # 3. Parse commands that don't create an event (e.g. simply changing the current branch) else: