mirror of
https://github.com/rakuri255/UltraSinger.git
synced 2026-11-20 13:07:52 +09:00
Refactoring
This commit is contained in:
1 parent
5b5e644fe0
commit
849d5b3fb7
24 files changed
+736
-638
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from src.modules.Audio.denoise import ffmpeg_reduce_noise
|
||||
from src.modules.Audio.denoise import denoise_vocal_audio
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class DenoiseTest(unittest.TestCase):
|
||||
test_output = test_dir + "/test_output"
|
||||
|
||||
# Act
|
||||
ffmpeg_reduce_noise(test_file_abs_path, test_output + "/output_" + test_file_name)
|
||||
denoise_vocal_audio(test_file_abs_path, test_output + "/output_" + test_file_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import unittest
|
||||
from packaging import version
|
||||
from unittest.mock import patch, mock_open
|
||||
from src.modules.Ultrastar.ultrastar_writer import create_ultrastar_txt_from_automation, deviation
|
||||
from src.modules.Ultrastar.ultrastar_writer import create_ultrastar_txt, deviation
|
||||
from src.modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from src.modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag
|
||||
|
||||
@@ -166,7 +166,7 @@ class TestCreateUltrastarTxt(unittest.TestCase):
|
||||
@staticmethod
|
||||
def act(bpm, default_ultrastar_class, note_numbers, transcribed_data, ultrastar_file_output):
|
||||
with patch("builtins.open", mock_open()) as mock_file:
|
||||
create_ultrastar_txt_from_automation(
|
||||
create_ultrastar_txt(
|
||||
transcribed_data, note_numbers, ultrastar_file_output,
|
||||
default_ultrastar_class, bpm
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import unittest
|
||||
import os
|
||||
from unittest.mock import patch, MagicMock
|
||||
from src.UltraSinger import format_separated_string
|
||||
from modules.Ultrastar.ultrastar_writer import format_separated_string
|
||||
from src.UltraSinger import extract_year
|
||||
from src.UltraSinger import parse_ultrastar_txt
|
||||
from src import UltraSinger
|
||||
|
||||
+2
-1
@@ -25,4 +25,5 @@ pylint~=3.2.3
|
||||
pytest~=8.2.2
|
||||
protobuf==3.20.*
|
||||
packaging~=24.1
|
||||
music21~=9.1.0
|
||||
music21~=9.1.0
|
||||
unidecode~=1.3.8
|
||||
@@ -25,9 +25,6 @@ class Settings:
|
||||
language = None
|
||||
format_version = "1.0.0"
|
||||
|
||||
# Transcribe
|
||||
audio_chunk_folder_name = "audio-chunks"
|
||||
|
||||
# Whisper
|
||||
transcriber = "whisper" # whisper
|
||||
whisper_model = "large-v2" # Multilingual model tiny|base|small|medium|large-v1|large-v2
|
||||
|
||||
+219
-490
@@ -6,55 +6,43 @@ import getopt
|
||||
import os
|
||||
import sys
|
||||
import Levenshtein
|
||||
import librosa
|
||||
|
||||
from tqdm import tqdm
|
||||
from packaging import version
|
||||
|
||||
import soundfile as sf
|
||||
|
||||
from modules import os_helper
|
||||
from modules.Audio.denoise import ffmpeg_reduce_noise
|
||||
from modules.Audio.separation import separate_audio
|
||||
from modules.Audio.denoise import denoise_vocal_audio
|
||||
from modules.Audio.separation import separate_vocal_from_audio
|
||||
from modules.Audio.vocal_chunks import (
|
||||
export_chunks_from_transcribed_data,
|
||||
export_chunks_from_ultrastar_data,
|
||||
create_audio_chunks_from_transcribed_data,
|
||||
create_audio_chunks_from_ultrastar_data,
|
||||
)
|
||||
from modules.Audio.silence_processing import remove_silence_from_transcription_data, get_silence_sections
|
||||
from modules.Speech_Recognition.TranscriptionResult import TranscriptionResult
|
||||
from modules.Ultrastar.ultrastar_score_calculator import Score
|
||||
from modules.csv_handler import export_transcribed_data_to_csv
|
||||
from modules.Audio.silence_processing import remove_silence_from_transcription_data, mute_no_singing_parts
|
||||
|
||||
from modules.Audio.convert_audio import convert_audio_to_mono_wav, convert_wav_to_mp3
|
||||
from modules.Audio.youtube import (
|
||||
download_youtube_audio,
|
||||
download_youtube_thumbnail,
|
||||
download_youtube_video,
|
||||
get_youtube_title,
|
||||
download_from_youtube,
|
||||
)
|
||||
from modules.Ultrastar.ultrastar_writer import format_separated_string
|
||||
from modules.console_colors import (
|
||||
ULTRASINGER_HEAD,
|
||||
blue_highlighted,
|
||||
gold_highlighted,
|
||||
light_blue_highlighted,
|
||||
red_highlighted,
|
||||
green_highlighted,
|
||||
)
|
||||
from modules.Midi.midi_creator import (
|
||||
convert_frequencies_to_notes,
|
||||
create_midi_notes_from_pitched_data,
|
||||
convert_midi_notes_to_ultrastar_notes,
|
||||
most_frequent,
|
||||
create_midi_instrument,
|
||||
instruments_to_midi
|
||||
create_midi_segments_from_transcribed_data,
|
||||
create_repitched_midi_segments_from_ultrastar_txt,
|
||||
create_midi_file,
|
||||
|
||||
)
|
||||
from modules.Pitcher.pitcher import (
|
||||
get_frequencies_with_high_confidence,
|
||||
get_pitch_with_crepe_file,
|
||||
)
|
||||
from modules.Speech_Recognition.TranscriptionResult import TranscriptionResult
|
||||
from modules.Speech_Recognition.hyphenation import (
|
||||
hyphenation,
|
||||
language_check,
|
||||
create_hyphenator,
|
||||
hyphenate_each_word,
|
||||
)
|
||||
from modules.Speech_Recognition.Whisper import transcribe_with_whisper
|
||||
from modules.Ultrastar import (
|
||||
@@ -62,51 +50,23 @@ from modules.Ultrastar import (
|
||||
ultrastar_writer,
|
||||
ultrastar_parser,
|
||||
)
|
||||
from modules.Ultrastar.ultrastar_score_calculator import Score
|
||||
from modules.Ultrastar.ultrastar_txt import FILE_ENCODING
|
||||
from modules.help_print import print_support, print_help
|
||||
from modules.os_helper import check_file_exists
|
||||
from modules.plot import plot, plot_spectrogram
|
||||
from modules.common_print import print_support, print_help, print_version
|
||||
from modules.os_helper import check_file_exists, get_unused_song_output_dir
|
||||
from modules.plot import create_plots
|
||||
from modules.musicbrainz_client import get_music_infos
|
||||
from modules.sheet import create_sheet
|
||||
from modules.ProcessData import *
|
||||
from Settings import Settings
|
||||
from modules.DeviceDetection.device_detection import check_gpu_support
|
||||
from modules.Audio.bpm import get_bpm_from_file
|
||||
|
||||
from Settings import Settings
|
||||
|
||||
settings = Settings()
|
||||
|
||||
def pitch_each_chunk_with_crepe(directory: str) -> list[str]:
|
||||
"""Pitch each chunk with crepe and return midi notes"""
|
||||
print(f"{ULTRASINGER_HEAD} Pitching each chunk with {blue_highlighted('crepe')}")
|
||||
|
||||
midi_notes = []
|
||||
for filename in sorted(
|
||||
[f for f in os.listdir(directory) if f.endswith(".wav")],
|
||||
key=lambda x: int(x.split("_")[1]),
|
||||
):
|
||||
filepath = os.path.join(directory, filename)
|
||||
# todo: stepsize = duration? then when shorter than "it" it should take the duration. Otherwise there a more notes
|
||||
pitched_data = get_pitch_with_crepe_file(
|
||||
filepath,
|
||||
settings.crepe_model_capacity,
|
||||
settings.crepe_step_size,
|
||||
settings.tensorflow_device,
|
||||
)
|
||||
conf_f = get_frequencies_with_high_confidence(
|
||||
pitched_data.frequencies, pitched_data.confidence
|
||||
)
|
||||
|
||||
notes = convert_frequencies_to_notes(conf_f)
|
||||
note = most_frequent(notes)[0][0]
|
||||
|
||||
midi_notes.append(note)
|
||||
# todo: Progress?
|
||||
# print(filename + " f: " + str(mean))
|
||||
|
||||
return midi_notes
|
||||
|
||||
|
||||
def add_hyphen_to_data(
|
||||
transcribed_data: list[TranscribedData], hyphen_words: list[list[str]]
|
||||
transcribed_data: list[TranscribedData], hyphen_words: list[list[str]]
|
||||
):
|
||||
"""Add hyphen to transcribed data return new data list"""
|
||||
new_data = []
|
||||
@@ -124,7 +84,7 @@ def add_hyphen_to_data(
|
||||
dup = copy.copy(data)
|
||||
dup.start = next_start
|
||||
next_start = data.end - chunk_duration * (
|
||||
len(hyphen_words[i]) - 1 - hyphenated_word_index
|
||||
len(hyphen_words[i]) - 1 - hyphenated_word_index
|
||||
)
|
||||
dup.end = next_start
|
||||
dup.word = hyphen_words[i][hyphenated_word_index]
|
||||
@@ -137,22 +97,7 @@ def add_hyphen_to_data(
|
||||
|
||||
return new_data
|
||||
|
||||
|
||||
def get_bpm_from_data(data, sampling_rate):
|
||||
"""Get real bpm from audio data"""
|
||||
onset_env = librosa.onset.onset_strength(y=data, sr=sampling_rate)
|
||||
wav_tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sampling_rate)
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} BPM is {blue_highlighted(str(round(wav_tempo[0], 2)))}")
|
||||
return wav_tempo[0]
|
||||
|
||||
|
||||
def get_bpm_from_file(wav_file: str) -> float:
|
||||
"""Get real bpm from audio file"""
|
||||
data, sampling_rate = librosa.load(wav_file, sr=None)
|
||||
return get_bpm_from_data(data, sampling_rate)
|
||||
|
||||
|
||||
# Todo: Unused
|
||||
def correct_words(recognized_words, word_list_file):
|
||||
"""Docstring"""
|
||||
with open(word_list_file, "r", encoding="utf-8") as file:
|
||||
@@ -178,51 +123,78 @@ def remove_unecessary_punctuations(transcribed_data: list[TranscribedData]) -> N
|
||||
data.word = data.word.translate({ord(i): None for i in punctuation})
|
||||
|
||||
|
||||
def hyphenate_each_word(
|
||||
language: str, transcribed_data: list[TranscribedData]
|
||||
) -> list[list[str]] | None:
|
||||
"""Hyphenate each word in the transcribed data."""
|
||||
lang_region = language_check(language)
|
||||
if lang_region is None:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {red_highlighted('Error in hyphenation for language ')} {blue_highlighted(language)}{red_highlighted(', maybe you want to disable it?')}"
|
||||
)
|
||||
return None
|
||||
|
||||
hyphenated_word = []
|
||||
try:
|
||||
hyphenator = create_hyphenator(lang_region)
|
||||
for i in tqdm(enumerate(transcribed_data)):
|
||||
pos = i[0]
|
||||
hyphenated_word.append(
|
||||
hyphenation(transcribed_data[pos].word, hyphenator)
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"{ULTRASINGER_HEAD} {red_highlighted('Error in hyphenation for language ')} {blue_highlighted(language)}{red_highlighted(', maybe you want to disable it?')}")
|
||||
print(f"\t{red_highlighted(f'->{e}')}")
|
||||
return None
|
||||
|
||||
return hyphenated_word
|
||||
|
||||
|
||||
def print_version() -> None:
|
||||
"""Print version text"""
|
||||
print()
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {gold_highlighted('*****************************')}"
|
||||
)
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {gold_highlighted('UltraSinger Version:')} {light_blue_highlighted(settings.APP_VERSION)}"
|
||||
)
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {gold_highlighted('*****************************')}"
|
||||
)
|
||||
|
||||
|
||||
def run() -> tuple[str, Score, Score]:
|
||||
"""The processing function of this program"""
|
||||
settings.input_file_is_ultrastar_txt = settings.input_file_path.endswith(".txt")
|
||||
|
||||
process_data = InitProcessData()
|
||||
|
||||
process_data.process_data_paths.cache_folder_path = (
|
||||
os.path.join(settings.output_folder_path, "cache")
|
||||
if settings.cache_override_path is None
|
||||
else settings.cache_override_path
|
||||
)
|
||||
|
||||
# Create process audio
|
||||
process_data.process_data_paths.processing_audio_path = CreateProcessAudio(process_data)
|
||||
|
||||
# Audio transcription
|
||||
process_data.media_info.language = settings.language
|
||||
if not settings.ignore_audio:
|
||||
TranscribeAudio(process_data)
|
||||
|
||||
# Create audio chunks
|
||||
if settings.create_audio_chunks:
|
||||
create_audio_chunks(process_data)
|
||||
|
||||
# Pitch audio
|
||||
process_data.pitched_data = pitch_audio(process_data.process_data_paths)
|
||||
|
||||
# Create Midi_Segments
|
||||
if not settings.ignore_audio:
|
||||
process_data.midi_segments = create_midi_segments_from_transcribed_data(process_data.transcribed_data, process_data.pitched_data)
|
||||
else:
|
||||
process_data.midi_segments = create_repitched_midi_segments_from_ultrastar_txt(process_data.pitched_data, process_data.parsed_file)
|
||||
|
||||
# Create plot
|
||||
if settings.create_plot:
|
||||
create_plots(process_data, settings.output_folder_path)
|
||||
|
||||
# Create Ultrastar txt
|
||||
accurate_score, simple_score, ultrastar_file_output = CreateUltraStarTxt(process_data)
|
||||
|
||||
# Create Midi
|
||||
if settings.create_midi:
|
||||
create_midi_file(process_data.media_info.bpm, settings.output_folder_path, process_data.midi_segments,
|
||||
process_data.basename)
|
||||
|
||||
# Sheet music
|
||||
create_sheet(process_data.midi_segments, settings.output_folder_path,
|
||||
process_data.process_data_paths.cache_folder_path, settings.musescore_path, process_data.basename,
|
||||
process_data.media_info)
|
||||
|
||||
# Cleanup
|
||||
if not settings.keep_cache:
|
||||
remove_cache_folder(process_data.process_data_paths.cache_folder_path)
|
||||
|
||||
# Print Support
|
||||
print_support()
|
||||
return ultrastar_file_output, simple_score, accurate_score
|
||||
|
||||
|
||||
def create_audio_chunks(process_data):
|
||||
if not settings.ignore_audio:
|
||||
create_audio_chunks_from_transcribed_data(
|
||||
process_data.process_data_paths,
|
||||
process_data.transcribed_data)
|
||||
else:
|
||||
create_audio_chunks_from_ultrastar_data(
|
||||
process_data.process_data_paths,
|
||||
process_data.parsed_file
|
||||
)
|
||||
|
||||
|
||||
def InitProcessData():
|
||||
settings.input_file_is_ultrastar_txt = settings.input_file_path.endswith(".txt")
|
||||
if settings.input_file_is_ultrastar_txt:
|
||||
# Parse Ultrastar txt
|
||||
(
|
||||
@@ -234,7 +206,7 @@ def run() -> tuple[str, Score, Score]:
|
||||
process_data = from_ultrastar_txt(ultrastar_class)
|
||||
process_data.basename = basename
|
||||
process_data.process_data_paths.audio_output_file_path = audio_file_path
|
||||
#todo: only ignore whisper
|
||||
# todo: ignore transcribe
|
||||
settings.ignore_audio = True
|
||||
|
||||
elif settings.input_file_path.startswith("https:"):
|
||||
@@ -257,12 +229,73 @@ def run() -> tuple[str, Score, Score]:
|
||||
process_data.process_data_paths.audio_output_file_path,
|
||||
process_data.media_info,
|
||||
) = infos_from_audio_input_file()
|
||||
return process_data
|
||||
|
||||
process_data.process_data_paths.cache_folder_path = (
|
||||
os.path.join(settings.output_folder_path, "cache")
|
||||
if settings.cache_override_path is None
|
||||
else settings.cache_override_path
|
||||
|
||||
def TranscribeAudio(process_data):
|
||||
transcription_result = transcribe_audio(process_data.process_data_paths.cache_folder_path,
|
||||
process_data.process_data_paths.processing_audio_path)
|
||||
|
||||
if process_data.media_info.language is None:
|
||||
process_data.media_info.language = transcription_result.detected_language
|
||||
|
||||
process_data.transcribed_data = transcription_result.transcribed_data
|
||||
|
||||
# Hyphen
|
||||
# Todo: Is it really unnecessary?
|
||||
remove_unecessary_punctuations(process_data.transcribed_data)
|
||||
if settings.hyphenation:
|
||||
hyphen_words = hyphenate_each_word(process_data.media_info.language, process_data.transcribed_data)
|
||||
|
||||
if hyphen_words is not None:
|
||||
process_data.transcribed_data = add_hyphen_to_data(process_data.transcribed_data, hyphen_words)
|
||||
|
||||
process_data.transcribed_data = remove_silence_from_transcription_data(
|
||||
process_data.process_data_paths.processing_audio_path, process_data.transcribed_data
|
||||
)
|
||||
|
||||
|
||||
def CreateUltraStarTxt(process_data: ProcessData):
|
||||
# Move instrumental and vocals
|
||||
if settings.create_karaoke and version.parse(settings.format_version) < version.parse("1.1.0"):
|
||||
karaoke_output_path = os.path.join(settings.output_folder_path, process_data.basename + " [Karaoke].mp3")
|
||||
convert_wav_to_mp3(process_data.process_data_paths.instrumental_audio_file_path, karaoke_output_path)
|
||||
|
||||
if version.parse(settings.format_version) >= version.parse("1.1.0"):
|
||||
instrumental_output_path = os.path.join(settings.output_folder_path,
|
||||
process_data.basename + " [Instrumental].mp3")
|
||||
convert_wav_to_mp3(process_data.process_data_paths.instrumental_audio_file_path, instrumental_output_path)
|
||||
vocals_output_path = os.path.join(settings.output_folder_path, process_data.basename + " [Vocals].mp3")
|
||||
convert_wav_to_mp3(process_data.process_data_paths.vocals_audio_file_path, vocals_output_path)
|
||||
|
||||
# Create Ultrastar txt
|
||||
if not settings.ignore_audio:
|
||||
ultrastar_file_output = create_ultrastar_txt_from_automation(
|
||||
process_data.basename,
|
||||
settings.output_folder_path,
|
||||
process_data.midi_segments,
|
||||
process_data.media_info
|
||||
)
|
||||
else:
|
||||
ultrastar_file_output = create_ultrastar_txt_from_ultrastar_data(
|
||||
settings.output_folder_path, process_data.media_info.title, process_data.midi_segments
|
||||
)
|
||||
|
||||
# Calc Points
|
||||
simple_score = None
|
||||
accurate_score = None
|
||||
if settings.calculate_score:
|
||||
ultrastar_class, simple_score, accurate_score = calculate_score_points(
|
||||
process_data, ultrastar_file_output
|
||||
)
|
||||
|
||||
# Add calculated score to Ultrastar txt
|
||||
#Todo: Missing Karaoke
|
||||
ultrastar_writer.add_score_to_ultrastar_txt(ultrastar_file_output, simple_score)
|
||||
return accurate_score, simple_score, ultrastar_file_output
|
||||
|
||||
|
||||
def CreateProcessAudio(process_data) -> str:
|
||||
# Set processing audio to cache file
|
||||
process_data.process_data_paths.processing_audio_path = os.path.join(
|
||||
process_data.process_data_paths.cache_folder_path, process_data.basename + ".wav"
|
||||
@@ -271,23 +304,17 @@ def run() -> tuple[str, Score, Score]:
|
||||
|
||||
# Separate vocal from audio
|
||||
audio_separation_folder_path = separate_vocal_from_audio(
|
||||
process_data.process_data_paths.cache_folder_path, process_data.process_data_paths.audio_output_file_path,
|
||||
process_data.process_data_paths.processing_audio_path
|
||||
process_data.process_data_paths.cache_folder_path,
|
||||
process_data.process_data_paths.audio_output_file_path,
|
||||
process_data.process_data_paths.processing_audio_path,
|
||||
settings.use_separated_vocal,
|
||||
settings.create_karaoke,
|
||||
settings.pytorch_device,
|
||||
settings.skip_cache_vocal_separation
|
||||
)
|
||||
process_data.process_data_paths.vocals_audio_file_path = os.path.join(audio_separation_folder_path, "vocals.wav")
|
||||
process_data.process_data_paths.instrumental_audio_file_path = os.path.join(audio_separation_folder_path, "no_vocals.wav")
|
||||
|
||||
# Move instrumental and vocals
|
||||
# Todo: this is ultraStar specific
|
||||
if settings.create_karaoke and version.parse(settings.format_version) < version.parse("1.1.0"):
|
||||
karaoke_output_path = os.path.join(settings.output_folder_path, process_data.basename + " [Karaoke].mp3")
|
||||
convert_wav_to_mp3(process_data.process_data_paths.instrumental_audio_file_path, karaoke_output_path)
|
||||
|
||||
if version.parse(settings.format_version) >= version.parse("1.1.0"):
|
||||
instrumental_output_path = os.path.join(settings.output_folder_path, process_data.basename + " [Instrumental].mp3")
|
||||
convert_wav_to_mp3(process_data.process_data_paths.instrumental_audio_file_path, instrumental_output_path)
|
||||
vocals_output_path = os.path.join(settings.output_folder_path, process_data.basename + " [Vocals].mp3")
|
||||
convert_wav_to_mp3(process_data.process_data_paths.vocals_audio_file_path, vocals_output_path)
|
||||
process_data.process_data_paths.instrumental_audio_file_path = os.path.join(audio_separation_folder_path,
|
||||
"no_vocals.wav")
|
||||
|
||||
if settings.use_separated_vocal:
|
||||
input_path = process_data.process_data_paths.vocals_audio_file_path
|
||||
@@ -298,7 +325,7 @@ def run() -> tuple[str, Score, Score]:
|
||||
denoised_output_path = os.path.join(
|
||||
process_data.process_data_paths.cache_folder_path, process_data.basename + "_denoised.wav"
|
||||
)
|
||||
denoise_vocal_audio(input_path, denoised_output_path)
|
||||
denoise_vocal_audio(input_path, denoised_output_path, settings.skip_cache_denoise_vocal_audio)
|
||||
|
||||
# Convert to mono audio
|
||||
mono_output_path = os.path.join(
|
||||
@@ -313,159 +340,7 @@ def run() -> tuple[str, Score, Score]:
|
||||
mute_no_singing_parts(mono_output_path, mute_output_path)
|
||||
|
||||
# Define the audio file to process
|
||||
process_data.process_data_paths.processing_audio_path = mute_output_path
|
||||
|
||||
# Audio transcription
|
||||
process_data.media_info.language = settings.language
|
||||
if not settings.ignore_audio:
|
||||
transcription_result = transcribe_audio(process_data.process_data_paths.cache_folder_path, process_data.process_data_paths.processing_audio_path)
|
||||
if process_data.media_info.language is None:
|
||||
process_data.media_info.language = transcription_result.detected_language
|
||||
|
||||
process_data.transcribed_data = transcription_result.transcribed_data
|
||||
remove_unecessary_punctuations(process_data.transcribed_data)
|
||||
|
||||
if settings.hyphenation:
|
||||
hyphen_words = hyphenate_each_word(process_data.media_info.language, process_data.transcribed_data)
|
||||
if hyphen_words is not None:
|
||||
process_data.transcribed_data = add_hyphen_to_data(process_data.transcribed_data, hyphen_words)
|
||||
|
||||
process_data.transcribed_data = remove_silence_from_transcription_data(
|
||||
process_data.process_data_paths.processing_audio_path, process_data.transcribed_data
|
||||
)
|
||||
|
||||
# todo: do we need to correct words?
|
||||
# lyric = 'input/faber_lyric.txt'
|
||||
# --corrected_words = correct_words(vosk_speech, lyric)
|
||||
|
||||
# Create audio chunks
|
||||
# Fixme
|
||||
if settings.create_audio_chunks:
|
||||
create_audio_chunks(
|
||||
process_data.process_data_paths.cache_folder_path,
|
||||
process_data.transcribed_data,
|
||||
process_data.process_data_paths.audio_output_file_path,
|
||||
ultrastar_class,
|
||||
)
|
||||
|
||||
# Pitch the audio
|
||||
process_data.pitched_data = pitch_audio(process_data.process_data_paths)
|
||||
|
||||
# Todo: to function
|
||||
start_times = []
|
||||
end_times = []
|
||||
words = []
|
||||
if not settings.ignore_audio:
|
||||
|
||||
for i, midi_segment in enumerate(process_data.transcribed_data):
|
||||
start_times.append(midi_segment.start)
|
||||
end_times.append(midi_segment.end)
|
||||
words.append(midi_segment.word)
|
||||
process_data.midi_segments = create_midi_notes_from_pitched_data(start_times, end_times, words, process_data.pitched_data)
|
||||
# Todo: this is also in converter?
|
||||
# else:
|
||||
# for i, note_lines in enumerate(ultrastar_class.UltrastarNoteLines):
|
||||
# start_times.append(note_lines.startTime)
|
||||
# end_times.append(note_lines.endTime)
|
||||
# words.append(note_lines.word)
|
||||
# midi_segments = create_midi_notes_from_pitched_data(start_times, end_times, words, process_data.pitched_data)
|
||||
|
||||
# fixme: whats that?
|
||||
new_transcribed_data = []
|
||||
for i, midi_segment in enumerate(process_data.midi_segments):
|
||||
transcribed_midi_data = TranscribedData(word=midi_segment.word, start=midi_segment.start, end=midi_segment.end, is_hyphen= None, confidence= 1)
|
||||
new_transcribed_data.append(transcribed_midi_data)
|
||||
process_data.transcribed_data = new_transcribed_data
|
||||
# ----
|
||||
|
||||
# Create plot
|
||||
if settings.create_plot:
|
||||
plot_spectrogram(process_data.process_data_paths.vocals_audio_file_path, settings.output_folder_path, "vocals.wav")
|
||||
plot_spectrogram(process_data.process_data_paths.processing_audio_path, settings.output_folder_path, "processing audio")
|
||||
plot(process_data.pitched_data, settings.output_folder_path, process_data.midi_segments)
|
||||
|
||||
# Write Ultrastar txt
|
||||
if not settings.ignore_audio:
|
||||
real_bpm, ultrastar_file_output = create_ultrastar_txt_from_automation(
|
||||
process_data.basename,
|
||||
settings.output_folder_path,
|
||||
process_data.transcribed_data,
|
||||
process_data.process_data_paths.audio_output_file_path,
|
||||
process_data.midi_segments,
|
||||
process_data.media_info
|
||||
)
|
||||
else:
|
||||
ultrastar_file_output = create_ultrastar_txt_from_ultrastar_data(
|
||||
settings.output_folder_path, process_data.media_info.title, process_data.midi_segments
|
||||
)
|
||||
|
||||
# Calc Points
|
||||
# Todo: UltraStar specific
|
||||
simple_score = None
|
||||
accurate_score = None
|
||||
if settings.calculate_score:
|
||||
ultrastar_class, simple_score, accurate_score = calculate_score_points(
|
||||
process_data, ultrastar_file_output
|
||||
)
|
||||
|
||||
# Add calculated score to Ultrastar txt #Todo: Missing Karaoke
|
||||
ultrastar_writer.add_score_to_ultrastar_txt(ultrastar_file_output, simple_score)
|
||||
|
||||
# Midi
|
||||
if settings.create_midi:
|
||||
create_midi_file(process_data.media_info.bpm, settings.output_folder_path, ultrastar_class, process_data.basename)
|
||||
|
||||
# Sheet music
|
||||
create_sheet(process_data.midi_segments, settings, process_data.basename, process_data.media_info, process_data.media_info.bpm)
|
||||
|
||||
# Cleanup
|
||||
if not settings.keep_cache:
|
||||
remove_cache_folder(process_data.process_data_paths.cache_folder_path)
|
||||
|
||||
# Print Support
|
||||
print_support()
|
||||
return ultrastar_file_output, simple_score, accurate_score
|
||||
|
||||
|
||||
def mute_no_singing_parts(mono_output_path, mute_output_path):
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} Mute audio parts with no singing"
|
||||
)
|
||||
silence_sections = get_silence_sections(mono_output_path)
|
||||
y, sr = librosa.load(mono_output_path, sr=None)
|
||||
# Mute the parts of the audio with no singing
|
||||
for i in silence_sections:
|
||||
# Define the time range to mute
|
||||
|
||||
start_time = i[0] # Start time in seconds
|
||||
end_time = i[1] # End time in seconds
|
||||
|
||||
# Convert time to sample indices
|
||||
start_sample = int(start_time * sr)
|
||||
end_sample = int(end_time * sr)
|
||||
|
||||
y[start_sample:end_sample] = 0
|
||||
sf.write(mute_output_path, y, sr)
|
||||
|
||||
|
||||
def get_unused_song_output_dir(path: str) -> str:
|
||||
"""Get an unused song output dir"""
|
||||
# check if dir exists and add (i) if it does
|
||||
i = 1
|
||||
if os_helper.check_if_folder_exists(path):
|
||||
path = f"{path} ({i})"
|
||||
else:
|
||||
return path
|
||||
|
||||
while os_helper.check_if_folder_exists(path):
|
||||
path = path.replace(f"({i - 1})", f"({i})")
|
||||
i += 1
|
||||
if i > 999:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {red_highlighted('Error: Could not create output folder! (999) is the maximum number of tries.')}"
|
||||
)
|
||||
raise ValueError("Could not create output folder! (999) is the maximum number of tries.")
|
||||
return path
|
||||
return mute_output_path
|
||||
|
||||
|
||||
def transcribe_audio(cache_folder_path: str, processing_audio_path: str) -> TranscriptionResult:
|
||||
@@ -497,40 +372,9 @@ def transcribe_audio(cache_folder_path: str, processing_audio_path: str) -> Tran
|
||||
return transcription_result
|
||||
|
||||
|
||||
def separate_vocal_from_audio(cache_folder_path: str, ultrastar_audio_input_path: str, processing_audio_path: str) -> str:
|
||||
"""Separate vocal from audio"""
|
||||
demucs_output_folder = os.path.splitext(
|
||||
os.path.basename(ultrastar_audio_input_path)
|
||||
)[0]
|
||||
audio_separation_path = os.path.join(
|
||||
cache_folder_path, "separated", "htdemucs", demucs_output_folder
|
||||
)
|
||||
|
||||
vocals_path = os.path.join(audio_separation_path, "vocals.wav")
|
||||
instrumental_path = os.path.join(audio_separation_path, "no_vocals.wav")
|
||||
if settings.use_separated_vocal or settings.create_karaoke:
|
||||
cache_available = check_file_exists(vocals_path) and check_file_exists(
|
||||
instrumental_path
|
||||
)
|
||||
if settings.skip_cache_vocal_separation or not cache_available:
|
||||
separate_audio(
|
||||
ultrastar_audio_input_path, cache_folder_path, settings.pytorch_device
|
||||
)
|
||||
else:
|
||||
print(f"{ULTRASINGER_HEAD} {green_highlighted('cache')} reusing cached separated vocals")
|
||||
|
||||
if settings.use_separated_vocal:
|
||||
input_path = vocals_path
|
||||
else:
|
||||
input_path = ultrastar_audio_input_path
|
||||
|
||||
convert_audio_to_mono_wav(input_path, processing_audio_path)
|
||||
|
||||
return audio_separation_path
|
||||
|
||||
def calculate_score_points(
|
||||
processed_data: ProcessData,
|
||||
ultrastar_file_output: str,
|
||||
processed_data: ProcessData,
|
||||
ultrastar_file_output: str,
|
||||
):
|
||||
"""Calculate score points"""
|
||||
if not settings.ignore_audio:
|
||||
@@ -562,9 +406,9 @@ def calculate_score_points(
|
||||
|
||||
|
||||
def create_ultrastar_txt_from_ultrastar_data(
|
||||
song_output: str,
|
||||
title: str,
|
||||
midi_segments: list[MidiSegment],
|
||||
song_output: str,
|
||||
title: str,
|
||||
midi_segments: list[MidiSegment],
|
||||
) -> str:
|
||||
"""Create Ultrastar txt from Ultrastar data"""
|
||||
output_repitched_ultrastar = os.path.join(
|
||||
@@ -581,67 +425,60 @@ def create_ultrastar_txt_from_ultrastar_data(
|
||||
|
||||
|
||||
def create_ultrastar_txt_from_automation(
|
||||
basename: str,
|
||||
song_output: str,
|
||||
transcribed_data: list[TranscribedData],
|
||||
ultrastar_audio_input_path: str,
|
||||
midi_segments: list[MidiSegment],
|
||||
media_info: MediaInfo
|
||||
basename: str,
|
||||
song_output: str,
|
||||
midi_segments: list[MidiSegment],
|
||||
media_info: MediaInfo
|
||||
):
|
||||
"""Create Ultrastar txt from automation"""
|
||||
ultrastar_header = UltrastarTxtValue()
|
||||
ultrastar_header.version = settings.format_version
|
||||
ultrastar_header.mp3 = basename + ".mp3"
|
||||
ultrastar_header.audio = basename + ".mp3"
|
||||
ultrastar_header.vocals = basename + " [Vocals].mp3"
|
||||
ultrastar_header.instrumental = basename + " [Instrumental].mp3"
|
||||
ultrastar_header.video = basename + ".mp4"
|
||||
ultrastar_header.language = media_info.language
|
||||
print(f"{ULTRASINGER_HEAD} Using UltraStar {blue_highlighted(f'Format Version {settings.format_version}')}")
|
||||
|
||||
ultrastar_txt = UltrastarTxtValue()
|
||||
ultrastar_txt.version = settings.format_version
|
||||
ultrastar_txt.mp3 = basename + ".mp3"
|
||||
ultrastar_txt.audio = basename + ".mp3"
|
||||
ultrastar_txt.vocals = basename + " [Vocals].mp3"
|
||||
ultrastar_txt.instrumental = basename + " [Instrumental].mp3"
|
||||
ultrastar_txt.video = basename + ".mp4"
|
||||
ultrastar_txt.language = media_info.language
|
||||
cover = basename + " [CO].jpg"
|
||||
ultrastar_header.cover = (
|
||||
ultrastar_txt.cover = (
|
||||
cover if os_helper.check_file_exists(os.path.join(song_output, cover)) else None
|
||||
)
|
||||
ultrastar_header.creator = f"{ultrastar_header.creator} {Settings.APP_VERSION}"
|
||||
ultrastar_header.comment = f"{ultrastar_header.comment} {Settings.APP_VERSION}"
|
||||
ultrastar_txt.creator = f"{ultrastar_txt.creator} {Settings.APP_VERSION}"
|
||||
ultrastar_txt.comment = f"{ultrastar_txt.comment} {Settings.APP_VERSION}"
|
||||
|
||||
# Additional data
|
||||
ultrastar_header.title = basename
|
||||
if media_info.title is not None:
|
||||
ultrastar_header.title = media_info.title
|
||||
ultrastar_header.artist = basename
|
||||
if media_info.artist is not None:
|
||||
ultrastar_header.artist = media_info.artist
|
||||
ultrastar_txt.title = media_info.title
|
||||
ultrastar_txt.artist = media_info.artist
|
||||
if media_info.year is not None:
|
||||
ultrastar_header.year = extract_year(media_info.year)
|
||||
ultrastar_txt.year = extract_year(media_info.year)
|
||||
if media_info.genre is not None:
|
||||
ultrastar_header.genre = format_separated_string(media_info.genre)
|
||||
ultrastar_txt.genre = format_separated_string(media_info.genre)
|
||||
|
||||
ultrastar_note_numbers = convert_midi_notes_to_ultrastar_notes(midi_segments)
|
||||
|
||||
real_bpm = get_bpm_from_file(ultrastar_audio_input_path) # todo: Wrong place
|
||||
ultrastar_file_output = os.path.join(song_output, basename + ".txt")
|
||||
ultrastar_writer.create_ultrastar_txt_from_automation(
|
||||
transcribed_data,
|
||||
ultrastar_note_numbers,
|
||||
ultrastar_writer.create_ultrastar_txt(
|
||||
midi_segments,
|
||||
ultrastar_file_output,
|
||||
ultrastar_header,
|
||||
real_bpm,
|
||||
ultrastar_txt,
|
||||
media_info.bpm,
|
||||
)
|
||||
if settings.create_karaoke and version.parse(settings.format_version) < version.parse("1.1.0"):
|
||||
title = basename + " [Karaoke]"
|
||||
ultrastar_header.title = title
|
||||
ultrastar_header.mp3 = title + ".mp3"
|
||||
ultrastar_txt.title = title
|
||||
ultrastar_txt.mp3 = title + ".mp3"
|
||||
karaoke_output_path = os.path.join(song_output, title)
|
||||
karaoke_txt_output_path = karaoke_output_path + ".txt"
|
||||
ultrastar_writer.create_ultrastar_txt_from_automation(
|
||||
transcribed_data,
|
||||
ultrastar_note_numbers,
|
||||
ultrastar_writer.create_ultrastar_txt(
|
||||
midi_segments,
|
||||
karaoke_txt_output_path,
|
||||
ultrastar_header,
|
||||
real_bpm,
|
||||
ultrastar_txt,
|
||||
media_info.bpm,
|
||||
)
|
||||
return real_bpm, ultrastar_file_output
|
||||
return ultrastar_file_output
|
||||
|
||||
|
||||
# Todo: Isnt it MusicBrainz? + Sanitize when parsing UltraStar?
|
||||
def extract_year(date: str) -> str:
|
||||
match = re.search(r'\b\d{4}\b', date)
|
||||
if match:
|
||||
@@ -649,26 +486,6 @@ def extract_year(date: str) -> str:
|
||||
else:
|
||||
return date
|
||||
|
||||
def format_separated_string(data: str) -> str:
|
||||
temp = re.sub(r'[;/]', ',', data)
|
||||
words = temp.split(',')
|
||||
words = [s for s in words if s.strip()]
|
||||
|
||||
for i, word in enumerate(words):
|
||||
if "-" not in word:
|
||||
words[i] = word.strip().capitalize() + ', '
|
||||
else:
|
||||
dash_words = word.split('-')
|
||||
capitalized_dash_words = [dash_word.strip().capitalize() for dash_word in dash_words]
|
||||
formatted_dash_word = '-'.join(capitalized_dash_words) + ', '
|
||||
words[i] = formatted_dash_word
|
||||
|
||||
formatted_string = ''.join(words)
|
||||
|
||||
if formatted_string.endswith(', '):
|
||||
formatted_string = formatted_string[:-2]
|
||||
|
||||
return formatted_string
|
||||
|
||||
def infos_from_audio_input_file() -> tuple[str, str, str, MediaInfo]:
|
||||
"""Infos from audio input file"""
|
||||
@@ -688,6 +505,9 @@ def infos_from_audio_input_file() -> tuple[str, str, str, MediaInfo]:
|
||||
if title_info is not None:
|
||||
title = title_info
|
||||
artist = artist_info
|
||||
else:
|
||||
title = basename_without_ext
|
||||
artist = "Unknown Artist"
|
||||
|
||||
if artist is not None and title is not None:
|
||||
basename_without_ext = f"{artist} - {title}"
|
||||
@@ -704,56 +524,12 @@ def infos_from_audio_input_file() -> tuple[str, str, str, MediaInfo]:
|
||||
)
|
||||
# Todo: Read ID3 tags
|
||||
ultrastar_audio_input_path = os.path.join(song_output, basename)
|
||||
real_bpm = get_bpm_from_file(settings.input_file_path)
|
||||
return (
|
||||
basename_without_ext,
|
||||
song_output,
|
||||
ultrastar_audio_input_path,
|
||||
MediaInfo(artist=artist, title=title, year=year_info, genre=genre_info),
|
||||
)
|
||||
|
||||
|
||||
FILENAME_REPLACEMENTS = (('?:"', ""), ("<", "("), (">", ")"), ("/\\|*", "-"))
|
||||
|
||||
|
||||
def sanitize_filename(fname: str) -> str:
|
||||
"""Sanitize filename"""
|
||||
for old, new in FILENAME_REPLACEMENTS:
|
||||
for char in old:
|
||||
fname = fname.replace(char, new)
|
||||
if fname.endswith("."):
|
||||
fname = fname.rstrip(" .") # Windows does not like trailing periods
|
||||
return fname
|
||||
|
||||
|
||||
def download_from_youtube() -> tuple[str, str, str, MediaInfo]:
|
||||
"""Download from YouTube"""
|
||||
(artist, title) = get_youtube_title(settings.input_file_path)
|
||||
|
||||
# Get additional data for song
|
||||
(title_info, artist_info, year_info, genre_info) = get_music_infos(
|
||||
f"{artist} - {title}"
|
||||
)
|
||||
|
||||
if title_info is not None:
|
||||
title = title_info
|
||||
artist = artist_info
|
||||
|
||||
basename_without_ext = sanitize_filename(f"{artist} - {title}")
|
||||
basename = basename_without_ext + ".mp3"
|
||||
song_output = os.path.join(settings.output_folder_path, basename_without_ext)
|
||||
song_output = get_unused_song_output_dir(song_output)
|
||||
os_helper.create_folder(song_output)
|
||||
download_youtube_audio(settings.input_file_path, basename_without_ext, song_output)
|
||||
download_youtube_video(settings.input_file_path, basename_without_ext, song_output)
|
||||
thumbnail_url = download_youtube_thumbnail(
|
||||
settings.input_file_path, basename_without_ext, song_output
|
||||
)
|
||||
audio_file_path = os.path.join(song_output, basename)
|
||||
return (
|
||||
basename_without_ext,
|
||||
song_output,
|
||||
audio_file_path,
|
||||
MediaInfo(artist=artist, title=title, year=year_info, genre=genre_info, youtube_thumbnail_url=thumbnail_url),
|
||||
MediaInfo(artist=artist, title=title, year=year_info, genre=genre_info, bpm=real_bpm),
|
||||
)
|
||||
|
||||
|
||||
@@ -790,31 +566,9 @@ def parse_ultrastar_txt() -> tuple[str, str, str, UltrastarTxtValue]:
|
||||
)
|
||||
|
||||
|
||||
def create_midi_file(
|
||||
real_bpm: float,
|
||||
song_output: str,
|
||||
midi_segments: list[MidiSegment],
|
||||
basename_without_ext: str,
|
||||
) -> None:
|
||||
"""Create midi file"""
|
||||
print(f"{ULTRASINGER_HEAD} Creating Midi with {blue_highlighted('pretty_midi')}")
|
||||
|
||||
# voice_instrument = [
|
||||
# convert_ultrastar_to_midi_instrument(ultrastar_class)
|
||||
# ]
|
||||
voice_instrument = [
|
||||
create_midi_instrument(midi_segments)
|
||||
]
|
||||
|
||||
midi_output = os.path.join(song_output, f"{basename_without_ext}.mid")
|
||||
instruments_to_midi(voice_instrument, real_bpm, midi_output, midi_segments)
|
||||
|
||||
|
||||
def pitch_audio(
|
||||
process_data_paths: ProcessDataPaths) -> PitchedData:
|
||||
process_data_paths: ProcessDataPaths) -> PitchedData:
|
||||
"""Pitch audio"""
|
||||
# todo: chunk pitching as option?
|
||||
# midi_notes = pitch_each_chunk_with_crepe(chunk_folder_name)
|
||||
|
||||
pitching_config = f"crepe_{settings.ignore_audio}_{settings.crepe_model_capacity}_{settings.crepe_step_size}_{settings.tensorflow_device}"
|
||||
pitched_data_path = os.path.join(process_data_paths.cache_folder_path, f"{pitching_config}.json")
|
||||
@@ -840,45 +594,19 @@ def pitch_audio(
|
||||
return pitched_data
|
||||
|
||||
|
||||
def create_audio_chunks(
|
||||
cache_folder_path: str,
|
||||
transcribed_data: list[TranscribedData],
|
||||
ultrastar_audio_input_path: str,
|
||||
ultrastar_class: UltrastarTxtValue
|
||||
) -> None:
|
||||
"""Create audio chunks"""
|
||||
audio_chunks_path = os.path.join(cache_folder_path, settings.audio_chunk_folder_name)
|
||||
os_helper.create_folder(audio_chunks_path)
|
||||
if not settings.ignore_audio: # and csv
|
||||
csv_filename = os.path.join(audio_chunks_path, "_chunks.csv")
|
||||
export_chunks_from_transcribed_data(
|
||||
process_data.process_data_paths.processing_audio_path, transcribed_data, audio_chunks_path
|
||||
)
|
||||
export_transcribed_data_to_csv(transcribed_data, csv_filename)
|
||||
else:
|
||||
export_chunks_from_ultrastar_data(
|
||||
ultrastar_audio_input_path, ultrastar_class, audio_chunks_path
|
||||
)
|
||||
|
||||
def denoise_vocal_audio(input_path: str, output_path: str) -> None:
|
||||
"""Denoise vocal audio"""
|
||||
cache_available = check_file_exists(output_path)
|
||||
if settings.skip_cache_denoise_vocal_audio or not cache_available:
|
||||
ffmpeg_reduce_noise(input_path, output_path)
|
||||
else:
|
||||
print(f"{ULTRASINGER_HEAD} {green_highlighted('cache')} reusing cached denoised audio")
|
||||
|
||||
def main(argv: list[str]) -> None:
|
||||
"""Main function"""
|
||||
print_version()
|
||||
print_version(settings.APP_VERSION)
|
||||
init_settings(argv)
|
||||
run()
|
||||
sys.exit()
|
||||
|
||||
|
||||
def remove_cache_folder(cache_folder_path: str) -> None:
|
||||
"""Remove cache folder"""
|
||||
os_helper.remove_folder(cache_folder_path)
|
||||
|
||||
|
||||
def init_settings(argv: list[str]) -> None:
|
||||
"""Init settings"""
|
||||
long, short = arg_options()
|
||||
@@ -983,5 +711,6 @@ def arg_options():
|
||||
]
|
||||
return long, short
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
@@ -0,0 +1,18 @@
|
||||
import librosa
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted
|
||||
|
||||
|
||||
def get_bpm_from_data(data, sampling_rate):
|
||||
"""Get real bpm from audio data"""
|
||||
onset_env = librosa.onset.onset_strength(y=data, sr=sampling_rate)
|
||||
wav_tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sampling_rate)
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} BPM is {blue_highlighted(str(round(wav_tempo[0], 2)))}")
|
||||
return wav_tempo[0]
|
||||
|
||||
|
||||
def get_bpm_from_file(wav_file: str) -> float:
|
||||
"""Get real bpm from audio file"""
|
||||
data, sampling_rate = librosa.load(wav_file, sr=None)
|
||||
return get_bpm_from_data(data, sampling_rate)
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
import ffmpeg
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted
|
||||
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, green_highlighted
|
||||
from modules.os_helper import check_file_exists
|
||||
|
||||
|
||||
def ffmpeg_reduce_noise(input_file_path: str, output_file: str) -> None:
|
||||
def __ffmpeg_reduce_noise(input_file_path: str, output_file: str) -> None:
|
||||
"""Reduce noise from vocal audio with ffmpeg."""
|
||||
|
||||
# Denoise audio samples with FFT.
|
||||
@@ -33,3 +34,12 @@ def ffmpeg_reduce_noise(input_file_path: str, output_file: str) -> None:
|
||||
print("ffmpeg stdout:", ffmpeg_exception.stdout.decode("utf8"))
|
||||
print("ffmpeg stderr:", ffmpeg_exception.stderr.decode("utf8"))
|
||||
raise ffmpeg_exception
|
||||
|
||||
|
||||
def denoise_vocal_audio(input_path: str, output_path: str, skip_cache: bool = False) -> None:
|
||||
"""Denoise vocal audio"""
|
||||
cache_available = check_file_exists(output_path)
|
||||
if skip_cache or not cache_available:
|
||||
__ffmpeg_reduce_noise(input_path, output_path)
|
||||
else:
|
||||
print(f"{ULTRASINGER_HEAD} {green_highlighted('cache')} reusing cached denoised audio")
|
||||
@@ -1,16 +1,17 @@
|
||||
"""Separate vocals from audio"""
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
|
||||
import demucs.separate
|
||||
|
||||
from modules.Audio.convert_audio import convert_audio_to_mono_wav
|
||||
|
||||
from modules.console_colors import (
|
||||
ULTRASINGER_HEAD,
|
||||
blue_highlighted,
|
||||
red_highlighted,
|
||||
red_highlighted, green_highlighted,
|
||||
)
|
||||
from modules.os_helper import current_executor_path, move, path_join
|
||||
from modules.os_helper import check_file_exists
|
||||
|
||||
|
||||
def separate_audio(input_file_path: str, output_folder: str, device="cpu") -> None:
|
||||
@@ -20,4 +21,34 @@ def separate_audio(input_file_path: str, output_folder: str, device="cpu") -> No
|
||||
f"{ULTRASINGER_HEAD} Separating vocals from audio with {blue_highlighted('demucs')} and {red_highlighted(device)} as worker."
|
||||
)
|
||||
|
||||
demucs.separate.main(shlex.split(f'--two-stems vocals -d {device} --float32 --out "{os.path.join(output_folder, "separated")}" "{input_file_path}"'))
|
||||
demucs.separate.main(shlex.split(f'--two-stems vocals -d {device} --float32 --out "{os.path.join(output_folder, "separated")}" "{input_file_path}"'))
|
||||
|
||||
|
||||
def separate_vocal_from_audio(cache_folder_path: str,
|
||||
ultrastar_audio_input_path: str,
|
||||
processing_audio_path: str,
|
||||
use_separated_vocal: bool,
|
||||
create_karaoke: bool,
|
||||
pytorch_device: str,
|
||||
skip_cache: bool = False) -> str:
|
||||
"""Separate vocal from audio"""
|
||||
demucs_output_folder = os.path.splitext(os.path.basename(ultrastar_audio_input_path))[0]
|
||||
audio_separation_path = os.path.join(cache_folder_path, "separated", "htdemucs", demucs_output_folder)
|
||||
|
||||
vocals_path = os.path.join(audio_separation_path, "vocals.wav")
|
||||
instrumental_path = os.path.join(audio_separation_path, "no_vocals.wav")
|
||||
if use_separated_vocal or create_karaoke:
|
||||
cache_available = check_file_exists(vocals_path) and check_file_exists(instrumental_path)
|
||||
if skip_cache or not cache_available:
|
||||
separate_audio(ultrastar_audio_input_path, cache_folder_path, pytorch_device)
|
||||
else:
|
||||
print(f"{ULTRASINGER_HEAD} {green_highlighted('cache')} reusing cached separated vocals")
|
||||
|
||||
if use_separated_vocal:
|
||||
input_path = vocals_path
|
||||
else:
|
||||
input_path = ultrastar_audio_input_path
|
||||
|
||||
convert_audio_to_mono_wav(input_path, processing_audio_path)
|
||||
|
||||
return audio_separation_path
|
||||
@@ -1,4 +1,6 @@
|
||||
"""Silence processing module"""
|
||||
import librosa
|
||||
import soundfile as sf
|
||||
|
||||
from pydub import AudioSegment, silence
|
||||
|
||||
@@ -117,4 +119,25 @@ def remove_silence(silence_parts_list: list[tuple[float, float]], transcribed_da
|
||||
if silence_start > origin_end:
|
||||
# Nothing to do with this word anymore, go to next word
|
||||
break
|
||||
return new_transcribed_data
|
||||
return new_transcribed_data
|
||||
|
||||
|
||||
def mute_no_singing_parts(mono_output_path, mute_output_path):
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} Mute audio parts with no singing"
|
||||
)
|
||||
silence_sections = get_silence_sections(mono_output_path)
|
||||
y, sr = librosa.load(mono_output_path, sr=None)
|
||||
# Mute the parts of the audio with no singing
|
||||
for i in silence_sections:
|
||||
# Define the time range to mute
|
||||
|
||||
start_time = i[0] # Start time in seconds
|
||||
end_time = i[1] # End time in seconds
|
||||
|
||||
# Convert time to sample indices
|
||||
start_sample = int(start_time * sr)
|
||||
end_sample = int(end_time * sr)
|
||||
|
||||
y[start_sample:end_sample] = 0
|
||||
sf.write(mute_output_path, y, sr)
|
||||
@@ -4,7 +4,12 @@ import os
|
||||
import re
|
||||
import wave
|
||||
|
||||
from modules import os_helper
|
||||
from modules.ProcessData import ProcessDataPaths
|
||||
from modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD
|
||||
from modules.csv_handler import export_transcribed_data_to_csv
|
||||
from modules.os_helper import create_folder
|
||||
from modules.Ultrastar.ultrastar_converter import (
|
||||
get_end_time_from_ultrastar,
|
||||
@@ -17,8 +22,8 @@ class AudioManipulation:
|
||||
"""Docstring"""
|
||||
|
||||
|
||||
def export_chunks_from_transcribed_data(
|
||||
audio_filename: str, transcribed_data: [], output_folder_name: str
|
||||
def __export_chunks_from_transcribed_data(
|
||||
audio_filename: str, transcribed_data: [], output_folder_name: str
|
||||
) -> None:
|
||||
"""Export transcribed_data as vocal chunks wav files"""
|
||||
print(
|
||||
@@ -32,16 +37,16 @@ def export_chunks_from_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 = __get_chunk(end_byte, start_byte, wave_file)
|
||||
__export_chunk_to_wav_file(
|
||||
chunk, output_folder_name, i, data.word, wave_file
|
||||
)
|
||||
|
||||
wave_file.close()
|
||||
|
||||
|
||||
def export_chunks_from_ultrastar_data(
|
||||
audio_filename: str, ultrastar_data: UltrastarTxtValue, folder_name: str
|
||||
def __export_chunks_from_ultrastar_data(
|
||||
audio_filename: str, ultrastar_data: UltrastarTxtValue, folder_name: str
|
||||
) -> None:
|
||||
"""Export ultrastar data as vocal chunks wav files"""
|
||||
print(f"{ULTRASINGER_HEAD} Export Ultrastar data as vocal chunks wav files")
|
||||
@@ -51,33 +56,33 @@ 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, word in enumerate(ultrastar_data.words):
|
||||
for i, note_line in enumerate(ultrastar_data.UltrastarNoteLines):
|
||||
start_time = get_start_time_from_ultrastar(ultrastar_data, i)
|
||||
end_time = get_end_time_from_ultrastar(ultrastar_data, i)
|
||||
|
||||
start_byte = int(start_time * sample_rate * n_channels)
|
||||
end_byte = int(end_time * sample_rate * n_channels)
|
||||
|
||||
chunk = get_chunk(end_byte, start_byte, wave_file)
|
||||
export_chunk_to_wav_file(
|
||||
chunk, folder_name, i, word, wave_file
|
||||
chunk = __get_chunk(end_byte, start_byte, wave_file)
|
||||
__export_chunk_to_wav_file(
|
||||
chunk, folder_name, i, note_line.word, wave_file
|
||||
)
|
||||
|
||||
|
||||
def export_chunk_to_wav_file(chunk, folder_name: str, i: int, word: str, wave_file) -> None:
|
||||
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)
|
||||
# todo: Progress?
|
||||
# print(f"{str(i)} {clean_word}")
|
||||
with wave.open(
|
||||
os.path.join(folder_name, f"chunk_{i}_{clean_word}.wav"), "wb"
|
||||
os.path.join(folder_name, f"chunk_{i}_{clean_word}.wav"), "wb"
|
||||
) as chunk_file:
|
||||
chunk_file.setparams(wave_file.getparams())
|
||||
chunk_file.writeframes(chunk)
|
||||
|
||||
|
||||
def get_chunk(end_byte: int, start_byte: int, 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.
|
||||
@@ -87,3 +92,35 @@ def get_chunk(end_byte: int, start_byte: int, wave_file):
|
||||
wave_file.setpos(start_byte) # ({:.2f})
|
||||
chunk = wave_file.readframes(end_byte - start_byte)
|
||||
return chunk
|
||||
|
||||
|
||||
def __get_chunk_path(cache_folder_path: str) -> str:
|
||||
audio_chunks_path = os.path.join(cache_folder_path, "audio-chunks")
|
||||
os_helper.create_folder(audio_chunks_path)
|
||||
return audio_chunks_path
|
||||
|
||||
|
||||
def create_audio_chunks_from_transcribed_data(
|
||||
process_data_paths: ProcessDataPaths,
|
||||
transcribed_data: list[TranscribedData],
|
||||
) -> None:
|
||||
"""Create audio chunks from transcribed data."""
|
||||
audio_chunks_path = __get_chunk_path(process_data_paths.cache_folder_path)
|
||||
|
||||
csv_filename = os.path.join(audio_chunks_path, "_chunks.csv")
|
||||
__export_chunks_from_transcribed_data(
|
||||
process_data_paths.processing_audio_path, transcribed_data, audio_chunks_path
|
||||
)
|
||||
export_transcribed_data_to_csv(transcribed_data, csv_filename)
|
||||
|
||||
|
||||
def create_audio_chunks_from_ultrastar_data(
|
||||
process_data_paths: ProcessDataPaths,
|
||||
ultrastar_class: UltrastarTxtValue
|
||||
) -> None:
|
||||
"""Create audio chunks from Ultrastar data."""
|
||||
audio_chunks_path = __get_chunk_path(process_data_paths.cache_folder_path)
|
||||
|
||||
__export_chunks_from_ultrastar_data(
|
||||
process_data_paths.audio_output_file_path, ultrastar_class, audio_chunks_path
|
||||
)
|
||||
@@ -6,8 +6,13 @@ import os
|
||||
import yt_dlp
|
||||
from PIL import Image
|
||||
|
||||
from modules.os_helper import sanitize_filename, get_unused_song_output_dir
|
||||
from modules import os_helper
|
||||
from modules.ProcessData import MediaInfo
|
||||
from modules.Audio.bpm import get_bpm_from_file
|
||||
from modules.console_colors import ULTRASINGER_HEAD
|
||||
from modules.Image.image_helper import crop_image_to_square
|
||||
from modules.musicbrainz_client import get_music_infos
|
||||
|
||||
|
||||
def get_youtube_title(url: str) -> tuple[str, str]:
|
||||
@@ -26,7 +31,7 @@ def get_youtube_title(url: str) -> tuple[str, str]:
|
||||
return result["channel"].strip(), result["title"].strip()
|
||||
|
||||
|
||||
def download_youtube_audio(url: str, clear_filename: str, output_path: str):
|
||||
def __download_youtube_audio(url: str, clear_filename: str, output_path: str):
|
||||
"""Download audio from YouTube"""
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} Downloading Audio")
|
||||
@@ -38,10 +43,10 @@ def download_youtube_audio(url: str, clear_filename: str, output_path: str):
|
||||
],
|
||||
}
|
||||
|
||||
start_download(ydl_opts, url)
|
||||
__start_download(ydl_opts, url)
|
||||
|
||||
|
||||
def download_youtube_thumbnail(url: str, clear_filename: str, output_path: str) -> str:
|
||||
def __download_youtube_thumbnail(url: str, clear_filename: str, output_path: str) -> str:
|
||||
"""Download thumbnail from YouTube"""
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} Downloading thumbnail")
|
||||
@@ -73,7 +78,7 @@ def download_and_convert_thumbnail(ydl_opts, url: str, clear_filename: str, outp
|
||||
return ""
|
||||
|
||||
|
||||
def download_youtube_video(url: str, clear_filename: str, output_path: str) -> None:
|
||||
def __download_youtube_video(url: str, clear_filename: str, output_path: str) -> None:
|
||||
"""Download video from YouTube"""
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} Downloading Video")
|
||||
@@ -81,13 +86,47 @@ def download_youtube_video(url: str, clear_filename: str, output_path: str) -> N
|
||||
"format": "bestvideo[ext=mp4]/mp4",
|
||||
"outtmpl": output_path + "/" + clear_filename + ".mp4",
|
||||
}
|
||||
start_download(ydl_opts, url)
|
||||
__start_download(ydl_opts, url)
|
||||
|
||||
|
||||
def start_download(ydl_opts, url: str) -> None:
|
||||
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)
|
||||
if errors:
|
||||
raise Exception("Download failed with error: " + str(errors))
|
||||
|
||||
|
||||
def download_from_youtube(input_file_path: str, output_folder_path: str) -> tuple[str, str, str, MediaInfo]:
|
||||
"""Download from YouTube"""
|
||||
(artist, title) = get_youtube_title(input_file_path)
|
||||
|
||||
# Get additional data for song
|
||||
(title_info, artist_info, year_info, genre_info) = get_music_infos(
|
||||
f"{artist} - {title}"
|
||||
)
|
||||
|
||||
if title_info is not None:
|
||||
title = title_info
|
||||
artist = artist_info
|
||||
|
||||
basename_without_ext = sanitize_filename(f"{artist} - {title}")
|
||||
basename = basename_without_ext + ".mp3"
|
||||
song_output = os.path.join(output_folder_path, basename_without_ext)
|
||||
song_output = get_unused_song_output_dir(song_output)
|
||||
os_helper.create_folder(song_output)
|
||||
__download_youtube_audio(input_file_path, basename_without_ext, song_output)
|
||||
__download_youtube_video(input_file_path, basename_without_ext, song_output)
|
||||
thumbnail_url = __download_youtube_thumbnail(
|
||||
input_file_path, basename_without_ext, song_output
|
||||
)
|
||||
audio_file_path = os.path.join(song_output, basename)
|
||||
real_bpm = get_bpm_from_file(input_file_path)
|
||||
return (
|
||||
basename_without_ext,
|
||||
song_output,
|
||||
audio_file_path,
|
||||
MediaInfo(artist=artist, title=title, year=year_info, genre=genre_info, bpm=real_bpm,
|
||||
youtube_thumbnail_url=thumbnail_url),
|
||||
)
|
||||
@@ -1,23 +1,26 @@
|
||||
"""Midi creator module"""
|
||||
|
||||
import math
|
||||
import os
|
||||
from collections import Counter
|
||||
|
||||
import librosa
|
||||
import numpy as np
|
||||
import pretty_midi
|
||||
import unidecode
|
||||
|
||||
from modules.Midi.MidiSegment import MidiSegment
|
||||
from modules.Pitcher.pitcher import get_frequencies_with_high_confidence
|
||||
from modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from modules.Ultrastar.ultrastar_converter import (
|
||||
midi_note_to_ultrastar_note,
|
||||
ultrastar_note_to_midi_note
|
||||
)
|
||||
from modules.console_colors import (
|
||||
ULTRASINGER_HEAD,
|
||||
ULTRASINGER_HEAD, blue_highlighted,
|
||||
)
|
||||
from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarNoteLine
|
||||
from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue
|
||||
from modules.Pitcher.pitched_data import PitchedData
|
||||
from modules.Pitcher.pitched_data_helper import get_frequencies_with_high_confidence
|
||||
|
||||
|
||||
def create_midi_instrument(midi_segments: list[MidiSegment]) -> object:
|
||||
@@ -29,7 +32,7 @@ def create_midi_instrument(midi_segments: list[MidiSegment]) -> object:
|
||||
velocity = 100
|
||||
|
||||
for i, midi_segment in enumerate(midi_segments):
|
||||
note = pretty_midi.Note(velocity, midi_segment.note, midi_segment.start, midi_segment.end)
|
||||
note = pretty_midi.Note(velocity, librosa.note_to_midi(midi_segment.note), midi_segment.start, midi_segment.end)
|
||||
instrument.notes.append(note)
|
||||
|
||||
return instrument
|
||||
@@ -50,14 +53,22 @@ def convert_ultrastar_to_midi_instrument(ultrastar_class: UltrastarTxtValue) ->
|
||||
return instrument
|
||||
|
||||
|
||||
def instruments_to_midi(instruments: list[object], bpm: float, midi_output: str, midi_segments: list[MidiSegment]) -> None:
|
||||
def sanitize_for_midi(text):
|
||||
"""
|
||||
Sanitize text for MIDI compatibility.
|
||||
Uses unidecode to approximate characters to ASCII.
|
||||
"""
|
||||
return unidecode.unidecode(text)
|
||||
|
||||
def __create_midi(instruments: list[object], bpm: float, midi_output: str, midi_segments: list[MidiSegment]) -> None:
|
||||
"""Write instruments to midi file"""
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} Creating midi file -> {midi_output}")
|
||||
|
||||
midi_data = pretty_midi.PrettyMIDI(initial_tempo=bpm)
|
||||
for i, midi_segment in enumerate(midi_segments):
|
||||
midi_data.lyrics.append(pretty_midi.Lyric(text=midi_segment.word, time=midi_segment.start))
|
||||
sanitized_word = sanitize_for_midi(midi_segment.word)
|
||||
midi_data.lyrics.append(pretty_midi.Lyric(text=sanitized_word, time=midi_segment.start))
|
||||
for instrument in instruments:
|
||||
midi_data.instruments.append(instrument)
|
||||
midi_data.write(midi_output)
|
||||
@@ -137,14 +148,63 @@ def convert_midi_notes_to_ultrastar_notes(midi_segments: list[MidiSegment]) -> l
|
||||
print(f"{ULTRASINGER_HEAD} Creating Ultrastar notes from midi data")
|
||||
|
||||
ultrastar_note_numbers = []
|
||||
for i, data in enumerate(midi_segments):
|
||||
note_number_librosa = librosa.note_to_midi(data.note)
|
||||
pitch = midi_note_to_ultrastar_note(
|
||||
note_number_librosa
|
||||
)
|
||||
ultrastar_note_numbers.append(pitch)
|
||||
for i, midi_segment in enumerate(midi_segments):
|
||||
ultrastar_note = convert_midi_note_to_ultrastar_note(midi_segment)
|
||||
ultrastar_note_numbers.append(ultrastar_note)
|
||||
# todo: Progress?
|
||||
# print(
|
||||
# f"Note: {midi_notes[i]} midi_note: {str(note_number_librosa)} pitch: {str(pitch)}"
|
||||
# )
|
||||
return ultrastar_note_numbers
|
||||
return ultrastar_note_numbers
|
||||
|
||||
def convert_midi_note_to_ultrastar_note(midi_segment: MidiSegment) -> int:
|
||||
"""Convert midi notes to ultrastar notes"""
|
||||
|
||||
note_number_librosa = librosa.note_to_midi(midi_segment.note)
|
||||
ultrastar_note = midi_note_to_ultrastar_note(note_number_librosa)
|
||||
return ultrastar_note
|
||||
|
||||
|
||||
def create_midi_segments_from_transcribed_data(transcribed_data: list[TranscribedData], pitched_data: PitchedData) -> list[MidiSegment]:
|
||||
start_times = []
|
||||
end_times = []
|
||||
words = []
|
||||
|
||||
if transcribed_data:
|
||||
for i, midi_segment in enumerate(transcribed_data):
|
||||
start_times.append(midi_segment.start)
|
||||
end_times.append(midi_segment.end)
|
||||
words.append(midi_segment.word)
|
||||
midi_segments = create_midi_notes_from_pitched_data(start_times, end_times, words,
|
||||
pitched_data)
|
||||
return midi_segments
|
||||
|
||||
|
||||
def create_repitched_midi_segments_from_ultrastar_txt(pitched_data: PitchedData, ultrastar_txt: UltrastarTxtValue) -> list[MidiSegment]:
|
||||
start_times = []
|
||||
end_times = []
|
||||
words = []
|
||||
|
||||
for i, note_lines in enumerate(ultrastar_txt.UltrastarNoteLines):
|
||||
start_times.append(note_lines.startTime)
|
||||
end_times.append(note_lines.endTime)
|
||||
words.append(note_lines.word)
|
||||
midi_segments = create_midi_notes_from_pitched_data(start_times, end_times, words, pitched_data)
|
||||
return midi_segments
|
||||
|
||||
|
||||
def create_midi_file(
|
||||
real_bpm: float,
|
||||
song_output: str,
|
||||
midi_segments: list[MidiSegment],
|
||||
basename_without_ext: str,
|
||||
) -> None:
|
||||
"""Create midi file"""
|
||||
print(f"{ULTRASINGER_HEAD} Creating Midi with {blue_highlighted('pretty_midi')}")
|
||||
|
||||
voice_instrument = [
|
||||
create_midi_instrument(midi_segments)
|
||||
]
|
||||
|
||||
midi_output = os.path.join(song_output, f"{basename_without_ext}.mid")
|
||||
__create_midi(voice_instrument, real_bpm, midi_output, midi_segments)
|
||||
@@ -0,0 +1,12 @@
|
||||
"""Pitched data helper module"""
|
||||
def get_frequencies_with_high_confidence(
|
||||
frequencies: list[float], confidences: list[float], threshold=0.4
|
||||
) -> list[float]:
|
||||
"""Get frequency with high confidence"""
|
||||
conf_f = []
|
||||
for i, conf in enumerate(confidences):
|
||||
if conf > threshold:
|
||||
conf_f.append(frequencies[i])
|
||||
if not conf_f:
|
||||
conf_f = frequencies
|
||||
return conf_f
|
||||
@@ -1,10 +1,13 @@
|
||||
"""Pitcher module"""
|
||||
import os
|
||||
|
||||
import crepe
|
||||
from scipy.io import wavfile
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted
|
||||
from modules.Midi.midi_creator import convert_frequencies_to_notes, most_frequent
|
||||
from modules.Pitcher.pitched_data import PitchedData
|
||||
from modules.Pitcher.pitched_data_helper import get_frequencies_with_high_confidence
|
||||
|
||||
|
||||
def get_pitch_with_crepe_file(
|
||||
@@ -50,19 +53,39 @@ def get_pitched_data_with_high_confidence(
|
||||
|
||||
return new_pitched_data
|
||||
|
||||
# Todo: Unused
|
||||
def pitch_each_chunk_with_crepe(directory: str,
|
||||
crepe_model_capacity: str,
|
||||
crepe_step_size: int,
|
||||
tensorflow_device: str) -> list[str]:
|
||||
"""Pitch each chunk with crepe and return midi notes"""
|
||||
print(f"{ULTRASINGER_HEAD} Pitching each chunk with {blue_highlighted('crepe')}")
|
||||
|
||||
def get_frequencies_with_high_confidence(
|
||||
frequencies: list[float], confidences: list[float], threshold=0.4
|
||||
) -> list[float]:
|
||||
"""Get frequency with high confidence"""
|
||||
conf_f = []
|
||||
for i, conf in enumerate(confidences):
|
||||
if conf > threshold:
|
||||
conf_f.append(frequencies[i])
|
||||
if not conf_f:
|
||||
conf_f = frequencies
|
||||
return conf_f
|
||||
midi_notes = []
|
||||
for filename in sorted(
|
||||
[f for f in os.listdir(directory) if f.endswith(".wav")],
|
||||
key=lambda x: int(x.split("_")[1]),
|
||||
):
|
||||
filepath = os.path.join(directory, filename)
|
||||
# todo: stepsize = duration? then when shorter than "it" it should take the duration. Otherwise there a more notes
|
||||
pitched_data = get_pitch_with_crepe_file(
|
||||
filepath,
|
||||
crepe_model_capacity,
|
||||
crepe_step_size,
|
||||
tensorflow_device,
|
||||
)
|
||||
conf_f = get_frequencies_with_high_confidence(
|
||||
pitched_data.frequencies, pitched_data.confidence
|
||||
)
|
||||
|
||||
notes = convert_frequencies_to_notes(conf_f)
|
||||
note = most_frequent(notes)[0][0]
|
||||
|
||||
midi_notes.append(note)
|
||||
# todo: Progress?
|
||||
# print(filename + " f: " + str(mean))
|
||||
|
||||
return midi_notes
|
||||
|
||||
class Pitcher:
|
||||
"""Docstring"""
|
||||
@@ -18,9 +18,9 @@ class ProcessDataPaths:
|
||||
@dataclass
|
||||
class MediaInfo:
|
||||
"""Media Info"""
|
||||
title: Optional[str] = None
|
||||
artist: Optional[str] = None
|
||||
bpm: Optional[float] = None
|
||||
title: str
|
||||
artist: str
|
||||
bpm: float
|
||||
year: Optional[str] = None
|
||||
genre: Optional[str] = None
|
||||
language: Optional[str] = None
|
||||
@@ -42,14 +42,16 @@ def from_ultrastar_txt(ultrastar_txt: UltrastarTxtValue) -> ProcessData:
|
||||
"""Converts an Ultrastar txt to ProcessData"""
|
||||
process_data = ProcessData()
|
||||
process_data.parsed_file = ultrastar_txt
|
||||
# todo: is this the real bpm? or calculate it from file?
|
||||
real_bpm = ultrastar_converter.ultrastar_bpm_to_real_bpm(float(ultrastar_txt.bpm.replace(",", ".")))
|
||||
process_data.media_info = MediaInfo(
|
||||
title=ultrastar_txt.title,
|
||||
artist=ultrastar_txt.artist,
|
||||
year=ultrastar_txt.year,
|
||||
genre=ultrastar_txt.genre,
|
||||
language=ultrastar_txt.language
|
||||
language=ultrastar_txt.language,
|
||||
bpm=real_bpm
|
||||
)
|
||||
process_data.media_info.bpm = ultrastar_converter.ultrastar_bpm_to_real_bpm(float(ultrastar_txt.bpm.replace(",", ".")))
|
||||
process_data.midi_segments = ultrastar_converter.ultrastar_to_midi_segments(ultrastar_txt)
|
||||
|
||||
return process_data
|
||||
@@ -3,10 +3,12 @@
|
||||
import string
|
||||
|
||||
from hyphen import Hyphenator, dictools
|
||||
from tqdm import tqdm
|
||||
|
||||
from modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from modules.console_colors import (
|
||||
ULTRASINGER_HEAD,
|
||||
blue_highlighted,
|
||||
blue_highlighted, red_highlighted,
|
||||
)
|
||||
|
||||
def language_check(language="en") -> str | None:
|
||||
@@ -52,7 +54,7 @@ def contains_punctuation(word: str) -> bool:
|
||||
return any(elem in word for elem in string.punctuation)
|
||||
|
||||
|
||||
def clean_word(word: str):
|
||||
def __clean_word(word: str):
|
||||
"""Remove punctuation from word"""
|
||||
cleaned_string = ""
|
||||
removed_indices = []
|
||||
@@ -66,7 +68,7 @@ def clean_word(word: str):
|
||||
return cleaned_string, removed_indices, removed_symbols
|
||||
|
||||
|
||||
def insert_removed_symbols(separated_array, removed_indices, symbols):
|
||||
def __insert_removed_symbols(separated_array, removed_indices, symbols):
|
||||
"""Insert symbols into the syllables"""
|
||||
result = []
|
||||
symbol_index = 0
|
||||
@@ -94,7 +96,7 @@ def insert_removed_symbols(separated_array, removed_indices, symbols):
|
||||
return result
|
||||
|
||||
|
||||
def create_hyphenator(lang_region: str) -> Hyphenator:
|
||||
def __create_hyphenator(lang_region: str) -> Hyphenator:
|
||||
"""Create hyphenator"""
|
||||
hyphenator = Hyphenator(lang_region)
|
||||
return hyphenator
|
||||
@@ -103,7 +105,7 @@ def create_hyphenator(lang_region: str) -> Hyphenator:
|
||||
def hyphenation(word: str, hyphenator: Hyphenator) -> list[str] | None:
|
||||
"""Hyphenate word"""
|
||||
|
||||
cleaned_string, removed_indices, removed_symbols = clean_word(word)
|
||||
cleaned_string, removed_indices, removed_symbols = __clean_word(word)
|
||||
|
||||
# Hyphenation of word longer than 100 characters throws exception
|
||||
if len(cleaned_string) > 100:
|
||||
@@ -116,8 +118,36 @@ def hyphenation(word: str, hyphenator: Hyphenator) -> list[str] | None:
|
||||
hyphen = []
|
||||
for i in range(length):
|
||||
hyphen.append(syllabus[i])
|
||||
hyphen = insert_removed_symbols(hyphen, removed_indices, removed_symbols)
|
||||
hyphen = __insert_removed_symbols(hyphen, removed_indices, removed_symbols)
|
||||
else:
|
||||
hyphen = None
|
||||
|
||||
return hyphen
|
||||
|
||||
|
||||
def hyphenate_each_word(
|
||||
language: str, transcribed_data: list[TranscribedData]
|
||||
) -> list[list[str]] | None:
|
||||
"""Hyphenate each word in the transcribed data."""
|
||||
lang_region = language_check(language)
|
||||
if lang_region is None:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {red_highlighted('Error in hyphenation for language ')} {blue_highlighted(language)}{red_highlighted(', maybe you want to disable it?')}"
|
||||
)
|
||||
return None
|
||||
|
||||
hyphenated_word = []
|
||||
try:
|
||||
hyphenator = __create_hyphenator(lang_region)
|
||||
for i in tqdm(enumerate(transcribed_data)):
|
||||
pos = i[0]
|
||||
hyphenated_word.append(
|
||||
hyphenation(transcribed_data[pos].word, hyphenator)
|
||||
)
|
||||
except Exception as e:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {red_highlighted('Error in hyphenation for language ')} {blue_highlighted(language)}{red_highlighted(', maybe you want to disable it?')}")
|
||||
print(f"\t{red_highlighted(f'->{e}')}")
|
||||
return None
|
||||
|
||||
return hyphenated_word
|
||||
@@ -2,6 +2,8 @@
|
||||
import librosa
|
||||
from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue
|
||||
from modules.Midi.MidiSegment import MidiSegment
|
||||
|
||||
|
||||
def real_bpm_to_ultrastar_bpm(real_bpm: float) -> float:
|
||||
"""Converts real BPM to UltraStar BPM"""
|
||||
# The UltraStar BPM info is a fourth beat of the real BPM
|
||||
@@ -51,38 +53,53 @@ def ultrastar_note_to_midi_note(ultrastar_note: int) -> int:
|
||||
def get_start_time_from_ultrastar(ultrastar_class: UltrastarTxtValue, pos: int) -> float:
|
||||
"""Calculates the start time from the Ultrastar txt"""
|
||||
|
||||
start_time = get_start_time(ultrastar_class.gap, ultrastar_class.bpm, ultrastar_class.UltrastarNoteLines[pos].startBeat)
|
||||
start_time = get_start_time(ultrastar_class.gap, ultrastar_class.bpm,
|
||||
ultrastar_class.UltrastarNoteLines[pos].startBeat)
|
||||
return start_time
|
||||
|
||||
|
||||
def get_start_time(gap: str, ultrastar_bpm: str, startBeat: float) -> float:
|
||||
"""Calculates the start time from the Ultrastar txt"""
|
||||
|
||||
gap = int(float(gap.replace(",", ".")) / 1000)
|
||||
real_bpm = ultrastar_bpm_to_real_bpm(float(ultrastar_bpm.replace(",", ".")))
|
||||
gap = __convert_gap(gap)
|
||||
real_bpm = __convert_bpm(ultrastar_bpm)
|
||||
start_time = beat_to_second(int(startBeat), real_bpm) + gap
|
||||
return start_time
|
||||
|
||||
|
||||
def get_end_time_from_ultrastar(ultrastar_class: UltrastarTxtValue, pos: int) -> float:
|
||||
"""Calculates the end time from the Ultrastar txt"""
|
||||
|
||||
end_time = get_end_time(ultrastar_class.gap, ultrastar_class.bpm, ultrastar_class.UltrastarNoteLines[pos].startBeat, ultrastar_class.UltrastarNoteLines[pos].duration)
|
||||
end_time = get_end_time(ultrastar_class.gap, ultrastar_class.bpm, ultrastar_class.UltrastarNoteLines[pos].startBeat,
|
||||
ultrastar_class.UltrastarNoteLines[pos].duration)
|
||||
return end_time
|
||||
|
||||
|
||||
def get_end_time(gap: str, ultrastar_bpm: str, startBeat: float, duration: float) -> float:
|
||||
"""Calculates the end time from the Ultrastar txt"""
|
||||
|
||||
gap = int(float(gap.replace(",", ".")) / 1000)
|
||||
real_bpm = ultrastar_bpm_to_real_bpm(float(ultrastar_bpm.replace(",", ".")))
|
||||
gap = __convert_gap(gap)
|
||||
real_bpm = __convert_bpm(ultrastar_bpm)
|
||||
end_time = (
|
||||
beat_to_second(
|
||||
int(startBeat) + int(duration),
|
||||
real_bpm,
|
||||
)
|
||||
+ gap
|
||||
beat_to_second(
|
||||
int(startBeat) + int(duration),
|
||||
real_bpm,
|
||||
)
|
||||
+ gap
|
||||
)
|
||||
return end_time
|
||||
|
||||
|
||||
def __convert_gap(gap: str) -> float:
|
||||
gap = float(gap.replace(",", ".")) / 1000
|
||||
return gap
|
||||
|
||||
|
||||
def __convert_bpm(ultrastar_bpm: str) -> float:
|
||||
real_bpm = ultrastar_bpm_to_real_bpm(float(ultrastar_bpm.replace(",", ".")))
|
||||
return real_bpm
|
||||
|
||||
|
||||
def ultrastar_to_midi_segments(ultrastar_txt: UltrastarTxtValue) -> list[MidiSegment]:
|
||||
"""Converts an Ultrastar txt to Midi segments"""
|
||||
midi_segments = []
|
||||
@@ -91,9 +108,9 @@ def ultrastar_to_midi_segments(ultrastar_txt: UltrastarTxtValue) -> list[MidiSeg
|
||||
end_time = get_end_time_from_ultrastar(ultrastar_txt, i)
|
||||
midi_segments.append(
|
||||
MidiSegment(librosa.midi_to_note(ultrastar_note_to_midi_note(data.pitch)),
|
||||
start_time,
|
||||
end_time,
|
||||
data.word,
|
||||
)
|
||||
start_time,
|
||||
end_time,
|
||||
data.word,
|
||||
)
|
||||
)
|
||||
return midi_segments
|
||||
return midi_segments
|
||||
@@ -98,6 +98,7 @@ def calculate_score(pitched_data: PitchedData, ultrastar_class: UltrastarTxtValu
|
||||
accurate_points = Points()
|
||||
|
||||
reachable_line_bonus_per_word = MAX_SONG_LINE_BONUS / len(ultrastar_class.UltrastarNoteLines)
|
||||
step_size = 0.09 # Todo: Whats is the step size of the game? Its not 1/bps -> one beat in seconds s = 60/bpm
|
||||
|
||||
for i, note_line in enumerate(ultrastar_class.UltrastarNoteLines):
|
||||
if note_line.word == "":
|
||||
@@ -109,7 +110,6 @@ def calculate_score(pitched_data: PitchedData, ultrastar_class: UltrastarTxtValu
|
||||
start_time = get_start_time_from_ultrastar(ultrastar_class, i)
|
||||
end_time = get_end_time_from_ultrastar(ultrastar_class, i)
|
||||
duration = end_time - start_time
|
||||
step_size = 0.09 # Todo: Whats is the step size of the game? Its not 1/bps -> one beat in seconds s = 60/bpm
|
||||
parts = int(duration / step_size)
|
||||
parts = 1 if parts == 0 else parts
|
||||
|
||||
|
||||
@@ -4,15 +4,16 @@ import re
|
||||
import langcodes
|
||||
from packaging import version
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted
|
||||
from modules.console_colors import ULTRASINGER_HEAD
|
||||
from modules.Ultrastar.ultrastar_converter import (
|
||||
real_bpm_to_ultrastar_bpm,
|
||||
second_to_beat,
|
||||
)
|
||||
from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue, UltrastarTxtTag, UltrastarTxtNoteTypeTag, \
|
||||
FILE_ENCODING
|
||||
from modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from modules.Ultrastar.ultrastar_score_calculator import Score
|
||||
from modules.Midi.MidiSegment import MidiSegment
|
||||
from modules.Midi.midi_creator import convert_midi_note_to_ultrastar_note
|
||||
|
||||
|
||||
def get_thirtytwo_note_second(real_bpm: float):
|
||||
@@ -65,25 +66,22 @@ def get_language_name(language: str) -> str:
|
||||
return langcodes.Language.make(language=language).display_name()
|
||||
|
||||
|
||||
def create_ultrastar_txt_from_automation(
|
||||
transcribed_data: list[TranscribedData],
|
||||
note_numbers: list[int],
|
||||
def create_ultrastar_txt(
|
||||
midi_segments: list[MidiSegment],
|
||||
ultrastar_file_output: str,
|
||||
ultrastar_class: UltrastarTxtValue,
|
||||
real_bpm=120,
|
||||
) -> None:
|
||||
real_bpm: float) -> None:
|
||||
"""Creates an Ultrastar txt file from the automation data"""
|
||||
|
||||
print(f"{ULTRASINGER_HEAD} Creating UltraStar file {ultrastar_file_output}")
|
||||
print(f"{ULTRASINGER_HEAD} Using UltraStar {blue_highlighted(f'Format Version {ultrastar_class.version}')}")
|
||||
|
||||
ultrastar_bpm = real_bpm_to_ultrastar_bpm(real_bpm)
|
||||
multiplication = get_multiplier(ultrastar_bpm)
|
||||
ultrastar_bpm = ultrastar_bpm * get_multiplier(ultrastar_bpm)
|
||||
silence_split_duration = calculate_silent_beat_length(transcribed_data)
|
||||
silence_split_duration = calculate_silent_beat_length(midi_segments)
|
||||
|
||||
with open(ultrastar_file_output, "w", encoding=FILE_ENCODING) as file:
|
||||
gap = transcribed_data[0].start
|
||||
gap = midi_segments[0].start
|
||||
|
||||
if version.parse(ultrastar_class.version) >= version.parse("1.0.0"):
|
||||
file.write(f"#{UltrastarTxtTag.VERSION}:{ultrastar_class.version}\n"),
|
||||
@@ -121,10 +119,10 @@ def create_ultrastar_txt_from_automation(
|
||||
previous_end_beat = 0
|
||||
separated_word_silence = [] # This is a workaround for separated words that get his ends to far away
|
||||
|
||||
for i, data in enumerate(transcribed_data):
|
||||
start_time = (data.start - gap) * multiplication
|
||||
for i, midi_segment in enumerate(midi_segments):
|
||||
start_time = (midi_segment.start - gap) * multiplication
|
||||
end_time = (
|
||||
data.end - data.start
|
||||
midi_segment.end - midi_segment.start
|
||||
) * multiplication
|
||||
start_beat = round(second_to_beat(start_time, real_bpm))
|
||||
duration = round(second_to_beat(end_time, real_bpm))
|
||||
@@ -134,8 +132,8 @@ def create_ultrastar_txt_from_automation(
|
||||
previous_end_beat = start_beat + duration
|
||||
|
||||
# Calculate the silence between the words
|
||||
if i < len(transcribed_data) - 1:
|
||||
silence = (transcribed_data[i + 1].start - data.end)
|
||||
if i < len(midi_segments) - 1:
|
||||
silence = (midi_segments[i + 1].start - midi_segment.end)
|
||||
else:
|
||||
silence = 0
|
||||
|
||||
@@ -148,23 +146,24 @@ def create_ultrastar_txt_from_automation(
|
||||
line = f"{UltrastarTxtNoteTypeTag.NORMAL} " \
|
||||
f"{str(start_beat)} " \
|
||||
f"{str(duration)} " \
|
||||
f"{str(note_numbers[i])} " \
|
||||
f"{data.word}\n"
|
||||
f"{str(convert_midi_note_to_ultrastar_note(midi_segment))} " \
|
||||
f"{midi_segment.word}\n"
|
||||
|
||||
file.write(line)
|
||||
|
||||
# detect silence between words
|
||||
if not transcribed_data[i].is_word_end:
|
||||
#if not transcribed_data[i].is_word_end:
|
||||
if not midi_segment.word.endswith(" "):
|
||||
separated_word_silence.append(silence)
|
||||
continue
|
||||
|
||||
if silence_split_duration != 0 and silence > silence_split_duration or any(
|
||||
s > silence_split_duration for s in separated_word_silence) and i != len(transcribed_data) - 1:
|
||||
s > silence_split_duration for s in separated_word_silence) and i != len(midi_segments) - 1:
|
||||
# - 10
|
||||
# '-' end of current sing part
|
||||
# 'n1' show next at time in real beat
|
||||
show_next = (
|
||||
second_to_beat(data.end - gap, real_bpm)
|
||||
second_to_beat(midi_segment.end - gap, real_bpm)
|
||||
* multiplication
|
||||
)
|
||||
linebreak = f"{UltrastarTxtTag.LINEBREAK} " \
|
||||
@@ -192,13 +191,13 @@ def deviation(silence_parts):
|
||||
return mean
|
||||
|
||||
|
||||
def calculate_silent_beat_length(transcribed_data: list[TranscribedData]):
|
||||
def calculate_silent_beat_length(midi_segments: list[MidiSegment]):
|
||||
print(f"{ULTRASINGER_HEAD} Calculating silence parts for linebreaks.")
|
||||
|
||||
silent_parts = []
|
||||
for i, data in enumerate(transcribed_data):
|
||||
if i < len(transcribed_data) - 1:
|
||||
silent_parts.append(transcribed_data[i + 1].start - data.end)
|
||||
for i, data in enumerate(midi_segments):
|
||||
if i < len(midi_segments) - 1:
|
||||
silent_parts.append(midi_segments[i + 1].start - data.end)
|
||||
|
||||
return deviation(silent_parts)
|
||||
|
||||
@@ -208,9 +207,7 @@ def create_repitched_txt_from_ultrastar_data(
|
||||
) -> None:
|
||||
"""Creates a repitched ultrastar txt file from the original one"""
|
||||
# todo: just add '_repitched' to input_file
|
||||
print(
|
||||
"{PRINT_ULTRASTAR} Creating repitched ultrastar txt -> {input_file}_repitch.txt"
|
||||
)
|
||||
print(f"{ULTRASINGER_HEAD} Creating repitched ultrastar txt -> {input_file}_repitch.txt")
|
||||
|
||||
# todo: to reader
|
||||
with open(input_file, "r", encoding=FILE_ENCODING) as file:
|
||||
@@ -269,3 +266,25 @@ def add_score_to_ultrastar_txt(ultrastar_file_output: str, score: Score) -> None
|
||||
|
||||
class UltraStarWriter:
|
||||
"""Docstring"""
|
||||
|
||||
|
||||
def format_separated_string(data: str) -> str:
|
||||
temp = re.sub(r'[;/]', ',', data)
|
||||
words = temp.split(',')
|
||||
words = [s for s in words if s.strip()]
|
||||
|
||||
for i, word in enumerate(words):
|
||||
if "-" not in word:
|
||||
words[i] = word.strip().capitalize() + ', '
|
||||
else:
|
||||
dash_words = word.split('-')
|
||||
capitalized_dash_words = [dash_word.strip().capitalize() for dash_word in dash_words]
|
||||
formatted_dash_word = '-'.join(capitalized_dash_words) + ', '
|
||||
words[i] = formatted_dash_word
|
||||
|
||||
formatted_string = ''.join(words)
|
||||
|
||||
if formatted_string.endswith(', '):
|
||||
formatted_string = formatted_string[:-2]
|
||||
|
||||
return formatted_string
|
||||
@@ -1,3 +1,5 @@
|
||||
"""Common Prints"""
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD, gold_highlighted, light_blue_highlighted
|
||||
|
||||
|
||||
@@ -73,3 +75,11 @@ def print_support() -> None:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {gold_highlighted('This will help a lot to keep this project alive and improved.')}"
|
||||
)
|
||||
|
||||
|
||||
def print_version(app_version: str) -> None:
|
||||
"""Print version text"""
|
||||
print()
|
||||
print(f"{ULTRASINGER_HEAD} {gold_highlighted('*****************************')}")
|
||||
print(f"{ULTRASINGER_HEAD} {gold_highlighted('UltraSinger Version:')} {light_blue_highlighted(app_version)}")
|
||||
print(f"{ULTRASINGER_HEAD} {gold_highlighted('*****************************')}")
|
||||
@@ -3,7 +3,7 @@
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from modules.console_colors import ULTRASINGER_HEAD
|
||||
from modules.console_colors import ULTRASINGER_HEAD, red_highlighted
|
||||
|
||||
|
||||
def create_folder(folder_name: str) -> None:
|
||||
@@ -51,4 +51,37 @@ def check_if_folder_exists(song_output: str) -> bool:
|
||||
|
||||
def remove_folder(cache_path):
|
||||
"""Removes a folder."""
|
||||
shutil.rmtree(cache_path)
|
||||
shutil.rmtree(cache_path)
|
||||
|
||||
|
||||
FILENAME_REPLACEMENTS = (('?:"', ""), ("<", "("), (">", ")"), ("/\\|*", "-"))
|
||||
|
||||
|
||||
def sanitize_filename(fname: str) -> str:
|
||||
"""Sanitize filename"""
|
||||
for old, new in FILENAME_REPLACEMENTS:
|
||||
for char in old:
|
||||
fname = fname.replace(char, new)
|
||||
if fname.endswith("."):
|
||||
fname = fname.rstrip(" .") # Windows does not like trailing periods
|
||||
return fname
|
||||
|
||||
|
||||
def get_unused_song_output_dir(path: str) -> str:
|
||||
"""Get an unused song output dir"""
|
||||
# check if dir exists and add (i) if it does
|
||||
i = 1
|
||||
if check_if_folder_exists(path):
|
||||
path = f"{path} ({i})"
|
||||
else:
|
||||
return path
|
||||
|
||||
while check_if_folder_exists(path):
|
||||
path = path.replace(f"({i - 1})", f"({i})")
|
||||
i += 1
|
||||
if i > 999:
|
||||
print(
|
||||
f"{ULTRASINGER_HEAD} {red_highlighted('Error: Could not create output folder! (999) is the maximum number of tries.')}"
|
||||
)
|
||||
raise ValueError("Could not create output folder! (999) is the maximum number of tries.")
|
||||
return path
|
||||
+31
-25
@@ -8,11 +8,10 @@ import numpy
|
||||
from matplotlib import pyplot as plt
|
||||
from matplotlib.patches import Rectangle
|
||||
|
||||
from modules.Ultrastar.ultrastar_txt import UltrastarTxtValue
|
||||
from modules.ProcessData import ProcessData
|
||||
from modules.console_colors import ULTRASINGER_HEAD
|
||||
from modules.Pitcher.pitched_data import PitchedData
|
||||
from modules.Pitcher.pitcher import get_pitched_data_with_high_confidence
|
||||
from modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from modules.Midi.MidiSegment import MidiSegment
|
||||
|
||||
|
||||
@@ -31,14 +30,14 @@ OCTAVES = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
||||
X_TICK_SIZE = 5
|
||||
|
||||
|
||||
def get_frequency_range(midi_note: str) -> float:
|
||||
def __get_frequency_range(midi_note: str) -> float:
|
||||
"""Get frequency range"""
|
||||
midi = librosa.note_to_midi(midi_note)
|
||||
frequency_range = librosa.midi_to_hz(midi + 1) - librosa.midi_to_hz(midi)
|
||||
return frequency_range
|
||||
|
||||
|
||||
def create_plot_notes(notes: list[str], octaves: list[int]) -> list[PlottedNote]:
|
||||
def __create_plot_notes(notes: list[str], octaves: list[int]) -> list[PlottedNote]:
|
||||
"""Create list of notes for plot y axis"""
|
||||
plotted_notes = []
|
||||
for octave in octaves:
|
||||
@@ -53,7 +52,7 @@ def create_plot_notes(notes: list[str], octaves: list[int]) -> list[PlottedNote]
|
||||
return plotted_notes
|
||||
|
||||
|
||||
PLOTTED_NOTES = create_plot_notes(NOTES, OCTAVES)
|
||||
PLOTTED_NOTES = __create_plot_notes(NOTES, OCTAVES)
|
||||
|
||||
|
||||
def plot(
|
||||
@@ -81,11 +80,11 @@ def plot(
|
||||
frequencies_log_10 = numpy.log10(pitched_data.frequencies)
|
||||
|
||||
# add 'nan' where there are gaps for frequency values so the graph is only continuous where it should be
|
||||
pitched_data_with_gaps = create_gaps(pitched_data, step_size)
|
||||
pitched_data_with_gaps = __create_gaps(pitched_data, step_size)
|
||||
frequencies_log_10_with_gaps = numpy.log10(pitched_data_with_gaps.frequencies)
|
||||
|
||||
# dynamically set the minimum and maximum values for x and y axes based on data
|
||||
y_lower_bound, y_upper_bound = determine_bounds(frequencies_log_10)
|
||||
y_lower_bound, y_upper_bound = __determine_bounds(frequencies_log_10)
|
||||
ymin = max(0, y_lower_bound - 0.05)
|
||||
ymax = y_upper_bound + 0.05
|
||||
plt.ylim(ymin, ymax)
|
||||
@@ -96,7 +95,7 @@ def plot(
|
||||
plt.xlabel("Time (s)")
|
||||
plt.ylabel("log10 of Frequency (Hz)")
|
||||
|
||||
notes_within_range = set_axes_ticks_and_labels(pitched_data.times, ymin, ymax)
|
||||
notes_within_range = __set_axes_ticks_and_labels(pitched_data.times, ymin, ymax)
|
||||
|
||||
# draw horizontal lines for each note
|
||||
for note in notes_within_range:
|
||||
@@ -118,9 +117,9 @@ def plot(
|
||||
)
|
||||
plt.figure(1).colorbar(scatter_path_collection, label="confidence")
|
||||
|
||||
set_figure_dimensions(xmax - xmin, y_upper_bound - y_lower_bound)
|
||||
__set_figure_dimensions(xmax - xmin, y_upper_bound - y_lower_bound)
|
||||
|
||||
plot_words(midi_segments)
|
||||
__plot_words(midi_segments)
|
||||
|
||||
if title is not None:
|
||||
plt.title(label=title)
|
||||
@@ -130,7 +129,7 @@ def plot(
|
||||
dpi = 200
|
||||
plt.savefig(
|
||||
os.path.join(
|
||||
output_path, f"plot{'' if title is None else '_' + snake(title)}.svg"
|
||||
output_path, f"plot{'' if title is None else '_' + __snake(title)}.svg"
|
||||
),
|
||||
dpi=dpi,
|
||||
)
|
||||
@@ -138,7 +137,7 @@ def plot(
|
||||
plt.cla()
|
||||
|
||||
|
||||
def set_axes_ticks_and_labels(confidence, ymin, ymax):
|
||||
def __set_axes_ticks_and_labels(confidence, ymin, ymax):
|
||||
"""Set ticks and their labels for x and y axes"""
|
||||
notes_within_range = [
|
||||
x for x in PLOTTED_NOTES if ymin <= x.frequency_log_10 <= ymax
|
||||
@@ -165,7 +164,7 @@ def set_axes_ticks_and_labels(confidence, ymin, ymax):
|
||||
return notes_within_range
|
||||
|
||||
|
||||
def determine_bounds(frequency_log_10: list[float]) -> tuple[float, float]:
|
||||
def __determine_bounds(frequency_log_10: list[float]) -> tuple[float, float]:
|
||||
"""Determine bounds based on 1st and 99th percentile of data"""
|
||||
lower = numpy.percentile(numpy.array(frequency_log_10), 1)
|
||||
upper = numpy.percentile(numpy.array(frequency_log_10), 99)
|
||||
@@ -173,7 +172,7 @@ def determine_bounds(frequency_log_10: list[float]) -> tuple[float, float]:
|
||||
return lower, upper
|
||||
|
||||
|
||||
def set_figure_dimensions(time_range, frequency_log_10_range):
|
||||
def __set_figure_dimensions(time_range, frequency_log_10_range):
|
||||
"""Dynamically scale the figure dimensions based on the duration/frequency amplitude of the song"""
|
||||
height = frequency_log_10_range / 0.06
|
||||
width = time_range / 2
|
||||
@@ -182,7 +181,7 @@ def set_figure_dimensions(time_range, frequency_log_10_range):
|
||||
plt.figure(1).set_figheight(max(4, height))
|
||||
|
||||
|
||||
def create_gaps(pitched_data: PitchedData, step_size: float) -> PitchedData:
|
||||
def __create_gaps(pitched_data: PitchedData, step_size: float) -> PitchedData:
|
||||
"""
|
||||
Add 'nan' where there are no high confidence frequency values.
|
||||
This way the graph is only continuous where it should be.
|
||||
@@ -211,9 +210,9 @@ def create_gaps(pitched_data: PitchedData, step_size: float) -> PitchedData:
|
||||
return pitched_data_with_gaps
|
||||
|
||||
|
||||
def plot_word(midi_note: str, start, end, word):
|
||||
def __plot_word(midi_note: str, start, end, word):
|
||||
note_frequency = librosa.note_to_hz(midi_note)
|
||||
frequency_range = get_frequency_range(midi_note)
|
||||
frequency_range = __get_frequency_range(midi_note)
|
||||
|
||||
half_frequency_range = frequency_range / 2
|
||||
height = (
|
||||
@@ -237,14 +236,14 @@ def plot_word(midi_note: str, start, end, word):
|
||||
plt.text(start + width / 4, numpy.log10([note_frequency + half_frequency_range])[0], word, rotation=90)
|
||||
|
||||
|
||||
def plot_words(midi_segments: list[MidiSegment]):
|
||||
def __plot_words(midi_segments: list[MidiSegment]):
|
||||
"""Draw rectangles for each word"""
|
||||
if midi_segments is not None:
|
||||
for i, midi_segment in enumerate(midi_segments):
|
||||
plot_word(midi_segment.note, midi_segment.start, midi_segment.end, midi_segment.word)
|
||||
__plot_word(midi_segment.note, midi_segment.start, midi_segment.end, midi_segment.word)
|
||||
|
||||
|
||||
def snake(s):
|
||||
def __snake(s):
|
||||
"""Turn any string into a snake case string"""
|
||||
return "_".join(
|
||||
sub(
|
||||
@@ -253,11 +252,11 @@ def snake(s):
|
||||
).lower()
|
||||
|
||||
|
||||
def plot_spectrogram(audio_seperation_path: str,
|
||||
output_path: str,
|
||||
title: str = "Spectrogram",
|
||||
def __plot_spectrogram(audio_seperation_path: str,
|
||||
output_path: str,
|
||||
title: str = "Spectrogram",
|
||||
|
||||
) -> None:
|
||||
) -> None:
|
||||
"""Plot spectrogram of data"""
|
||||
|
||||
print(
|
||||
@@ -289,9 +288,16 @@ def plot_spectrogram(audio_seperation_path: str,
|
||||
dpi = 200
|
||||
plt.savefig(
|
||||
os.path.join(
|
||||
output_path, f"plot{'_' + snake(title)}.svg"
|
||||
output_path, f"plot{'_' + __snake(title)}.svg"
|
||||
),
|
||||
dpi=dpi,
|
||||
)
|
||||
plt.clf()
|
||||
plt.cla()
|
||||
|
||||
|
||||
def create_plots(process_data: ProcessData, output_folder_path: str) -> None:
|
||||
__plot_spectrogram(process_data.process_data_paths.vocals_audio_file_path, output_folder_path, "vocals.wav")
|
||||
__plot_spectrogram(process_data.process_data_paths.processing_audio_path, output_folder_path,
|
||||
"processing audio")
|
||||
plot(process_data.pitched_data, output_folder_path, process_data.midi_segments)
|
||||
@@ -32,19 +32,20 @@ def add_midi_segments_to_stream(stream, midi_segments: list[MidiSegment]):
|
||||
|
||||
|
||||
def create_sheet(midi_segments: list[MidiSegment],
|
||||
settings,
|
||||
output_folder_path: str,
|
||||
cache_folder_path: str,
|
||||
musescore_path: str,
|
||||
filename: str,
|
||||
media_info: MediaInfo,
|
||||
bpm: float,):
|
||||
media_info: MediaInfo):
|
||||
print(f"{ULTRASINGER_HEAD} Creating music sheet with {blue_highlighted('MuseScore')}")
|
||||
success = set_environment_variables(settings.musescore_path)
|
||||
success = set_environment_variables(musescore_path)
|
||||
if not success:
|
||||
return
|
||||
s = stream.Stream()
|
||||
add_metadata_to_stream(s, media_info.artist, media_info.title, int(bpm))
|
||||
add_metadata_to_stream(s, media_info.artist, media_info.title, int(media_info.bpm))
|
||||
add_midi_segments_to_stream(s, midi_segments)
|
||||
export_stream_to_pdf(s, os.path.join(settings.song_output, f"{filename}.pdf"))
|
||||
move(os.path.join(settings.song_output, f"{filename}.musicxml"), os.path.join(settings.cache_path, f"{filename}.musicxml"))
|
||||
export_stream_to_pdf(s, os.path.join(output_folder_path, f"{filename}.pdf"))
|
||||
move(os.path.join(output_folder_path, f"{filename}.musicxml"), os.path.join(cache_folder_path, f"{filename}.musicxml"))
|
||||
|
||||
def round_to_nearest_quarter(number: float) -> float:
|
||||
return round(number * 4) / 4
|
||||
|
||||
Reference in new issue
Block a user