import os
import pickle
import pretty_midi
import numpy as np
from progress.bar import Bar
from processing import *
import random


def preprocess_midi_files_under(midi_dir='', save_dir=''):
    midi_paths = []
    for root, dirs, files in os.walk(os.path.abspath(midi_dir)):
        for file in files:
            if file.endswith(tuple(['.mid', '.midi'])):
                midi_paths.append(os.path.join(root, file))
    midi_paths = random.sample(midi_paths, 1)
    os.makedirs(save_dir, exist_ok=True)

    transpositions = np.arange(-5, 7, 1)
    for path in Bar('Processing').iter(midi_paths):
        print(' ', end='[{}]'.format(path), flush=True)
        for t in transpositions:
            try:
                data = encode_midi(path)
                if data is not None:
                    with open(f'{save_dir}/{path.split("/")[-1]}.pickle', 'wb') as f:
                        pickle.dump(data, f)
                else:
                    pass
            except:
                pass


preprocess_midi_files_under(midi_dir='', save_dir='')

import os
import pickle
import pretty_midi
import numpy as np
from progress.bar import Bar
from processing import *
import random


def preprocess_midi_files_under(midi_dir='', save_dir=''):
    midi_paths = []
    for root, dirs, files in os.walk(os.path.abspath(midi_dir)):
        for file in files:
            if file.endswith(tuple(['.mid', '.midi'])):
                midi_paths.append(os.path.join(root, file))
    midi_paths = random.sample(midi_paths, 1)
    os.makedirs(save_dir, exist_ok=True)

    transpositions = np.arange(-5, 7, 1)
    for path in Bar('Processing').iter(midi_paths):
        print(' ', end='[{}]'.format(path), flush=True)
        for t in transpositions:
            try:
                data = encode_midi(path)
                if data is not None:
                    with open(f'{save_dir}/{path.split("/")[-1]}.pickle', 'wb') as f:
                        pickle.dump(data, f)
                else:
                    pass
            except:
                pass


preprocess_midi_files_under(midi_dir='', save_dir='')
