add rudimentary intra-syllable pitch detection

This commit is contained in:
Benedikt Wagener committed 2023-09-03 18:47:52 +02:00
1 parent 34237671b6
commit 3fde8522fe
2 files changed
+76 -1

No files matched your search

+73
View File
@@ -133,6 +133,75 @@ def add_hyphen_to_data(transcribed_data: list[TranscribedData], hyphen_words: li
return new_data
def split_syllables_into_segments(transcribed_data: list[TranscribedData]) -> list[TranscribedData]:
"""Split every syllable into sub-segments"""
segment_size = 0.05
segment_size_decimal_points = len(str(segment_size).split(".")[1])
new_data = []
for i, data in enumerate(transcribed_data):
duration = data.end - data.start
if duration <= segment_size:
new_data.append(data)
continue
has_space = str(data.word).endswith(" ")
first_segment = copy.deepcopy(data)
filler_words_start = data.start + segment_size
remainder = data.end - (filler_words_start)
first_segment.end = filler_words_start
if has_space:
first_segment.word = first_segment.word[:-1]
new_data.append(first_segment)
full_segments, partial_segment = divmod(remainder, segment_size)
if full_segments >= 1:
for i in range(int(full_segments)):
segment = TranscribedData()
segment.word = "~"
segment.start = filler_words_start + round(i * segment_size, segment_size_decimal_points)
segment.end = segment.start + segment_size
new_data.append(segment)
if partial_segment >= 0.01:
segment = TranscribedData()
segment.word = "~"
segment.start = filler_words_start + round(full_segments * segment_size, segment_size_decimal_points)
segment.end = segment.start + partial_segment
new_data.append(segment)
if has_space:
new_data[-1].word += " "
return new_data
def merge_syllable_segments(
transcribed_data: list[TranscribedData],
midi_notes: list[str],
us_notes = list[int]
) -> tuple[list[TranscribedData], list[str], list[int]]:
"""Merge sub-segments of a syllable where the pitch is the same"""
max_gap = 0.05
new_data = []
new_midi_notes = []
new_us_notes = []
previous_data = None
for i, data in enumerate(transcribed_data):
if str(data.word).startswith("~") and previous_data is not None and data.start - previous_data.end <= max_gap and midi_notes[i] == midi_notes[i-1]:
new_data[-1].end = data.end
else:
new_data.append(data)
new_midi_notes.append(midi_notes[i])
new_us_notes.append(us_notes[i])
previous_data = data
return new_data, new_midi_notes, new_us_notes
def get_bpm_from_data(data, sampling_rate):
"""Get real bpm from audio data"""
onset_env = librosa.onset.onset_strength(y=data, sr=sampling_rate)
@@ -329,6 +398,8 @@ def run() -> None:
# lyric = 'input/faber_lyric.txt'
# --corrected_words = correct_words(vosk_speech, lyric)
transcribed_data = split_syllables_into_segments(transcribed_data)
# Create audio chunks
if settings.create_audio_chunks:
create_audio_chunks(
@@ -344,6 +415,8 @@ def run() -> None:
is_audio, transcribed_data, ultrastar_class
)
transcribed_data, midi_notes, ultrastar_note_numbers = merge_syllable_segments(transcribed_data, midi_notes, ultrastar_note_numbers)
# Create plot
if settings.create_plot:
plot(pitched_data, transcribed_data, midi_notes, song_output)
@@ -4,7 +4,9 @@
class TranscribedData:
"""Transcribed data from json file"""
def __init__(self, transcribed_json):
def __init__(self, transcribed_json = None):
if transcribed_json is None:
return
# Vosk = conf, Whisper = confidence
self.conf = transcribed_json.get(
"conf", transcribed_json.get("confidence", None)