Optimized the calculation for linebreaks

This commit is contained in:
Vadim committed 2024-06-02 17:58:24 +02:00
1 parent 2801ae9541
commit fe27fa344e
6 files changed
+113 -23

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
class Settings:
APP_VERSION = "0.0.10"
APP_VERSION = "0.0.11-dev1"
create_midi = True
create_plot = False
+58 -15
View File
@@ -1,39 +1,51 @@
"""Ultrastar writer module"""
import re
from packaging import version
import langcodes
from packaging import version
from modules.console_colors import ULTRASINGER_HEAD
from modules.Ultrastar.ultrastar_converter import (
real_bpm_to_ultrastar_bpm,
second_to_beat,
beat_to_second,
)
from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag, UltrastarTxtNoteTypeTag, \
FILE_ENCODING
from modules.Speech_Recognition.TranscribedData import TranscribedData
from modules.Ultrastar.ultrastar_score_calculator import Score
def get_thirtytwo_note_second(real_bpm: float):
"""Converts a beat to a 1/32 note in second"""
return 60 / real_bpm / 8
def get_sixteenth_note_second(real_bpm: float):
"""Converts a beat to a 1/16 note in second"""
return 60 / real_bpm / 4
def get_eighth_note_second(real_bpm: float):
"""Converts a beat to a 1/8 note in second"""
return 60 / real_bpm / 2
def get_quarter_note_second(real_bpm: float):
"""Converts a beat to a 1/4 note in second"""
return 60 / real_bpm
def get_half_note_second(real_bpm: float):
"""Converts a beat to a 1/2 note in second"""
return 60 / real_bpm * 2
def get_whole_note_second(real_bpm: float):
"""Converts a beat to a 1/1 note in second"""
return 60 / real_bpm * 4
def get_multiplier(real_bpm: float) -> int:
"""Calculates the multiplier for the BPM"""
@@ -47,6 +59,7 @@ def get_multiplier(real_bpm: float) -> int:
multiplier += 1
return multiplier - 2
def get_language_name(language: str) -> str:
"""Creates the language name from the language code"""
@@ -58,7 +71,7 @@ def create_ultrastar_txt_from_automation(
note_numbers: list[int],
ultrastar_file_output: str,
ultrastar_class: UltrastarTxtValue,
bpm=120,
real_bpm=120,
) -> None:
"""Creates an Ultrastar txt file from the automation data"""
@@ -66,10 +79,10 @@ def create_ultrastar_txt_from_automation(
f"{ULTRASINGER_HEAD} Creating {ultrastar_file_output} from transcription."
)
ultrastar_bpm = real_bpm_to_ultrastar_bpm(bpm)
ultrastar_bpm = real_bpm_to_ultrastar_bpm(real_bpm)
multiplication = get_multiplier(ultrastar_bpm)
ultrastar_bpm = ultrastar_bpm * get_multiplier(ultrastar_bpm)
silence_split_duration = get_sixteenth_note_second(bpm)
silence_split_duration = calculate_silent_beat_length(transcribed_data)
with open(ultrastar_file_output, "w", encoding=FILE_ENCODING) as file:
gap = transcribed_data[0].start
@@ -103,18 +116,26 @@ def create_ultrastar_txt_from_automation(
# Write the singing part
previous_end_beat = 0
separated_word_silence = [] # This is a workaround for separated words that get his ends to far away
for i, data in enumerate(transcribed_data):
start_time = (data.start - gap) * multiplication
end_time = (
data.end - data.start
) * multiplication
start_beat = round(second_to_beat(start_time, bpm))
duration = round(second_to_beat(end_time, bpm))
start_beat = round(second_to_beat(start_time, real_bpm))
duration = round(second_to_beat(end_time, real_bpm))
# Fix the round issue, so the beats don’t overlap
start_beat = max(start_beat, previous_end_beat)
previous_end_beat = start_beat + duration
# Calculate the silence between the words
if i < len(transcribed_data) - 1:
silence = (transcribed_data[i + 1].start - data.end)
else:
silence = 0
# : 10 10 10 w
# ':' start midi part
# 'n1' start at real beat
@@ -126,33 +147,55 @@ def create_ultrastar_txt_from_automation(
f"{str(duration)} " \
f"{str(note_numbers[i])} " \
f"{data.word}\n"
file.write(line)
# detect silence between words
if not transcribed_data[i].is_word_end:
separated_word_silence.append(silence)
continue
if i < len(transcribed_data) - 1:
silence = (
transcribed_data[i + 1].start - data.end
)
else:
silence = 0
if silence > silence_split_duration and i != len(transcribed_data) - 1:
if silence_split_duration != 0 and silence > silence_split_duration or any(
s > silence_split_duration for s in separated_word_silence) and i != len(transcribed_data) - 1:
# - 10
# '-' end of current sing part
# 'n1' show next at time in real beat
show_next = (
second_to_beat(data.end - gap, bpm)
second_to_beat(data.end - gap, real_bpm)
* multiplication
)
linebreak = f"{UltrastarTxtTag.LINEBREAK} " \
f"{str(round(show_next))}\n"
file.write(linebreak)
separated_word_silence = []
file.write(f"{UltrastarTxtTag.FILE_END}")
def deviation(silence_parts):
"""Calculates the deviation of the silence parts"""
if len(silence_parts) < 5:
return 0
# Remove the longest part so the deviation is not that high
sorted_parts = sorted(silence_parts)
filtered_parts = [part for part in sorted_parts if part < sorted_parts[-1]]
mean = sum(filtered_parts) / len(filtered_parts)
return mean
def calculate_silent_beat_length(transcribed_data: list[TranscribedData]):
print(f"{ULTRASINGER_HEAD} Calculating silence parts for linebreaks.")
silent_parts = []
for i, data in enumerate(transcribed_data):
if i < len(transcribed_data) - 1:
silent_parts.append(transcribed_data[i + 1].start - data.end)
return deviation(silent_parts)
def create_repitched_txt_from_ultrastar_data(
input_file: str, note_numbers: list[int], output_repitched_ultrastar: str
) -> None: