#!/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 from collections import defaultdict import pickle from music21 import converter, instrument, note, chord, stream import music21 midi_folder_path = sys.argv[1] output_path = sys.argv[2] def to_sequence(midi_path): seq_by_instrument = defaultdict( lambda : [] ) midi_file = music21.converter.parse(midi_path) stream = music21.instrument.partitionByInstrument(midi_file) for part in stream: for event in part: if part.partName != None: # TODO: add note lenght as parameter if isinstance(event, music21.note.Note): # to_export_event = (str(event.pitch), event.quarterLength) to_export_event = str(event.pitch) seq_by_instrument[part.partName].append(to_export_event) elif isinstance(event, music21.chord.Chord): to_export_event = ' '.join(str(note) for note in event.pitches) # to_export_event = (' '.join(str(note) for note in event.pitches), event.quarterLength) seq_by_instrument[part.partName].append(to_export_event) X_train_by_instrument = defaultdict( lambda : [] ) y_train_by_instrument = defaultdict( lambda : [] ) for instrument, sequence in seq_by_instrument.items(): for i in range(len(sequence)-8) : X_train_by_instrument[instrument].append(np.array(sequence[i: i + 8])) #