mirror of
https://github.com/rakuri255/UltraSinger.git
synced 2026-12-02 09:58:09 +09:00
wip: implement framework for comparing pitch clustering algorithms
This commit is contained in:
1 parent
3fde8522fe
commit
cdcb599ec8
4 files changed
+193
-90
No files matched your search
@@ -0,0 +1,105 @@
|
||||
from modules.Pitcher import pitched_data
|
||||
import librosa
|
||||
|
||||
def merge_pitch_data(datas: list[pitched_data.PitchedData], step_size: float = 0.1) -> pitched_data.PitchedData:
|
||||
merged_data = pitched_data.PitchedData()
|
||||
merged_data.times = []
|
||||
merged_data.frequencies = []
|
||||
merged_data.confidence = []
|
||||
|
||||
offset = float(0)
|
||||
for data in datas:
|
||||
if (len(data.times) > 0):
|
||||
merged_data.times.extend(map(apply_offset(offset), data.times))
|
||||
merged_data.frequencies.extend(data.frequencies)
|
||||
merged_data.confidence.extend(data.frequencies)
|
||||
offset = merged_data.times[-1] + step_size
|
||||
|
||||
return merged_data
|
||||
|
||||
def apply_offset(offset: float) -> pitched_data.PitchedData:
|
||||
def with_offset(x: float):
|
||||
return x + offset
|
||||
|
||||
return with_offset
|
||||
|
||||
def flat_pitch(duration: float, frequency: float, step_size: float = 0.1, confidence: float = 1) -> pitched_data.PitchedData:
|
||||
times = []
|
||||
frequencies = []
|
||||
confidences = []
|
||||
|
||||
step_size_decimal_points = len(str(step_size).split(".")[1])
|
||||
steps = int(duration / step_size) + 1
|
||||
|
||||
for step in range(int(steps)):
|
||||
times.append(round(step * step_size, step_size_decimal_points))
|
||||
frequencies.append(frequency)
|
||||
confidences.append(confidence)
|
||||
|
||||
data = pitched_data.PitchedData()
|
||||
data.times = times
|
||||
data.frequencies = frequencies
|
||||
data.confidence = confidences
|
||||
|
||||
return data
|
||||
|
||||
def steady_pitch_change(duration: float, start_frequency: float, end_frequency: float, step_size: float = 0.1, confidence: float = 1) -> pitched_data.PitchedData:
|
||||
times = []
|
||||
frequencies = []
|
||||
confidences = []
|
||||
|
||||
step_size_decimal_points = len(str(step_size).split(".")[1])
|
||||
|
||||
steps = int(duration / step_size) + 1
|
||||
|
||||
frequency_step_size = abs(start_frequency - end_frequency) / (steps - 1)
|
||||
# does the frequency increase or decrease?
|
||||
frequency_shift_factor = -1 if start_frequency > end_frequency else 1
|
||||
|
||||
for step in range(int(steps)):
|
||||
times.append(round(step * step_size, step_size_decimal_points))
|
||||
frequencies.append(start_frequency + (frequency_shift_factor * round(step * frequency_step_size, 1)))
|
||||
confidences.append(confidence)
|
||||
|
||||
data = pitched_data.PitchedData()
|
||||
data.times = times
|
||||
data.frequencies = frequencies
|
||||
data.confidence = confidences
|
||||
|
||||
return data
|
||||
|
||||
def expect_midi(note: str, start_time: float, duration: float, step_size: float = 0.01) -> tuple[int, float, float]:
|
||||
return note, start_time, start_time + duration - step_size
|
||||
|
||||
def merge_expected_midis_to_expected_output(expected_midis: list[tuple[str, float, float]]) -> tuple[list[str], list[float], list[float]]:
|
||||
notes = []
|
||||
start_times = []
|
||||
end_times = []
|
||||
|
||||
for expected_midi in expected_midis:
|
||||
notes.append(expected_midi[0])
|
||||
start_times.append(expected_midi[1])
|
||||
end_times.append(expected_midi[2])
|
||||
|
||||
return notes, start_times, end_times
|
||||
|
||||
# @dataclass
|
||||
# class ScoringResult:
|
||||
# score: float
|
||||
# relative_match: float
|
||||
# relative_miss: float
|
||||
# absolute_match: int
|
||||
# possible_match: int
|
||||
# absolute_miss: int
|
||||
# possible_miss: int
|
||||
|
||||
# def scoring(ideal_notes, actual_notes) -> ScoringResult:
|
||||
# scoring_step_size = 0.05
|
||||
|
||||
# last_compared_index = 0
|
||||
# for index, ideal in enumerate(ideal_notes):
|
||||
# start = ideal[1]
|
||||
# end = ideal[2]
|
||||
# between_start_and_end = find_overlapping_notes(start, end, actual_notes[last_compared_index:])
|
||||
|
||||
# def find_overlapping_notes(start: float, end: float, to_compare: tuple[list[str], list[float], list[float]]) -> tuple[list[str], list[float], list[float]]
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Tests for midi_creator.py"""
|
||||
|
||||
import unittest
|
||||
import src.modules.Midi.midi_creator as test_subject
|
||||
from src.modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from src.modules.Speech_Recognition.Whisper import convert_to_transcribed_data
|
||||
from helper import *
|
||||
import librosa
|
||||
|
||||
def n(note: str) -> float:
|
||||
return librosa.note_to_hz(note)
|
||||
def m(note: str) -> float:
|
||||
return librosa.note_to_midi(note)
|
||||
|
||||
class MidiCreatorTest(unittest.TestCase):
|
||||
def test_create_midi_notes_from_pitched_data(self):
|
||||
|
||||
# Arrange
|
||||
pitch_data = []
|
||||
pitch_data.append(flat_pitch(5, n('A2')))
|
||||
pitch_data.append(steady_pitch_change(1, n('A2'), n('B2')))
|
||||
pitch_data.append(flat_pitch(5, n('B2')))
|
||||
pitch_data.append(steady_pitch_change(1, n('B2'), n('A2')))
|
||||
pitch_data.append(flat_pitch(5, n('A2')))
|
||||
|
||||
merged_pitch_data = merge_pitch_data(pitch_data)
|
||||
|
||||
ideal_midis = []
|
||||
ideal_midis.append(expect_midi('A2', 0, 5))
|
||||
ideal_midis.append(expect_midi('B2', 6, 5))
|
||||
ideal_midis.append(expect_midi('A2', 12, 5))
|
||||
|
||||
ideal_result = merge_expected_midis_to_expected_output(ideal_midis)
|
||||
|
||||
# Act
|
||||
result = test_subject.create_midi_note_from_pitched_data(0, merged_pitch_data.times[-1], merged_pitch_data)
|
||||
|
||||
# Assert
|
||||
print(merged_pitch_data)
|
||||
print(ideal_result)
|
||||
print(result)
|
||||
print("end")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+14
-80
@@ -133,75 +133,6 @@ 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)
|
||||
@@ -398,8 +329,6 @@ 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(
|
||||
@@ -411,12 +340,10 @@ def run() -> None:
|
||||
)
|
||||
|
||||
# Pitch the audio
|
||||
midi_notes, pitched_data, ultrastar_note_numbers = pitch_audio(
|
||||
midi_notes, pitched_data, ultrastar_note_numbers, transcribed_data = pitch_audio(
|
||||
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)
|
||||
@@ -765,7 +692,7 @@ def create_midi_file(real_bpm: float,
|
||||
|
||||
|
||||
def pitch_audio(is_audio: bool, transcribed_data: list[TranscribedData], ultrastar_class: UltrastarTxtValue) -> tuple[
|
||||
list[str], PitchedData, list[int]]:
|
||||
list[str], PitchedData, list[int], list[TranscribedData]]:
|
||||
"""Pitch audio"""
|
||||
# todo: chunk pitching as option?
|
||||
# midi_notes = pitch_each_chunk_with_crepe(chunk_folder_name)
|
||||
@@ -777,19 +704,26 @@ def pitch_audio(is_audio: bool, transcribed_data: list[TranscribedData], ultrast
|
||||
if is_audio:
|
||||
start_times = []
|
||||
end_times = []
|
||||
words = []
|
||||
for i, data in enumerate(transcribed_data):
|
||||
start_times.append(data.start)
|
||||
end_times.append(data.end)
|
||||
midi_notes = create_midi_notes_from_pitched_data(
|
||||
start_times, end_times, pitched_data
|
||||
words.append(data.word)
|
||||
midi_notes, new_start_times, new_end_times, new_words = create_midi_notes_from_pitched_data(
|
||||
start_times, end_times, words, pitched_data
|
||||
)
|
||||
|
||||
else:
|
||||
midi_notes = create_midi_notes_from_pitched_data(
|
||||
ultrastar_class.startTimes, ultrastar_class.endTimes, pitched_data
|
||||
midi_notes, new_start_times, new_end_times, new_words = create_midi_notes_from_pitched_data(
|
||||
ultrastar_class.startTimes, ultrastar_class.endTimes, ultrastar_class.words, pitched_data
|
||||
)
|
||||
ultrastar_note_numbers = convert_midi_notes_to_ultrastar_notes(midi_notes)
|
||||
return midi_notes, pitched_data, ultrastar_note_numbers
|
||||
|
||||
new_transcribed_data = []
|
||||
for index in enumerate(midi_notes):
|
||||
new_transcribed_data.append(TranscribedData({"word": new_words[index], "start": new_start_times[index], "end": new_end_times[index], "is_hyphen": None, "confidence": 1}))
|
||||
|
||||
return midi_notes, pitched_data, ultrastar_note_numbers, new_transcribed_data
|
||||
|
||||
|
||||
def create_audio_chunks(
|
||||
|
||||
@@ -81,28 +81,47 @@ def find_nearest_index(array: list[float], value: float) -> int:
|
||||
return idx
|
||||
|
||||
|
||||
def create_midi_notes_from_pitched_data(start_times: list[float], end_times: list[float], pitched_data: PitchedData) -> list[str]:
|
||||
def create_midi_notes_from_pitched_data(start_times: list[float], end_times: list[float], words: list[str], pitched_data: PitchedData) -> tuple[list[str], list[float], list[float], list[str]]:
|
||||
"""Create midi notes from pitched data"""
|
||||
print(f"{ULTRASINGER_HEAD} Creating midi notes from pitched data")
|
||||
|
||||
midi_notes = []
|
||||
new_start_times = []
|
||||
new_end_times = []
|
||||
new_words = []
|
||||
|
||||
for i in enumerate(start_times):
|
||||
pos = i[0]
|
||||
start_time = start_times[pos]
|
||||
end_time = end_times[pos]
|
||||
for index in enumerate(start_times):
|
||||
start_time = start_times[index]
|
||||
end_time = end_times[index]
|
||||
word = str(words[index])
|
||||
|
||||
note = create_midi_note_from_pitched_data(
|
||||
notes, segment_start_times, segment_end_times = create_midi_note_from_pitched_data(
|
||||
start_time, end_time, pitched_data
|
||||
)
|
||||
|
||||
midi_notes.append(note)
|
||||
midi_notes.extend(notes)
|
||||
new_start_times.extend(segment_start_times)
|
||||
new_end_times.extend(segment_end_times)
|
||||
|
||||
segment_words = []
|
||||
segment_size = len(notes)
|
||||
if segment_size > 1:
|
||||
segment_words.extend(np.repeat(["~"], segment_size - 1))
|
||||
|
||||
ends_with_space = word.endswith(" ")
|
||||
if ends_with_space:
|
||||
word = word[:-1]
|
||||
segment_words[-1] = segment_words[-1] + " "
|
||||
|
||||
segment_words.insert(0, word)
|
||||
|
||||
new_words.extend(segment_words)
|
||||
# todo: Progress?
|
||||
# print(filename + " f: " + str(mean))
|
||||
return midi_notes
|
||||
return midi_notes, new_start_times, new_end_times, new_words
|
||||
|
||||
|
||||
def create_midi_note_from_pitched_data(start_time: float, end_time: float, pitched_data: PitchedData) -> str:
|
||||
def create_midi_note_from_pitched_data(start_time: float, end_time: float, pitched_data: PitchedData) -> tuple[list[str], list[float], list[float]]:
|
||||
"""Create midi note from pitched data"""
|
||||
|
||||
start = find_nearest_index(pitched_data.times, start_time)
|
||||
@@ -121,4 +140,4 @@ def create_midi_note_from_pitched_data(start_time: float, end_time: float, pitch
|
||||
|
||||
note = most_frequent(notes)[0][0]
|
||||
|
||||
return note
|
||||
return [note], [start_time], [end_time]
|
||||
Reference in new issue
Block a user