<jupyter_start><jupyter_text>Music Generator<jupyter_code>from music21 import converter,instrument,note,chord,stream
from keras.utils import np_utils
import glob
import pickle
import numpy as np
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import ModelCheckpoint<jupyter_output><empty_output><jupyter_text>Reading a midi file<jupyter_code>midi = converter.parse('./midi_songs/costadsol.mid')
midi
midi.show('midi')
midi.show('text')
elements_to_parse = midi.flat.notes
len(elements_to_parse)
for i in elements_to_parse:
    print(i,i.offset)
notes_demo = []
for e in elements_to_parse:
    if isinstance(e,note.Note):
        notes_demo.append(str(e.pitch))
    elif isinstance(e,chord.Chord):
        notes_demo.append('+'.join(str(n) for n in e.normalOrder))
print(len(notes_demo))
print(notes_demo)<jupyter_output>273
['0+4+7', 'A2', '0+4+7', 'A2', '0+4+7', '0+4+6', 'D3', '0+4+6', '0+4+6', 'D3', '0+4+6', '0+4+7', 'A2', '0+4+7', 'A2', '0+4+7', '0+4+6', 'D3', '0+4+6', '0+4+6', 'D3', '0+4+6', '0+4+7', 'A2', '0+4+7', 'A2', 'A2', '0+4+7', '0+4+6', 'A2', 'D3', '0+4+6', '0+4+6', 'D3', 'D3', 'F#2', '0+4+6', 'G2', 'G#2', '0+4+7', 'A2', '0+4+7', 'A2', 'A2', '0+4+7', '0+4+6', 'A2', 'D3', '0+4+6', '0+4+6', 'D3', 'D3', 'C5', 'E5', 'D5', 'D3', 'A2', 'G3', 'A2', 'A2', 'G4', 'G#4', 'A4', 'A2', 'D3', 'F#3', 'D3', 'D3', 'E4', 'F#2', 'G4', 'G2', 'A4', 'G#2', 'A2', 'G3', 'A4', 'G4', 'A2', 'A4', 'A2', 'C5', 'D5', 'A2', 'D3', 'F#3', 'A4', 'D3', 'D3', 'A4', 'C5', 'A2', 'B4', 'G#2', 'B-4', 'G2', 'F3', 'G2', 'G2', 'F4', 'F#4', 'G4', 'G2', 'C3', 'E3', 'C3', 'B2', 'G4', 'E2', 'A4', 'F2', 'B-4', 'F#2', 'G2', 'F3', 'A4', 'C5', 'G2', 'D5', 'G2', 'F5', 'E5', 'G2', 'C3', 'E3', 'C3', 'C3', 'C5', 'E5', 'B2', 'D5', 'A2', 'G3', 'A2', 'A2', 'G4', 'G#4', 'A4', 'A2', 'D3', 'F#3', 'D3', 'D3', 'E4', 'F#2', 'G4', 'G2', 'A4', 'G#2', 'A2'[...]<jupyter_text>Preprocessing all midi files<jupyter_code>notes = []
for file in glob.glob('./midi_songs/*.mid'):
    midi = converter.parse(file)
    print("Parsing : %s"%file.split('/')[-1])
    elements = midi.flat.notes
    for e in elements:
        if isinstance(e,note.Note): # if element is a note, store its pitch
            notes.append(str(e.pitch))
        elif isinstance(e,chord.Chord): # if element is a chord, split each note and join
            notes.append('+'.join(str(n) for n in e.normalOrder))
with open('notes.pkl','wb') as f:
    pickle.dump(notes,f)
with open('notes.pkl','rb') as f:
    notes = pickle.load(f)
n_vocab = len(set(notes))
print('Total Notes :',len(notes))
print('Unique Notes :',n_vocab)<jupyter_output>Total Notes : 60498
Unique Notes : 359<jupyter_text>Prepare Sequential Data for LSTM<jupyter_code>seq_len = 100 # Length of data fot LSTM
classes = sorted(set(notes)) 
note2int = dict((e,num) for num,e in enumerate(classes)) # map classes to integer
int2note = dict((num,e) for num,e in enumerate(classes)) # map integer to classes
X = [] # input data for LSTM
Y = [] # output data for LSTM
for i in range(len(notes)-seq_len):
    inp = notes[i:i+seq_len]
    out = notes[i+seq_len]
    X.append([note2int[note] for note in inp])
    Y.append(note2int[out])
X = np.reshape(X,(len(X),seq_len,1))
X.shape
# Normalize the data
normalized_X = X/float(n_vocab)
# convert output to one hot vector
Y = np_utils.to_categorical(Y)
print(normalized_X.shape)
print(Y.shape)<jupyter_output>(60398, 100, 1)
(60398, 359)<jupyter_text>Model Architecture<jupyter_code>model = Sequential()
model.add(LSTM(units=512,input_shape=normalized_X.shape[1:],return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(units=512,return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(units=512))
model.add(Dense(256))
model.add(Dropout(0.3))
model.add(Dense(n_vocab,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam')
model.summary()
checkpoint = ModelCheckpoint('model.h5',monitor='loss',verbose=0,save_best_only=True,mode='min')
model.fit(normalized_X,Y,epochs=100,batch_size=64,callbacks=[checkpoint])
# Trained on Google Colaboratory
model = load_model('model.h5',compile=False)<jupyter_output><empty_output><jupyter_text>Making Predictions<jupyter_code># Prerpating data for making predictions
X = []
for i in range(len(notes)-seq_len):
    inp = notes[i:i+seq_len]
    X.append([note2int[note] for note in inp])
start = np.random.randint(len(X)-1)
seq = X[start]
pred_output = []
for i in range(300):
    pred_input = np.reshape(seq,(1,len(seq),1))
    pred_input = pred_input/float(n_vocab)
    pred = model.predict(pred_input,verbose=0)
    idx = np.argmax(pred)
    pred_output.append(int2note[idx])
    seq.append(idx)
    seq = seq[1:]
print(pred_output)<jupyter_output>['A5', 'E4', 'G4', 'G5', 'E4', 'G4', 'A5', 'E4', 'G4', 'F#5', 'B3', 'F#4', 'D4', 'F#4', 'D6', 'D7', 'D4', 'F#4', 'D4', 'F#4', 'C#6', 'C#7', 'B3', 'F#4', 'D4', 'F#4', 'B5', 'B6', 'D4', 'F#4', 'D4', 'F#4', '2+4', 'C4', 'G4', 'G5', 'E4', 'G4', 'F#5', 'E4', 'G4', 'G5', 'E4', 'G4', 'B5', 'C4', 'G4', 'A5', 'E4', 'G4', 'G5', 'E4', 'G4', 'A5', 'E4', 'G4', '5+9+0', 'F3', 'C4', 'C6', 'A3', 'C4', 'B5', 'A3', 'C4', 'C6', 'A3', 'C4', '4+5+9', 'F3', 'C4', 'D6', 'A3', 'C4', 'C6', 'A3', 'C4', 'D6', 'A3', 'C4', 'B5', 'E3', 'B3', 'G#3', 'B3', 'G#3', 'B3', 'G#3', 'B5', 'B3', 'C6', 'C#6', 'D6', 'G3', 'D4', 'B3', 'D4', 'B3', 'D4', 'B3', 'D6', 'D4', 'C#6', 'C6', 'B5', 'E3', 'G#3', 'B3', 'E4', 'G#4', 'B4', 'E5', 'G#5', 'E6', '9+0+4', '9', '9+0+4', '9+0+4', '0+4+7', '6+9+0', '2', '9', '9+2', '6', 'E5', '5+9+0', '5', '7+9+0', '5+9+0', '7', '9+0+4', '9', '9+0+4', '9+0+4', '9+0+4', '10+2+5', '7', '2', '10', '7', 'C5', '2+5+8', '5', '0', '8', '5', '0+5', '0+4', '0', '4', '7', '0', '11', '9+0+4', '9', '9+0+4', '9+[...]<jupyter_text>Generating midi File<jupyter_code>offset = 0
out_notes = []
for ele in pred_output:
    if ('+' in ele) or ele.isdigit(): # if the element is chord
        notes_in_chord = ele.split('+')
        temp_notes = []
        for c_note in notes_in_chord:
            new_note = note.Note(int(c_note))
            new_note.storedInstrument = instrument.Piano()
            temp_notes.append(new_note)
        new_chord = chord.Chord(temp_notes)
        new_chord.offset = offset
        out_notes.append(new_chord)
    else: # if the element is note
        new_note = note.Note(ele)
        new_note.offset = offset
        new_note.storedInstrument = instrument.Piano()
        out_notes.append(new_note)
    offset += 1
midi_stream = stream.Stream(out_notes)
# Playing the generated midi file
midi_stream.show('midi')
# Saving the generated midi file
midi_stream.write('midi',fp = 'output.midi')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.25212972649828125, 'fraction_numerical': 0.07203706471379465, 'mean_word_length': 5.111415525114155, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '544364', 'license': 'MIT', 'n_tokens_mistral': 3153, 'n_tokens_neox': 3086, 'n_words': 640, 'path': 'Music Generator.ipynb', 'repo_name': 'amitguptapc/Music_Generator', 'size': '6691'}