2019-05-29 10:36:34 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import settings
|
|
|
|
import pypianoroll as roll
|
|
|
|
import numpy as np
|
|
|
|
import os
|
|
|
|
from tqdm import tqdm
|
|
|
|
from math import floor
|
|
|
|
import sys
|
2019-05-30 11:23:34 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
import pickle
|
2019-05-29 10:36:34 +02:00
|
|
|
|
2019-05-30 20:47:47 +02:00
|
|
|
midi_folder_path = sys.argv[1]
|
|
|
|
output_path = sys.argv[2]
|
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
def to_samples(multitrack, midi_res=settings.midi_resolution, how='by_group'):
|
2019-05-29 10:36:34 +02:00
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
#how = 'by_group', 'by_instrument', 'merged',
|
|
|
|
|
|
|
|
# TODO: add transpositions of every sample to every possible key transposition
|
2019-05-30 11:23:34 +02:00
|
|
|
# np.roll(sample, pitch_interval, axis=1) for transposition
|
|
|
|
# np.roll(sample, time_steps, axis=0) for time shifting
|
2019-05-29 10:36:34 +02:00
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
samples_by_instrument = defaultdict( lambda : [] )
|
|
|
|
|
|
|
|
for track in multitrack.tracks:
|
2019-05-29 10:36:34 +02:00
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
key = settings.midi_group[track.program + 1] if not track.is_drum else 'Drums'
|
2019-05-30 13:36:15 +02:00
|
|
|
|
2019-05-30 23:11:25 +02:00
|
|
|
# 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)
|
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
# save collected pack of data to dictionary with samples packs for groups of instruments
|
|
|
|
for sample in track_beats:
|
|
|
|
if sample.sum() != 0:
|
|
|
|
samples_by_instrument[key].append(sample)
|
|
|
|
|
|
|
|
# TODO: add posibility of choosing between saving samples to groups of instrument, or to every instrument separatly or with no differance
|
|
|
|
# TODO: add option, for looking only for one instrument/group
|
|
|
|
# TODO: add option for colecting, more than one beat per sample (min 4)
|
2019-05-30 13:36:15 +02:00
|
|
|
|
2019-05-30 11:23:34 +02:00
|
|
|
return samples_by_instrument
|
2019-05-29 10:36:34 +02:00
|
|
|
|
2019-05-30 23:11:25 +02:00
|
|
|
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)]
|
2019-05-29 10:36:34 +02:00
|
|
|
return_midi = roll.Multitrack(tracks=tracks, tempo=tempo, downbeat=[0, 96, 192, 288], beat_resolution=beat_resolution)
|
2019-05-30 11:23:34 +02:00
|
|
|
roll.write(return_midi, output_path)
|
2019-05-30 20:47:47 +02:00
|
|
|
return return_midi
|
2019-05-29 10:36:34 +02:00
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
# TODO: Make optial function to erase information of note lenth - ??
|
|
|
|
def ignore_note_lenght():
|
|
|
|
pass
|
2019-05-30 13:36:15 +02:00
|
|
|
|
2019-05-29 10:36:34 +02:00
|
|
|
def main():
|
2019-05-30 11:23:34 +02:00
|
|
|
print('Exporting...')
|
2019-05-31 10:25:16 +02:00
|
|
|
|
|
|
|
samples_pack_by_instrument = defaultdict( lambda : list() )
|
|
|
|
|
2019-05-30 20:47:47 +02:00
|
|
|
for directory, subdirectories, files in os.walk(midi_folder_path):
|
|
|
|
for midi_file in tqdm(files):
|
|
|
|
midi_file_path = os.path.join(directory, midi_file)
|
2019-05-31 10:25:16 +02:00
|
|
|
#load midi ro pypianoroll - Multirack
|
2019-05-30 20:47:47 +02:00
|
|
|
try:
|
2019-05-31 10:25:16 +02:00
|
|
|
multitrack = roll.parse(midi_file_path)
|
2019-05-30 20:47:47 +02:00
|
|
|
except:
|
2019-05-31 10:25:16 +02:00
|
|
|
# IDEA: Log errors, and save to file?
|
2019-05-30 20:47:47 +02:00
|
|
|
continue
|
2019-05-30 23:11:25 +02:00
|
|
|
|
2019-05-31 10:25:16 +02:00
|
|
|
for key, value in to_samples(multitrack).items():
|
|
|
|
samples_pack_by_instrument[key].extend(value)
|
2019-05-30 11:23:34 +02:00
|
|
|
|
|
|
|
# this is for intrument separation
|
|
|
|
print('Saving...')
|
2019-05-31 10:25:16 +02:00
|
|
|
if not os.path.exists(output_path):
|
|
|
|
os.makedirs(output_path)
|
2019-05-30 12:36:59 +02:00
|
|
|
for key, value in tqdm(samples_pack_by_instrument.items()):
|
2019-05-31 10:25:16 +02:00
|
|
|
np.savez_compressed('{}/{}.npz'.format(output_path, key), np.array(value))
|
2019-05-30 11:23:34 +02:00
|
|
|
|
|
|
|
print('Done!')
|
2019-05-29 10:36:34 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|