diff --git a/README.md b/README.md index 09352ba..3393da5 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ You need FFmpeg installed. _Not all options working now!_ ```commandline - UltraSinger.py [opt] [mode] [transcription] [rec model] [pitcher] [extra] + UltraSinger.py [opt] [mode] [transcription] [pitcher] [extra] [opt] -h This help text. @@ -49,15 +49,12 @@ _Not all options working now!_ --whisper (default) tiny|base|small|medium|large --vosk Needs model - [rec model] - (-k Keep audio chunks) # In Progress - [extra] - -k Keep audio chunks + (-k Keep audio chunks) # In Progress + --hyphenation (default) True|False [pitcher] --crepe (default) tiny|small|medium|large|full - ''' ``` ### Input @@ -109,6 +106,17 @@ For the first test run, use the `small model`, to be accurate use the `gigaspeec -i "input/music.mp3" -v "models\vosk-model-en-us-0.42-gigaspeech" ``` +#### Hyphenation + +Is on by default. Can also be deactivated if hyphenation does not produce +anything useful. Note that the word is simply split, +without paying attention to whether the separated word really +starts at the place or is heard. + +```commandline +-i XYZ --hyphenation True +``` + ### Pitcher Pitching is done with the `crepe` model. diff --git a/Settings.py b/Settings.py index 66cf59d..6da257f 100644 --- a/Settings.py +++ b/Settings.py @@ -2,6 +2,7 @@ class Settings: create_midi = True create_plot = False create_audio_chunks = False + hyphenation = True input_file_path = '' output_file_path = '' diff --git a/UltraSinger.py b/UltraSinger.py index 884cfdd..f8a5e09 100644 --- a/UltraSinger.py +++ b/UltraSinger.py @@ -1,4 +1,5 @@ import getopt +import copy import os import sys import Levenshtein @@ -8,18 +9,19 @@ import math import shutil import re -from moduls.Audio.vocal_chunks import export_chunks_from_ultrastar_data, convert_audio_to_mono_wav +from moduls.Audio.vocal_chunks import export_chunks_from_ultrastar_data, convert_audio_to_mono_wav, export_chunks_from_transcribed_data, remove_silence_from_transcribtion_data +from moduls.Audio.youtube import download_youtube_video, download_youtube_audio, get_youtube_title from moduls.Midi import midi_creator from moduls.Pitcher.pitcher import get_frequency_with_high_confidence, get_pitch_with_crepe_file from moduls.Ultrastar import ultrastar_parser, ultrastar_converter, ultrastar_writer -from moduls.Speech_Recognition.Vosk import transcribe_with_vosk, export_vosk_data_to_csv -from moduls.Audio.vocal_chunks import export_chunks_from_vosk_data +from moduls.Speech_Recognition.Vosk import transcribe_with_vosk, export_transcribed_data_to_csv +from moduls.Speech_Recognition.hyphenation import hyphenation +from moduls.Speech_Recognition.Whisper import transcribe_with_whisper from moduls.os_helper import create_folder from matplotlib import pyplot as plt from collections import Counter from Settings import Settings -from moduls.Audio.youtube import download_youtube_video, download_youtube_audio, get_youtube_title -from moduls.Speech_Recognition.Whisper import transcribe_with_whisper + settings = Settings() @@ -49,6 +51,8 @@ def most_frequent(ar): def convert_ultrastar_note_numbers(midi_notes): + print("Creating Ultrastar notes from midi data") + ultrastar_note_numbers = [] for i in range(len(midi_notes)): note_number_librosa = librosa.note_to_midi(midi_notes[i]) @@ -88,8 +92,30 @@ def find_nearest_index(array, value): return idx + +def add_hyphen_to_data(transcribed_data, hyphen_words): + data = [] + + for i in range(len(transcribed_data)): + if not hyphen_words[i]: + data.append(transcribed_data[i]) + else: + chunk_duration = transcribed_data[i].end - transcribed_data[i].start + chunk_duration = chunk_duration / (len(hyphen_words[i])) + + for j in range(len(hyphen_words[i])): + dup = copy.copy(transcribed_data[i]) + dup.start = transcribed_data[i].start + chunk_duration * j + dup.end = transcribed_data[i].end - chunk_duration * (len(hyphen_words[i]) - 1 - j) + dup.word = hyphen_words[i][j] + dup.is_hyphen = True + data.append(dup) + + return data + + def create_midi_notes_from_pitched_data(start_times, end_times, pitched_data): - print("Create midi notes from pitched data") + print("Creating midi notes from pitched data") midi_notes = [] @@ -110,6 +136,7 @@ def create_midi_notes_from_pitched_data(start_times, end_times, pitched_data): conf_f = get_frequency_with_high_confidence(f, c) notes = convert_frequencies_to_notes(conf_f) + note = most_frequent(notes)[0][0] midi_notes.append(note) @@ -148,7 +175,7 @@ def correct_words(recognized_words, word_list_file): def print_help(): m = ''' - UltraSinger.py [opt] [mode] [transcription] [rec model] [pitcher] [extra] + UltraSinger.py [opt] [mode] [transcription] [pitcher] [extra] [opt] -h This help text. @@ -176,7 +203,8 @@ def print_help(): --vosk Needs model [extra] - (-k Keep audio chunks) # In Progress + (-k Keep audio chunks) # In Progress + --hyphenation (default) True|False [pitcher] --crepe (default) tiny|small|medium|large|full @@ -244,6 +272,14 @@ def plot(input_file, vosk_transcribed_data, midi_notes): plt.savefig('test/pit.png', dpi=2000) +def hyphenate_each_word(language, transcribed_data): + print("Hyphenation each word") + hyphenated_word = [] + for i in range(len(transcribed_data)): + hyphenated_word.append(hyphenation(transcribed_data[i].word, language)) + return hyphenated_word + + def do_audio_stuff(): # Youtube if settings.input_file_path.startswith('https:'): @@ -273,16 +309,24 @@ def do_audio_stuff(): # Audio transcription if settings.transcriber == "whisper": - transcribed_data = transcribe_with_whisper(settings.mono_audio_path, settings.whisper_model) - else: # vosk + transcribed_data, language = transcribe_with_whisper(settings.mono_audio_path, settings.whisper_model) + else: # vosk transcribed_data = transcribe_with_vosk(settings.mono_audio_path, settings.vosk_model_path) + # todo: make language selectable + language = 'en' + + transcribed_data = remove_silence_from_transcribtion_data(settings.mono_audio_path, transcribed_data) + + if settings.hyphenation: + hyphen_words = hyphenate_each_word(language, transcribed_data) + transcribed_data = add_hyphen_to_data(transcribed_data, hyphen_words) if settings.create_audio_chunks: audio_chunks_path = cache_path + '/' + settings.audio_chunk_folder_name create_folder(audio_chunks_path) csv_filename = audio_chunks_path + "/_chunks.csv" - export_chunks_from_vosk_data(settings.mono_audio_path, transcribed_data, audio_chunks_path) - export_vosk_data_to_csv(transcribed_data, csv_filename) + export_chunks_from_transcribed_data(settings.mono_audio_path, transcribed_data, audio_chunks_path) + export_transcribed_data_to_csv(transcribed_data, csv_filename) # todo: do we need to correct words? # lyric = 'input/faber_lyric.txt' @@ -299,8 +343,8 @@ def do_audio_stuff(): for i in range(len(transcribed_data)): start_times.append(transcribed_data[i].start) end_times.append(transcribed_data[i].end) - midi_notes = create_midi_notes_from_pitched_data(start_times, end_times, pitched_data) + ultrastar_note_numbers = convert_ultrastar_note_numbers(midi_notes) if settings.create_plot: @@ -309,8 +353,9 @@ def do_audio_stuff(): # Ultrastar txt creation real_bpm = get_bpm_from_file(ultrastar_audio_input_path) ultrastar_file_output = song_output + '/' + basename_without_ext + '.txt' - ultrastar_writer.create_txt_from_transcription(transcribed_data, ultrastar_note_numbers, ultrastar_file_output, - basename_without_ext, real_bpm) + ultrastar_writer.create_ultrastar_txt_from_automation(transcribed_data, ultrastar_note_numbers, + ultrastar_file_output, + basename_without_ext, real_bpm) if settings.create_midi: ultrastar_class = ultrastar_parser.parse_ultrastar_txt(ultrastar_file_output) @@ -321,7 +366,7 @@ def do_audio_stuff(): def main(argv): short = "hi:o:amv:" - long = ["ifile=", "ofile=", "crepe_model=", "vosk=", "whisper="] + long = ["ifile=", "ofile=", "crepe_model=", "vosk=", "whisper=", "hyphenation="] opts, args = getopt.getopt(argv, short, long) @@ -345,6 +390,8 @@ def main(argv): settings.vosk_model_path = arg elif opt in ("--crepe"): settings.crepe_model_capacity = arg + elif opt in ("--hyphenation"): + settings.hyphenation = arg if settings.output_file_path == '': if settings.input_file_path.startswith('https:'): diff --git a/moduls/Audio/vocal_chunks.py b/moduls/Audio/vocal_chunks.py index d213586..c130e57 100644 --- a/moduls/Audio/vocal_chunks.py +++ b/moduls/Audio/vocal_chunks.py @@ -11,6 +11,7 @@ from pydub import AudioSegment def convert_audio_to_mono_wav(input_file, output_file): """Convert audio to mono wav""" + print("Converting audio for AI") if '.mp3' in input_file: sound = AudioSegment.from_mp3(input_file) @@ -27,7 +28,7 @@ class AudioManipulation: pass -def export_chunks_from_vosk_data(audio_filename, vosk_transcribed_data, output_folder_name): +def export_chunks_from_transcribed_data(audio_filename, vosk_transcribed_data, output_folder_name): """Export vosk data as vocal chunks wav files""" print("Export vosk data as vocal chunks wav files") @@ -44,8 +45,8 @@ def export_chunks_from_vosk_data(audio_filename, vosk_transcribed_data, output_f wf.close() -def remove_silence_from_vosk_data(audio_path, vosk_transcribed_data): - print("Removing silent start and ending, from vosk data") +def remove_silence_from_transcribtion_data(audio_path, vosk_transcribed_data): + print("Removing silent start and ending, from transcription data") y, sr = librosa.load(audio_path, sr=None) diff --git a/moduls/Audio/youtube.py b/moduls/Audio/youtube.py index 1364eae..cc48742 100644 --- a/moduls/Audio/youtube.py +++ b/moduls/Audio/youtube.py @@ -13,6 +13,7 @@ def get_youtube_title(url): def download_youtube_audio(url, clear_filename, output_path): + print("Downloading Audio") ydl_opts = { 'format': 'm4a/bestaudio/best', 'outtmpl': output_path + '/' + clear_filename + '.mp3', @@ -29,6 +30,7 @@ def download_youtube_audio(url, clear_filename, output_path): def download_youtube_video(url, clear_filename, output_path): + print("Downloading Video") ydl_opts = { 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', 'outtmpl': output_path + '/' + clear_filename + '.mp4' diff --git a/moduls/Pitcher/pitcher.py b/moduls/Pitcher/pitcher.py index 8d43c7b..2f314d1 100644 --- a/moduls/Pitcher/pitcher.py +++ b/moduls/Pitcher/pitcher.py @@ -5,6 +5,8 @@ from scipy.io import wavfile def get_pitch_with_crepe_file(filename, step_size, model_capacity): + print("Transcribing with crepe and model {}".format(model_capacity)) + sr, audio = wavfile.read(filename) pitched_data = PitchedData() diff --git a/moduls/Speech_Recognition/TranscribedData.py b/moduls/Speech_Recognition/TranscribedData.py index b65f01b..cf37c06 100644 --- a/moduls/Speech_Recognition/TranscribedData.py +++ b/moduls/Speech_Recognition/TranscribedData.py @@ -8,4 +8,4 @@ class TranscribedData: self.word = transcribed_json.get("word", transcribed_json.get("text", None)) self.end = transcribed_json["end"] self.start = transcribed_json["start"] - + self.is_hyphen = None diff --git a/moduls/Speech_Recognition/Vosk.py b/moduls/Speech_Recognition/Vosk.py index a83f458..abc688d 100644 --- a/moduls/Speech_Recognition/Vosk.py +++ b/moduls/Speech_Recognition/Vosk.py @@ -4,12 +4,11 @@ import csv from vosk import Model, KaldiRecognizer from moduls.Speech_Recognition.TranscribedData import TranscribedData -from moduls.Audio.vocal_chunks import remove_silence_from_vosk_data # todo: Rename to Transcoder? -def export_vosk_data_to_csv(vosk_transcribed_data, filename): +def export_transcribed_data_to_csv(vosk_transcribed_data, filename): """Export vosk data to csv""" print("Exporting Vosk data to CSV") @@ -48,7 +47,7 @@ def transcribe_with_vosk(audio_filename, model_path): results.append(part_result) # convert list of JSON dictionaries to list of 'Word' objects - vosk_transcribed_data = [] + transcribed_data = [] for sentence in results: if len(sentence) == 1: # sometimes there are bugs in recognition @@ -57,7 +56,8 @@ def transcribe_with_vosk(audio_filename, model_path): continue for obj in sentence['result']: vtd = TranscribedData(obj) # create custom Word object - vosk_transcribed_data.append(vtd) # and add it to list + vtd.word = vtd.word + ' ' + transcribed_data.append(vtd) # and add it to list # Todo: remove silent part from each word @@ -66,9 +66,7 @@ def transcribe_with_vosk(audio_filename, model_path): # for word in vosk_transcribed_data: # print(word.to_string()) - vosk_transcribed_data = remove_silence_from_vosk_data(audio_filename, vosk_transcribed_data) - - return vosk_transcribed_data + return transcribed_data class SpeechToText: diff --git a/moduls/Speech_Recognition/Whisper.py b/moduls/Speech_Recognition/Whisper.py index aea99db..10da894 100644 --- a/moduls/Speech_Recognition/Whisper.py +++ b/moduls/Speech_Recognition/Whisper.py @@ -3,6 +3,8 @@ from moduls.Speech_Recognition.TranscribedData import TranscribedData def transcribe_with_whisper(audio, model): + print("Transcribing {} with whisper and model {}".format(audio, model)) + model = whisper.load_model(model, device="cpu") # load audio and pad/trim it to fit 30 seconds @@ -29,7 +31,8 @@ def transcribe_with_whisper(audio, model): # to class for obj in segment["words"]: vtd = TranscribedData(obj) # create custom Word object + vtd.word = vtd.word + ' ' transcribed_data.append(vtd) # and add it to list - return transcribed_data + return transcribed_data, language diff --git a/moduls/Speech_Recognition/hyphenation.py b/moduls/Speech_Recognition/hyphenation.py new file mode 100644 index 0000000..d98680e --- /dev/null +++ b/moduls/Speech_Recognition/hyphenation.py @@ -0,0 +1,11 @@ +import pyphen + + +def hyphenation(word, language='en'): + dic = pyphen.Pyphen(lang=language) + + hyphen = None + for pair in dic.iterate(word): + hyphen = pair # Todo: ignore the others? + + return hyphen diff --git a/moduls/Ultrastar/ultrastar_writer.py b/moduls/Ultrastar/ultrastar_writer.py index 92ecd11..b5b92a9 100644 --- a/moduls/Ultrastar/ultrastar_writer.py +++ b/moduls/Ultrastar/ultrastar_writer.py @@ -2,13 +2,13 @@ from moduls.Ultrastar.ultrastar_converter import real_bpm_to_ultrastar_bpm, seco import re -def create_txt_from_transcription(vosk_transcribed_data, note_numbers, ultrastar_file_output, title, bpm=120): +def create_ultrastar_txt_from_automation(transcribed_data, note_numbers, ultrastar_file_output, title, bpm=120): print("Creating {} from transcription.".format(ultrastar_file_output)) # todo: Optimize multiplication multiplication = 32 with open(ultrastar_file_output, 'w') as f: - gap = vosk_transcribed_data[0].start + gap = transcribed_data[0].start ultrastar_bpm = real_bpm_to_ultrastar_bpm(bpm) * multiplication f.write('#ARTIST:' + title + '\n') @@ -21,9 +21,9 @@ def create_txt_from_transcription(vosk_transcribed_data, note_numbers, ultrastar f.write('#GAP:' + str(int(gap * 1000)) + '\n') # Write the singing part - for i in range(len(vosk_transcribed_data)): - start_time = (vosk_transcribed_data[i].start - gap) * multiplication - end_time = (vosk_transcribed_data[i].end - vosk_transcribed_data[i].start) * multiplication + for i in range(len(transcribed_data)): + start_time = (transcribed_data[i].start - gap) * multiplication + end_time = (transcribed_data[i].end - transcribed_data[i].start) * multiplication start_beat = second_to_beat(start_time, bpm) duration = second_to_beat(end_time, bpm) @@ -37,12 +37,12 @@ def create_txt_from_transcription(vosk_transcribed_data, note_numbers, ultrastar f.write(str(round(start_beat)) + ' ') f.write(str(round(duration)) + ' ') f.write(str(note_numbers[i]) + ' ') - f.write(vosk_transcribed_data[i].word + ' ') + f.write(transcribed_data[i].word) f.write('\n') # detect silence between words - if i < len(vosk_transcribed_data) - 1: - silence = vosk_transcribed_data[i + 1].start - vosk_transcribed_data[i].end + if i < len(transcribed_data) - 1: + silence = transcribed_data[i + 1].start - transcribed_data[i].end else: silence = 0 @@ -51,7 +51,7 @@ def create_txt_from_transcription(vosk_transcribed_data, note_numbers, ultrastar # '-' end of current sing part # 'n1' show next at time in real beat f.write('- ') - show_next = second_to_beat(vosk_transcribed_data[i].end - gap, bpm) * multiplication + show_next = second_to_beat(transcribed_data[i].end - gap, bpm) * multiplication f.write(str(round(show_next))) f.write('\n') f.write('E') diff --git a/moduls/os_helper.py b/moduls/os_helper.py index 566f0fe..0e5074b 100644 --- a/moduls/os_helper.py +++ b/moduls/os_helper.py @@ -2,6 +2,7 @@ import os def create_folder(folder_name): + print("Creating output folder. -> " + folder_name) is_exist = os.path.exists(folder_name) if not is_exist: os.makedirs(folder_name) diff --git a/requirements.txt b/requirements.txt index f189f4d..0a1d0c1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,9 @@ scipy==1.10.0 vosk==0.3.45 tensorflow==2.11.0 matplotlib~=3.6.3 -numpy~=1.23.5 yt_dlp~=2023.1.6 -git+https://github.com/linto-ai/whisper-timestamped \ No newline at end of file +yt_dlp~=2023.1.6 +pyphen~=0.13.2numpy~=1.23.5 +git+https://github.com/linto-ai/whisper-timestamped +pyphen~=0.13.2 +numpy~=1.23.5 \ No newline at end of file