midi-machine
Process MIDI files and use them to train learning machines that generate music in the style of the input data.
Dependencies
- Torch7
- Bash
- timidity
- jsb Chorales dataset
The timidity
player will play midi files generated by the midi-machine
. It
is available via homebrew on the Mac platform and via
apt-get
on Ubuntu. Check your preferred package manager.
Installation
Install Torch7 and timidity
. Then clone the
midi-machine
repository using
$ git clone https://github.com/blr246/midi-machine.git
Get the jsb chorales data.
$ wget http://www.jsbchorales.net/down/sets/jsb403.zip
and extract it on your machine. A good location is a folder called midi
in
the root of this repository.
If your environment is configured properly, the proceed to the Usage section.
Usage
There are a number of scripts in the bin
directory. Each
of these tools allows for training and testing various models.
All of the bin
scripts have command-line help strings. Run the command without
arguments to read its usage string as in
$ th ./bin/main.lua
To train a simple model, run
$ th ./bin/main.lua MIDI_DIR TIME_SIGNATURE OUTPUT_DIR
The MIDI_DIR
is the location of the jsb chorales data. A TIME_SIGNATURE
is a string such as 4/2-8-24-4-256
. This string indicates the meter of the
music (4/2), the number of 32 notes per quarter note (8), the number of
metronome ticks per click (24), the number of channels (4), and the GCD of all
note event times (256).
The model and an example midi file will be written to OUTPUT_DIR
. To play any
midi file, run
$ timidity PATH_TO_MIDI
To hear several example songs generated by a model, use the jukebox.sh
script
as in
$ ./bin/jukebox.sh MIDI_DIR MODEL_PATH LENGTH
where MODEL_PATH
is the model saved by the training job and LENGTH
is the
number of time steps to generate. The script will save generated songs to the
/tmp
directory and play them in turn.
Note that jukebox.sh
is a wrapper for compose.lua
.
Some reasonable examples
To generate some machines that make reasonable music, try
$ th bin/main.lua -h 32 -t iso -o 5 -s 0.2 -w l2 midi/ 4/2-8-24-4-512 experiments/
or
$ th bin/main.lua -h 32 -t iso -o 20 -r -s 0.2 -w l2 midi/ 4/2-8-24-4-512 experiments/
Note where the example song and model are saved. You can play the example song
with timidity
or use the model with ./bin/jukebox.sh
to generate songs.
The mid library
A major component of the midi-machine
is the mid
library located in the
lua/mid/
directory. The library contains the following core functions:
mid.data.read()
Read a .mid
file as extracted events.
mid.data.write()
Write events to a .mid
file.
mid.dataset.load()
Load a dataset used with Feedforward models.
mid.dataset.load_rnn()
Load a dataset used RNN models.
mid.dataset.compose()
Generate a song from predicted data.
Modules
There are several smaller modules in the lua
directory.
models.lua
Contains setup for network types and as well as train_model()
and predict()
helpers.
RluMax.lua
A simple rectified linear unit that takes only positive values.
PerceptualLoss.lua
An experimental module for creating a perceptual loss function.
Rnn.lua
A general Recurrent Neural Network module that allows backpropagation through time training.