Generate Piano Music Using Deep-Learning

Yonghui Li (Aries)
5 min readJan 26, 2022

Part-1: Introduction and setup the development environment

Photo by Lorenzo Spoleti on Unsplash

Aims

We aim to develop a prototype deep-learning program which will be able to automatically generate piano music once we provided it with a short, say 1 minute, “kick-off” music fragment. We will share how to build such program up from scratch step by step, in a series of articles, so that each of them will be more chewable to the readers. We devote to make these articles easy to follow, so that the readers may feel comfortable to try them out. Below is one example piece of music generated by the program we developed. And we hope the readers will accomplish something similar or even better at the end.

A few call outs about this result:

  1. For those who is familiar with it, they should recognise that the first 50 seconds is the beginning of Für Elise, the master piece by Beethoven. And the program started to take over and continue to generate the rest of the piece.
  2. Readers may quickly notice some limitations of the example music generated, such as the beat is set to constant, which we will spend time to discuss later.
  3. The video we shared used one of the AWS DeepComposer posters as background image, simply because we feel the DeepComposer is a very cool project in this domain. But we did not build any robotic components and our work has nothing to do with the AWS DeepComposer.

Set Up Development Environment

Hardware

The computer we employed for this program is a DELL Alienware laptop with below specs:

  • Windows 10 Home (i7 core, 16GB Ram).
  • NVIDIA GeForce GTX 1080 display card with Max-Q Design (8GB ram). This enables GPU computing, which is critical for a reasonable turn around.

Although our setup and subsequent codes are all based on local Windows 10 with NVIDIA GPU environment, most of them should be transferrable to Mac or Linux systems with some small change required. If the readers did not process a machine with any GPU computing power, then one alternate option may be the online Google Colab (free but with certain restriction). In that specific case, some specific setup and codes we introduced here will no longer be applicable directly, but the main work flow, particularly the deep-learning part, as well as the high level design we share should still be largely valid for reference.

Software

  1. Download and install the up-to-date Anaconda for Windows 64 bit here: https://www.anaconda.com/products/individual. It provides some very good out-of-box Python capabilities and more, but it may be an overkill for us, as what we really need is the below two components from it:
    - The capability to create and manage “virtual environment”.
    - The capability to install and manage Python packages by “pip” (within the virtual environment).
    These two capabilities will allow us to setup a development environment specific to this project, in which we can have strong control over the version of not only Python itself, but also all the corresponding packages we will need. This may not be a very critical point to pay attention to when we are developing some one-off random project. But we believe it is an important practice to get familiar with, if one wanted to become more professional in machine learning field. At minimum, following these practice will allow our work be more re-producible and easy to share with other collaborators.
  2. For more advanced user, this is where we should create a remote GitHub repository and clone (sync) to our local, but we will not cover that in this article. Let’s simply assume a local folder named “deep_piano” will be created and used to store all our work in this project on our local computer from here onward. We will refer to this folder as our “working directory”.
  3. We then create a file named “conda_dnn_gpu_setup.bat” under our working directory with the content as below:
:: Win10:: Ananconda 3:: How to run this script: directly input this file name into Anaconda Prompt, and Enter
:: Set the name of the virtual env
set env_name=piano
:: clean upcls
:: If virtual env already exist, remove itcall conda deactivatecall echo y|conda remove --name %env_name% --all:: create new conda env with the Python kernel we wantcall echo y|conda create --name %env_name% python=3.9:: deactivate & activate the new envcall conda deactivatecall conda activate %env_name%
:: Install CUDNN for GPUcall echo y|conda install cudnn
:: Install Tensorflow for deep learningcall pip install tensorflow:: install project specific specific packagescall pip install numpy pandas seaborn sklearn progressbar2 music21 beautifulsoup4 nbstripout jupytercall nbstripout --install
::Add env to jupyter kernelcall python -m ipykernel install --user --name %env_name%

Lets explain more clearly what we are doing:

  • In Windows, “.bat” file (batch file) is a script file that contains a series of commands we wish to execute (see https://en.wikipedia.org/wiki/Batch_file for more details). It is similar to a Make file for Linux or Mac. Employing a batch file allows us to make the setup step much more reproducible, and also more ready to be automated if desired.
  • Double colons “::” indicates “comment”, which is where we drop note about the code.
  • The “call” keyword allows us to keep processing various commands without quitting the current batch file.

Once ready, reader can simply “double click” this batch file, which will execute it. However, we highly recommend the reader to learn how to execute this batch file from the command prompt of Windows. Linux/Mac users may be more familiar with navigating their computer via the command line window, while Windows users tend to be relying more on their “windows” and “mouse”. However, we highly recommend the reader to get familiar with the “Command Prompt” of Windows 10, and learn how to accomplish a lot of the navigation there. With Anaconda installed, you should have the “Anaconda Prompt” available, which is an even more convenient replacement of the default command prompt for Windows.

By now, we should have successfully setup the development environment, named “piano”, which can be activated by running the below command from your Anaconda Prompt (Command Prompt):

conda activate piano

Next time we will talk about how to obtain music data for training, using simple web scraping technique via Python:

Part-2: Obtain music data using simple web scraping technique via Python

--

--