mirror of
https://github.com/rakuri255/UltraSinger.git
synced 2026-11-26 08:58:34 +09:00
Added sheet music generation
This commit is contained in:
1 parent
398027b724
commit
660c8d3acb
5 files changed
+135
-25
No files matched your search
@@ -5,6 +5,8 @@ Date: 2024.06.02
|
||||
- Remove cache folder when finished
|
||||
- Remove audio from yt video
|
||||
- Added install and start scripts
|
||||
- Added Docker support
|
||||
- Added sheet music generation
|
||||
|
||||
# Version: 0.0.10
|
||||
Date: 2024.05.01
|
||||
|
||||
+2
-1
@@ -23,4 +23,5 @@ black~=24.4.2
|
||||
pylint~=3.2.3
|
||||
pytest~=8.2.2
|
||||
protobuf==3.20.*
|
||||
packaging~=24.1
|
||||
packaging~=24.1
|
||||
music21~=9.1.0
|
||||
+3
-1
@@ -1,5 +1,5 @@
|
||||
class Settings:
|
||||
APP_VERSION = "0.0.11-dev4"
|
||||
APP_VERSION = "0.0.11-dev5"
|
||||
|
||||
create_midi = True
|
||||
create_plot = False
|
||||
@@ -12,6 +12,8 @@ class Settings:
|
||||
input_file_path = ""
|
||||
output_file_path = ""
|
||||
processing_audio_path = ""
|
||||
cache_path = ""
|
||||
song_output = ""
|
||||
|
||||
language = None
|
||||
format_version = "1.0.0"
|
||||
|
||||
+27
-23
@@ -60,6 +60,7 @@ from Settings import Settings
|
||||
from modules.Speech_Recognition.TranscribedData import TranscribedData
|
||||
from modules.plot import plot, plot_spectrogram
|
||||
from modules.musicbrainz_client import get_music_infos
|
||||
from modules.sheet import create_sheet
|
||||
|
||||
settings = Settings()
|
||||
|
||||
@@ -295,7 +296,7 @@ def run() -> None:
|
||||
(
|
||||
basename_without_ext,
|
||||
real_bpm,
|
||||
song_output,
|
||||
settings.song_output,
|
||||
ultrastar_audio_input_path,
|
||||
ultrastar_class,
|
||||
) = parse_ultrastar_txt()
|
||||
@@ -305,7 +306,7 @@ def run() -> None:
|
||||
)
|
||||
(
|
||||
basename_without_ext,
|
||||
song_output,
|
||||
settings.song_output,
|
||||
ultrastar_audio_input_path,
|
||||
(title, artist, year, genre)
|
||||
) = download_from_youtube()
|
||||
@@ -315,33 +316,33 @@ def run() -> None:
|
||||
)
|
||||
(
|
||||
basename_without_ext,
|
||||
song_output,
|
||||
settings.song_output,
|
||||
ultrastar_audio_input_path,
|
||||
(title, artist, year, genre)
|
||||
) = infos_from_audio_input_file()
|
||||
|
||||
cache_path = os.path.join(song_output, "cache")
|
||||
settings.cache_path = os.path.join(settings.song_output, "cache")
|
||||
settings.processing_audio_path = os.path.join(
|
||||
cache_path, basename_without_ext + ".wav"
|
||||
settings.cache_path, basename_without_ext + ".wav"
|
||||
)
|
||||
os_helper.create_folder(cache_path)
|
||||
os_helper.create_folder(settings.cache_path)
|
||||
|
||||
# Separate vocal from audio
|
||||
audio_separation_path = separate_vocal_from_audio(
|
||||
basename_without_ext, cache_path, ultrastar_audio_input_path
|
||||
basename_without_ext, settings.cache_path, ultrastar_audio_input_path
|
||||
)
|
||||
vocals_path = os.path.join(audio_separation_path, "vocals.wav")
|
||||
instrumental_path = os.path.join(audio_separation_path, "no_vocals.wav")
|
||||
|
||||
# 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(song_output, basename_without_ext + " [Karaoke].mp3")
|
||||
karaoke_output_path = os.path.join(settings.song_output, basename_without_ext + " [Karaoke].mp3")
|
||||
convert_wav_to_mp3(instrumental_path, karaoke_output_path)
|
||||
|
||||
if version.parse(settings.format_version) >= version.parse("1.1.0"):
|
||||
instrumental_output_path = os.path.join(song_output, basename_without_ext + " [Instrumental].mp3")
|
||||
instrumental_output_path = os.path.join(settings.song_output, basename_without_ext + " [Instrumental].mp3")
|
||||
convert_wav_to_mp3(instrumental_path, instrumental_output_path)
|
||||
vocals_output_path = os.path.join(song_output, basename_without_ext + " [Vocals].mp3")
|
||||
vocals_output_path = os.path.join(settings.song_output, basename_without_ext + " [Vocals].mp3")
|
||||
convert_wav_to_mp3(vocals_path, vocals_output_path)
|
||||
|
||||
if settings.use_separated_vocal:
|
||||
@@ -351,19 +352,19 @@ def run() -> None:
|
||||
|
||||
# Denoise vocal audio
|
||||
denoised_output_path = os.path.join(
|
||||
cache_path, basename_without_ext + "_denoised.wav"
|
||||
settings.cache_path, basename_without_ext + "_denoised.wav"
|
||||
)
|
||||
denoise_vocal_audio(input_path, denoised_output_path)
|
||||
|
||||
# Convert to mono audio
|
||||
mono_output_path = os.path.join(
|
||||
cache_path, basename_without_ext + "_mono.wav"
|
||||
settings.cache_path, basename_without_ext + "_mono.wav"
|
||||
)
|
||||
convert_audio_to_mono_wav(denoised_output_path, mono_output_path)
|
||||
|
||||
# Mute silence sections
|
||||
mute_output_path = os.path.join(
|
||||
cache_path, basename_without_ext + "_mute.wav"
|
||||
settings.cache_path, basename_without_ext + "_mute.wav"
|
||||
)
|
||||
mute_no_singing_parts(mono_output_path, mute_output_path)
|
||||
|
||||
@@ -396,7 +397,7 @@ def run() -> None:
|
||||
# Create audio chunks
|
||||
if settings.create_audio_chunks:
|
||||
create_audio_chunks(
|
||||
cache_path,
|
||||
settings.cache_path,
|
||||
is_audio,
|
||||
transcribed_data,
|
||||
ultrastar_audio_input_path,
|
||||
@@ -411,15 +412,15 @@ def run() -> None:
|
||||
# Create plot
|
||||
if settings.create_plot:
|
||||
vocals_path = os.path.join(audio_separation_path, "vocals.wav")
|
||||
plot_spectrogram(vocals_path, song_output, "vocals.wav")
|
||||
plot_spectrogram(settings.processing_audio_path, song_output, "processing audio")
|
||||
plot(pitched_data, song_output, midi_segments)
|
||||
plot_spectrogram(vocals_path, settings.song_output, "vocals.wav")
|
||||
plot_spectrogram(settings.processing_audio_path, settings.song_output, "processing audio")
|
||||
plot(pitched_data, settings.song_output, midi_segments)
|
||||
|
||||
# Write Ultrastar txt
|
||||
if is_audio:
|
||||
real_bpm, ultrastar_file_output = create_ultrastar_txt_from_automation(
|
||||
basename_without_ext,
|
||||
song_output,
|
||||
settings.song_output,
|
||||
transcribed_data,
|
||||
ultrastar_audio_input_path,
|
||||
ultrastar_note_numbers,
|
||||
@@ -431,7 +432,7 @@ def run() -> None:
|
||||
)
|
||||
else:
|
||||
ultrastar_file_output = create_ultrastar_txt_from_ultrastar_data(
|
||||
song_output, ultrastar_class, ultrastar_note_numbers
|
||||
settings.song_output, ultrastar_class, ultrastar_note_numbers
|
||||
)
|
||||
|
||||
# Calc Points
|
||||
@@ -446,11 +447,14 @@ def run() -> None:
|
||||
|
||||
# Midi
|
||||
if settings.create_midi:
|
||||
create_midi_file(real_bpm, song_output, ultrastar_class, basename_without_ext)
|
||||
create_midi_file(real_bpm, settings.song_output, ultrastar_class, basename_without_ext)
|
||||
|
||||
# Sheet music
|
||||
create_sheet(midi_segments, settings, basename_without_ext, artist, title, real_bpm)
|
||||
|
||||
# Cleanup
|
||||
if not settings.keep_cache:
|
||||
remove_cache_folder(cache_path)
|
||||
remove_cache_folder(settings.cache_path)
|
||||
|
||||
# Print Support
|
||||
print_support()
|
||||
@@ -711,8 +715,8 @@ def infos_from_audio_input_file() -> tuple[str, str, str, tuple[str, str, str, s
|
||||
extension = os.path.splitext(basename)[1]
|
||||
basename = f"{basename_without_ext}{extension}"
|
||||
|
||||
song_output = os.path.join(settings.output_file_path, basename_without_ext)
|
||||
song_output = get_unused_song_output_dir(song_output)
|
||||
settings.song_output = os.path.join(settings.output_file_path, basename_without_ext)
|
||||
song_output = get_unused_song_output_dir(settings.song_output)
|
||||
os_helper.create_folder(song_output)
|
||||
os_helper.copy(settings.input_file_path, song_output)
|
||||
os_helper.rename(os.path.join(song_output, os.path.basename(settings.input_file_path)), os.path.join(song_output, basename))
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import os.path
|
||||
import re
|
||||
|
||||
from music21 import stream, note, duration, environment, metadata, tempo
|
||||
from modules.Midi.midi_creator import MidiSegment
|
||||
from modules.console_colors import ULTRASINGER_HEAD, red_highlighted, blue_highlighted
|
||||
from modules.os_helper import move
|
||||
|
||||
|
||||
def add_metadata_to_stream(stream, artist: str, title: str, bpm: int):
|
||||
stream.metadata = metadata.Metadata()
|
||||
stream.metadata.title = title
|
||||
stream.metadata.composer = artist
|
||||
metronome_mark = tempo.MetronomeMark(number=bpm)
|
||||
stream.insert(0, metronome_mark)
|
||||
|
||||
|
||||
def add_midi_segments_to_stream(stream, midi_segments: MidiSegment):
|
||||
for segment in midi_segments:
|
||||
# Convert the note name to a music21 note
|
||||
m21_note = note.Note(replace_unsupported_accidentals(segment.note))
|
||||
|
||||
# Calculate the note's duration in quarter lengths
|
||||
note_duration = segment.end - segment.start
|
||||
note_quarter = round_to_nearest_quarter(note_duration)
|
||||
if(note_quarter == 0):
|
||||
note_quarter = 0.25
|
||||
|
||||
m21_note.duration = duration.Duration(note_quarter)
|
||||
m21_note.lyrics.append(note.Lyric(text=segment.word))
|
||||
stream.append(m21_note)
|
||||
|
||||
|
||||
def create_sheet(midi_segments: MidiSegment,
|
||||
settings,
|
||||
filename: str,
|
||||
artist: str,
|
||||
title: str,
|
||||
bpm: float,
|
||||
musescore_path: str = None):
|
||||
print(f"{ULTRASINGER_HEAD} Creating music sheet with {blue_highlighted('MuseScore')}")
|
||||
success = set_environment_variables(musescore_path)
|
||||
if not success:
|
||||
return
|
||||
s = stream.Stream()
|
||||
add_metadata_to_stream(s, artist, title, int(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"))
|
||||
|
||||
def round_to_nearest_quarter(number: float) -> float:
|
||||
return round(number * 4) / 4
|
||||
|
||||
|
||||
def find_musescore_version_in_path(path) -> int:
|
||||
pattern = r"MuseScore\s+(\d+)"
|
||||
match = None
|
||||
for i, data in enumerate(os.listdir(path)):
|
||||
match = re.findall(pattern, data)
|
||||
if match:
|
||||
break
|
||||
|
||||
if match:
|
||||
try:
|
||||
version = int(match[0])
|
||||
return version
|
||||
except ValueError:
|
||||
# int cast error
|
||||
return -1
|
||||
else:
|
||||
# MuseScore is not found or version is unknown
|
||||
return -1
|
||||
|
||||
def set_environment_variables(musescorePath=None) -> bool:
|
||||
if(musescorePath is None):
|
||||
version = find_musescore_version_in_path('C:\\Program Files')
|
||||
if(version == -1):
|
||||
print(f"{ULTRASINGER_HEAD} {red_highlighted('MuseScore is not installed or version is unknown')}")
|
||||
return False
|
||||
musescorePath = f'C:\\Program Files\\MuseScore {version}\\bin\\MuseScore{version}.exe'
|
||||
print(f"{ULTRASINGER_HEAD} Using MuseScore version {version} in path {musescorePath}")
|
||||
env = environment.UserSettings()
|
||||
env['musicxmlPath'] = musescorePath
|
||||
env['musescoreDirectPNGPath'] = musescorePath
|
||||
return True
|
||||
|
||||
def export_stream_to_pdf(stream, pdf_path):
|
||||
print(f"{ULTRASINGER_HEAD} Creating sheet PDF -> {pdf_path}")
|
||||
stream.write('musicxml.pdf', fp=pdf_path)
|
||||
|
||||
|
||||
|
||||
|
||||
def replace_unsupported_accidentals(note_name):
|
||||
accidental_replacements = {
|
||||
"♯": "#",
|
||||
"♭": "b",
|
||||
}
|
||||
for unsupported, supported in accidental_replacements.items():
|
||||
note_name = note_name.replace(unsupported, supported)
|
||||
return note_name
|
||||
Reference in new issue
Block a user