From c3fc70d9f8254409fbb14ecbe2046dc29ec61d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Bo=CC=88hning?= <1497707+bohning@users.noreply.github.com> Date: Sun, 28 May 2023 13:18:30 +0200 Subject: [PATCH] Use f-strings instead of .format() strings. --- UltraSinger.py | 27 +++++++++---------- moduls/Audio/denoise.py | 2 +- moduls/Audio/separation.py | 2 +- moduls/Audio/vocal_chunks.py | 12 ++++----- moduls/Audio/youtube.py | 4 +-- moduls/DeviceDetection/device_detection.py | 7 +++-- moduls/Midi/midi_creator.py | 6 ++--- moduls/Pitcher/pitcher.py | 5 +--- moduls/Speech_Recognition/Vosk.py | 2 +- moduls/Speech_Recognition/Whisper.py | 6 ++--- moduls/Speech_Recognition/hyphenation.py | 2 +- .../Speech_Recognition/speech_recognition.py | 4 +-- moduls/Ultrastar/ultrastar_parser.py | 2 +- .../Ultrastar/ultrastar_score_calculator.py | 8 +++--- moduls/Ultrastar/ultrastar_writer.py | 4 +-- moduls/os_helper.py | 2 +- 16 files changed, 42 insertions(+), 53 deletions(-) diff --git a/UltraSinger.py b/UltraSinger.py index 56fb43f..0cbe5dc 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -46,7 +46,7 @@ def get_confidence(pitched_data, threshold): def convert_ultrastar_note_numbers(midi_notes): - print(PRINT_ULTRASTAR + " Creating Ultrastar notes from midi data") + print(f"{PRINT_ULTRASTAR} Creating Ultrastar notes from midi data") ultrastar_note_numbers = [] for i in range(len(midi_notes)): @@ -59,7 +59,7 @@ def convert_ultrastar_note_numbers(midi_notes): def pitch_each_chunk_with_crepe(directory): - print("{} Pitching each chunk with {}.".format(PRINT_ULTRASTAR, print_blue_highlighted_text("crepe"))) + print(f"{PRINT_ULTRASTAR} Pitching each chunk with {print_blue_highlighted_text("crepe")}) midi_notes = [] for filename in sorted([f for f in os.listdir(directory) if f.endswith('.wav')], @@ -106,7 +106,7 @@ def get_bpm_from_data(data, sr): onset_env = librosa.onset.onset_strength(y=data, sr=sr) wav_tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sr) - print("{} BPM is {}".format(PRINT_ULTRASTAR, print_blue_highlighted_text(str(round(wav_tempo[0], 2))))) + print(f"{PRINT_ULTRASTAR} BPM is {print_blue_highlighted_text(str(round(wav_tempo[0], 2)))}") return wav_tempo[0] @@ -209,12 +209,9 @@ def hyphenate_each_word(language, transcribed_data): def print_support(): print() - print("{} {} {}{}".format(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("!"))) - print("{} See project page -> https://github.com/rakuri255/UltraSinger".format(PRINT_ULTRASTAR)) - print("{} {}".format(PRINT_ULTRASTAR, - print_gold_highlighted_text("This will help alot to keep this project alive and improved."))) + 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("!")}") + print(f"{PRINT_ULTRASTAR} 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.")}" def run(): @@ -223,13 +220,13 @@ def run(): real_bpm = None if not isAudio: # Parse Ultrastar txt - print("{} {}".format(PRINT_ULTRASTAR, print_gold_highlighted_text("re-pitch mode"))) + print(f"{PRINT_ULTRASTAR} {print_gold_highlighted_text("re-pitch mode")}" basename_without_ext, real_bpm, song_output, ultrastar_audio_input_path, ultrastar_class = parse_ultrastar_txt() elif settings.input_file_path.startswith('https:'): # Youtube - print("{} {}".format(PRINT_ULTRASTAR, print_gold_highlighted_text("full automatic mode"))) + print(f"{PRINT_ULTRASTAR} {print_gold_highlighted_text("full automatic mode")}") basename_without_ext, song_output, ultrastar_audio_input_path = download_from_youtube() else: # Audio File - print("{} {}".format(PRINT_ULTRASTAR, print_gold_highlighted_text("full automatic mode"))) + print(f"{PRINT_ULTRASTAR} {print_gold_highlighted_text("full automatic mode")}") basename_without_ext, song_output, ultrastar_audio_input_path = setup_audio_input_file() cache_path = os.path.join(song_output, 'cache') @@ -320,9 +317,9 @@ def calculate_score_points(isAudio, pitched_data, ultrastar_class, ultrastar_fil ultrastar_class = ultrastar_parser.parse_ultrastar_txt(ultrastar_file_output) ultrastar_score_calculator.print_score_calculation(pitched_data, ultrastar_class) else: - print("{} {}".format(PRINT_ULTRASTAR, print_blue_highlighted_text("Score of original Ultrastar txt"))) + print(f"{PRINT_ULTRASTAR} {print_blue_highlighted_text("Score of original Ultrastar txt")}") ultrastar_score_calculator.print_score_calculation(pitched_data, ultrastar_class) - print("{} {}".format(PRINT_ULTRASTAR, print_blue_highlighted_text("Score of re-pitched Ultrastar txt"))) + print(f"{PRINT_ULTRASTAR} {print_blue_highlighted_text("Score of re-pitched Ultrastar txt")}") ultrastar_class = ultrastar_parser.parse_ultrastar_txt(ultrastar_file_output) ultrastar_score_calculator.print_score_calculation(pitched_data, ultrastar_class) return ultrastar_class @@ -411,7 +408,7 @@ def parse_ultrastar_txt(): def create_midi_file(isAudio, real_bpm, song_output, ultrastar_class): - print("{} Creating Midi with {}".format(PRINT_ULTRASTAR, print_blue_highlighted_text("pretty_midi"))) + print(f"{PRINT_ULTRASTAR} Creating Midi with {print_blue_highlighted_text("pretty_midi")}") if isAudio: voice_instrument = [midi_creator.convert_ultrastar_to_midi_instrument(ultrastar_class)] midi_output = os.path.join(song_output, ultrastar_class.title + '.mid') diff --git a/moduls/Audio/denoise.py b/moduls/Audio/denoise.py index 7d015f2..f265c69 100644 --- a/moduls/Audio/denoise.py +++ b/moduls/Audio/denoise.py @@ -13,7 +13,7 @@ def ffmpeg_reduce_noise(input_file_path, output_file): # track_noise, tn # Enable noise floor tracking. By default is disabled. With this enabled, noise floor is automatically adjusted. - print("{} Reduce noise from vocal audio with {}.".format(PRINT_ULTRASTAR, print_blue_highlighted_text("ffmpeg"))) + print(f"{PRINT_ULTRASTAR} Reduce noise from vocal audio with {print_blue_highlighted_text("ffmpeg")}.") ( ffmpeg .input(input_file_path) diff --git a/moduls/Audio/separation.py b/moduls/Audio/separation.py index 20f4da3..9642613 100644 --- a/moduls/Audio/separation.py +++ b/moduls/Audio/separation.py @@ -4,7 +4,7 @@ from moduls.Log import PRINT_ULTRASTAR, print_blue_highlighted_text, print_red_h def separate_audio(input_file_path, output_file, device="cpu"): - print("{} Separating vocals from audio with {} and {} as worker.".format(PRINT_ULTRASTAR, print_blue_highlighted_text("demucs"), print_red_highlighted_text(device))) + print(f"{PRINT_ULTRASTAR} Separating vocals from audio with {print_blue_highlighted_text("demucs")} and {print_red_highlighted_text(device)} as worker.") # Model selection? # -n mdx_q # -n htdemucs_ft diff --git a/moduls/Audio/vocal_chunks.py b/moduls/Audio/vocal_chunks.py index 9d82997..56406ba 100644 --- a/moduls/Audio/vocal_chunks.py +++ b/moduls/Audio/vocal_chunks.py @@ -13,7 +13,7 @@ from moduls.Log import PRINT_ULTRASTAR def convert_audio_to_mono_wav(input_file, output_file): """Convert audio to mono wav""" - print(PRINT_ULTRASTAR + " Converting audio for AI") + print(f"{PRINT_ULTRASTAR} Converting audio for AI") if '.mp3' in input_file: sound = AudioSegment.from_mp3(input_file) @@ -37,7 +37,7 @@ class AudioManipulation: def export_chunks_from_transcribed_data(audio_filename, transcribed_data, output_folder_name): """Export transcribed_data as vocal chunks wav files""" - print("{} Export transcribed data as vocal chunks wav files".format(PRINT_ULTRASTAR)) + print(f"{PRINT_ULTRASTAR} Export transcribed data as vocal chunks wav files") wf = wave.open(audio_filename, "rb") sr, n_channels = wf.getparams()[2], wf.getparams()[0] @@ -53,7 +53,7 @@ def export_chunks_from_transcribed_data(audio_filename, transcribed_data, output def remove_silence_from_transcribtion_data(audio_path, transcribed_data): - print(PRINT_ULTRASTAR + " Removing silent start and ending, from transcription data") + print(f"{PRINT_ULTRASTAR} Removing silent start and ending, from transcription data") y, sr = librosa.load(audio_path, sr=None) @@ -88,7 +88,7 @@ def remove_silence_from_transcribtion_data(audio_path, transcribed_data): def export_chunks_from_ultrastar_data(audio_filename, ultrastar_data, folder_name): """Export ultrastar data as vocal chunks wav files""" - print(PRINT_ULTRASTAR + " Export Ultrastar data as vocal chunks wav files") + print(f"{PRINT_ULTRASTAR} Export Ultrastar data as vocal chunks wav files") create_folder(folder_name) @@ -112,7 +112,7 @@ def export_chunk_to_wav_file(chunk, folder_name, i, word, wf): clean_word = re.sub('[^A-Za-z0-9]+', '', word) # todo: Progress? # print(str(i) + ' ' + clean_word) - with wave.open(os.path.join(folder_name, "chunk_{}_{}.wav".format(i, clean_word)), + with wave.open(os.path.join(folder_name, f"chunk_{i}_{clean_word}.wav"), "wb") as chunk_file: chunk_file.setparams(wf.getparams()) chunk_file.writeframes(chunk) @@ -132,7 +132,7 @@ def get_chunk(end_byte, start_byte, wf): def export_transcribed_data_to_csv(transcribed_data, filename): """Export transcribed data to csv""" - print(PRINT_ULTRASTAR + " Exporting transcribed data to CSV") + print(f"{PRINT_ULTRASTAR} Exporting transcribed data to CSV") with open(filename, 'w', newline='') as csvfile: writer = csv.writer(csvfile) diff --git a/moduls/Audio/youtube.py b/moduls/Audio/youtube.py index 450bfcf..d48c933 100644 --- a/moduls/Audio/youtube.py +++ b/moduls/Audio/youtube.py @@ -13,7 +13,7 @@ def get_youtube_title(url): def download_youtube_audio(url, clear_filename, output_path): - print(PRINT_ULTRASTAR + " Downloading Audio") + print(f"{PRINT_ULTRASTAR} Downloading Audio") ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': output_path + '/' + clear_filename, @@ -30,7 +30,7 @@ def download_youtube_audio(url, clear_filename, output_path): def download_youtube_video(url, clear_filename, output_path): - print(PRINT_ULTRASTAR + " Downloading Video") + print(f"{PRINT_ULTRASTAR} Downloading Video") ydl_opts = { 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', 'outtmpl': output_path + '/' + clear_filename + '.mp4' diff --git a/moduls/DeviceDetection/device_detection.py b/moduls/DeviceDetection/device_detection.py index 120138d..3f53283 100644 --- a/moduls/DeviceDetection/device_detection.py +++ b/moduls/DeviceDetection/device_detection.py @@ -7,14 +7,13 @@ def get_available_device(): gpus = tf.config.list_physical_devices('GPU') if not gpus: - print("{} There are no GPUs available. Using {}.".format(PRINT_ULTRASTAR, print_red_highlighted_text("cpu"))) + print(f"{PRINT_ULTRASTAR} There are no GPUs available. Using {print_red_highlighted_text("cpu")}.") return "cpu" - print("{} Found available GPUs:".format(PRINT_ULTRASTAR)) + print(f"{PRINT_ULTRASTAR} Found available GPUs:") 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"))) + print("{PRINT_ULTRASTAR} GPU usage are currently in development. Using {print_red_highlighted_text("cpu")}") return "cpu" diff --git a/moduls/Midi/midi_creator.py b/moduls/Midi/midi_creator.py index c6b705c..1ea7b1a 100644 --- a/moduls/Midi/midi_creator.py +++ b/moduls/Midi/midi_creator.py @@ -11,7 +11,7 @@ from moduls.Ultrastar.ultrastar_converter import ultrastar_note_to_midi_note, ge from moduls.Log import PRINT_ULTRASTAR def convert_ultrastar_to_midi_instrument(ultrastar_class): - print(PRINT_ULTRASTAR + " Creating midi instrument from Ultrastar txt") + print(f"{PRINT_ULTRASTAR} Creating midi instrument from Ultrastar txt") instrument = pretty_midi.Instrument(program=0) velocity = 100 @@ -28,7 +28,7 @@ def convert_ultrastar_to_midi_instrument(ultrastar_class): def instruments_to_midi(instruments, bpm, midi_output): - print("{} Creating midi file -> {}".format(PRINT_ULTRASTAR, midi_output)) + print(f"{PRINT_ULTRASTAR} Creating midi file -> {midi_output}") midi_data = pretty_midi.PrettyMIDI(initial_tempo=bpm) for instrument in instruments: @@ -60,7 +60,7 @@ def find_nearest_index(array, value): def create_midi_notes_from_pitched_data(start_times, end_times, pitched_data): - print(PRINT_ULTRASTAR + " Creating midi notes from pitched data") + print(f"{PRINT_ULTRASTAR} Creating midi notes from pitched data") midi_notes = [] diff --git a/moduls/Pitcher/pitcher.py b/moduls/Pitcher/pitcher.py index f7b701a..976bbbd 100644 --- a/moduls/Pitcher/pitcher.py +++ b/moduls/Pitcher/pitcher.py @@ -7,10 +7,7 @@ from moduls.Log import print_blue_highlighted_text, print_red_highlighted_text def get_pitch_with_crepe_file(filename, step_size, model_capacity): - print("{} Pitching with {} and model {} and {} as worker".format(PRINT_ULTRASTAR, - print_blue_highlighted_text("crepe"), - print_blue_highlighted_text(model_capacity), - print_red_highlighted_text("cpu"))) + 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") # Todo: add GPU support by using torchcrepe sr, audio = wavfile.read(filename) diff --git a/moduls/Speech_Recognition/Vosk.py b/moduls/Speech_Recognition/Vosk.py index 66a462a..7857b37 100644 --- a/moduls/Speech_Recognition/Vosk.py +++ b/moduls/Speech_Recognition/Vosk.py @@ -11,7 +11,7 @@ from moduls.Log import PRINT_ULTRASTAR def transcribe_with_vosk(audio_filename, model_path): # Code from here: https://towardsdatascience.com/speech-recognition-with-timestamps-934ede4234b2 - print(PRINT_ULTRASTAR + " Transcribing {} with vosk and model {}".format(audio_filename, model_path)) + print(f"{PRINT_ULTRASTAR} Transcribing {audio_filename} with vosk and model {model_path}") model = Model(model_path) wf = wave.open(audio_filename, "rb") diff --git a/moduls/Speech_Recognition/Whisper.py b/moduls/Speech_Recognition/Whisper.py index 3af7909..dd88a3a 100644 --- a/moduls/Speech_Recognition/Whisper.py +++ b/moduls/Speech_Recognition/Whisper.py @@ -5,9 +5,7 @@ from moduls.Log import print_blue_highlighted_text, print_red_highlighted_text def transcribe_with_whisper(audioPath, model, device="cpu"): - print("{} Loading {} with model {} and {} as worker".format(PRINT_ULTRASTAR, print_blue_highlighted_text("whisper"), - print_blue_highlighted_text(model), - print_red_highlighted_text(device))) + 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") model = whisper.load_model(model, device=device) @@ -26,7 +24,7 @@ def transcribe_with_whisper(audioPath, model, device="cpu"): print(f"{PRINT_ULTRASTAR} Detected language: {print_blue_highlighted_text(language)}") - print("{} Transcribing {}".format(PRINT_ULTRASTAR, audioPath)) + print(f"{PRINT_ULTRASTAR} Transcribing {audioPath}") results = whisper.transcribe(model, audio, language=language) transcribed_data = [] diff --git a/moduls/Speech_Recognition/hyphenation.py b/moduls/Speech_Recognition/hyphenation.py index ce31b26..969b997 100644 --- a/moduls/Speech_Recognition/hyphenation.py +++ b/moduls/Speech_Recognition/hyphenation.py @@ -17,7 +17,7 @@ def language_check(language='en'): if lang_region is None: return None - print("{} Hyphenate using language code: {}".format(PRINT_ULTRASTAR, print_blue_highlighted_text(lang_region))) + print(f"{PRINT_ULTRASTAR} Hyphenate using language code: {print_blue_highlighted_text(lang_region)}") return lang_region diff --git a/moduls/Speech_Recognition/speech_recognition.py b/moduls/Speech_Recognition/speech_recognition.py index 611dd0d..31dc1e0 100644 --- a/moduls/Speech_Recognition/speech_recognition.py +++ b/moduls/Speech_Recognition/speech_recognition.py @@ -79,9 +79,9 @@ def transcribe_audio(audio_file): end_time = transcript['result'][0]['alternative'][0]['words'][-1]['endTime'] return transcript['result'][0]['alternative'][0]['transcript'], start_time, end_time except sr.UnknownValueError: - print(PRINT_ULTRASTAR + " Could not understand audio") + print(f"{PRINT_ULTRASTAR} Could not understand audio") except sr.RequestError as e: - print(PRINT_ULTRASTAR + " Error with recognizing service; {0}".format(e)) + print(f"{PRINT_ULTRASTAR} Error with recognizing service; {e}") class SpeechToText: diff --git a/moduls/Ultrastar/ultrastar_parser.py b/moduls/Ultrastar/ultrastar_parser.py index e3aa53a..aaed72f 100644 --- a/moduls/Ultrastar/ultrastar_parser.py +++ b/moduls/Ultrastar/ultrastar_parser.py @@ -4,7 +4,7 @@ from moduls.Log import PRINT_ULTRASTAR def parse_ultrastar_txt(input_file): - print(PRINT_ULTRASTAR + " Parse ultrastar txt -> {}".format(input_file)) + print(f"{PRINT_ULTRASTAR} Parse ultrastar txt -> {input_file}") file = open(input_file, 'r', encoding='utf8') txt = file.readlines() diff --git a/moduls/Ultrastar/ultrastar_score_calculator.py b/moduls/Ultrastar/ultrastar_score_calculator.py index 10c272d..dfe5a1f 100644 --- a/moduls/Ultrastar/ultrastar_score_calculator.py +++ b/moduls/Ultrastar/ultrastar_score_calculator.py @@ -36,9 +36,7 @@ def print_score(points): notes = max_score * (points.notes + points.rap) / points.parts golden = points.golden_notes + points.golden_rap score = notes + points.line_bonus + golden - print(PRINT_ULTRASTAR + " Total: {}, notes: {}, line bonus: {}, golden notes: {}".format( - print_cyan_highlighted_text(str(round(score))), print_blue_highlighted_text(str(round(notes))), - print_light_blue_highlighted_text(str(round(points.line_bonus))), print_gold_highlighted_text(str(round(golden))))) + print(f"{PRINT_ULTRASTAR} Total: {print_cyan_highlighted_text(str(round(score)))}, notes: {print_blue_highlighted_text(str(round(notes)))}, line bonus: {print_light_blue_highlighted_text(str(round(points.line_bonus)))}, golden notes: {print_gold_highlighted_text(str(round(golden)))}") def print_score_calculation(pitched_data, ultrastar_class): @@ -93,8 +91,8 @@ def print_score_calculation(pitched_data, ultrastar_class): if simple_part_line_bonus_points >= parts: simple_points.line_bonus += reachable_line_bonus_per_word - print("{} {} points".format(PRINT_ULTRASTAR, print_underlined_text("Simple (octave high ignored)"))) + print(f"{PRINT_ULTRASTAR} {print_underlined_text("Simple (octave high ignored)")} points") print_score(simple_points) - print("{} {} points:".format(PRINT_ULTRASTAR, print_underlined_text("Accurate (octave high matches)"))) + print(f"{PRINT_ULTRASTAR} {print_underlined_text("Accurate (octave high matches)")} points:") print_score(accurate_points) diff --git a/moduls/Ultrastar/ultrastar_writer.py b/moduls/Ultrastar/ultrastar_writer.py index 1e28984..6a6b121 100644 --- a/moduls/Ultrastar/ultrastar_writer.py +++ b/moduls/Ultrastar/ultrastar_writer.py @@ -22,7 +22,7 @@ def get_language_name(language): def create_ultrastar_txt_from_automation(transcribed_data, note_numbers, ultrastar_file_output, ultrastar_class, bpm=120): - print("{} Creating {} from transcription.".format(PRINT_ULTRASTAR, ultrastar_file_output)) + print(f"{PRINT_ULTRASTAR} Creating {ultrastar_file_output} from transcription.") real_bpm = real_bpm_to_ultrastar_bpm(bpm) multiplication = get_multiplier(real_bpm) @@ -87,7 +87,7 @@ def create_ultrastar_txt_from_automation(transcribed_data, note_numbers, ultrast def create_repitched_txt_from_ultrastar_data(input_file, note_numbers, output_repitched_ultrastar): # todo: just add '_repitched' to input_file - print("{} Creating repitched ultrastar txt -> {}_repitch.txt".format(PRINT_ULTRASTAR, input_file)) + print("{PRINT_ULTRASTAR} Creating repitched ultrastar txt -> {input_file}_repitch.txt") # todo: to reader file = open(input_file, 'r') diff --git a/moduls/os_helper.py b/moduls/os_helper.py index cecf37f..2e7f8ca 100644 --- a/moduls/os_helper.py +++ b/moduls/os_helper.py @@ -4,7 +4,7 @@ from moduls.Log import PRINT_ULTRASTAR def create_folder(folder_name): - print(PRINT_ULTRASTAR + " Creating output folder. -> " + folder_name) + print(f"{PRINT_ULTRASTAR} Creating output folder. -> {folder_name}") is_exist = os.path.exists(folder_name) if not is_exist: os.makedirs(folder_name)