mirror of
https://github.com/rakuri255/UltraSinger.git
synced 2026-11-30 09:13:57 +09:00
optimized pitch recognizion
Added plot
This commit is contained in:
1 parent
f457e9a89d
commit
98eed0f307
3 files changed
+74
-27
No files matched your search
+70
-25
@@ -1,17 +1,30 @@
|
||||
import getopt
|
||||
import os
|
||||
import statistics
|
||||
import sys
|
||||
import Levenshtein
|
||||
import librosa
|
||||
|
||||
from moduls.Audio.vocal_chunks import export_chunks_from_ultrastar_data, convert_audio_to_mono_wav
|
||||
from moduls.Midi import midi_creator
|
||||
from moduls.Pitcher import pitcher
|
||||
from moduls.Pitcher.pitcher import get_frequency_with_high_confidance
|
||||
from moduls.Pitcher.pitcher import get_frequency_with_high_confidance, get_pitch_with_crepe_file
|
||||
from moduls.Ultrastar import ultrastar_parser, ultrastar_converter, ultrastar_writer
|
||||
from moduls.Speech_Recognition.Vosk import transcribe_with_vosk
|
||||
from moduls.os_helper import create_folder
|
||||
from matplotlib import pyplot as plt
|
||||
from collections import Counter
|
||||
|
||||
|
||||
def get_confidance(t, f, c, threashold):
|
||||
# todo: replace get_frequency_with_high_conf from pitcher
|
||||
conf_t = []
|
||||
conf_f = []
|
||||
conf_c = []
|
||||
for i in range(len(t)):
|
||||
if c[i] > threashold:
|
||||
conf_t.append(t[i])
|
||||
conf_f.append(f[i])
|
||||
conf_c.append(c[i])
|
||||
return conf_t, conf_f, conf_c
|
||||
|
||||
|
||||
def convert_frequencies_to_notes(frequency):
|
||||
@@ -21,31 +34,40 @@ def convert_frequencies_to_notes(frequency):
|
||||
return notes
|
||||
|
||||
|
||||
def pitch_each_chunk_with_crepe(directory):
|
||||
print("Pitching each chunk with crepe.")
|
||||
_pitcher = pitcher
|
||||
def most_frequent(ar):
|
||||
return Counter(ar).most_common(1)
|
||||
|
||||
frequency = []
|
||||
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)
|
||||
t, f, c = _pitcher.get_pitch_with_crepe_file(filepath, 100)
|
||||
conf_f = get_frequency_with_high_confidance(f, c)
|
||||
mean = statistics.mean(conf_f)
|
||||
frequency.append(mean)
|
||||
# todo: Progress?
|
||||
# print(filename + " f: " + str(mean))
|
||||
|
||||
midi_notes = convert_frequencies_to_notes(frequency)
|
||||
note_numbers = []
|
||||
def convert_ultrastar_note_numbers(midi_notes):
|
||||
ultrastar_note_numbers = []
|
||||
for i in range(len(midi_notes)):
|
||||
note_number_librosa = librosa.note_to_midi(midi_notes[i])
|
||||
pitch = ultrastar_converter.midi_note_to_ultrastar_note(note_number_librosa)
|
||||
note_numbers.append(pitch)
|
||||
ultrastar_note_numbers.append(pitch)
|
||||
# todo: Progress?
|
||||
# print("Note: " + midi_notes[i] + " midi_note: " + str(note_number_librosa) + ' pitch: ' + str(pitch))
|
||||
return ultrastar_note_numbers
|
||||
|
||||
return note_numbers
|
||||
|
||||
def pitch_each_chunk_with_crepe(directory):
|
||||
print("Pitching each chunk with 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
|
||||
t, f, c = get_pitch_with_crepe_file(filepath, 10)
|
||||
conf_f = get_frequency_with_high_confidance(f, c)
|
||||
|
||||
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 get_bpm_from_data(data, sr):
|
||||
@@ -125,16 +147,18 @@ def do_ultrastar_stuff(input_file, chunk_folder_name, do_create_midi):
|
||||
ultrastar_audio_input_path = 'input/' + ultrastar_class.mp3.replace('\n', '')
|
||||
# todo: check if chunk folder is empty
|
||||
# todo: here we have to remove all silance, and dont need to store it!
|
||||
#ultrastar_class = remove_silence_from_ultrastar_data(ultrastar_audio_input_path, ultrastar_class)
|
||||
# ultrastar_class = remove_silence_from_ultrastar_data(ultrastar_audio_input_path, ultrastar_class)
|
||||
export_chunks_from_ultrastar_data(ultrastar_audio_input_path, ultrastar_class, chunk_folder_name)
|
||||
|
||||
# Pitch the audio
|
||||
note_numbers = pitch_each_chunk_with_crepe(chunk_folder_name)
|
||||
midi_notes = pitch_each_chunk_with_crepe(chunk_folder_name)
|
||||
ultrastar_note_numbers = convert_ultrastar_note_numbers(midi_notes)
|
||||
|
||||
# Create new repitched ultrastar txt
|
||||
# todo: output
|
||||
output_repitched_ultrastar = 'output/ultrastar_repitch.txt'
|
||||
ultrastar_writer.create_repitched_txt_from_ultrastar_data(input_file, note_numbers, output_repitched_ultrastar)
|
||||
ultrastar_writer.create_repitched_txt_from_ultrastar_data(input_file, ultrastar_note_numbers,
|
||||
output_repitched_ultrastar)
|
||||
|
||||
if do_create_midi:
|
||||
# todo: remove .txt
|
||||
@@ -145,6 +169,21 @@ def do_ultrastar_stuff(input_file, chunk_folder_name, do_create_midi):
|
||||
midi_creator.instruments_to_midi(voice_instrument, real_bpm, midi_output)
|
||||
|
||||
|
||||
def plot(input_file, vosk_transcribed_data, midi_notes):
|
||||
t, f, c = get_pitch_with_crepe_file(input_file, 10)
|
||||
t, f, c = get_confidance(t, f, c, 0.4)
|
||||
|
||||
plt.ylim(0, 600)
|
||||
plt.xlim(0, 50)
|
||||
plt.plot(t, f, linewidth=0.1)
|
||||
plt.savefig('test/crepe_0.4.png')
|
||||
|
||||
for i in range(len(vosk_transcribed_data)):
|
||||
nf = librosa.note_to_hz(midi_notes[i])
|
||||
plt.plot([vosk_transcribed_data[i].start, vosk_transcribed_data[i].end], [nf, nf], linewidth=1, alpha=0.5)
|
||||
plt.savefig('test/pit.png', dpi=2000)
|
||||
|
||||
|
||||
def do_audio_stuff(input_file, chunk_folder_name, model_path, do_create_midi):
|
||||
# Audio transcription
|
||||
# todo: convert to mono wav
|
||||
@@ -154,13 +193,19 @@ def do_audio_stuff(input_file, chunk_folder_name, model_path, do_create_midi):
|
||||
# --corrected_words = correct_words(vosk_speech, lyric)
|
||||
|
||||
# Pitch detection
|
||||
note_numbers = pitch_each_chunk_with_crepe(chunk_folder_name)
|
||||
midi_notes = pitch_each_chunk_with_crepe(chunk_folder_name)
|
||||
ultrastar_note_numbers = convert_ultrastar_note_numbers(midi_notes)
|
||||
|
||||
export_plot = False
|
||||
if export_plot:
|
||||
plot(input_file, vosk_transcribed_data, midi_notes)
|
||||
|
||||
# Ultrastar txt creation
|
||||
real_bpm = get_bpm_from_file(input_file)
|
||||
# todo: filename from audio / yt title
|
||||
ultrastar_file_output = 'output/ultrastar.txt'
|
||||
ultrastar_writer.create_txt_from_transcription(vosk_transcribed_data, note_numbers, ultrastar_file_output, real_bpm)
|
||||
ultrastar_writer.create_txt_from_transcription(vosk_transcribed_data, ultrastar_note_numbers, ultrastar_file_output,
|
||||
real_bpm)
|
||||
|
||||
if do_create_midi:
|
||||
ultrastar_class = ultrastar_parser.parse_ultrastar_txt(ultrastar_file_output)
|
||||
|
||||
@@ -5,6 +5,7 @@ from scipy.io import wavfile
|
||||
|
||||
def get_pitch_with_crepe_file(filename, step_size):
|
||||
sr, audio = wavfile.read(filename)
|
||||
|
||||
pitch_time, pitch_frequency, pitch_confidence, activation = crepe.predict(audio, sr, 'tiny',
|
||||
step_size=step_size,
|
||||
viterbi=True)
|
||||
@@ -37,7 +38,7 @@ def read_data_from_csv(filename):
|
||||
return headless_data
|
||||
|
||||
|
||||
def get_frequency_with_high_confidance(f, c, threshold=0.6):
|
||||
def get_frequency_with_high_confidance(f, c, threshold=0.4):
|
||||
conf_f = []
|
||||
for i in range(len(c)):
|
||||
if c[i] > threshold:
|
||||
|
||||
+2
-1
@@ -5,4 +5,5 @@ pydub==0.25.1
|
||||
Levenshtein==0.20.9
|
||||
scipy==1.10.0
|
||||
vosk==0.3.45
|
||||
tensorflow==2.11.0
|
||||
tensorflow==2.11.0
|
||||
matplotlib~=3.6.3
|
||||
Reference in new issue
Block a user