diff --git a/README.md b/README.md index 84c6e07..08ddc0a 100644 --- a/README.md +++ b/README.md @@ -115,9 +115,7 @@ _Not all options working now!_ --whisper Multilingual model > tiny|base|small|medium|large-v1|large-v2 >> ((default) is large-v2 English-only model > tiny.en|base.en|small.en|medium.en --align_model Use other languages model for Whisper provided from huggingface.co - - --vosk Needs model - + [pitcher] # Default is crepe --crepe tiny|small|medium|large|full >> ((default) is full) @@ -158,10 +156,7 @@ This re-pitch the audio and creates a new txt file. ### Transcriber -For transcription, `whisper` is used by default. It is more accurate than the other even with the `large` model. -And it finds the language automatically. -But anyway, it depends! Try the other one if `Whisper` does not suit you. -Also keep in mind that while a larger model is more accurate, it also takes longer to transcribe. +Keep in mind that while a larger model is more accurate, it also takes longer to transcribe. #### Whisper @@ -182,16 +177,6 @@ Example for romanian: -i XYZ --align_model "gigant/romanian-wav2vec2" ``` -#### Vosk - -If you want to use `Vosk` than you need the model. It is not included. You can download it here [Link](https://alphacephei.com/vosk/models). -Make sure you take the right language. -For the first test run, use the `small model`, to be accurate use the `gigaspeech` model - -```commandline --i XYZ -v "models\vosk-model-en-us-0.42-gigaspeech" -``` - #### Hyphenation Is on by default. Can also be deactivated if hyphenation does not produce diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e71128e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[tool.pytest.ini_options] +pythonpath = [ + "src", +] \ No newline at end of file diff --git a/pytest/modules/Speech_Recognition/test_Whisper.py b/pytest/modules/Speech_Recognition/test_Whisper.py new file mode 100644 index 0000000..8f78701 --- /dev/null +++ b/pytest/modules/Speech_Recognition/test_Whisper.py @@ -0,0 +1,56 @@ +"""Tests for whisper.py""" + +import unittest +from src.modules.Speech_Recognition.TranscribedData import TranscribedData +from src.modules.Speech_Recognition.Whisper import convert_to_transcribed_data + + +class ConvertToTranscribedDataTest(unittest.TestCase): + def test_convert_to_transcribed_data(self): + # Arrange + result_aligned = { + "segments": [ + { + "words": [ + {"word": "UltraSinger", "start": 1.23, "end": 2.34, "confidence": 0.95}, + {"word": "is", "start": 2.34, "end": 3.45, "confidence": 0.9}, + {"word": "cool!", "start": 3.45, "end": 4.56, "confidence": 0.85}, + ] + }, + { + "words": [ + {"word": "And", "start": 4.56, "end": 5.67, "confidence": 0.95}, + {"word": "will", "start": 5.67, "end": 6.78, "confidence": 0.9}, + {"word": "be", "start": 6.78, "end": 7.89, "confidence": 0.85}, + {"word": "better!", "start": 7.89, "end": 9.01, "confidence": 0.8}, + ] + }, + ] + } + + # Words should have space at the end + expected_output = [ + TranscribedData( + {"word": "UltraSinger ", "start": 1.23, "end": 2.34, "is_hyphen": None, "confidence": 0.95}), + TranscribedData({"word": "is ", "start": 2.34, "end": 3.45, "is_hyphen": None, "confidence": 0.9}), + TranscribedData({"word": "cool! ", "start": 3.45, "end": 4.56, "is_hyphen": None, "confidence": 0.85}), + TranscribedData({"word": "And ", "start": 4.56, "end": 5.67, "is_hyphen": None, "confidence": 0.95}), + TranscribedData({"word": "will ", "start": 5.67, "end": 6.78, "is_hyphen": None, "confidence": 0.9}), + TranscribedData({"word": "be ", "start": 6.78, "end": 7.89, "is_hyphen": None, "confidence": 0.85}), + TranscribedData({"word": "better! ", "start": 7.89, "end": 9.01, "is_hyphen": None, "confidence": 0.8}), + ] + + # Act + transcribed_data = convert_to_transcribed_data(result_aligned) + + # Assert + self.assertEqual(len(transcribed_data), len(expected_output)) + for i in range(len(transcribed_data)): + self.assertEqual(transcribed_data[i].word, expected_output[i].word) + self.assertEqual(transcribed_data[i].end, expected_output[i].end) + self.assertEqual(transcribed_data[i].start, expected_output[i].start) + self.assertEqual(transcribed_data[i].is_hyphen, expected_output[i].is_hyphen) + + +if __name__ == "__main__": + unittest.main() diff --git a/pytest/modules/Speech_Recognition/test_hyphenation.py b/pytest/modules/Speech_Recognition/test_hyphenation.py new file mode 100644 index 0000000..cfd5dd8 --- /dev/null +++ b/pytest/modules/Speech_Recognition/test_hyphenation.py @@ -0,0 +1,17 @@ +"""Tests for hyphenation.py""" + +import unittest +from src.modules.Speech_Recognition.hyphenation import hyphenation +from hyphen import Hyphenator + + +def test_hypenation(): + """Test case for hyphenation function.""" + + assert hyphenation("begegnen", Hyphenator("de_DE")) == ["be", "geg", "nen"] + assert hyphenation(".b,e~g'eg*nen, ", Hyphenator("de_DE")) == [".b,e", "~g'eg", "*nen, "] + assert hyphenation("Abend, ", Hyphenator("de_AT")) == None + assert hyphenation("Abend.", Hyphenator("de_AT")) == None + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/pytest/modules/UltraStar/test_ultrastar_writer.py b/pytest/modules/UltraStar/test_ultrastar_writer.py new file mode 100644 index 0000000..4e82f4f --- /dev/null +++ b/pytest/modules/UltraStar/test_ultrastar_writer.py @@ -0,0 +1,138 @@ +"""Tests for the ultrastar_writer.py module.""" + +import unittest +from unittest.mock import patch, mock_open +from src.modules.Ultrastar.ultrastar_writer import create_ultrastar_txt_from_automation +from src.modules.Speech_Recognition.TranscribedData import TranscribedData +from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag + + +class TestCreateUltrastarTxt(unittest.TestCase): + def test_create_ultrastar_txt_from_automation_default_values(self): + # Arrange + bpm, note_numbers, transcribed_data, ultrastar_file_output = self.arrange() + + # Assert default values + class_under_test = UltrastarTxtValue() + expected_calls = self.default_values(class_under_test) + + self.act_and_assert(bpm, class_under_test, expected_calls, note_numbers, transcribed_data, + ultrastar_file_output) + + def test_create_ultrastar_txt_from_automation_full_values(self): + # Arrange + bpm, note_numbers, transcribed_data, ultrastar_file_output = self.arrange() + + # Assert full values + class_under_test = UltrastarTxtValue() + class_under_test.artist = "artist" + class_under_test.title = "title" + class_under_test.year = "2023" + class_under_test.language = "de" + class_under_test.genre = "pop, rock" + class_under_test.cover = "cover [CO].jpg" + class_under_test.video = "video.mp4" + class_under_test.mp3 = "music.mp3" + + expected_calls = self.full_values(class_under_test) + + self.act_and_assert(bpm, class_under_test, expected_calls, note_numbers, transcribed_data, + ultrastar_file_output) + + def arrange(self): + # Arrange + transcribed_data = [ + TranscribedData({ + "conf": 0.95, + "word": "UltraSinger ", + "end": 2.5, + "start": 0.5 + }), + TranscribedData({ + "conf": 0.9, + "word": "is ", + "end": 4.5, + "start": 3.0 + }), + TranscribedData({ + "conf": 0.85, + "word": "cool! ", + "end": 7.5, + "start": 5.5 + }), + ] + note_numbers = [1, 2, 3] + ultrastar_file_output = "output.txt" + bpm = 120 + return bpm, note_numbers, transcribed_data, ultrastar_file_output + + def act_and_assert(self, bpm, default_ultrastar_class, expected_calls_default_values, note_numbers, + transcribed_data, ultrastar_file_output): + # Act + mock_file = self.act(bpm, default_ultrastar_class, note_numbers, transcribed_data, ultrastar_file_output) + + # Assert the file was opened and is utf-8 + mock_file.assert_called_once_with(ultrastar_file_output, "w", encoding='utf-8') + + # Assert that expected_calls_default_values were written to the file + mock_file_handle = mock_file.return_value.__enter__.return_value.write + write_calls = [args[0][0] for args in mock_file_handle.call_args_list] + self.assertEqual(write_calls, expected_calls_default_values) + + @staticmethod + def default_values(default_ultrastar_class): + expected_calls = [ + f"#{UltrastarTxtTag.ARTIST}:{default_ultrastar_class.artist}\n", + f"#{UltrastarTxtTag.TITLE}:{default_ultrastar_class.title}\n", + f"#{UltrastarTxtTag.MP3}:{default_ultrastar_class.mp3}\n", + f"#{UltrastarTxtTag.VIDEO}:{default_ultrastar_class.video}\n", # todo: video is optional + f"#{UltrastarTxtTag.BPM}:390.0\n", + f"#{UltrastarTxtTag.GAP}:500\n", + f"#{UltrastarTxtTag.CREATOR}:{default_ultrastar_class.creator}\n", + f"#{UltrastarTxtTag.FIXER}:{default_ultrastar_class.fixer}\n", # todo: fixer is special + f"#{UltrastarTxtTag.COMMENT}:{default_ultrastar_class.comment}\n", + ": 0 52 1 UltraSinger \n", + "- 52\n", + ": 65 39 2 is \n", + "- 104\n", + ": 130 52 3 cool! \n", + "E" + ] + return expected_calls + + @staticmethod + def full_values(default_ultrastar_class): + expected_calls = [ + f"#{UltrastarTxtTag.ARTIST}:{default_ultrastar_class.artist}\n", + f"#{UltrastarTxtTag.TITLE}:{default_ultrastar_class.title}\n", + f"#{UltrastarTxtTag.YEAR}:{default_ultrastar_class.year}\n", + f"#{UltrastarTxtTag.LANGUAGE}:German\n", + f"#{UltrastarTxtTag.GENRE}:{default_ultrastar_class.genre}\n", + f"#{UltrastarTxtTag.COVER}:{default_ultrastar_class.cover}\n", + f"#{UltrastarTxtTag.MP3}:{default_ultrastar_class.mp3}\n", + f"#{UltrastarTxtTag.VIDEO}:{default_ultrastar_class.video}\n", + f"#{UltrastarTxtTag.BPM}:390.0\n", + f"#{UltrastarTxtTag.GAP}:500\n", + f"#{UltrastarTxtTag.CREATOR}:{default_ultrastar_class.creator}\n", + f"#{UltrastarTxtTag.FIXER}:{default_ultrastar_class.fixer}\n", + f"#{UltrastarTxtTag.COMMENT}:{default_ultrastar_class.comment}\n", + ": 0 52 1 UltraSinger \n", + "- 52\n", + ": 65 39 2 is \n", + "- 104\n", + ": 130 52 3 cool! \n", + "E" + ] + return expected_calls + + @staticmethod + def act(bpm, default_ultrastar_class, note_numbers, transcribed_data, ultrastar_file_output): + with patch("builtins.open", mock_open()) as mock_file: + create_ultrastar_txt_from_automation( + transcribed_data, note_numbers, ultrastar_file_output, + default_ultrastar_class, bpm + ) + return mock_file + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/pytest/modules/test_musicbrainz_client.py b/pytest/modules/test_musicbrainz_client.py new file mode 100644 index 0000000..7186ff0 --- /dev/null +++ b/pytest/modules/test_musicbrainz_client.py @@ -0,0 +1,93 @@ +"""Test the musicbrainz_client module.""" + +import unittest +from unittest.mock import patch +from src.modules.musicbrainz_client import get_music_infos + + +class TestGetMusicInfos(unittest.TestCase): + + @patch('musicbrainzngs.search_artists') + @patch('musicbrainzngs.search_release_groups') + def test_get_music_infos(self, mock_search_release_groups, mock_search_artists): + # Arrange + artist = 'UltraSinger' + title = 'That\'s Rocking!' + search = f'{artist} - {title} (UltrStar 2023) FULL HD' + + # Set up mock return values for the MusicBrainz API calls + mock_search_artists.return_value = { + 'artist-list': [ + {'name': artist} + ] + } + + mock_search_release_groups.return_value = { + 'release-group-list': [ + { + 'title': title, + 'artist-credit-phrase': artist, + 'first-release-date': '2023-01-01', + 'tag-list': [ + {'name': 'Genre 1'}, + {'name': 'Genre 2'} + ] + } + ] + } + + # Call the function to test + title, artist, year, genre = get_music_infos(search) + + # Assert the returned values + self.assertEqual(title, 'That\'s Rocking!') + self.assertEqual(artist, 'UltraSinger') + self.assertEqual(year, '2023-01-01') + self.assertEqual(genre, 'Genre 1,Genre 2,') + + @patch('musicbrainzngs.search_artists') + @patch('musicbrainzngs.search_release_groups') + def test_get_music_infos_when_title_and_artist_are_the_same(self, mock_search_release_groups, mock_search_artists): + # Arrange + artist = "ArtistIsTitle" + title = "ArtistIsTitle" + search_not_same = "ArtistIsTitle - ArtistNotTitle" + search_is_same = f"{artist} - {title}" + + # Set up mock return values for the MusicBrainz API calls + mock_search_artists.return_value = { + 'artist-list': [ + {'name': artist} + ] + } + + mock_search_release_groups.return_value = { + 'release-group-list': [ + { + 'title': title, + 'artist-credit-phrase': artist, + } + ] + } + + # Act search_not_same but musicbrainz returns the same artist and title + title, artist, year, genre = get_music_infos(search_not_same) + + # Assert + self.assertEqual(title, None) + self.assertEqual(artist, None) + self.assertEqual(year, None) + self.assertEqual(genre, None) + + # Act search_is_same and musicbrainz returns the same artist and title + title, artist, year, genre = get_music_infos(search_is_same) + + # Assert + self.assertEqual(title, 'ArtistIsTitle') + self.assertEqual(artist, 'ArtistIsTitle') + self.assertEqual(year, None) + self.assertEqual(genre, None) + + +if __name__ == '__main__': + unittest.main() diff --git a/requirements.txt b/requirements.txt index 3953a78..be7f1ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,10 +4,7 @@ pretty_midi~=0.2.10 pydub==0.25.1 Levenshtein~=0.21.0 scipy~=1.10.1 -vosk==0.3.44 tensorflow~=2.12.0 -#tensorflow-directml-plugin~=0.4.0.dev230202 -#cudatoolkit==11.8.0 matplotlib~=3.7.1 yt_dlp~=2023.3.4 numpy~=1.23.5 @@ -21,4 +18,5 @@ Pillow~=9.5.0 isort==5.12 black==23.3 pylint==2.17 -pytest~=7.3.1 \ No newline at end of file +pytest~=7.3.1 +musicbrainzngs~=0.7.1 \ No newline at end of file diff --git a/src/Settings.py b/src/Settings.py index 93d5b25..4863e99 100644 --- a/src/Settings.py +++ b/src/Settings.py @@ -14,14 +14,11 @@ class Settings: audio_chunk_folder_name = "audio-chunks" # Whisper - transcriber = "whisper" # whisper|vosk + transcriber = "whisper" # whisper whisper_model = "large-v2" # Multilingual model tiny|base|small|medium|large-v1|large-v2 # English-only model tiny.en|base.en|small.en|medium.en whisper_align_model = None # Model for other languages from huggingface.co e.g -> "gigant/romanian-wav2vec2" - # Vosk - vosk_model_path = "" # "models/vosk-model-small-en-us-0.15" - # Pitch crepe_model_capacity = "full" # tiny|small|medium|large|full crepe_step_size = 10 diff --git a/src/UltraSinger.py b/src/UltraSinger.py index d866826..957c990 100644 --- a/src/UltraSinger.py +++ b/src/UltraSinger.py @@ -10,49 +10,49 @@ import Levenshtein import librosa from tqdm import tqdm -from src.modules import os_helper -from src.modules.Audio.denoise import ffmpeg_reduce_noise -from src.modules.Audio.separation import separate_audio -from src.modules.Audio.vocal_chunks import ( +from modules import os_helper +from modules.Audio.denoise import ffmpeg_reduce_noise +from modules.Audio.separation import separate_audio +from modules.Audio.vocal_chunks import ( export_chunks_from_transcribed_data, export_chunks_from_ultrastar_data, ) -from src.modules.Audio.silence_processing import remove_silence_from_transcribtion_data -from src.modules.csv_handler import export_transcribed_data_to_csv -from src.modules.Audio.convert_audio import convert_audio_to_mono_wav, convert_wav_to_mp3 -from src.modules.Audio.youtube import ( +from modules.Audio.silence_processing import remove_silence_from_transcribtion_data +from modules.csv_handler import export_transcribed_data_to_csv +from modules.Audio.convert_audio import convert_audio_to_mono_wav, convert_wav_to_mp3 +from modules.Audio.youtube import ( download_youtube_audio, download_youtube_thumbnail, download_youtube_video, get_youtube_title, ) -from src.modules.DeviceDetection.device_detection import get_available_device -from src.modules.console_colors import ( +from modules.DeviceDetection.device_detection import get_available_device +from modules.console_colors import ( ULTRASINGER_HEAD, blue_highlighted, gold_highlighted, light_blue_highlighted, red_highlighted, ) -from src.modules.Midi import midi_creator -from src.modules.Midi.midi_creator import ( +from modules.Midi import midi_creator +from modules.Midi.midi_creator import ( convert_frequencies_to_notes, create_midi_notes_from_pitched_data, most_frequent, ) -from src.modules.Pitcher.pitcher import ( +from modules.Pitcher.pitcher import ( get_frequency_with_high_confidence, get_pitch_with_crepe_file, ) -from src.modules.Pitcher.pitched_data import PitchedData -from src.modules.Speech_Recognition.hyphenation import hyphenation, language_check -from src.modules.Speech_Recognition.Vosk import transcribe_with_vosk -from src.modules.Speech_Recognition.Whisper import transcribe_with_whisper -from src.modules.Ultrastar import ultrastar_score_calculator, ultrastar_writer, ultrastar_converter, ultrastar_parser -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue +from modules.Pitcher.pitched_data import PitchedData +from modules.Speech_Recognition.hyphenation import hyphenation, language_check, create_hyphenator +from modules.Speech_Recognition.Whisper import transcribe_with_whisper +from modules.Ultrastar import ultrastar_score_calculator, ultrastar_writer, ultrastar_converter, ultrastar_parser +from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue from Settings import Settings -from src.modules.Speech_Recognition.TranscribedData import TranscribedData -from src.modules.plot import plot +from modules.Speech_Recognition.TranscribedData import TranscribedData +from modules.plot import plot +from modules.musicbrainz_client import get_music_infos settings = Settings() @@ -144,7 +144,7 @@ def get_bpm_from_data(data, sampling_rate): return wav_tempo[0] -def get_bpm_from_file(wav_file: str): +def get_bpm_from_file(wav_file: str) -> float: """Get real bpm from audio file""" data, sampling_rate = librosa.load(wav_file, sr=None) return get_bpm_from_data(data, sampling_rate) @@ -182,7 +182,8 @@ def print_help() -> None: [mode] ## INPUT is audio ## default Creates all - + + # Single file creation selection is in progress, you currently getting all! (-u Create ultrastar txt file) # In Progress (-m Create midi file) # In Progress (-s Create sheet file) # In Progress @@ -190,22 +191,27 @@ def print_help() -> None: ## INPUT is ultrastar.txt ## default Creates all + # Single selection is in progress, you currently getting all! (-r repitch Ultrastar.txt (input has to be audio)) # In Progress (-p Check pitch of Ultrastar.txt input) # In Progress (-m Create midi file) # In Progress [transcription] - --whisper (default) tiny|base|small|medium|large - --vosk Needs model + # Default is whisper + --whisper Multilingual model > tiny|base|small|medium|large-v1|large-v2 >> ((default) is large-v2 + English-only model > tiny.en|base.en|small.en|medium.en + --align_model Use other languages model for Whisper provided from huggingface.co + + [pitcher] + # Default is crepe + --crepe tiny|small|medium|large|full >> ((default) is full) [extra] - (-k Keep audio chunks) # In Progress - --hyphenation (default) True|False - --disable_separation True|False - --disable_karaoke True|False - - [pitcher] - --crepe (default) tiny|small|medium|large|full + --hyphenation True|False >> ((default) is True) + --disable_separation True|False >> ((default) is False) + --disable_karaoke True|False >> ((default) is False) + --create_audio_chunks True|False >> ((default) is False) + --plot True|False >> ((default) is False) """ print(help_string) @@ -230,9 +236,11 @@ def hyphenate_each_word(language: str, transcribed_data: list[TranscribedData]) return None sleep(0.1) - for i, data in tqdm(enumerate(transcribed_data)): + hyphenator = create_hyphenator(lang_region) + for i in tqdm(enumerate(transcribed_data)): + pos = i[0] hyphenated_word.append( - hyphenation(data.word, lang_region) + hyphenation(transcribed_data[pos].word, hyphenator) ) return hyphenated_word @@ -256,6 +264,7 @@ def run() -> None: is_audio = ".txt" not in settings.input_file_path ultrastar_class = None real_bpm = None + (title, artist, year, genre) = (None, None, None, None) if not is_audio: # Parse Ultrastar txt print( @@ -276,6 +285,7 @@ def run() -> None: basename_without_ext, song_output, ultrastar_audio_input_path, + (title, artist, year, genre) ) = download_from_youtube() else: # Audio File print( @@ -285,6 +295,7 @@ def run() -> None: basename_without_ext, song_output, ultrastar_audio_input_path, + (title, artist, year, genre) ) = infos_from_audio_input_file() cache_path = os.path.join(song_output, "cache") @@ -351,6 +362,10 @@ def run() -> None: ultrastar_audio_input_path, ultrastar_note_numbers, language, + title, + artist, + year, + genre ) else: ultrastar_file_output = create_ultrastar_txt_from_ultrastar_data( @@ -369,7 +384,7 @@ def run() -> None: # Midi if settings.create_midi: - create_midi_file(is_audio, real_bpm, song_output, ultrastar_class) + create_midi_file(real_bpm, song_output, ultrastar_class, basename_without_ext) # Print Support print_support() @@ -401,12 +416,8 @@ def transcribe_audio() -> (str, list[TranscribedData]): device = "cpu" if settings.force_whisper_cpu else settings.device transcribed_data, language = transcribe_with_whisper( settings.mono_audio_path, settings.whisper_model, device, settings.whisper_align_model) - else: # vosk - transcribed_data = transcribe_with_vosk( - settings.mono_audio_path, settings.vosk_model_path - ) - # todo: make language selectable - language = "en" + else: + raise NotImplementedError return language, transcribed_data @@ -501,6 +512,10 @@ def create_ultrastar_txt_from_automation( ultrastar_audio_input_path: str, ultrastar_note_numbers: list[int], language: str, + title: str, + artist: str, + year: str, + genre: str ): """Create Ultrastar txt from automation""" ultrastar_header = UltrastarTxtValue() @@ -516,6 +531,16 @@ def create_ultrastar_txt_from_automation( else None ) + # Additional data + if title is not None: + ultrastar_header.title = title + if artist is not None: + ultrastar_header.artist = artist + if year is not None: + ultrastar_header.year = year + if genre is not None: + ultrastar_header.genre = genre + real_bpm = get_bpm_from_file(ultrastar_audio_input_path) ultrastar_file_output = os.path.join( song_output, basename_without_ext + ".txt" @@ -546,16 +571,37 @@ def create_ultrastar_txt_from_automation( return real_bpm, ultrastar_file_output -def infos_from_audio_input_file() -> tuple[str, str, str]: +def infos_from_audio_input_file() -> tuple[str, str, str, tuple[str, str, str, str]]: """Infos from audio input file""" basename = os.path.basename(settings.input_file_path) basename_without_ext = os.path.splitext(basename)[0] + + artist, title = None, None + if " - " in basename_without_ext: + artist, title = basename_without_ext.split(" - ", 1) + search_string = f"{artist} - {title}" + else: + search_string = basename_without_ext + + # Get additional data for song + (title_info, artist_info, year_info, genre_info) = get_music_infos(search_string) + + if title_info is not None: + title = title_info + artist = artist_info + + if artist is not None and title is not None: + basename_without_ext = f"{artist} - {title}" + extension = os.path.splitext(basename)[1] + basename = f"{basename_without_ext}{extension}" + song_output = os.path.join(settings.output_file_path, basename_without_ext) song_output = get_unused_song_output_dir(song_output) os_helper.create_folder(song_output) os_helper.copy(settings.input_file_path, song_output) + os_helper.rename(os.path.join(song_output, os.path.basename(settings.input_file_path)), os.path.join(song_output, basename)) ultrastar_audio_input_path = os.path.join(song_output, basename) - return basename_without_ext, song_output, ultrastar_audio_input_path + return basename_without_ext, song_output, ultrastar_audio_input_path, (title, artist, year_info, genre_info) FILENAME_REPLACEMENTS = (('?:"', ""), ("<", "("), (">", ")"), ("/\\|*", "-")) @@ -571,10 +617,18 @@ def sanitize_filename(fname: str) -> str: return fname -def download_from_youtube() -> tuple[str, str, str]: +def download_from_youtube() -> tuple[str, str, str, tuple[str, str, str, str]]: """Download from YouTube""" - title = get_youtube_title(settings.input_file_path) - basename_without_ext = sanitize_filename(title) + (artist, title) = get_youtube_title(settings.input_file_path) + + # Get additional data for song + (title_info, artist_info, year_info, genre_info) = get_music_infos(f"{artist} - {title}") + + if title_info is not None: + title = title_info + artist = artist_info + + basename_without_ext = sanitize_filename(f"{artist} - {title}") basename = basename_without_ext + ".mp3" song_output = os.path.join(settings.output_file_path, basename_without_ext) song_output = get_unused_song_output_dir(song_output) @@ -589,7 +643,7 @@ def download_from_youtube() -> tuple[str, str, str]: settings.input_file_path, basename_without_ext, song_output ) ultrastar_audio_input_path = os.path.join(song_output, basename) - return basename_without_ext, song_output, ultrastar_audio_input_path + return basename_without_ext, song_output, ultrastar_audio_input_path, (title, artist, year_info, genre_info) def parse_ultrastar_txt() -> tuple[str, float, str, str, UltrastarTxtValue]: @@ -619,27 +673,22 @@ def parse_ultrastar_txt() -> tuple[str, float, str, str, UltrastarTxtValue]: ) -def create_midi_file(is_audio: bool, real_bpm: float, song_output: str, ultrastar_class: UltrastarTxtValue) -> None: +def create_midi_file(real_bpm: float, + song_output: str, + ultrastar_class: UltrastarTxtValue, + basename_without_ext: str) -> None: """Create midi file""" print( f"{ULTRASINGER_HEAD} Creating Midi with {blue_highlighted('pretty_midi')}" ) - if is_audio: - voice_instrument = [ - midi_creator.convert_ultrastar_to_midi_instrument(ultrastar_class) - ] - midi_output = os.path.join(song_output, ultrastar_class.title + ".mid") - midi_creator.instruments_to_midi( - voice_instrument, real_bpm, midi_output - ) - else: - voice_instrument = [ - midi_creator.convert_ultrastar_to_midi_instrument(ultrastar_class) - ] - midi_output = os.path.join(song_output, ultrastar_class.title + ".mid") - midi_creator.instruments_to_midi( - voice_instrument, real_bpm, midi_output - ) + + voice_instrument = [ + midi_creator.convert_ultrastar_to_midi_instrument(ultrastar_class) + ] + midi_output = os.path.join(song_output, f"{basename_without_ext}.mid") + midi_creator.instruments_to_midi( + voice_instrument, real_bpm, midi_output + ) def pitch_audio(is_audio: bool, transcribed_data: list[TranscribedData], ultrastar_class: UltrastarTxtValue) -> tuple[ @@ -731,9 +780,6 @@ def init_settings(argv: list[str]) -> None: settings.whisper_model = arg elif opt in ("--align_model"): settings.whisper_align_model = arg - elif opt in ("--vosk"): - settings.transcriber = "vosk" - settings.vosk_model_path = arg elif opt in ("--crepe"): settings.crepe_model_capacity = arg elif opt in ("--plot"): @@ -765,7 +811,6 @@ def arg_options(): "ifile=", "ofile=", "crepe=", - "vosk=", "whisper=", "align_model=", "plot=", diff --git a/src/modules/Audio/convert_audio.py b/src/modules/Audio/convert_audio.py index f865f00..6642706 100644 --- a/src/modules/Audio/convert_audio.py +++ b/src/modules/Audio/convert_audio.py @@ -2,7 +2,7 @@ from pydub import AudioSegment -from src.modules.console_colors import ULTRASINGER_HEAD +from modules.console_colors import ULTRASINGER_HEAD def convert_audio_to_mono_wav(input_file: str, output_file: str) -> None: diff --git a/src/modules/Audio/denoise.py b/src/modules/Audio/denoise.py index 1801dbe..5987739 100644 --- a/src/modules/Audio/denoise.py +++ b/src/modules/Audio/denoise.py @@ -2,7 +2,7 @@ import ffmpeg -from src.modules.console_colors import ULTRASINGER_HEAD, blue_highlighted +from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted def ffmpeg_reduce_noise(input_file_path: str, output_file: str) -> None: diff --git a/src/modules/Audio/separation.py b/src/modules/Audio/separation.py index bcb2c26..d34241e 100644 --- a/src/modules/Audio/separation.py +++ b/src/modules/Audio/separation.py @@ -2,12 +2,12 @@ import subprocess -from src.modules.console_colors import ( +from modules.console_colors import ( ULTRASINGER_HEAD, blue_highlighted, red_highlighted, ) -from src.modules.os_helper import current_executor_path, move, path_join +from modules.os_helper import current_executor_path, move, path_join def separate_audio(input_file_path: str, output_file: str, device="cpu") -> None: diff --git a/src/modules/Audio/silence_processing.py b/src/modules/Audio/silence_processing.py index 9e824a6..e139547 100644 --- a/src/modules/Audio/silence_processing.py +++ b/src/modules/Audio/silence_processing.py @@ -2,8 +2,8 @@ import librosa -from src.modules.console_colors import ULTRASINGER_HEAD -from src.modules.Speech_Recognition.TranscribedData import TranscribedData +from modules.console_colors import ULTRASINGER_HEAD +from modules.Speech_Recognition.TranscribedData import TranscribedData def remove_silence_from_transcribtion_data(audio_path: str, transcribed_data: list[TranscribedData]) -> list[TranscribedData]: diff --git a/src/modules/Audio/vocal_chunks.py b/src/modules/Audio/vocal_chunks.py index 88aed81..b5301ec 100644 --- a/src/modules/Audio/vocal_chunks.py +++ b/src/modules/Audio/vocal_chunks.py @@ -4,13 +4,13 @@ import os import re import wave -from src.modules.console_colors import ULTRASINGER_HEAD -from src.modules.os_helper import create_folder -from src.modules.Ultrastar.ultrastar_converter import ( +from modules.console_colors import ULTRASINGER_HEAD +from modules.os_helper import create_folder +from modules.Ultrastar.ultrastar_converter import ( get_end_time_from_ultrastar, get_start_time_from_ultrastar, ) -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue +from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue class AudioManipulation: diff --git a/src/modules/Audio/youtube.py b/src/modules/Audio/youtube.py index bcda43c..d03ee44 100644 --- a/src/modules/Audio/youtube.py +++ b/src/modules/Audio/youtube.py @@ -6,10 +6,10 @@ import os import yt_dlp from PIL import Image -from src.modules.console_colors import ULTRASINGER_HEAD +from modules.console_colors import ULTRASINGER_HEAD +from modules.Image.image_helper import crop_image_to_square - -def get_youtube_title(url: str) -> str: +def get_youtube_title(url: str) -> tuple[str, str]: """Get the title of the YouTube video""" ydl_opts = {} @@ -18,7 +18,11 @@ def get_youtube_title(url: str) -> str: url, download=False # We just want to extract the info ) - return result["title"] + if "artist" in result: + return result["artist"], result["track"] + if "-" in result["title"]: + return result["title"].split("-")[0], result["title"].split("-")[1] + return result["channel"], result["title"] def download_youtube_audio(url: str, clear_filename: str, output_path: str): @@ -58,9 +62,9 @@ def download_and_convert_thumbnail(ydl_opts, url: str, clear_filename: str, outp response = ydl.urlopen(thumbnail_url) image_data = response.read() image = Image.open(io.BytesIO(image_data)) - image.save( - os.path.join(output_path, clear_filename + " [CO].jpg"), "JPEG" - ) + image_path = os.path.join(output_path, clear_filename + " [CO].jpg") + image.save(image_path, "JPEG") + crop_image_to_square(image_path) def download_youtube_video(url: str, clear_filename: str, output_path: str) -> None: diff --git a/src/modules/DeviceDetection/device_detection.py b/src/modules/DeviceDetection/device_detection.py index 8545711..5912438 100644 --- a/src/modules/DeviceDetection/device_detection.py +++ b/src/modules/DeviceDetection/device_detection.py @@ -2,7 +2,7 @@ import torch -from src.modules.console_colors import ULTRASINGER_HEAD, red_highlighted +from modules.console_colors import ULTRASINGER_HEAD, red_highlighted def get_available_device() -> str: diff --git a/src/modules/Image/image_helper.py b/src/modules/Image/image_helper.py new file mode 100644 index 0000000..2dd02d0 --- /dev/null +++ b/src/modules/Image/image_helper.py @@ -0,0 +1,18 @@ +from PIL import Image + + +def crop_image_to_square(image_path): + image = Image.open(image_path) + width, height = image.size + size = min(width, height) + + # Calculate the coordinates for the crop + left = (width - size) // 2 + top = (height - size) // 2 + right = left + size + bottom = top + size + + cropped_image = image.crop((left, top, right, bottom)) + + # Override the original image with the cropped version + cropped_image.save(image_path) \ No newline at end of file diff --git a/src/modules/Midi/midi_creator.py b/src/modules/Midi/midi_creator.py index 33f397a..b91b279 100644 --- a/src/modules/Midi/midi_creator.py +++ b/src/modules/Midi/midi_creator.py @@ -7,18 +7,18 @@ import librosa import numpy as np import pretty_midi -from src.modules.Pitcher.pitcher import get_frequency_with_high_confidence -from src.modules.Ultrastar.ultrastar_converter import ( +from modules.Pitcher.pitcher import get_frequency_with_high_confidence +from modules.Ultrastar.ultrastar_converter import ( get_end_time_from_ultrastar, get_start_time_from_ultrastar, ultrastar_note_to_midi_note, ) -from src.modules.console_colors import ( +from modules.console_colors import ( ULTRASINGER_HEAD, red_highlighted, ) -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue -from src.modules.Pitcher.pitched_data import PitchedData +from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue +from modules.Pitcher.pitched_data import PitchedData def convert_ultrastar_to_midi_instrument(ultrastar_class: UltrastarTxtValue) -> object: diff --git a/src/modules/Pitcher/pitcher.py b/src/modules/Pitcher/pitcher.py index ef614d7..2d17c67 100644 --- a/src/modules/Pitcher/pitcher.py +++ b/src/modules/Pitcher/pitcher.py @@ -3,12 +3,12 @@ import crepe from scipy.io import wavfile -from src.modules.console_colors import ( +from modules.console_colors import ( ULTRASINGER_HEAD, blue_highlighted, red_highlighted, ) -from src.modules.Pitcher.pitched_data import PitchedData +from modules.Pitcher.pitched_data import PitchedData def get_pitch_with_crepe_file(filename: str, step_size: int, model_capacity: str) -> PitchedData: diff --git a/src/modules/Speech_Recognition/Vosk.py b/src/modules/Speech_Recognition/Vosk.py deleted file mode 100644 index dfdf1d2..0000000 --- a/src/modules/Speech_Recognition/Vosk.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Vosk speech recognition module.""" - -import json -import wave - -from vosk import KaldiRecognizer, Model - -from src.modules.console_colors import ULTRASINGER_HEAD -from src.modules.Speech_Recognition.TranscribedData import TranscribedData - -# todo: Rename to Transcoder? - - -def transcribe_with_vosk(audio_filename: str, model_path: str) -> list[TranscribedData]: - """Transcribe with vosk""" - # Code from here: https://towardsdatascience.com/speech-recognition-with-timestamps-934ede4234b2 - print( - f"{ULTRASINGER_HEAD} Transcribing {audio_filename} with vosk and model {model_path}" - ) - - model = Model(model_path) - wave_file = wave.open(audio_filename, "rb") - recognizer = KaldiRecognizer(model, wave_file.getframerate()) - - recognizer.SetWords(True) - - # get the list of JSON dictionaries - results = [] - # recognize speech using vosk model - while True: - data = wave_file.readframes(4000) - if len(data) == 0: - break - if recognizer.AcceptWaveform(data): - part_result = json.loads(recognizer.Result()) - results.append(part_result) - wave_file.close() # close audiofile - part_result = json.loads(recognizer.FinalResult()) - results.append(part_result) - - # convert list of JSON dictionaries to list of 'Word' objects - transcribed_data = [] - for sentence in results: - if len(sentence) == 1: - # sometimes there are bugs in recognition - # and it returns an empty dictionary - # {'text': ''} - continue - for obj in sentence["result"]: - vtd = TranscribedData(obj) # create custom Word object - vtd.word = vtd.word + " " - transcribed_data.append(vtd) # and add it to list - - # Todo: remove silent part from each word - - # output to the screen - # todo: progress? - # for word in vosk_transcribed_data: - # print(word.to_string()) - - return transcribed_data - - -class SpeechToText: - """Docstring""" diff --git a/src/modules/Speech_Recognition/Whisper.py b/src/modules/Speech_Recognition/Whisper.py index a827f3e..0e391d3 100644 --- a/src/modules/Speech_Recognition/Whisper.py +++ b/src/modules/Speech_Recognition/Whisper.py @@ -3,15 +3,16 @@ import whisperx import sys -from src.modules.console_colors import ( +from modules.console_colors import ( ULTRASINGER_HEAD, blue_highlighted, red_highlighted, ) -from src.modules.Speech_Recognition.TranscribedData import TranscribedData +from modules.Speech_Recognition.TranscribedData import TranscribedData -def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_name: str = None) -> (list[TranscribedData], str): +def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_name: str = None) -> ( +list[TranscribedData], str): """Transcribe with whisper""" print( @@ -25,7 +26,6 @@ def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_nam "float16" if device == "cuda" else "int8" ) # change to "int8" if low on GPU mem (may reduce accuracy) - # transcribe with original whisper loaded_whisper_model = whisperx.load_model( model, device=device, compute_type=compute_type ) @@ -57,8 +57,13 @@ def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_nam return_char_alignments=False, ) - transcribed_data = [] + transcribed_data = convert_to_transcribed_data(result_aligned) + return transcribed_data, language + + +def convert_to_transcribed_data(result_aligned): + transcribed_data = [] for segment in result_aligned["segments"]: for obj in segment["words"]: if len(obj) < 4: @@ -67,7 +72,6 @@ def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_nam ) continue vtd = TranscribedData(obj) # create custom Word object - vtd.word = vtd.word + " " + vtd.word = vtd.word + " " # add space to end of word transcribed_data.append(vtd) # and add it to list - - return transcribed_data, language + return transcribed_data diff --git a/src/modules/Speech_Recognition/hyphenation.py b/src/modules/Speech_Recognition/hyphenation.py index 4d65b9c..a166c56 100644 --- a/src/modules/Speech_Recognition/hyphenation.py +++ b/src/modules/Speech_Recognition/hyphenation.py @@ -4,7 +4,7 @@ import string from hyphen import Hyphenator, dictools -from src.modules.console_colors import ( +from modules.console_colors import ( ULTRASINGER_HEAD, blue_highlighted, ) @@ -36,20 +36,66 @@ def contains_punctuation(word: str) -> bool: return any(elem in word for elem in string.punctuation) -def hyphenation(word: str, lang_region: str) -> list[str] | None: +def clean_word(word: str): + """Remove punctuation from word""" + cleaned_string = "" + removed_indices = [] + removed_symbols = [] + for i, char in enumerate(word): + if char not in string.punctuation and char not in " ": + cleaned_string += char + else: + removed_indices.append(i) + removed_symbols.append(char) + return cleaned_string, removed_indices, removed_symbols + + +def insert_removed_symbols(separated_array, removed_indices, symbols): + """Insert symbols into the syllables""" + result = [] + symbol_index = 0 + i = 0 + + # Add removed symbols to the syllables + for syllable in separated_array: + tmp = "" + for char in syllable: + if i in removed_indices: + tmp += symbols[symbol_index] + symbol_index += 1 + i += 1 + tmp += char + i += 1 + result.append(tmp) + + # Add remaining symbols to the last syllable + if symbol_index < len(symbols): + tmp = result[-1] + for i in range(symbol_index, len(symbols)): + tmp += symbols[i] + result[-1] = tmp + + return result + + +def create_hyphenator(lang_region: str) -> Hyphenator: + """Create hyphenator""" + hyphenator = Hyphenator(lang_region) + return hyphenator + + +def hyphenation(word: str, hyphenator: Hyphenator) -> list[str] | None: """Hyphenate word""" - if contains_punctuation(word): - return None - - hyphenator = Hyphenator(lang_region) - syllabus = hyphenator.syllables(word) + cleaned_string, removed_indices, removed_symbols = clean_word(word) + syllabus = hyphenator.syllables(cleaned_string) length = len(syllabus) if length > 1: hyphen = [] for i in range(length): hyphen.append(syllabus[i]) + hyphen = insert_removed_symbols(hyphen, removed_indices, removed_symbols) else: hyphen = None diff --git a/src/modules/Speech_Recognition/speech_recognition.py b/src/modules/Speech_Recognition/speech_recognition.py index 6a343bf..08d829d 100644 --- a/src/modules/Speech_Recognition/speech_recognition.py +++ b/src/modules/Speech_Recognition/speech_recognition.py @@ -6,7 +6,7 @@ import speech_recognition as sr from pydub import AudioSegment from pydub.silence import split_on_silence -from src.modules.console_colors import ULTRASINGER_HEAD +from modules.console_colors import ULTRASINGER_HEAD # todo: Code from here: https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python diff --git a/src/modules/Ultrastar/ultrastar_converter.py b/src/modules/Ultrastar/ultrastar_converter.py index d07b8f2..d9978eb 100644 --- a/src/modules/Ultrastar/ultrastar_converter.py +++ b/src/modules/Ultrastar/ultrastar_converter.py @@ -1,17 +1,17 @@ """Ultrastar Converter""" -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue +from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue def real_bpm_to_ultrastar_bpm(real_bpm: float) -> float: """Converts real BPM to UltraStar BPM""" - # The UltraStar BPM info is a fifth beat of the real BPM + # The UltraStar BPM info is a fourth beat of the real BPM ultrastar_bpm = real_bpm / 4 return ultrastar_bpm def ultrastar_bpm_to_real_bpm(ultrastar_bpm: float) -> float: """Converts UltraStar BPM to real BPM""" - # The UltraStar BPM info is a fifth beat of the real BPM + # The UltraStar BPM info is a fourth beat of the real BPM bpm = ultrastar_bpm * 4 return bpm diff --git a/src/modules/Ultrastar/ultrastar_parser.py b/src/modules/Ultrastar/ultrastar_parser.py index 12cef3c..9bba162 100644 --- a/src/modules/Ultrastar/ultrastar_parser.py +++ b/src/modules/Ultrastar/ultrastar_parser.py @@ -1,11 +1,11 @@ """Ultrastar txt parser""" -from src.modules.console_colors import ULTRASINGER_HEAD -from src.modules.Ultrastar.ultrastar_converter import ( +from modules.console_colors import ULTRASINGER_HEAD +from modules.Ultrastar.ultrastar_converter import ( get_end_time_from_ultrastar, get_start_time_from_ultrastar, ) -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag, UltrastarTxtNoteTypeTag, FILE_ENCODING +from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag, UltrastarTxtNoteTypeTag, FILE_ENCODING def parse_ultrastar_txt(input_file: str) -> UltrastarTxtValue: """Parse ultrastar txt file to UltrastarTxt class""" diff --git a/src/modules/Ultrastar/ultrastar_score_calculator.py b/src/modules/Ultrastar/ultrastar_score_calculator.py index 588abd8..a02194c 100644 --- a/src/modules/Ultrastar/ultrastar_score_calculator.py +++ b/src/modules/Ultrastar/ultrastar_score_calculator.py @@ -2,7 +2,7 @@ import librosa -from src.modules.console_colors import ( +from modules.console_colors import ( ULTRASINGER_HEAD, blue_highlighted, cyan_highlighted, @@ -10,14 +10,14 @@ from src.modules.console_colors import ( light_blue_highlighted, underlined, ) -from src.modules.Midi.midi_creator import create_midi_note_from_pitched_data -from src.modules.Ultrastar.ultrastar_converter import ( +from modules.Midi.midi_creator import create_midi_note_from_pitched_data +from modules.Ultrastar.ultrastar_converter import ( get_end_time_from_ultrastar, get_start_time_from_ultrastar, ultrastar_note_to_midi_note, ) -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue -from src.modules.Pitcher.pitched_data import PitchedData +from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue +from modules.Pitcher.pitched_data import PitchedData MAX_SONG_SCORE = 10000 MAX_SONG_LINE_BONUS = 1000 @@ -107,7 +107,7 @@ def calculate_score(pitched_data: PitchedData, ultrastar_class: UltrastarTxtValu start_time = get_start_time_from_ultrastar(ultrastar_class, pos) end_time = get_end_time_from_ultrastar(ultrastar_class, pos) duration = end_time - start_time - step_size = 0.01 # todo: should be beat length ? + step_size = 0.09 # Todo: Whats is the step size of the game? Its not 1/bps -> one beat in seconds s = 60/bpm parts = int(duration / step_size) parts = 1 if parts == 0 else parts @@ -122,6 +122,8 @@ def calculate_score(pitched_data: PitchedData, ultrastar_class: UltrastarTxtValu for part in range(parts): start = start_time + step_size * part end = start + step_size + if end_time < end or part == parts - 1: + end = end_time pitch_note = create_midi_note_from_pitched_data( start, end, pitched_data ) diff --git a/src/modules/Ultrastar/ultrastar_txt.py b/src/modules/Ultrastar/ultrastar_txt.py index 0c8bd2f..d22f91d 100644 --- a/src/modules/Ultrastar/ultrastar_txt.py +++ b/src/modules/Ultrastar/ultrastar_txt.py @@ -59,6 +59,8 @@ class UltrastarTxtValue: artist = "" title = "" + year = None + genre = "" mp3 = "" video = None gap = "" diff --git a/src/modules/Ultrastar/ultrastar_writer.py b/src/modules/Ultrastar/ultrastar_writer.py index 3e8703f..ecd616b 100644 --- a/src/modules/Ultrastar/ultrastar_writer.py +++ b/src/modules/Ultrastar/ultrastar_writer.py @@ -4,14 +4,15 @@ import re import langcodes -from src.modules.console_colors import ULTRASINGER_HEAD -from src.modules.Ultrastar.ultrastar_converter import ( +from modules.console_colors import ULTRASINGER_HEAD +from modules.Ultrastar.ultrastar_converter import ( real_bpm_to_ultrastar_bpm, second_to_beat, ) -from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag, UltrastarTxtNoteTypeTag, FILE_ENCODING -from src.modules.Speech_Recognition.TranscribedData import TranscribedData -from src.modules.Ultrastar.ultrastar_score_calculator import Score +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_multiplier(real_bpm: float) -> int: @@ -47,27 +48,29 @@ def create_ultrastar_txt_from_automation( f"{ULTRASINGER_HEAD} Creating {ultrastar_file_output} from transcription." ) - real_bpm = real_bpm_to_ultrastar_bpm(bpm) - multiplication = get_multiplier(real_bpm) - ultrastar_bpm = real_bpm * get_multiplier(real_bpm) + ultrastar_bpm = real_bpm_to_ultrastar_bpm(bpm) + multiplication = get_multiplier(ultrastar_bpm) + ultrastar_bpm = ultrastar_bpm * get_multiplier(ultrastar_bpm) with open(ultrastar_file_output, "w", encoding=FILE_ENCODING) as file: gap = transcribed_data[0].start file.write(f"#{UltrastarTxtTag.ARTIST}:{ultrastar_class.artist}\n") file.write(f"#{UltrastarTxtTag.TITLE}:{ultrastar_class.title}\n") - file.write(f"#{UltrastarTxtTag.CREATOR}:{ultrastar_class.creator}\n") - file.write(f"#{UltrastarTxtTag.FIXER}:{ultrastar_class.fixer}\n") + if ultrastar_class.year is not None: + file.write(f"#{UltrastarTxtTag.YEAR}:{ultrastar_class.year}\n") if ultrastar_class.language is not None: - file.write( - f"#{UltrastarTxtTag.LANGUAGE}:{get_language_name(ultrastar_class.language)}\n" - ) + file.write(f"#{UltrastarTxtTag.LANGUAGE}:{get_language_name(ultrastar_class.language)}\n") + if ultrastar_class.genre: + file.write(f"#{UltrastarTxtTag.GENRE}:{ultrastar_class.genre}\n") if ultrastar_class.cover is not None: file.write(f"#{UltrastarTxtTag.COVER}:{ultrastar_class.cover}\n") file.write(f"#{UltrastarTxtTag.MP3}:{ultrastar_class.mp3}\n") file.write(f"#{UltrastarTxtTag.VIDEO}:{ultrastar_class.video}\n") file.write(f"#{UltrastarTxtTag.BPM}:{round(ultrastar_bpm, 2)}\n") # not the real BPM! file.write(f"#{UltrastarTxtTag.GAP}:{int(gap * 1000)}\n") + file.write(f"#{UltrastarTxtTag.CREATOR}:{ultrastar_class.creator}\n") + file.write(f"#{UltrastarTxtTag.FIXER}:{ultrastar_class.fixer}\n") file.write(f"#{UltrastarTxtTag.COMMENT}:{ultrastar_class.comment}\n") # Write the singing part @@ -90,12 +93,12 @@ def create_ultrastar_txt_from_automation( # 'n2' duration at real beat # 'n3' pitch where 0 == C4 # 'w' lyric - file.write(f"{UltrastarTxtNoteTypeTag.NORMAL} ") - file.write(str(start_beat) + " ") - file.write(str(duration) + " ") - file.write(str(note_numbers[i]) + " ") - file.write(data.word) - file.write("\n") + line = f"{UltrastarTxtNoteTypeTag.NORMAL} " \ + f"{str(start_beat)} " \ + f"{str(duration)} " \ + f"{str(note_numbers[i])} " \ + f"{data.word}\n" + file.write(line) # detect silence between words if i < len(transcribed_data) - 1: @@ -109,13 +112,13 @@ def create_ultrastar_txt_from_automation( # - 10 # '-' end of current sing part # 'n1' show next at time in real beat - file.write(f"{UltrastarTxtTag.LINEBREAK} ") show_next = ( second_to_beat(data.end - gap, bpm) * multiplication ) - file.write(str(round(show_next))) - file.write("\n") + linebreak = f"{UltrastarTxtTag.LINEBREAK} " \ + f"{str(round(show_next))}\n" + file.write(linebreak) file.write(f"{UltrastarTxtTag.FILE_END}") diff --git a/src/modules/csv_handler.py b/src/modules/csv_handler.py index f2952fa..58cceeb 100644 --- a/src/modules/csv_handler.py +++ b/src/modules/csv_handler.py @@ -2,8 +2,8 @@ import csv -from src.modules.console_colors import ULTRASINGER_HEAD -from src.modules.Speech_Recognition.TranscribedData import TranscribedData +from modules.console_colors import ULTRASINGER_HEAD +from modules.Speech_Recognition.TranscribedData import TranscribedData def export_transcribed_data_to_csv(transcribed_data: list[TranscribedData], filename: str) -> None: diff --git a/src/modules/musicbrainz_client.py b/src/modules/musicbrainz_client.py new file mode 100644 index 0000000..29eb781 --- /dev/null +++ b/src/modules/musicbrainz_client.py @@ -0,0 +1,56 @@ +import musicbrainzngs +import string +from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted + + +def get_music_infos(search_string: str) -> tuple[str, str, str, str]: + print(f"{ULTRASINGER_HEAD} Searching song in {blue_highlighted('musicbrainz')}") + # https://python-musicbrainzngs.readthedocs.io/en/v0.7.1/usage/#searching + + musicbrainzngs.set_useragent("UltraSinger", "0.1", "https://github.com/rakuri255/UltraSinger") + + # search for artist and titel to get release on the first place + artists = musicbrainzngs.search_artists(search_string) + artist = artists['artist-list'][0]['name'] + release_groups = musicbrainzngs.search_release_groups(search_string, artist=artist) + release = release_groups['release-group-list'][0] + + if 'artist-credit-phrase' in release: + artist = release['artist-credit-phrase'] + + title = None + if 'title' in release: + clean_search_string = search_string.translate(str.maketrans('', '', string.punctuation)).lower() + clean_release_title = release['title'].translate(str.maketrans('', '', string.punctuation)).lower() + clean_artist = artist.translate(str.maketrans('', '', string.punctuation)).lower() + + # prepare search string when title and artist are the same + if clean_release_title == clean_artist: + # remmove the first acurance of the artist + clean_search_string = clean_search_string.replace(clean_artist, "", 1) + + if clean_release_title in clean_search_string: + title = release['title'] + else: + print( + f"{ULTRASINGER_HEAD} cant find title {red_highlighted(clean_release_title)} in {red_highlighted(clean_search_string)}") + + if title is None or artist is None: + print(f"{ULTRASINGER_HEAD} {red_highlighted('No match found')}") + return None, None, None, None + + print(f"{ULTRASINGER_HEAD} Found Titel and Artist {blue_highlighted(title)} by {blue_highlighted(artist)}") + + year = None + if 'first-release-date' in release: + year = release['first-release-date'] + print(f"{ULTRASINGER_HEAD} Found release year: {blue_highlighted(year)}") + + genres = None + if 'tag-list' in release: + genres = "" + for tag in release['tag-list']: + genres += f"{tag['name']}," + print(f"{ULTRASINGER_HEAD} Found genres: {blue_highlighted(genres)}") + + return title, artist, year, genres diff --git a/src/modules/os_helper.py b/src/modules/os_helper.py index 8cb65f6..cc356d2 100644 --- a/src/modules/os_helper.py +++ b/src/modules/os_helper.py @@ -3,7 +3,7 @@ import os import shutil -from src.modules.console_colors import ULTRASINGER_HEAD +from modules.console_colors import ULTRASINGER_HEAD def create_folder(folder_name: str) -> None: @@ -19,11 +19,16 @@ def move(original: str, target: str) -> None: shutil.move(original, target) -def copy(original:str, target: str) -> None: +def copy(original: str, target: str) -> None: """Copies a file from one location to another.""" shutil.copy(original, target) +def rename(original: str, target: str) -> None: + """Renames a file.""" + os.rename(original, target) + + def current_executor_path() -> str: """Current executor path""" return os.getcwd() diff --git a/src/modules/plot.py b/src/modules/plot.py index a0aa263..13bf9c7 100644 --- a/src/modules/plot.py +++ b/src/modules/plot.py @@ -4,9 +4,9 @@ import os import librosa from matplotlib import pyplot as plt from matplotlib.patches import Rectangle -from src.modules.Pitcher.pitched_data import PitchedData -from src.modules.Speech_Recognition.TranscribedData import TranscribedData -from src.modules.console_colors import ULTRASINGER_HEAD +from modules.Pitcher.pitched_data import PitchedData +from modules.Speech_Recognition.TranscribedData import TranscribedData +from modules.console_colors import ULTRASINGER_HEAD def get_frequency_range(midi_note: str) -> float: