TastyPiano / src /music /pipeline /music_pipeline.py
ccolas's picture
Update src/music/pipeline/music_pipeline.py
c9ba38e
raw
history blame contribute delete
5.16 kB
from src.music.pipeline.url2audio import url2audio
from src.music.pipeline.audio2midi import audio2midi
from src.music.pipeline.midi2processed import midi2processed
from src.music.pipeline.processed2encoded import processed2encoded
from src.music.pipeline.encoded2rep import encoded2rep
from src.music.config import RANDOM_CROP, NB_AUG, FROM_URL_PATH
# from src.music.pipeline.synth2audio import AudioRecorder
# from src.music.pipeline.processed2handcodedrep import processed2handcodedrep
import time
import hashlib
VERBOSE = True
AUGMENTATION, NOISE_INJECTED = False, False
CROP = 10# crop 30s before transcription
# AUDIO_RECORDER = AudioRecorder(place='home')
def encode_music(url=None,
audio_path=None,
midi_path=None,
processed_path=None,
record=False,
crop=CROP,
random_crop=RANDOM_CROP,
augmentation=AUGMENTATION,
noise_injection=NOISE_INJECTED,
apply_filtering=True,
nb_aug=NB_AUG,
level=0,
verbose=VERBOSE):
if not record: assert url is not None or audio_path is not None or midi_path is not None or processed_path is not None
init_time = time.time()
error = ''
try:
if record:
assert audio_path is None and midi_path is None
if verbose: print(' ' * level + 'Processing music, recorded from mic.')
audio_path = AUDIO_RECORDER.record_one()
error = ''
if processed_path is None:
if midi_path is None:
if audio_path is None:
if verbose and not record: print(' ' * level + 'Processing music, from audio source.')
init_t = time.time()
audio_path, _, error = url2audio(playlist_path=FROM_URL_PATH, video_url=url, verbose=verbose, level=level+2)
if verbose: print(' ' * (level + 4) + f'Audio downloaded in {int(time.time() - init_t)} seconds.')
else:
if verbose and not record: print(' ' * level + 'Processing music, from midi source.')
init_t = time.time()
midi_path, error = audio2midi(audio_path, crop=crop, random_crop=random_crop, verbose=verbose, level=level+2)
if verbose: print(' ' * (level + 4) + f'Audio transcribed to midi in {int(time.time() - init_t)} seconds.')
init_t = time.time()
processed_path, error = midi2processed(midi_path, apply_filtering=apply_filtering, verbose=verbose, level=level+2)
if verbose: print(' ' * (level + 4) + f'Midi preprocessed in {int(time.time() - init_t)} seconds.')
init_t = time.time()
encoded_path, error = processed2encoded(processed_path, augmentation=augmentation, nb_aug=nb_aug, noise_injection=noise_injection, verbose=verbose, level=level+2)
if verbose: print(' ' * (level + 4) + f'Midi encoded in {int(time.time() - init_t)} seconds.')
init_t = time.time()
representation_path, representation, error = encoded2rep(encoded_path, return_rep=True, level=level+2, verbose=verbose)
if verbose: print(' ' * (level + 4) + f'Music representation computed in {int(time.time() - init_t)} seconds.')
init_t = time.time()
handcoded_rep_path, handcoded_rep, error = None, None, ''
# handcoded_rep_path, handcoded_rep, error = processed2handcodedrep(processed_path, return_rep=True, level=level+2, verbose=verbose)
if verbose: print(' ' * (level + 4) + f'Music handcoded representation computed in {int(time.time() - init_t)} seconds.')
# assert handcoded_rep_path is not None and representation_path is not None
all_paths = dict(url=url, audio_path=audio_path, midi_path=midi_path, processed_path=processed_path, encoded_path=encoded_path,
representation_path=representation_path, handcoded_rep_path=handcoded_rep_path)
if audio_path is not None:
print('audio hash: ', hashlib.md5(open(audio_path, 'rb').read()).hexdigest())
if midi_path is not None:
print('midi hash: ', hashlib.md5(open(midi_path, 'rb').read()).hexdigest())
print('processed hash: ', hashlib.md5(open(processed_path, 'rb').read()).hexdigest())
print('encoded hash: ', hashlib.md5(open(encoded_path, 'rb').read()).hexdigest())
print('rep hash: ', hashlib.md5(open(representation_path, 'rb').read()).hexdigest())
print("rep:", representation[:10])
if verbose: print(' ' * (level + 2) + f'Music processed in {int(time.time() - init_time)} seconds.')
except Exception as err:
print(err, error)
if verbose: print(' ' * (level + 2) + f'Music FAILED to process in {int(time.time() - init_time)} seconds.')
representation = None
handcoded_rep = None
all_paths = dict()
return representation, handcoded_rep, all_paths, error
if __name__ == '__main__':
representation = encode_music(url="https://www.youtube.com/watch?v=a2LFVWBmoiw")[0]
# representation = encode_music(record=True)[0]