How to do it...

The strategy discussed above is coded as follows (the code file is available as Music_generation.ipynb in GitHub) along with the recommended audio file:

  1. Import the relevant packages and dataset:
!pip install mido music21import mido, glob, osfrom mido import MidiFile, MidiTrack, Messageimport numpy as npfrom music21 import converter, instrument, note, chordfrom keras.utils import np_utilsfrom keras.layers import Input, LSTM, Dropout, Dense, Activationfrom keras.models import Modelfname = '/content/nintendo.mid'
  1. Read the content of the file:
midi = converter.parse(fname)

The preceding code reads a stream of scores.

  1. Define a function that reads the stream of scores and extracts the notes from it (along with silence, if present ...

Get Neural Networks with Keras Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.