File size: 4,999 Bytes
e775f6d
 
 
 
 
 
 
 
 
e9d7c26
e775f6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d7c26
 
 
 
 
e775f6d
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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)
        print('audio hash: ', hashlib.md5(open(audio_path, 'rb').read()).hexdigest())
        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())
        if verbose: print(' ' * (level + 2) + f'Music processed in {int(time.time() - init_time)} seconds.')
    except:
        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]