<s> '''<STR_LIT>''' <EOL> import djitw <EOL> import librosa <EOL> import numpy as np <EOL> import sys <EOL> import argparse <EOL> import pretty_midi <EOL> FS = <NUM_LIT> <EOL> HOP = <NUM_LIT> <EOL> NOTE_START = <NUM_LIT> <EOL> N_NOTES = <NUM_LIT> <EOL> def extract_cqt ( audio_data , fs , hop , note_start , n_notes ) : <EOL> '''<STR_LIT>''' <EOL> cqt = librosa . cqt ( <EOL> audio_data , sr = fs , hop_length = hop , <EOL> fmin = librosa . midi_to_hz ( note_start ) , n_bins = n_notes ) <EOL> cqt = cqt . T <EOL> cqt = librosa . logamplitude ( cqt , ref_power = cqt . max ( ) ) <EOL> cqt = librosa . util . normalize ( cqt , norm = <NUM_LIT> , axis = <NUM_LIT:1> ) <EOL> times = librosa . frames_to_time ( np . arange ( cqt . shape [ <NUM_LIT:0> ] ) , fs , hop ) <EOL> return cqt , times <EOL> def align ( midi_object , audio_data , fs , hop , note_start , n_notes , penalty ) : <EOL> '''<STR_LIT>''' <EOL> midi_audio = midi_object . fluidsynth ( fs = fs ) <EOL> midi_gram , midi_times = extract_cqt ( <EOL> midi_audio , fs , hop , note_start , n_notes ) <EOL> audio_gram , audio_times = extract_cqt ( <EOL> audio_data , fs , hop , note_start , n_notes ) <EOL> distance_matrix = <NUM_LIT:1> - np . dot ( midi_gram , audio_gram . T ) <EOL> if penalty is None : <EOL> penalty = distance_matrix . mean ( ) <EOL> p , q , score = djitw . dtw ( <EOL> distance_matrix , gully = <NUM_LIT> , penalty = penalty ) <EOL> midi_object . adjust_times ( midi_times [ p ] , audio_times [ q ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' , <EOL> formatter_class = argparse . ArgumentDefaultsHelpFormatter ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = None , type = str , <EOL> action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = FS , type = int , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = HOP , type = int , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = NOTE_START , type = int , <EOL> action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = N_NOTES , type = int , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = None , type = float , <EOL> action = '<STR_LIT:store>' , help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parameters = vars ( parser . parse_args ( sys . argv [ <NUM_LIT:1> : ] ) ) <EOL> print "<STR_LIT>" . format ( parameters [ '<STR_LIT>' ] ) <EOL> audio_data , _ = librosa . load ( parameters [ '<STR_LIT>' ] , sr = parameters [ '<STR_LIT>' ] ) <EOL> print "<STR_LIT>" . format ( parameters [ '<STR_LIT>' ] ) <EOL> midi_object = pretty_midi . PrettyMIDI ( parameters [ '<STR_LIT>' ] ) <EOL> print "<STR_LIT>" . format ( parameters [ '<STR_LIT>' ] , <EOL> parameters [ '<STR_LIT>' ] ) <EOL> align ( midi_object , audio_data , parameters [ '<STR_LIT>' ] , parameters [ '<STR_LIT>' ] , <EOL> parameters [ '<STR_LIT>' ] , parameters [ '<STR_LIT>' ] , <EOL> parameters [ '<STR_LIT>' ] ) <EOL> print "<STR_LIT>" . format ( parameters [ '<STR_LIT>' ] ) <EOL> midi_object . write ( parameters [ '<STR_LIT>' ] ) <EOL> if parameters [ '<STR_LIT>' ] : <EOL> print "<STR_LIT>" . format ( parameters [ '<STR_LIT>' ] ) <EOL> midi_audio_aligned = midi_object . fluidsynth ( fs = parameters [ '<STR_LIT>' ] ) <EOL> if midi_audio_aligned . shape [ <NUM_LIT:0> ] > audio_data . shape [ <NUM_LIT:0> ] : <EOL> midi_audio_aligned = midi_audio_aligned [ : audio_data . shape [ <NUM_LIT:0> ] ] <EOL> else : <EOL> trim_amount = audio_data . shape [ <NUM_LIT:0> ] - midi_audio_aligned . shape [ <NUM_LIT:0> ] <EOL> midi_audio_aligned = np . append ( midi_audio_aligned , <EOL> np . zeros ( trim_amount ) ) <EOL> librosa . output . write_wav ( parameters [ '<STR_LIT>' ] , <EOL> np . vstack ( [ midi_audio_aligned , audio_data ] ) , <EOL> parameters [ '<STR_LIT>' ] ) </s>
