From 8a027666efd94628f98ea0c09599b3e0bde3914f Mon Sep 17 00:00:00 2001 From: Vadim Date: Sat, 20 May 2023 23:37:21 +0200 Subject: [PATCH 01/11] - Added GPU support for demucs and Whisper --- README.md | 16 +++++++++++++++- UltraSinger.py | 2 +- moduls/Audio/separation.py | 5 +++-- moduls/DeviceDetection/device_detection.py | 19 +++++++------------ requirements.txt | 7 +++++-- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4759e73..290bede 100644 --- a/README.md +++ b/README.md @@ -169,4 +169,18 @@ After that you need to install the requirements from `requirements.txt`. Also install ffmpeg separately. -Then just use the `How to use` commands, described above. \ No newline at end of file +Then just use the `How to use` commands, described above. + +### Use GPU + +You need a cuda device. +For best use you need to install the cuda driver for your gpu see [here](https://developer.nvidia.com/cuda-downloads). +Install torch with cuda separately in your `venv`. See this [link](https://pytorch.org/get-started/locally/). + +Command: +``` +pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 +``` + +If program crash because of low VRAM than use a smaller model. +Whisper needs more than 8GB VRAM in the `large` model! \ No newline at end of file diff --git a/UltraSinger.py b/UltraSinger.py index b89c7e7..161502b 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -283,7 +283,7 @@ def transcribe_audio(transcribed_data): def separate_vocal_from_audio(basename_without_ext, cache_path, ultrastar_audio_input_path): audio_separation_path = os.path.join(cache_path, "separated", "htdemucs", basename_without_ext) if settings.use_separated_vocal or settings.create_karaoke: - separate_audio(ultrastar_audio_input_path, cache_path) + separate_audio(ultrastar_audio_input_path, cache_path, settings.device) if settings.use_separated_vocal: vocals_path = os.path.join(audio_separation_path, "vocals.wav") convert_audio_to_mono_wav(vocals_path, settings.mono_audio_path) diff --git a/moduls/Audio/separation.py b/moduls/Audio/separation.py index 20f4da3..1bc8b5c 100644 --- a/moduls/Audio/separation.py +++ b/moduls/Audio/separation.py @@ -8,7 +8,8 @@ def separate_audio(input_file_path, output_file, device="cpu"): # Model selection? # -n mdx_q # -n htdemucs_ft - # todo: -d cpu otherwise it automatically uses gpu - subprocess.run(["demucs", "--two-stems=vocals", input_file_path]) + device = "-d cpu" if device == "cpu" else "cuda" + subprocess.run(["demucs", "-d", device, "--two-stems=vocals", input_file_path]) + separated_folder = path_join(current_executor_path(), "separated") move(separated_folder, output_file) diff --git a/moduls/DeviceDetection/device_detection.py b/moduls/DeviceDetection/device_detection.py index 120138d..bd0b3c2 100644 --- a/moduls/DeviceDetection/device_detection.py +++ b/moduls/DeviceDetection/device_detection.py @@ -1,20 +1,15 @@ import tensorflow as tf from moduls.Log import PRINT_ULTRASTAR, print_red_highlighted_text - +import torch def get_available_device(): - gpus = tf.config.list_physical_devices('GPU') + isCuda = torch.cuda.is_available() - if not gpus: - print("{} There are no GPUs available. Using {}.".format(PRINT_ULTRASTAR, print_red_highlighted_text("cpu"))) + if not isCuda: + print("{} There are no {} devices available. -> Using {}.".format(PRINT_ULTRASTAR, print_red_highlighted_text("cuda"), print_red_highlighted_text("cpu"))) return "cpu" + else: + print("{} Using {} GPU.".format(PRINT_ULTRASTAR, print_red_highlighted_text("cuda"))) - print("{} Found available GPUs:".format(PRINT_ULTRASTAR)) - for gpu in gpus: - print("Name:", gpu.name, " Type:", gpu.device_type) - - # Todo: Finish this - print("{} GPU usage are currently in development. Using {}".format(PRINT_ULTRASTAR, - print_red_highlighted_text("cpu"))) - return "cpu" + return "cuda" diff --git a/requirements.txt b/requirements.txt index 1a02274..f78f00e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,11 +5,14 @@ pydub==0.25.1 Levenshtein==0.20.9 scipy==1.10.0 vosk==0.3.45 -tensorflow~=2.11.0 +tensorflow==2.11.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 git+https://github.com/linto-ai/whisper-timestamped PyHyphen~=4.0.3 demucs~=4.0.0 -tqdm~=4.65.0 \ No newline at end of file +tqdm~=4.65.0 +torchaudio~=2.0.2 \ No newline at end of file From ccf17e5d264d127d549c9fedc207db778d2b2e3e Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 21 May 2023 00:13:43 +0200 Subject: [PATCH 02/11] - Update README.md --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 290bede..8823a0c 100644 --- a/README.md +++ b/README.md @@ -173,14 +173,20 @@ Then just use the `How to use` commands, described above. ### Use GPU -You need a cuda device. +With an GPU you can speed up the process and also the quality of the transcription and pitching is better. + +You need a cuda device for this to work. For best use you need to install the cuda driver for your gpu see [here](https://developer.nvidia.com/cuda-downloads). Install torch with cuda separately in your `venv`. See this [link](https://pytorch.org/get-started/locally/). -Command: +Command for `pip`: ``` pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 ``` -If program crash because of low VRAM than use a smaller model. +When you want to use `conda` instead you need a different installation command. See this [link](https://pytorch.org/get-started/locally/). + +#### Info + +If somthing crash because of low VRAM than use a smaller model. Whisper needs more than 8GB VRAM in the `large` model! \ No newline at end of file From b8ca18dee7b1c92eb87462ede287e1d323a6d6ec Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 21 May 2023 21:57:13 +0200 Subject: [PATCH 03/11] - Changed/Updated requirements --- moduls/DeviceDetection/device_detection.py | 2 -- requirements.txt | 13 ++++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/moduls/DeviceDetection/device_detection.py b/moduls/DeviceDetection/device_detection.py index bd0b3c2..6475505 100644 --- a/moduls/DeviceDetection/device_detection.py +++ b/moduls/DeviceDetection/device_detection.py @@ -1,5 +1,3 @@ -import tensorflow as tf - from moduls.Log import PRINT_ULTRASTAR, print_red_highlighted_text import torch diff --git a/requirements.txt b/requirements.txt index f78f00e..eb80b88 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,12 @@ crepe==0.0.13 librosa==0.9.2 -pretty_midi==0.2.9 +pretty_midi~=0.2.10 pydub==0.25.1 -Levenshtein==0.20.9 -scipy==1.10.0 +Levenshtein~=0.21.0 +scipy~=1.10.1 vosk==0.3.45 -tensorflow==2.11.0 -tensorflow-directml-plugin~=0.4.0.dev230202 +tensorflow~=2.12.0 +#tensorflow-directml-plugin~=0.4.0.dev230202 #cudatoolkit==11.8.0 matplotlib~=3.7.1 yt_dlp~=2023.3.4 @@ -14,5 +14,4 @@ numpy~=1.23.5 git+https://github.com/linto-ai/whisper-timestamped PyHyphen~=4.0.3 demucs~=4.0.0 -tqdm~=4.65.0 -torchaudio~=2.0.2 \ No newline at end of file +tqdm~=4.65.0 \ No newline at end of file From 13477dde06214a68f2e3c3f5e71cd2298af4a458 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 21 May 2023 22:25:47 +0200 Subject: [PATCH 04/11] - fix cpu argument for separation --- moduls/Audio/separation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moduls/Audio/separation.py b/moduls/Audio/separation.py index 1bc8b5c..6ef3d38 100644 --- a/moduls/Audio/separation.py +++ b/moduls/Audio/separation.py @@ -8,7 +8,7 @@ def separate_audio(input_file_path, output_file, device="cpu"): # Model selection? # -n mdx_q # -n htdemucs_ft - device = "-d cpu" if device == "cpu" else "cuda" + device = "cpu" if device != "cuda" else "cuda" subprocess.run(["demucs", "-d", device, "--two-stems=vocals", input_file_path]) separated_folder = path_join(current_executor_path(), "separated") From b3c9bb235e7a51aa18cea47535cbc1263c39b3c1 Mon Sep 17 00:00:00 2001 From: Vadim Rangnau <36919102+rakuri255@users.noreply.github.com> Date: Mon, 22 May 2023 10:51:35 +0200 Subject: [PATCH 05/11] Update GPU usage README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 12d8806..fbd3963 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ Install torch with cuda separately in your `venv`. See this [link](https://pytor Command for `pip`: ``` -pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 +pip3 install torch~=2.0.1+cu117 torchvision~=0.15.2+cu117 torchaudio~=2.0.2+cu117 --index-url https://download.pytorch.org/whl/cu117 ``` When you want to use `conda` instead you need a different installation command. See this [link](https://pytorch.org/get-started/locally/). @@ -189,4 +189,4 @@ When you want to use `conda` instead you need a different installation command. #### Info If somthing crash because of low VRAM than use a smaller model. -Whisper needs more than 8GB VRAM in the `large` model! \ No newline at end of file +Whisper needs more than 8GB VRAM in the `large` model! From 7e978a281470d95dd0d441d47100fd435139e930 Mon Sep 17 00:00:00 2001 From: Vadim Date: Mon, 22 May 2023 21:40:06 +0200 Subject: [PATCH 06/11] - added option to force cpu for separation and whisper --- README.md | 4 ++++ Settings.py | 6 ++++-- UltraSinger.py | 12 +++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fbd3963..0d58cc8 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ _Not all options working now!_ --disable_separation True|False >> ((default) is False) --disable_karaoke True|False >> ((default) is False) --create_audio_chunks True|False >> ((default) is False) + --force_whisper_cpu True|False >> ((default) is False) + --force_separation_cpu True|False >> ((default) is False) [pitcher] # Default is crepe full @@ -190,3 +192,5 @@ When you want to use `conda` instead you need a different installation command. If somthing crash because of low VRAM than use a smaller model. Whisper needs more than 8GB VRAM in the `large` model! + +But you can force cpu usage with the extra options `--force_whisper_cpu` and `--force_separation_cpu`. diff --git a/Settings.py b/Settings.py index f455436..e60f15a 100644 --- a/Settings.py +++ b/Settings.py @@ -15,7 +15,7 @@ class Settings: # Whisper transcriber = 'whisper' # whisper|vosk - whisper_model = 'large' # Multilingual model tiny|base|small|medium|large + whisper_model = 'large' # Multilingual model tiny|base|small|medium|large # English-only model tiny.en|base.en|small.en|medium.en # Vosk @@ -26,5 +26,7 @@ class Settings: crepe_step_size = 10 # Device - device = 'cpu' # cpu|cuda|gpu + device = 'cpu' # cpu|cuda + force_whisper_cpu = False + force_separation_cpu = False diff --git a/UltraSinger.py b/UltraSinger.py index a85e685..b345af9 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -280,8 +280,9 @@ def run(): def transcribe_audio(transcribed_data): if settings.transcriber == "whisper": + device = "cpu" if settings.force_whisper_cpu else settings.device transcribed_data, language = transcribe_with_whisper(settings.mono_audio_path, settings.whisper_model, - settings.device) + device) else: # vosk transcribed_data = transcribe_with_vosk(settings.mono_audio_path, settings.vosk_model_path) # todo: make language selectable @@ -291,8 +292,9 @@ def transcribe_audio(transcribed_data): def separate_vocal_from_audio(basename_without_ext, cache_path, ultrastar_audio_input_path): audio_separation_path = os.path.join(cache_path, "separated", "htdemucs", basename_without_ext) + device = "cpu" if settings.force_separation_cpu else settings.device if settings.use_separated_vocal or settings.create_karaoke: - separate_audio(ultrastar_audio_input_path, cache_path, settings.device) + separate_audio(ultrastar_audio_input_path, cache_path, device) if settings.use_separated_vocal: vocals_path = os.path.join(audio_separation_path, "vocals.wav") convert_audio_to_mono_wav(vocals_path, settings.mono_audio_path) @@ -433,7 +435,7 @@ def main(argv): def init_settings(argv): short = "hi:o:amv:" long = ["ifile=", "ofile=", "crepe=", "vosk=", "whisper=", "hyphenation=", "disable_separation=", - "disable_karaoke=", "create_audio_chunks="] + "disable_karaoke=", "create_audio_chunks=", "force_whisper_cpu=", "force_separation_cpu="] opts, args = getopt.getopt(argv, short, long) if len(opts) == 0: print_help() @@ -462,6 +464,10 @@ def init_settings(argv): settings.create_karaoke = not arg elif opt in ("--create_audio_chunks"): settings.create_audio_chunks = arg + elif opt in ("--force_whisper_cpu"): + settings.force_whisper_cpu = arg + elif opt in ("--force_separation_cpu"): + settings.force_separation_cpu = arg if settings.output_file_path == '': if settings.input_file_path.startswith('https:'): dirname = os.getcwd() From dcd18cab7919a6ed5e7823f8c8ee44bba15a8bdd Mon Sep 17 00:00:00 2001 From: Vadim Date: Mon, 29 May 2023 13:55:56 +0200 Subject: [PATCH 07/11] - Added f strings --- moduls/DeviceDetection/device_detection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moduls/DeviceDetection/device_detection.py b/moduls/DeviceDetection/device_detection.py index 6475505..788ce0f 100644 --- a/moduls/DeviceDetection/device_detection.py +++ b/moduls/DeviceDetection/device_detection.py @@ -5,9 +5,9 @@ def get_available_device(): isCuda = torch.cuda.is_available() if not isCuda: - print("{} There are no {} devices available. -> Using {}.".format(PRINT_ULTRASTAR, print_red_highlighted_text("cuda"), print_red_highlighted_text("cpu"))) + print(f"{PRINT_ULTRASTAR} There are no {print_red_highlighted_text('cuda')} devices available. -> Using {print_red_highlighted_text('cpu')}.") return "cpu" else: - print("{} Using {} GPU.".format(PRINT_ULTRASTAR, print_red_highlighted_text("cuda"))) + print(f"{PRINT_ULTRASTAR} Using {print_red_highlighted_text('cuda')} GPU.") return "cuda" From 2ba180140c26a250ec0f3c5394342fa4dab59daa Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 1 Jun 2023 16:40:15 +0200 Subject: [PATCH 08/11] - Changed readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 1b9eb70..73f60fc 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ This will help me alot to keep this project alive and improve it. * Type `.venv\Scripts\activate` and press enter. * You should see now a `(.venv)` in front of your console line. * Install the requirements with `pip install -r requirements.txt`. +* Install gpu requirements `pip3 install torch==2.0.1+cu117 torchvision==0.15.2+cu117 torchaudio==2.0.2+cu117 --index-url https://download.pytorch.org/whl/cu117` * Now you can use the UltraSinger source code with `py UltraSinger.py [opt] [mode] [transcription] [pitcher] [extra]`. See [How to use](#how-to-use) for more information. For more information about Python environments look [here](https://code.visualstudio.com/docs/python/environments#_global-virtual-or-conda-environments). @@ -51,6 +52,7 @@ As copy: py -m venv .venv .venv\Scripts\activate pip install -r requirements.txt +pip3 install torch==2.0.1+cu117 torchvision==0.15.2+cu117 torchaudio==2.0.2+cu117 --index-url https://download.pytorch.org/whl/cu117 ``` @@ -214,3 +216,16 @@ If somthing crash because of low VRAM than use a smaller model. Whisper needs more than 8GB VRAM in the `large` model! But you can force cpu usage with the extra options `--force_whisper_cpu` and `--force_separation_cpu`. + +### Use GPU + +With an GPU you can speed up the process and also the quality of the transcription and pitching is better. +You need a cuda device for this to work. +If you use MAC than sorry, there is no cuda device for MAC machinges. + +#### Info + +If somthing crash because of low VRAM than use a smaller model. +Whisper needs more than 8GB VRAM in the `large` model! + +But you can force cpu usage with the extra options `--force_whisper_cpu` and `--force_separation_cpu`. From fbd18ee7afbf9181bcc2f1ee9438c05c179e655b Mon Sep 17 00:00:00 2001 From: Vadim Rangnau Date: Wed, 7 Jun 2023 12:16:43 +0200 Subject: [PATCH 09/11] - fix enumaration --- UltraSinger.py | 51 ++++++++++--------- modules/Audio/vocal_chunks.py | 36 ++++++------- modules/Midi/midi_creator.py | 22 +++++--- modules/Pitcher/pitcher.py | 11 ++-- .../Ultrastar/ultrastar_score_calculator.py | 13 ++--- modules/Ultrastar/ultrastar_writer.py | 20 ++++---- 6 files changed, 83 insertions(+), 70 deletions(-) diff --git a/UltraSinger.py b/UltraSinger.py index 665a25c..0cc98e4 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -68,10 +68,11 @@ def get_confidence(pitched_data, threshold): conf_f = [] conf_c = [] for i in enumerate(pitched_data.times): + pos = i[0] if pitched_data.confidence[i] > threshold: - conf_t.append(pitched_data.times[i]) - conf_f.append(pitched_data.frequencies[i]) - conf_c.append(pitched_data.confidence[i]) + conf_t.append(pitched_data.times[pos]) + conf_f.append(pitched_data.frequencies[pos]) + conf_c.append(pitched_data.confidence[pos]) return conf_t, conf_f, conf_c @@ -80,8 +81,8 @@ def convert_ultrastar_note_numbers(midi_notes): print(f"{PRINT_ULTRASTAR} Creating Ultrastar notes from midi data") ultrastar_note_numbers = [] - for i in enumerate(midi_notes): - note_number_librosa = librosa.note_to_midi(midi_notes[i]) + for i, note in enumerate(midi_notes): + note_number_librosa = librosa.note_to_midi(note) pitch = ultrastar_converter.midi_note_to_ultrastar_note( note_number_librosa ) @@ -125,28 +126,28 @@ def pitch_each_chunk_with_crepe(directory): def add_hyphen_to_data(transcribed_data, hyphen_words): """Docstring""" - data = [] + new_data = [] - for i in enumerate(transcribed_data): + for i, data in enumerate(transcribed_data): if not hyphen_words[i]: - data.append(transcribed_data[i]) + new_data.append(data) else: - chunk_duration = transcribed_data[i].end - transcribed_data[i].start + chunk_duration = data.end - data.start chunk_duration = chunk_duration / (len(hyphen_words[i])) - next_start = transcribed_data[i].start - for j in enumerate(hyphen_words[i]): - dup = copy.copy(transcribed_data[i]) + next_start = data.start + for j, hyphens in enumerate(hyphen_words[i]): + dup = copy.copy(data) dup.start = next_start - next_start = transcribed_data[i].end - chunk_duration * ( - len(hyphen_words[i]) - 1 - j + next_start = data.end - chunk_duration * ( + len(hyphens) - 1 - j ) dup.end = next_start dup.word = hyphen_words[i][j] dup.is_hyphen = True - data.append(dup) + new_data.append(dup) - return data + return new_data def get_bpm_from_data(data, sampling_rate): @@ -238,10 +239,10 @@ def plot(input_file, vosk_transcribed_data, midi_notes): plt.plot(conf_t, conf_f, linewidth=0.1) plt.savefig(os.path.join("test", "crepe_0.4.png")) - for i in enumerate(vosk_transcribed_data): + for i, data in enumerate(vosk_transcribed_data): note_frequency = librosa.note_to_hz(midi_notes[i]) plt.plot( - [vosk_transcribed_data[i].start, vosk_transcribed_data[i].end], + [data.start, data.end], [note_frequency, note_frequency], linewidth=1, alpha=0.5, @@ -252,8 +253,8 @@ def plot(input_file, vosk_transcribed_data, midi_notes): def remove_unecessary_punctuations(transcribed_data): """Docstring""" punctuation = ".," - for i in enumerate(transcribed_data): - transcribed_data[i].word = transcribed_data[i].word.translate( + for i, data in enumerate(transcribed_data): + data.word = data.word.translate( {ord(i): None for i in punctuation} ) @@ -269,9 +270,9 @@ def hyphenate_each_word(language, transcribed_data): return None sleep(0.1) - for i in tqdm(enumerate(transcribed_data)): + for i, data in tqdm(enumerate(transcribed_data)): hyphenated_word.append( - hyphenation(transcribed_data[i].word, lang_region) + hyphenation(data.word, lang_region) ) return hyphenated_word @@ -692,9 +693,9 @@ def pitch_audio(is_audio, transcribed_data, ultrastar_class): if is_audio: start_times = [] end_times = [] - for i in enumerate(transcribed_data): - start_times.append(transcribed_data[i].start) - end_times.append(transcribed_data[i].end) + 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 ) diff --git a/modules/Audio/vocal_chunks.py b/modules/Audio/vocal_chunks.py index 54749cf..e24fbc0 100644 --- a/modules/Audio/vocal_chunks.py +++ b/modules/Audio/vocal_chunks.py @@ -53,13 +53,13 @@ def export_chunks_from_transcribed_data( wave_file = wave.open(audio_filename, "rb") sample_rate, n_channels = wave_file.getparams()[2], wave_file.getparams()[0] - for i in enumerate(transcribed_data): - start_byte = int(transcribed_data[i].start * sample_rate * n_channels) - end_byte = int(transcribed_data[i].end * sample_rate * n_channels) + for i, data in enumerate(transcribed_data): + start_byte = int(data.start * sample_rate * n_channels) + end_byte = int(data.end * sample_rate * n_channels) chunk = get_chunk(end_byte, start_byte, wave_file) export_chunk_to_wav_file( - chunk, output_folder_name, i, transcribed_data[i].word, wave_file + chunk, output_folder_name, i, data.word, wave_file ) wave_file.close() @@ -74,9 +74,9 @@ def remove_silence_from_transcribtion_data(audio_path, transcribed_data): audio, sample_rate = librosa.load(audio_path, sr=None) - for i in enumerate(transcribed_data): - start_time = transcribed_data[i].start - end_time = transcribed_data[i].end + for i, data in enumerate(transcribed_data): + start_time = data.start + end_time = data.end start_sample = int(start_time * sample_rate) end_sample = int(end_time * sample_rate) chunk = audio[start_sample:end_sample] @@ -103,11 +103,11 @@ def remove_silence_from_transcribtion_data(audio_path, transcribed_data): first_silence_duration = 0 last_silence_duration = 0 - transcribed_data[i].start = ( - transcribed_data[i].start + first_silence_duration + data.start = ( + data.start + first_silence_duration ) - transcribed_data[i].end = ( - transcribed_data[i].end - last_silence_duration + data.end = ( + data.end - last_silence_duration ) return transcribed_data @@ -124,7 +124,7 @@ def export_chunks_from_ultrastar_data( wave_file = wave.open(audio_filename, "rb") sample_rate, n_channels = wave_file.getparams()[2], wave_file.getparams()[0] - for i in enumerate(ultrastar_data.words): + for i, word in enumerate(ultrastar_data.words): start_time = get_start_time_from_ultrastar(ultrastar_data, i) end_time = get_end_time_from_ultrastar(ultrastar_data, i) @@ -133,7 +133,7 @@ def export_chunks_from_ultrastar_data( chunk = get_chunk(end_byte, start_byte, wave_file) export_chunk_to_wav_file( - chunk, folder_name, i, ultrastar_data.words[i], wave_file + chunk, folder_name, i, word, wave_file ) @@ -170,12 +170,12 @@ def export_transcribed_data_to_csv(transcribed_data, filename): writer = csv.writer(csvfile) header = ["word", "start", "end", "confidence"] writer.writerow(header) - for i in enumerate(transcribed_data): + for i, data in enumerate(transcribed_data): writer.writerow( [ - transcribed_data[i].word, - transcribed_data[i].start, - transcribed_data[i].end, - transcribed_data[i].conf, + data.word, + data.start, + data.end, + data.conf, ] ) diff --git a/modules/Midi/midi_creator.py b/modules/Midi/midi_creator.py index 68a02ca..1b3806d 100644 --- a/modules/Midi/midi_creator.py +++ b/modules/Midi/midi_creator.py @@ -7,13 +7,16 @@ import librosa import numpy as np import pretty_midi -from modules.Log import PRINT_ULTRASTAR 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 modules.Log import ( + PRINT_ULTRASTAR, + print_red_highlighted_text, +) def convert_ultrastar_to_midi_instrument(ultrastar_class): @@ -25,9 +28,10 @@ def convert_ultrastar_to_midi_instrument(ultrastar_class): velocity = 100 for i in enumerate(ultrastar_class.words): - start_time = get_start_time_from_ultrastar(ultrastar_class, i) - end_time = get_end_time_from_ultrastar(ultrastar_class, i) - pitch = ultrastar_note_to_midi_note(int(ultrastar_class.pitches[i])) + pos = i[0] + start_time = get_start_time_from_ultrastar(ultrastar_class, pos) + end_time = get_end_time_from_ultrastar(ultrastar_class, pos) + pitch = ultrastar_note_to_midi_note(int(ultrastar_class.pitches[pos])) note = pretty_midi.Note(velocity, pitch, start_time, end_time) instrument.notes.append(note) @@ -82,8 +86,9 @@ def create_midi_notes_from_pitched_data(start_times, end_times, pitched_data): midi_notes = [] for i in enumerate(start_times): - start_time = start_times[i] - end_time = end_times[i] + pos = i[0] + start_time = start_times[pos] + end_time = end_times[pos] note = create_midi_note_from_pitched_data( start_time, end_time, pitched_data @@ -101,6 +106,11 @@ def create_midi_note_from_pitched_data(start_time, end_time, pitched_data): start = find_nearest_index(pitched_data.times, start_time) end = find_nearest_index(pitched_data.times, end_time) + # fixme: why is end smaller as start? + if end < start: + print(f"{PRINT_ULTRASTAR} {print_red_highlighted_text('Error: end time is smaller as start time. The note is C0 now ...')}") + return "C0" + if start == end: freqs = [pitched_data.frequencies[start]] confs = [pitched_data.confidence[start]] diff --git a/modules/Pitcher/pitcher.py b/modules/Pitcher/pitcher.py index 1ee6193..e58e29a 100644 --- a/modules/Pitcher/pitcher.py +++ b/modules/Pitcher/pitcher.py @@ -47,14 +47,15 @@ def get_pitch_with_crepe(audio, sample_rate, step_size, model_capacity): return pitched_data -def write_lists_to_csv(time, frequency, confidence, filename): +def write_lists_to_csv(times, frequencies, confidences, filename): """Docstring""" with open(filename, "w", encoding="utf-8", newline="") as csvfile: writer = csv.writer(csvfile) header = ["time", "frequency", "confidence"] writer.writerow(header) - for i in enumerate(time): - writer.writerow([time[i], frequency[i], confidence[i]]) + for i in enumerate(times): + pos = i[0] + writer.writerow([times[pos], frequencies[pos], confidences[pos]]) def read_data_from_csv(filename): @@ -71,8 +72,8 @@ def read_data_from_csv(filename): def get_frequency_with_high_confidence(freqs, confs, threshold=0.4): """Docstring""" conf_f = [] - for i in enumerate(confs): - if confs[i] > threshold: + for i, conf in enumerate(confs): + if conf > threshold: conf_f.append(freqs[i]) if not conf_f: conf_f = freqs diff --git a/modules/Ultrastar/ultrastar_score_calculator.py b/modules/Ultrastar/ultrastar_score_calculator.py index 4bb12c5..e4d4cb5 100644 --- a/modules/Ultrastar/ultrastar_score_calculator.py +++ b/modules/Ultrastar/ultrastar_score_calculator.py @@ -95,14 +95,15 @@ def calculate_score(pitched_data, ultrastar_class): ) for i in enumerate(ultrastar_class.words): + pos = i[0] if ultrastar_class.words == "": continue - if ultrastar_class.noteType[i] == "F": + if ultrastar_class.noteType[pos] == "F": continue - start_time = get_start_time_from_ultrastar(ultrastar_class, i) - end_time = get_end_time_from_ultrastar(ultrastar_class, i) + 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 ? parts = int(duration / step_size) @@ -112,7 +113,7 @@ def calculate_score(pitched_data, ultrastar_class): simple_part_line_bonus_points = 0 ultrastar_midi_note = ultrastar_note_to_midi_note( - int(ultrastar_class.pitches[i]) + int(ultrastar_class.pitches[pos]) ) ultrastar_note = librosa.midi_to_note(ultrastar_midi_note) @@ -126,14 +127,14 @@ def calculate_score(pitched_data, ultrastar_class): if pitch_note[:-1] == ultrastar_note[:-1]: # Ignore octave high simple_points = add_point( - ultrastar_class.noteType[i], simple_points + ultrastar_class.noteType[pos], simple_points ) simple_part_line_bonus_points += 1 if pitch_note == ultrastar_note: # Octave high must be the same accurate_points = add_point( - ultrastar_class.noteType[i], accurate_points + ultrastar_class.noteType[pos], accurate_points ) accurate_part_line_bonus_points += 1 diff --git a/modules/Ultrastar/ultrastar_writer.py b/modules/Ultrastar/ultrastar_writer.py index 9831c8d..dd7f52e 100644 --- a/modules/Ultrastar/ultrastar_writer.py +++ b/modules/Ultrastar/ultrastar_writer.py @@ -69,10 +69,10 @@ def create_ultrastar_txt_from_automation( # Write the singing part previous_end_beat = 0 - for i in enumerate(transcribed_data): - start_time = (transcribed_data[i].start - gap) * multiplication + for i, data in enumerate(transcribed_data): + start_time = (data.start - gap) * multiplication end_time = ( - transcribed_data[i].end - transcribed_data[i].start + data.end - data.start ) * multiplication start_beat = round(second_to_beat(start_time, bpm)) duration = round(second_to_beat(end_time, bpm)) @@ -91,13 +91,13 @@ def create_ultrastar_txt_from_automation( file.write(str(start_beat) + " ") file.write(str(duration) + " ") file.write(str(note_numbers[i]) + " ") - file.write(transcribed_data[i].word) + file.write(data.word) file.write("\n") # detect silence between words if i < len(transcribed_data) - 1: silence = ( - transcribed_data[i + 1].start - transcribed_data[i].end + transcribed_data[i + 1].start - data.end ) else: silence = 0 @@ -108,7 +108,7 @@ def create_ultrastar_txt_from_automation( # 'n1' show next at time in real beat file.write("- ") show_next = ( - second_to_beat(transcribed_data[i].end - gap, bpm) + second_to_beat(data.end - gap, bpm) * multiplication ) file.write(str(round(show_next))) @@ -155,14 +155,14 @@ def add_score_to_ultrastar_txt(ultrastar_file_output, score): text = file.read() text = text.split("\n") - for i in enumerate(text): - if text[i].startswith("#COMMENT:"): + for i, line in enumerate(text): + if line.startswith("#COMMENT:"): text[ i - ] = f"{text[i]} | Score: total: {score.score}, notes: {score.notes} line: {score.line_bonus}, golden: {score.golden}" + ] = f"{line} | Score: total: {score.score}, notes: {score.notes} line: {score.line_bonus}, golden: {score.golden}" break - if text[i].startswith(("F ", ": ", "* ", "R ", "G ")): + if line.startswith(("F ", ": ", "* ", "R ", "G ")): text.insert( i, f"#COMMENT: UltraSinger [GitHub] | Score: total: {score.score}, notes: {score.notes} line: {score.line_bonus}, golden: {score.golden}", From 550ba49c47668f0e4fbcef33928cfe7c337a9ccf Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 8 Jun 2023 00:28:38 +0200 Subject: [PATCH 10/11] - Small refactoring --- UltraSinger.py | 40 ++++++++--------- modules/Audio/denoise.py | 4 +- modules/Audio/separation.py | 10 ++--- modules/Audio/vocal_chunks.py | 12 ++--- modules/Audio/youtube.py | 8 ++-- modules/DeviceDetection/device_detection.py | 8 ++-- modules/Log.py | 45 ------------------- modules/Midi/midi_creator.py | 14 +++--- modules/Pitcher/pitcher.py | 10 ++--- modules/Speech_Recognition/Vosk.py | 4 +- modules/Speech_Recognition/Whisper.py | 14 +++--- modules/Speech_Recognition/hyphenation.py | 8 ++-- .../Speech_Recognition/speech_recognition.py | 6 +-- modules/Ultrastar/ultrastar_parser.py | 4 +- .../Ultrastar/ultrastar_score_calculator.py | 20 ++++----- modules/Ultrastar/ultrastar_writer.py | 4 +- modules/console_colors.py | 45 +++++++++++++++++++ modules/os_helper.py | 4 +- 18 files changed, 130 insertions(+), 130 deletions(-) delete mode 100644 modules/Log.py create mode 100644 modules/console_colors.py diff --git a/UltraSinger.py b/UltraSinger.py index 0cc98e4..07e3b3a 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -29,12 +29,12 @@ from modules.Audio.youtube import ( get_youtube_title, ) from modules.DeviceDetection.device_detection import get_available_device -from modules.Log import ( - PRINT_ULTRASTAR, - print_blue_highlighted_text, - print_gold_highlighted_text, - print_light_blue_highlighted_text, - print_red_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + blue_highlighted, + gold_highlighted, + light_blue_highlighted, + red_highlighted, ) from modules.Midi import midi_creator from modules.Midi.midi_creator import ( @@ -78,7 +78,7 @@ def get_confidence(pitched_data, threshold): def convert_ultrastar_note_numbers(midi_notes): """Docstring""" - print(f"{PRINT_ULTRASTAR} Creating Ultrastar notes from midi data") + print(f"{ULTRASINGER_HEAD} Creating Ultrastar notes from midi data") ultrastar_note_numbers = [] for i, note in enumerate(midi_notes): @@ -97,7 +97,7 @@ def convert_ultrastar_note_numbers(midi_notes): def pitch_each_chunk_with_crepe(directory): """Docstring""" print( - f"{PRINT_ULTRASTAR} Pitching each chunk with {print_blue_highlighted_text('crepe')}" + f"{ULTRASINGER_HEAD} Pitching each chunk with {blue_highlighted('crepe')}" ) midi_notes = [] @@ -156,7 +156,7 @@ def get_bpm_from_data(data, sampling_rate): wav_tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sampling_rate) print( - f"{PRINT_ULTRASTAR} BPM is {print_blue_highlighted_text(str(round(wav_tempo[0], 2)))}" + f"{ULTRASINGER_HEAD} BPM is {blue_highlighted(str(round(wav_tempo[0], 2)))}" ) return wav_tempo[0] @@ -265,7 +265,7 @@ def hyphenate_each_word(language, transcribed_data): lang_region = language_check(language) if lang_region is None: print( - f"{PRINT_ULTRASTAR} {print_red_highlighted_text('Error in hyphenation for language ')} {print_blue_highlighted_text(language)} {print_red_highlighted_text(', maybe you want to disable it?')}" + f"{ULTRASINGER_HEAD} {red_highlighted('Error in hyphenation for language ')} {blue_highlighted(language)} {red_highlighted(', maybe you want to disable it?')}" ) return None @@ -281,13 +281,13 @@ def print_support(): """Docstring""" print() print( - f"{PRINT_ULTRASTAR} {print_gold_highlighted_text('Do you like UltraSinger? And want it to be even better? Then help with your')} {print_light_blue_highlighted_text('support')}{print_gold_highlighted_text('!')}" + f"{ULTRASINGER_HEAD} {gold_highlighted('Do you like UltraSinger? And want it to be even better? Then help with your')} {light_blue_highlighted('support')}{gold_highlighted('!')}" ) print( - f"{PRINT_ULTRASTAR} See project page -> https://github.com/rakuri255/UltraSinger" + f"{ULTRASINGER_HEAD} See project page -> https://github.com/rakuri255/UltraSinger" ) print( - f"{PRINT_ULTRASTAR} {print_gold_highlighted_text('This will help alot to keep this project alive and improved.')}" + f"{ULTRASINGER_HEAD} {gold_highlighted('This will help alot to keep this project alive and improved.')}" ) @@ -299,7 +299,7 @@ def run(): if not is_audio: # Parse Ultrastar txt print( - f"{PRINT_ULTRASTAR} {print_gold_highlighted_text('re-pitch mode')}" + f"{ULTRASINGER_HEAD} {gold_highlighted('re-pitch mode')}" ) ( basename_without_ext, @@ -310,7 +310,7 @@ def run(): ) = parse_ultrastar_txt() elif settings.input_file_path.startswith("https:"): # Youtube print( - f"{PRINT_ULTRASTAR} {print_gold_highlighted_text('full automatic mode')}" + f"{ULTRASINGER_HEAD} {gold_highlighted('full automatic mode')}" ) ( basename_without_ext, @@ -319,7 +319,7 @@ def run(): ) = download_from_youtube() else: # Audio File print( - f"{PRINT_ULTRASTAR} {print_gold_highlighted_text('full automatic mode')}" + f"{ULTRASINGER_HEAD} {gold_highlighted('full automatic mode')}" ) ( basename_without_ext, @@ -429,7 +429,7 @@ def get_unused_song_output_dir(path): i += 1 if i > 999: print( - f"{PRINT_ULTRASTAR} {print_red_highlighted_text('Error: Could not create output folder! (999) is the maximum number of tries.')}" + f"{ULTRASINGER_HEAD} {red_highlighted('Error: Could not create output folder! (999) is the maximum number of tries.')}" ) sys.exit(1) return path @@ -488,7 +488,7 @@ def calculate_score_points( ) else: print( - f"{PRINT_ULTRASTAR} {print_blue_highlighted_text('Score of original Ultrastar txt')}" + f"{ULTRASINGER_HEAD} {blue_highlighted('Score of original Ultrastar txt')}" ) ( simple_score, @@ -500,7 +500,7 @@ def calculate_score_points( simple_score, accurate_score ) print( - f"{PRINT_ULTRASTAR} {print_blue_highlighted_text('Score of re-pitched Ultrastar txt')}" + f"{ULTRASINGER_HEAD} {blue_highlighted('Score of re-pitched Ultrastar txt')}" ) ultrastar_class = ultrastar_parser.parse_ultrastar_txt( ultrastar_file_output @@ -661,7 +661,7 @@ def parse_ultrastar_txt(): def create_midi_file(is_audio, real_bpm, song_output, ultrastar_class): """Docstring""" print( - f"{PRINT_ULTRASTAR} Creating Midi with {print_blue_highlighted_text('pretty_midi')}" + f"{ULTRASINGER_HEAD} Creating Midi with {blue_highlighted('pretty_midi')}" ) if is_audio: voice_instrument = [ diff --git a/modules/Audio/denoise.py b/modules/Audio/denoise.py index 16297c0..42b5e9d 100644 --- a/modules/Audio/denoise.py +++ b/modules/Audio/denoise.py @@ -2,7 +2,7 @@ import ffmpeg -from modules.Log import PRINT_ULTRASTAR, print_blue_highlighted_text +from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted def ffmpeg_reduce_noise(input_file_path, output_file): @@ -19,7 +19,7 @@ def ffmpeg_reduce_noise(input_file_path, output_file): # With this enabled, noise floor is automatically adjusted. print( - f"{PRINT_ULTRASTAR} Reduce noise from vocal audio with {print_blue_highlighted_text('ffmpeg')}." + f"{ULTRASINGER_HEAD} Reduce noise from vocal audio with {blue_highlighted('ffmpeg')}." ) ( ffmpeg.input(input_file_path) diff --git a/modules/Audio/separation.py b/modules/Audio/separation.py index 902dfe2..7a767f4 100644 --- a/modules/Audio/separation.py +++ b/modules/Audio/separation.py @@ -2,10 +2,10 @@ import subprocess -from modules.Log import ( - PRINT_ULTRASTAR, - print_blue_highlighted_text, - print_red_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + blue_highlighted, + red_highlighted, ) from modules.os_helper import current_executor_path, move, path_join @@ -14,7 +14,7 @@ def separate_audio(input_file_path, output_file, device="cpu"): """Docstring""" print( - f"{PRINT_ULTRASTAR} Separating vocals from audio with {print_blue_highlighted_text('demucs')} and {print_red_highlighted_text(device)} as worker." + f"{ULTRASINGER_HEAD} Separating vocals from audio with {blue_highlighted('demucs')} and {red_highlighted(device)} as worker." ) # Model selection? # -n mdx_q diff --git a/modules/Audio/vocal_chunks.py b/modules/Audio/vocal_chunks.py index e24fbc0..2fcdfc2 100644 --- a/modules/Audio/vocal_chunks.py +++ b/modules/Audio/vocal_chunks.py @@ -8,7 +8,7 @@ import wave import librosa from pydub import AudioSegment -from modules.Log import PRINT_ULTRASTAR +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, @@ -18,7 +18,7 @@ from modules.Ultrastar.ultrastar_converter import ( def convert_audio_to_mono_wav(input_file, output_file): """Convert audio to mono wav""" - print(f"{PRINT_ULTRASTAR} Converting audio for AI") + print(f"{ULTRASINGER_HEAD} Converting audio for AI") if ".mp3" in input_file: sound = AudioSegment.from_mp3(input_file) @@ -47,7 +47,7 @@ def export_chunks_from_transcribed_data( ): """Export transcribed_data as vocal chunks wav files""" print( - f"{PRINT_ULTRASTAR} Export transcribed data as vocal chunks wav files" + f"{ULTRASINGER_HEAD} Export transcribed data as vocal chunks wav files" ) wave_file = wave.open(audio_filename, "rb") @@ -69,7 +69,7 @@ def remove_silence_from_transcribtion_data(audio_path, transcribed_data): """Docstring""" print( - f"{PRINT_ULTRASTAR} Removing silent start and ending, from transcription data" + f"{ULTRASINGER_HEAD} Removing silent start and ending, from transcription data" ) audio, sample_rate = librosa.load(audio_path, sr=None) @@ -117,7 +117,7 @@ def export_chunks_from_ultrastar_data( audio_filename, ultrastar_data, folder_name ): """Export ultrastar data as vocal chunks wav files""" - print(f"{PRINT_ULTRASTAR} Export Ultrastar data as vocal chunks wav files") + print(f"{ULTRASINGER_HEAD} Export Ultrastar data as vocal chunks wav files") create_folder(folder_name) @@ -164,7 +164,7 @@ def get_chunk(end_byte, start_byte, wave_file): def export_transcribed_data_to_csv(transcribed_data, filename): """Export transcribed data to csv""" - print(f"{PRINT_ULTRASTAR} Exporting transcribed data to CSV") + print(f"{ULTRASINGER_HEAD} Exporting transcribed data to CSV") with open(filename, "w", encoding="utf-8", newline="") as csvfile: writer = csv.writer(csvfile) diff --git a/modules/Audio/youtube.py b/modules/Audio/youtube.py index ffdde66..3e70e71 100644 --- a/modules/Audio/youtube.py +++ b/modules/Audio/youtube.py @@ -6,7 +6,7 @@ import os import yt_dlp from PIL import Image -from modules.Log import PRINT_ULTRASTAR +from modules.console_colors import ULTRASINGER_HEAD def get_youtube_title(url): @@ -24,7 +24,7 @@ def get_youtube_title(url): def download_youtube_audio(url, clear_filename, output_path): """Docstring""" - print(f"{PRINT_ULTRASTAR} Downloading Audio") + print(f"{ULTRASINGER_HEAD} Downloading Audio") ydl_opts = { "format": "bestaudio/best", "outtmpl": output_path + "/" + clear_filename, @@ -39,7 +39,7 @@ def download_youtube_audio(url, clear_filename, output_path): def download_youtube_thumbnail(url, clear_filename, output_path): """Docstring""" - print(f"{PRINT_ULTRASTAR} Downloading thumbnail") + print(f"{ULTRASINGER_HEAD} Downloading thumbnail") ydl_opts = { "skip_download": True, "writethumbnail": True, @@ -66,7 +66,7 @@ def download_and_convert_thumbnail(ydl_opts, url, clear_filename, output_path): def download_youtube_video(url, clear_filename, output_path): """Docstring""" - print(f"{PRINT_ULTRASTAR} Downloading Video") + print(f"{ULTRASINGER_HEAD} Downloading Video") ydl_opts = { "format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4", "outtmpl": output_path + "/" + clear_filename + ".mp4", diff --git a/modules/DeviceDetection/device_detection.py b/modules/DeviceDetection/device_detection.py index cb8e43c..6ba0cec 100644 --- a/modules/DeviceDetection/device_detection.py +++ b/modules/DeviceDetection/device_detection.py @@ -2,7 +2,7 @@ import tensorflow as tf -from modules.Log import PRINT_ULTRASTAR, print_red_highlighted_text +from modules.console_colors import ULTRASINGER_HEAD, red_highlighted def get_available_device(): @@ -11,16 +11,16 @@ def get_available_device(): if not gpus: print( - f"{PRINT_ULTRASTAR} There are no GPUs available. Using {print_red_highlighted_text('cpu')}." + f"{ULTRASINGER_HEAD} There are no GPUs available. Using {red_highlighted('cpu')}." ) return "cpu" - print(f"{PRINT_ULTRASTAR} Found available GPUs:") + print(f"{ULTRASINGER_HEAD} Found available GPUs:") for gpu in gpus: print(f"Name: {gpu.name}, Type: {gpu.device_type}") # Todo: Finish this print( - f"{PRINT_ULTRASTAR} GPU usage are currently in development. Using {print_red_highlighted_text('cpu')}" + f"{ULTRASINGER_HEAD} GPU usage are currently in development. Using {red_highlighted('cpu')}" ) return "cpu" diff --git a/modules/Log.py b/modules/Log.py deleted file mode 100644 index 8f0dd27..0000000 --- a/modules/Log.py +++ /dev/null @@ -1,45 +0,0 @@ -"""Docstring""" - -PRINT_ULTRASTAR = "\033[92m[UltraSinger]\033[0m" - - -def print_blue_highlighted_text(text): - """Docstring""" - return Bcolors.BLUE + text + Bcolors.ENDC - - -def print_gold_highlighted_text(text): - """Docstring""" - return Bcolors.Gold + text + Bcolors.ENDC - - -def print_light_blue_highlighted_text(text): - """Docstring""" - return Bcolors.LightBlue + text + Bcolors.ENDC - - -def print_underlined_text(text): - """Docstring""" - return Bcolors.Underline + text + Bcolors.ENDC - - -def print_red_highlighted_text(text): - """Docstring""" - return Bcolors.RED + text + Bcolors.ENDC - - -def print_cyan_highlighted_text(text): - """Docstring""" - return Bcolors.Cyan + text + Bcolors.ENDC - - -class Bcolors: - """Docstring""" - - BLUE = "\033[94m" - RED = "\033[91m" - LightBlue = "\033[96m" - Cyan = "\033[36m" - Gold = "\033[93m" - Underline = "\033[4m" - ENDC = "\033[0m" diff --git a/modules/Midi/midi_creator.py b/modules/Midi/midi_creator.py index 1b3806d..0141148 100644 --- a/modules/Midi/midi_creator.py +++ b/modules/Midi/midi_creator.py @@ -13,16 +13,16 @@ from modules.Ultrastar.ultrastar_converter import ( get_start_time_from_ultrastar, ultrastar_note_to_midi_note, ) -from modules.Log import ( - PRINT_ULTRASTAR, - print_red_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + red_highlighted, ) def convert_ultrastar_to_midi_instrument(ultrastar_class): """Docstring""" - print(f"{PRINT_ULTRASTAR} Creating midi instrument from Ultrastar txt") + print(f"{ULTRASINGER_HEAD} Creating midi instrument from Ultrastar txt") instrument = pretty_midi.Instrument(program=0) velocity = 100 @@ -42,7 +42,7 @@ def convert_ultrastar_to_midi_instrument(ultrastar_class): def instruments_to_midi(instruments, bpm, midi_output): """Docstring""" - print(f"{PRINT_ULTRASTAR} Creating midi file -> {midi_output}") + print(f"{ULTRASINGER_HEAD} Creating midi file -> {midi_output}") midi_data = pretty_midi.PrettyMIDI(initial_tempo=bpm) for instrument in instruments: @@ -81,7 +81,7 @@ def find_nearest_index(array, value): def create_midi_notes_from_pitched_data(start_times, end_times, pitched_data): """Docstring""" - print(f"{PRINT_ULTRASTAR} Creating midi notes from pitched data") + print(f"{ULTRASINGER_HEAD} Creating midi notes from pitched data") midi_notes = [] @@ -108,7 +108,7 @@ def create_midi_note_from_pitched_data(start_time, end_time, pitched_data): # fixme: why is end smaller as start? if end < start: - print(f"{PRINT_ULTRASTAR} {print_red_highlighted_text('Error: end time is smaller as start time. The note is C0 now ...')}") + print(f"{ULTRASINGER_HEAD} {red_highlighted('Error: end time is smaller as start time. The note is C0 now ...')}") return "C0" if start == end: diff --git a/modules/Pitcher/pitcher.py b/modules/Pitcher/pitcher.py index e58e29a..39d2837 100644 --- a/modules/Pitcher/pitcher.py +++ b/modules/Pitcher/pitcher.py @@ -5,10 +5,10 @@ import csv import crepe from scipy.io import wavfile -from modules.Log import ( - PRINT_ULTRASTAR, - print_blue_highlighted_text, - print_red_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + blue_highlighted, + red_highlighted, ) from modules.Pitcher.pitched_data import PitchedData @@ -16,7 +16,7 @@ from modules.Pitcher.pitched_data import PitchedData def get_pitch_with_crepe_file(filename, step_size, model_capacity): """Docstring""" print( - f"{PRINT_ULTRASTAR} Pitching with {print_blue_highlighted_text('crepe')} and model {print_blue_highlighted_text(model_capacity)} and {print_red_highlighted_text('cpu')} as worker" + f"{ULTRASINGER_HEAD} Pitching with {blue_highlighted('crepe')} and model {blue_highlighted(model_capacity)} and {red_highlighted('cpu')} as worker" ) # Todo: add GPU support by using torchcrepe sample_rate, audio = wavfile.read(filename) diff --git a/modules/Speech_Recognition/Vosk.py b/modules/Speech_Recognition/Vosk.py index fdd465a..0f1c0b7 100644 --- a/modules/Speech_Recognition/Vosk.py +++ b/modules/Speech_Recognition/Vosk.py @@ -5,7 +5,7 @@ import wave from vosk import KaldiRecognizer, Model -from modules.Log import PRINT_ULTRASTAR +from modules.console_colors import ULTRASINGER_HEAD from modules.Speech_Recognition.TranscribedData import TranscribedData # todo: Rename to Transcoder? @@ -15,7 +15,7 @@ def transcribe_with_vosk(audio_filename, model_path): """Docstring""" # Code from here: https://towardsdatascience.com/speech-recognition-with-timestamps-934ede4234b2 print( - f"{PRINT_ULTRASTAR} Transcribing {audio_filename} with vosk and model {model_path}" + f"{ULTRASINGER_HEAD} Transcribing {audio_filename} with vosk and model {model_path}" ) model = Model(model_path) diff --git a/modules/Speech_Recognition/Whisper.py b/modules/Speech_Recognition/Whisper.py index 11cd68e..09d1e0e 100644 --- a/modules/Speech_Recognition/Whisper.py +++ b/modules/Speech_Recognition/Whisper.py @@ -2,10 +2,10 @@ import whisperx -from modules.Log import ( - PRINT_ULTRASTAR, - print_blue_highlighted_text, - print_red_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + blue_highlighted, + red_highlighted, ) from modules.Speech_Recognition.TranscribedData import TranscribedData @@ -14,7 +14,7 @@ def transcribe_with_whisper(audio_path, model, device="cpu"): """Docstring""" print( - f"{PRINT_ULTRASTAR} Loading {print_blue_highlighted_text('whisper')} with model {print_blue_highlighted_text(model)} and {print_red_highlighted_text(device)} as worker" + f"{ULTRASINGER_HEAD} Loading {blue_highlighted('whisper')} with model {blue_highlighted(model)} and {red_highlighted(device)} as worker" ) batch_size = 16 # reduce if low on GPU mem @@ -28,7 +28,7 @@ def transcribe_with_whisper(audio_path, model, device="cpu"): ) audio = whisperx.load_audio(audio_path) - print(f"{PRINT_ULTRASTAR} Transcribing {audio_path}") + print(f"{ULTRASINGER_HEAD} Transcribing {audio_path}") result = loaded_whisper_model.transcribe(audio, batch_size=batch_size) language = result["language"] @@ -54,7 +54,7 @@ def transcribe_with_whisper(audio_path, model, device="cpu"): for obj in segment["words"]: if len(obj) < 4: print( - f"{print_red_highlighted_text('Error: Skipping Word {}, because of missing timings'.format(obj['word']))}" + f"{red_highlighted('Error: Skipping Word {}, because of missing timings'.format(obj['word']))}" ) continue vtd = TranscribedData(obj) # create custom Word object diff --git a/modules/Speech_Recognition/hyphenation.py b/modules/Speech_Recognition/hyphenation.py index 85cef3c..89f7009 100644 --- a/modules/Speech_Recognition/hyphenation.py +++ b/modules/Speech_Recognition/hyphenation.py @@ -4,9 +4,9 @@ import string from hyphen import Hyphenator, dictools -from modules.Log import ( - PRINT_ULTRASTAR, - print_blue_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + blue_highlighted, ) @@ -25,7 +25,7 @@ def language_check(language="en"): return None print( - f"{PRINT_ULTRASTAR} Hyphenate using language code: {print_blue_highlighted_text(lang_region)}" + f"{ULTRASINGER_HEAD} Hyphenate using language code: {blue_highlighted(lang_region)}" ) return lang_region diff --git a/modules/Speech_Recognition/speech_recognition.py b/modules/Speech_Recognition/speech_recognition.py index 88c5de7..08d829d 100644 --- a/modules/Speech_Recognition/speech_recognition.py +++ b/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 modules.Log import PRINT_ULTRASTAR +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 @@ -96,9 +96,9 @@ def transcribe_audio(audio_file): end_time, ) except sr.UnknownValueError: - print(f"{PRINT_ULTRASTAR} Could not understand audio") + print(f"{ULTRASINGER_HEAD} Could not understand audio") except sr.RequestError as error: - print(f"{PRINT_ULTRASTAR} Error with recognizing service; {error}") + print(f"{ULTRASINGER_HEAD} Error with recognizing service; {error}") class SpeechToText: diff --git a/modules/Ultrastar/ultrastar_parser.py b/modules/Ultrastar/ultrastar_parser.py index b51d7d9..e68ab30 100644 --- a/modules/Ultrastar/ultrastar_parser.py +++ b/modules/Ultrastar/ultrastar_parser.py @@ -1,6 +1,6 @@ """Docstring""" -from modules.Log import PRINT_ULTRASTAR +from modules.console_colors import ULTRASINGER_HEAD from modules.Ultrastar.ultrastar_converter import ( get_end_time_from_ultrastar, get_start_time_from_ultrastar, @@ -10,7 +10,7 @@ from modules.Ultrastar.ultrastar_txt import UltrastarTxt def parse_ultrastar_txt(input_file): """Docstring""" - print(f"{PRINT_ULTRASTAR} Parse ultrastar txt -> {input_file}") + print(f"{ULTRASINGER_HEAD} Parse ultrastar txt -> {input_file}") with open(input_file, "r", encoding="utf-8") as file: txt = file.readlines() diff --git a/modules/Ultrastar/ultrastar_score_calculator.py b/modules/Ultrastar/ultrastar_score_calculator.py index e4d4cb5..365087e 100644 --- a/modules/Ultrastar/ultrastar_score_calculator.py +++ b/modules/Ultrastar/ultrastar_score_calculator.py @@ -2,12 +2,12 @@ import librosa -from modules.Log import ( - PRINT_ULTRASTAR, - print_blue_highlighted_text, - print_cyan_highlighted_text, - print_gold_highlighted_text, - print_light_blue_highlighted_text, +from modules.console_colors import ( + ULTRASINGER_HEAD, + blue_highlighted, + cyan_highlighted, + gold_highlighted, + light_blue_highlighted, print_underlined_text, ) from modules.Midi.midi_creator import create_midi_note_from_pitched_data @@ -78,14 +78,14 @@ def print_score(score): """Docstring""" print( - f"{PRINT_ULTRASTAR} Total: {print_cyan_highlighted_text(str(score.score))}, notes: {print_blue_highlighted_text(str(score.notes))}, line bonus: {print_light_blue_highlighted_text(str(score.line_bonus))}, golden notes: {print_gold_highlighted_text(str(score.golden))}" + f"{ULTRASINGER_HEAD} Total: {cyan_highlighted(str(score.score))}, notes: {blue_highlighted(str(score.notes))}, line bonus: {light_blue_highlighted(str(score.line_bonus))}, golden notes: {gold_highlighted(str(score.golden))}" ) def calculate_score(pitched_data, ultrastar_class): """Docstring""" - print(PRINT_ULTRASTAR + " Calculating Ultrastar Points") + print(ULTRASINGER_HEAD + " Calculating Ultrastar Points") simple_points = Points() accurate_points = Points() @@ -154,11 +154,11 @@ def print_score_calculation(simple_points, accurate_points): """Docstring""" print( - f"{PRINT_ULTRASTAR} {print_underlined_text('Simple (octave high ignored)')} points" + f"{ULTRASINGER_HEAD} {print_underlined_text('Simple (octave high ignored)')} points" ) print_score(simple_points) print( - f"{PRINT_ULTRASTAR} {print_underlined_text('Accurate (octave high matches)')} points:" + f"{ULTRASINGER_HEAD} {print_underlined_text('Accurate (octave high matches)')} points:" ) print_score(accurate_points) diff --git a/modules/Ultrastar/ultrastar_writer.py b/modules/Ultrastar/ultrastar_writer.py index dd7f52e..8304ae4 100644 --- a/modules/Ultrastar/ultrastar_writer.py +++ b/modules/Ultrastar/ultrastar_writer.py @@ -4,7 +4,7 @@ import re import langcodes -from modules.Log import PRINT_ULTRASTAR +from modules.console_colors import ULTRASINGER_HEAD from modules.Ultrastar.ultrastar_converter import ( real_bpm_to_ultrastar_bpm, second_to_beat, @@ -41,7 +41,7 @@ def create_ultrastar_txt_from_automation( """Docstring""" print( - f"{PRINT_ULTRASTAR} Creating {ultrastar_file_output} from transcription." + f"{ULTRASINGER_HEAD} Creating {ultrastar_file_output} from transcription." ) real_bpm = real_bpm_to_ultrastar_bpm(bpm) diff --git a/modules/console_colors.py b/modules/console_colors.py new file mode 100644 index 0000000..c4cc6f8 --- /dev/null +++ b/modules/console_colors.py @@ -0,0 +1,45 @@ +"""Colors for the console""" + +ULTRASINGER_HEAD = "\033[92m[UltraSinger]\033[0m" + + +def blue_highlighted(text: str) -> str: + """Returns a blue highlighted text""" + return f"{Bcolors.blue}{text}{Bcolors.endc}" + + +def gold_highlighted(text: str) -> str: + """Returns a gold highlighted text""" + return f"{Bcolors.gold}{text}{Bcolors.endc}" + + +def light_blue_highlighted(text: str) -> str: + """Returns a light blue highlighted text""" + return f"{Bcolors.light_blue}{text}{Bcolors.endc}" + + +def underlined(text: str) -> str: + """Returns an underlined text""" + return f"{Bcolors.underline}{text}{Bcolors.endc}" + + +def red_highlighted(text: str) -> str: + """Returns a red highlighted text""" + return f"{Bcolors.red}{text}{Bcolors.endc}" + + +def cyan_highlighted(text: str) -> str: + """Returns a cyan highlighted text""" + return f"{Bcolors.cyan}{text}{Bcolors.endc}" + + +class Bcolors: + """Colors for the console""" + + blue = "\033[94m" + red = "\033[91m" + light_blue = "\033[96m" + cyan = "\033[36m" + gold = "\033[93m" + underline = "\033[4m" + endc = "\033[0m" diff --git a/modules/os_helper.py b/modules/os_helper.py index 0a2870e..8f2f9bd 100644 --- a/modules/os_helper.py +++ b/modules/os_helper.py @@ -3,12 +3,12 @@ import os import shutil -from modules.Log import PRINT_ULTRASTAR +from modules.console_colors import ULTRASINGER_HEAD def create_folder(folder_name): """Docstring""" - print(f"{PRINT_ULTRASTAR} Creating output folder. -> {folder_name}") + print(f"{ULTRASINGER_HEAD} Creating output folder. -> {folder_name}") is_exist = os.path.exists(folder_name) if not is_exist: os.makedirs(folder_name) From 5f6705559fe9a6db08f09c3c3bfe8c3be51310b2 Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 8 Jun 2023 01:24:06 +0200 Subject: [PATCH 11/11] - refactoring --- UltraSinger.py | 7 +- modules/Audio/convert_audio.py | 28 +++++ modules/Audio/denoise.py | 7 +- modules/Audio/separation.py | 6 +- modules/Audio/silence_processing.py | 53 +++++++++ modules/Audio/vocal_chunks.py | 108 ++---------------- modules/Audio/youtube.py | 26 ++--- .../Ultrastar/ultrastar_score_calculator.py | 6 +- modules/Ultrastar/ultrastar_txt.py | 2 +- modules/csv_export.py | 24 ++++ 10 files changed, 140 insertions(+), 127 deletions(-) create mode 100644 modules/Audio/convert_audio.py create mode 100644 modules/Audio/silence_processing.py create mode 100644 modules/csv_export.py diff --git a/UltraSinger.py b/UltraSinger.py index 07e3b3a..f4ac493 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -15,13 +15,12 @@ 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 ( - convert_audio_to_mono_wav, - convert_wav_to_mp3, export_chunks_from_transcribed_data, export_chunks_from_ultrastar_data, - export_transcribed_data_to_csv, - remove_silence_from_transcribtion_data, ) +from modules.Audio.silence_processing import remove_silence_from_transcribtion_data +from modules.csv_export 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, diff --git a/modules/Audio/convert_audio.py b/modules/Audio/convert_audio.py new file mode 100644 index 0000000..6642706 --- /dev/null +++ b/modules/Audio/convert_audio.py @@ -0,0 +1,28 @@ +"""Convert audio to other formats""" + +from pydub import AudioSegment + +from modules.console_colors import ULTRASINGER_HEAD + + +def convert_audio_to_mono_wav(input_file: str, output_file: str) -> None: + """Convert audio to mono wav""" + print(f"{ULTRASINGER_HEAD} Converting audio for AI") + + if ".mp3" in input_file: + sound = AudioSegment.from_mp3(input_file) + elif ".wav" in input_file: + sound = AudioSegment.from_wav(input_file) + else: + raise ValueError("data format not supported") + + sound = sound.set_channels(1) + sound.export(output_file, format="wav") + + +def convert_wav_to_mp3(input_file: str, output_file: str) -> None: + """Convert wav to mp3""" + print(f"{ULTRASINGER_HEAD} Converting wav to mp3") + + sound = AudioSegment.from_wav(input_file) + sound.export(output_file, format="mp3") diff --git a/modules/Audio/denoise.py b/modules/Audio/denoise.py index 42b5e9d..5987739 100644 --- a/modules/Audio/denoise.py +++ b/modules/Audio/denoise.py @@ -1,12 +1,13 @@ -"""Docstring""" +"""Reduce noise from audio""" import ffmpeg from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted -def ffmpeg_reduce_noise(input_file_path, output_file): - """Docstring""" +def ffmpeg_reduce_noise(input_file_path: str, output_file: str) -> None: + """Reduce noise from vocal audio with ffmpeg.""" + # Denoise audio samples with FFT. # A description of the accepted parameters follows. diff --git a/modules/Audio/separation.py b/modules/Audio/separation.py index 7a767f4..4797ad1 100644 --- a/modules/Audio/separation.py +++ b/modules/Audio/separation.py @@ -1,4 +1,4 @@ -"""Docstring""" +"""Separate vocals from audio""" import subprocess @@ -10,8 +10,8 @@ from modules.console_colors import ( from modules.os_helper import current_executor_path, move, path_join -def separate_audio(input_file_path, output_file, device="cpu"): - """Docstring""" +def separate_audio(input_file_path: str, output_file: str, device="cpu") -> None: + """Separate vocals from audio with demucs.""" print( f"{ULTRASINGER_HEAD} Separating vocals from audio with {blue_highlighted('demucs')} and {red_highlighted(device)} as worker." diff --git a/modules/Audio/silence_processing.py b/modules/Audio/silence_processing.py new file mode 100644 index 0000000..c140555 --- /dev/null +++ b/modules/Audio/silence_processing.py @@ -0,0 +1,53 @@ +"""Silence processing module""" + +import librosa + +from modules.console_colors import ULTRASINGER_HEAD + + +def remove_silence_from_transcribtion_data(audio_path: str, transcribed_data: []) -> []: + """Remove silence from given transcribtion data""" + + print( + f"{ULTRASINGER_HEAD} Removing silent start and ending, from transcription data" + ) + + audio, sample_rate = librosa.load(audio_path, sr=None) + + for i, data in enumerate(transcribed_data): + start_time = data.start + end_time = data.end + start_sample = int(start_time * sample_rate) + end_sample = int(end_time * sample_rate) + chunk = audio[start_sample:end_sample] + + # todo: why 5 works good? It should be 40db ?!? + # max_dB = librosa.amplitude_to_db(chunk, ref=np.max) + silence_threshold = 5 + onsets = librosa.effects.split( + chunk, top_db=silence_threshold, frame_length=2048, hop_length=100 + ) + + # Get the duration of the first and last silent intervals + if len(onsets) > 0: + first_silence = onsets[0][0] + last_silence = len(chunk) - onsets[-1][1] + + first_silence_duration = librosa.samples_to_time( + first_silence, sr=sample_rate + ) + last_silence_duration = librosa.samples_to_time( + last_silence, sr=sample_rate + ) + else: + first_silence_duration = 0 + last_silence_duration = 0 + + data.start = ( + data.start + first_silence_duration + ) + data.end = ( + data.end - last_silence_duration + ) + + return transcribed_data diff --git a/modules/Audio/vocal_chunks.py b/modules/Audio/vocal_chunks.py index 2fcdfc2..72d6f04 100644 --- a/modules/Audio/vocal_chunks.py +++ b/modules/Audio/vocal_chunks.py @@ -1,41 +1,16 @@ -"""Docstring""" +"""Vocal chunks module.""" -import csv import os import re import wave -import librosa -from pydub import AudioSegment - 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, ) - - -def convert_audio_to_mono_wav(input_file, output_file): - """Convert audio to mono wav""" - print(f"{ULTRASINGER_HEAD} Converting audio for AI") - - if ".mp3" in input_file: - sound = AudioSegment.from_mp3(input_file) - elif ".wav" in input_file: - sound = AudioSegment.from_wav(input_file) - else: - raise ValueError("data format not supported") - - sound = sound.set_channels(1) - sound.export(output_file, format="wav") - - -def convert_wav_to_mp3(input_file, output_file): - """Docstring""" - - sound = AudioSegment.from_wav(input_file) - sound.export(output_file, format="mp3") +from modules.Ultrastar.ultrastar_txt import UltrastarTxt class AudioManipulation: @@ -43,8 +18,8 @@ class AudioManipulation: def export_chunks_from_transcribed_data( - audio_filename, transcribed_data, output_folder_name -): + audio_filename: str, transcribed_data: [], output_folder_name: str +) -> None: """Export transcribed_data as vocal chunks wav files""" print( f"{ULTRASINGER_HEAD} Export transcribed data as vocal chunks wav files" @@ -65,57 +40,9 @@ def export_chunks_from_transcribed_data( wave_file.close() -def remove_silence_from_transcribtion_data(audio_path, transcribed_data): - """Docstring""" - - print( - f"{ULTRASINGER_HEAD} Removing silent start and ending, from transcription data" - ) - - audio, sample_rate = librosa.load(audio_path, sr=None) - - for i, data in enumerate(transcribed_data): - start_time = data.start - end_time = data.end - start_sample = int(start_time * sample_rate) - end_sample = int(end_time * sample_rate) - chunk = audio[start_sample:end_sample] - - # todo: why 5 works good? It should be 40db ?!? - # max_dB = librosa.amplitude_to_db(chunk, ref=np.max) - silence_threshold = 5 - onsets = librosa.effects.split( - chunk, top_db=silence_threshold, frame_length=2048, hop_length=100 - ) - - # Get the duration of the first and last silent intervals - if len(onsets) > 0: - first_silence = onsets[0][0] - last_silence = len(chunk) - onsets[-1][1] - - first_silence_duration = librosa.samples_to_time( - first_silence, sr=sample_rate - ) - last_silence_duration = librosa.samples_to_time( - last_silence, sr=sample_rate - ) - else: - first_silence_duration = 0 - last_silence_duration = 0 - - data.start = ( - data.start + first_silence_duration - ) - data.end = ( - data.end - last_silence_duration - ) - - return transcribed_data - - def export_chunks_from_ultrastar_data( - audio_filename, ultrastar_data, folder_name -): + audio_filename: str, ultrastar_data: UltrastarTxt, folder_name: str +) -> None: """Export ultrastar data as vocal chunks wav files""" print(f"{ULTRASINGER_HEAD} Export Ultrastar data as vocal chunks wav files") @@ -137,7 +64,7 @@ def export_chunks_from_ultrastar_data( ) -def export_chunk_to_wav_file(chunk, folder_name, i, word, wave_file): +def export_chunk_to_wav_file(chunk, folder_name: str, i: int, word: str, wave_file) -> None: """Export vocal chunks to wav file""" clean_word = re.sub("[^A-Za-z0-9]+", "", word) @@ -150,7 +77,7 @@ def export_chunk_to_wav_file(chunk, folder_name, i, word, wave_file): chunk_file.writeframes(chunk) -def get_chunk(end_byte, start_byte, wave_file): +def get_chunk(end_byte: int, start_byte: int, wave_file): """ Gets the chunk from wave file. Returns chunk as n frames of audio, as a bytes object. @@ -160,22 +87,3 @@ def get_chunk(end_byte, start_byte, wave_file): wave_file.setpos(start_byte) # ({:.2f}) chunk = wave_file.readframes(end_byte - start_byte) return chunk - - -def export_transcribed_data_to_csv(transcribed_data, filename): - """Export transcribed data to csv""" - print(f"{ULTRASINGER_HEAD} Exporting transcribed data to CSV") - - with open(filename, "w", encoding="utf-8", newline="") as csvfile: - writer = csv.writer(csvfile) - header = ["word", "start", "end", "confidence"] - writer.writerow(header) - for i, data in enumerate(transcribed_data): - writer.writerow( - [ - data.word, - data.start, - data.end, - data.conf, - ] - ) diff --git a/modules/Audio/youtube.py b/modules/Audio/youtube.py index 3e70e71..6ea553e 100644 --- a/modules/Audio/youtube.py +++ b/modules/Audio/youtube.py @@ -1,4 +1,4 @@ -"""Docstring""" +"""YouTube Downloader""" import io import os @@ -9,8 +9,8 @@ from PIL import Image from modules.console_colors import ULTRASINGER_HEAD -def get_youtube_title(url): - """Docstring""" +def get_youtube_title(url: str) -> str: + """Get the title of the YouTube video""" ydl_opts = {} with yt_dlp.YoutubeDL(ydl_opts) as ydl: @@ -21,8 +21,8 @@ def get_youtube_title(url): return result["title"] -def download_youtube_audio(url, clear_filename, output_path): - """Docstring""" +def download_youtube_audio(url: str, clear_filename: str, output_path: str): + """Download audio from YouTube""" print(f"{ULTRASINGER_HEAD} Downloading Audio") ydl_opts = { @@ -36,8 +36,8 @@ def download_youtube_audio(url, clear_filename, output_path): start_download(ydl_opts, url) -def download_youtube_thumbnail(url, clear_filename, output_path): - """Docstring""" +def download_youtube_thumbnail(url: str, clear_filename: str, output_path: str): + """Download thumbnail from YouTube""" print(f"{ULTRASINGER_HEAD} Downloading thumbnail") ydl_opts = { @@ -48,8 +48,8 @@ def download_youtube_thumbnail(url, clear_filename, output_path): download_and_convert_thumbnail(ydl_opts, url, clear_filename, output_path) -def download_and_convert_thumbnail(ydl_opts, url, clear_filename, output_path): - """Docstring""" +def download_and_convert_thumbnail(ydl_opts, url: str, clear_filename: str, output_path: str) -> None: + """Download and convert thumbnail from YouTube""" with yt_dlp.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(url, download=False) @@ -63,8 +63,8 @@ def download_and_convert_thumbnail(ydl_opts, url, clear_filename, output_path): ) -def download_youtube_video(url, clear_filename, output_path): - """Docstring""" +def download_youtube_video(url: str, clear_filename: str, output_path: str) -> None: + """Download video from YouTube""" print(f"{ULTRASINGER_HEAD} Downloading Video") ydl_opts = { @@ -74,8 +74,8 @@ def download_youtube_video(url, clear_filename, output_path): start_download(ydl_opts, url) -def start_download(ydl_opts, url): - """Docstring""" +def start_download(ydl_opts, url: str) -> None: + """Start the download the ydl_opts""" with yt_dlp.YoutubeDL(ydl_opts) as ydl: errors = ydl.download(url) diff --git a/modules/Ultrastar/ultrastar_score_calculator.py b/modules/Ultrastar/ultrastar_score_calculator.py index 365087e..20fe33b 100644 --- a/modules/Ultrastar/ultrastar_score_calculator.py +++ b/modules/Ultrastar/ultrastar_score_calculator.py @@ -8,7 +8,7 @@ from modules.console_colors import ( cyan_highlighted, gold_highlighted, light_blue_highlighted, - print_underlined_text, + underlined, ) from modules.Midi.midi_creator import create_midi_note_from_pitched_data from modules.Ultrastar.ultrastar_converter import ( @@ -154,11 +154,11 @@ def print_score_calculation(simple_points, accurate_points): """Docstring""" print( - f"{ULTRASINGER_HEAD} {print_underlined_text('Simple (octave high ignored)')} points" + f"{ULTRASINGER_HEAD} {underlined('Simple (octave high ignored)')} points" ) print_score(simple_points) print( - f"{ULTRASINGER_HEAD} {print_underlined_text('Accurate (octave high matches)')} points:" + f"{ULTRASINGER_HEAD} {underlined('Accurate (octave high matches)')} points:" ) print_score(accurate_points) diff --git a/modules/Ultrastar/ultrastar_txt.py b/modules/Ultrastar/ultrastar_txt.py index 2121059..7425378 100644 --- a/modules/Ultrastar/ultrastar_txt.py +++ b/modules/Ultrastar/ultrastar_txt.py @@ -11,7 +11,7 @@ class UltrastarTxt: bpm = "" cover = None creator = "UltraSinger [GitHub]" - fixer = "YOUR NAME" + fixer = "YOUR NAME" # Only for UltraStar World Party comment = "UltraSinger [GitHub]" startBeat = [] startTimes = [] diff --git a/modules/csv_export.py b/modules/csv_export.py new file mode 100644 index 0000000..680507e --- /dev/null +++ b/modules/csv_export.py @@ -0,0 +1,24 @@ +"""CSV export module""" + +import csv + +from modules.console_colors import ULTRASINGER_HEAD + + +def export_transcribed_data_to_csv(transcribed_data: [], filename: str) -> None: + """Export transcribed data to csv""" + print(f"{ULTRASINGER_HEAD} Exporting transcribed data to CSV") + + with open(filename, "w", encoding="utf-8", newline="") as csvfile: + writer = csv.writer(csvfile) + header = ["word", "start", "end", "confidence"] + writer.writerow(header) + for i, data in enumerate(transcribed_data): + writer.writerow( + [ + data.word, + data.start, + data.end, + data.conf, + ] + )