From dac8add955162efabee7ac05f8804a9103c1b91c Mon Sep 17 00:00:00 2001 From: Cezary Pukownik Date: Thu, 30 May 2019 23:11:25 +0200 Subject: [PATCH] data extraction to groups of instruments; drums is now avaible --- project/midi.py | 29 ++++----- project/settings.py | 147 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 14 deletions(-) diff --git a/project/midi.py b/project/midi.py index 28e38dd..42f1627 100644 --- a/project/midi.py +++ b/project/midi.py @@ -30,22 +30,22 @@ def to_samples(midi_file_path, midi_res=settings.midi_resolution): for track in roll.Multitrack(midi_file_path).tracks: if not track.is_drum: - key = track.program + 1 - - # this makes pack of samples of N x 96 x 128 shape - number_of_beats = floor(track.pianoroll.shape[0] / midi_res) - track_pianoroll = track.pianoroll[: number_of_beats * midi_res] - track_beats = track_pianoroll.reshape(number_of_beats, midi_res, 128) - - # save collected pack of data to dictionary with samples packs for every instrument - samples_by_instrument[track.program + 1] = np.concatenate([track_beats, samples_by_instrument[ track.program + 1]], axis=0) + key = settings.midi_group[track.program + 1] else: - # TODO: add code for drums samples - pass + key = 'Drums' + + # this makes pack of samples of N x 96 x 128 shape + number_of_beats = floor(track.pianoroll.shape[0] / midi_res) + track_pianoroll = track.pianoroll[: number_of_beats * midi_res] + track_beats = track_pianoroll.reshape(number_of_beats, midi_res, 128) + + # save collected pack of data to dictionary with samples packs for every instrument + samples_by_instrument[key] = np.concatenate([track_beats, samples_by_instrument[key]], axis=0) + return samples_by_instrument -def to_midi(samples, output_path=settings.generated_midi_path, program=0, tempo=120, beat_resolution=settings.beat_resolution): - tracks = [roll.Track(samples, program=program)] +def to_midi(samples, output_path=settings.generated_midi_path, program=0, tempo=120, is_drum=False, beat_resolution=settings.beat_resolution): + tracks = [roll.Track(samples, program=program, is_drum=is_drum)] return_midi = roll.Multitrack(tracks=tracks, tempo=tempo, downbeat=[0, 96, 192, 288], beat_resolution=beat_resolution) roll.write(return_midi, output_path) return return_midi @@ -72,6 +72,7 @@ def main(): pass if midi_samples is None: continue + # this is for intrument separation for key, value in midi_samples.items(): value = delete_empty_samples(value) @@ -86,7 +87,7 @@ def main(): for key, value in tqdm(samples_pack_by_instrument.items()): if not os.path.exists(output_path): os.makedirs(output_path) - np.savez_compressed('{}/{}.npz'.format(output_path, settings.midi_program[key]), value) + np.savez_compressed('{}/{}.npz'.format(output_path, key), value) # # Give a preview of what samples looks like # fig, axes = plt.subplots(nrows=10, ncols=10, figsize=(20, 20)) diff --git a/project/settings.py b/project/settings.py index 874046d..24e163c 100644 --- a/project/settings.py +++ b/project/settings.py @@ -164,3 +164,150 @@ midi_program = { 127 : 'Applause', 128 : 'Gunshot' } + +midi_group = { +# Piano + 1 : 'Piano', + 2 : 'Piano', + 3 : 'Piano', + 4 : 'Piano', + 5 : 'Piano', + 6 : 'Piano', + 7 : 'Piano', + 8 : 'Piano', +# Chromatic Percussion + 9 : 'Chromatic_Percussion', + 10 : 'Chromatic_Percussion', + 11 : 'Chromatic_Percussion', + 12 : 'Chromatic_Percussion', + 13 : 'Chromatic_Percussion', + 14 : 'Chromatic_Percussion', + 15 : 'Chromatic_Percussion', + 16 : 'Chromatic_Percussion', +# Organ + 17 : 'Organ', + 18 : 'Organ', + 19 : 'Organ', + 20 : 'Organ', + 21 : 'Organ', + 22 : 'Organ', + 23 : 'Organ', + 24 : 'Organ', +# Guitar + 25 : 'Guitar', + 26 : 'Guitar', + 27 : 'Guitar', + 28 : 'Guitar', + 29 : 'Guitar', + 30 : 'Guitar', + 31 : 'Guitar', + 32 : 'Guitar', +# Bass + 33 : 'Bass', + 34 : 'Bass', + 35 : 'Bass', + 36 : 'Bass', + 37 : 'Bass', + 38 : 'Bass', + 39 : 'Bass', + 40 : 'Bass', +# Strings + 41 : 'Strings', + 42 : 'Strings', + 43 : 'Strings', + 44 : 'Strings', + 45 : 'Strings', + 46 : 'Strings', + 47 : 'Strings', + 48 : 'Strings', +# Ensemble + 49 : 'Ensemble', + 50 : 'Ensemble', + 51 : 'Ensemble', + 52 : 'Ensemble', + 53 : 'Ensemble', + 54 : 'Ensemble', + 55 : 'Ensemblee', + 56 : 'Ensemble', +# Brass + 57 : 'Brass', + 58 : 'Brass', + 59 : 'Brass', + 60 : 'Brass', + 61 : 'Brass', + 62 : 'Brass', + 63 : 'Brass', + 64 : 'Brass', +# Reed + 65 : 'Reed', + 66 : 'Reed', + 67 : 'Reed', + 68 : 'Reed', + 69 : 'Reed', + 70 : 'Reed', + 71 : 'Reed', + 72 : 'Reed', +# Pipe + 73 : 'Pipe', + 74 : 'Pipe', + 75 : 'Pipe', + 76 : 'Pipe', + 77 : 'Pipe', + 78 : 'Pipe', + 79 : 'Pipe', + 80 : 'Pipe', +# Synth Lead + 81 : 'Synth_Lead', + 82 : 'Synth_Lead', + 83 : 'Synth_Lead', + 84 : 'Synth_Lead', + 85 : 'Synth_Lead', + 86 : 'Synth_Lead', + 87 : 'Synth_Lead', + 88 : 'Synth_Lead', +# Synth Pad + 89 : 'Synth_Pad', + 90 : 'Synth_Pad', + 91 : 'Synth_Pad', + 92 : 'Synth_Pad', + 93 : 'Synth_Pad', + 94 : 'Synth_Pad', + 95 : 'Synth_Pad', + 96 : 'Synth_Pad', +# Synth Effects + 97 : 'Synth_Effects', + 98 : 'Synth_Effects', + 99 : 'Synth_Effects', + 100 : 'Synth_Effects', + 101 : 'Synth_Effects', + 102 : 'Synth_Effects', + 103 : 'Synth_Effects', + 104 : 'Synth_Effects', +# Ethnic + 105 : 'Ethnic', + 106 : 'Ethnic', + 107 : 'Ethnic', + 108 : 'Ethnic', + 109 : 'Ethnic', + 110 : 'Ethnic', + 111 : 'Ethnic', + 112 : 'Ethnic', +# Percussive + 113 : 'Percussive', + 114 : 'Percussive', + 115 : 'Percussive', + 116 : 'Percussive', + 117 : 'Percussive', + 118 : 'Percussive', + 119 : 'Percussive', + 120 : 'Percussive', +# Sound Effects + 121 : 'Sound_Effects', + 122 : 'Sound_Effects', + 123 : 'Sound_Effects', + 124 : 'Sound_Effects', + 125 : 'Sound_Effects', + 126 : 'Sound_Effects', + 127 : 'Sound_Effects', + 128 : 'Sound_Effects' +}