Skip to main content

Unit tests Pylint License: MIT

Bach music generator

A Bach music generator using a neural network trained with a genetic algorithm.

Motivation

The neural network was setup and trained from scratch. The main motivation behind this was learning how one might code a neural network without using a library abstracting away all the details. It's obviously not very efficient, because it is not optimized for speed and doesn't use the GPU (so if you want to run this, I hope you have a fast CPU), but this doesn't matter, as speed is not within the scope of this experiment.

Getting started

Install the package using pip, then run it with no arguments to open the graphical user interface:

python -m pip install bach_generator
python -m bach_generator

Bypass the graphical user interface by calling the package and passing a midi filepath to it:

python -m bach_generator <midi_filepath>

For more CLI options, please see the CLI documentation or run the help command:

python -m bach_generator -h

Algorithm

At the start, several separate neural networks are instantiated and fed the input data. The top ranked models (based on the correlation of their output to the input) get cloned into the next generation with randomly adjusted weights. This process is repeated over a number of generations.

Currently, the neural network iterates through all notes in a given midi file, feeding the neural network the current note and a calibratable number of past notes. Each node in the model is connected to all nodes from the previous network. When inputs are fed in to the model, they arrive at the input layer, get multiplied by the weights of the input nodes, then get propagated to the next layer. Each node in the middle layers averages all values it receives, then weights that value and propagates it further.

This mixture of past and present inputs makes this stateless model behave as if it was stateful, e.g. like a recurrent neural network (RNN).

Finally, the output values are received, decoded from numbers into notes and written to an output midi file.

Currently the node decoding happens using a simple frequency ranking analogue to the input note frequency. The rhythms in the input midi file are retained in the output.

Training data

Training data was downloaded from www.jsbach.net and is not included in this repository.

A pre-trained object model (reaching 78% correlation for the entire Goldberg variations BWV 988, with over 90% correlation for some variations) can be found here. Load and run further simulations with it by using the CLI --load flag:

python -m bach_generator --load=models/models.json --layer-type=object <midi_filepath>

Command line interface

The Bach generator package supports a number of command line interface arguments, including handling output, model save/load, and parameters regarding model size and cloning.

A brief overview of the CLI is shown below:

usage: __main__.py [-h] [--generations GENERATIONS] [--save]
                   [--load LOAD_FILEPATH] [--load-best LOAD_BEST]
                   [--models MODELS] [--inputs INPUTS] [--layers LAYERS]
                   [--layer-size LAYER_SIZE] [--select-models SELECT_MODELS]
                   [--clones CLONES] [--weight-jumble-by {offset,selection}]
                   [--weight-divergence WEIGHT_DIVERGENCE]
                   [--write-interval WRITE_INTERVAL] [--output-dir OUTPUT_DIR]
                   [--seed SEED]
                   filepath

positional arguments:
  filepath              The filepath to the midi file to be analysed

optional arguments:
  -h, --help            show this help message and exit

To see the full CLI documentation, run the CLI help command:

python -m bach_generator -h

Running the package without specifying any CLI arguments with python -m bach_generator instead opens the graphical user interface:

Screenshot of the analysis GUI

Contributions

To contribute, please read the contribution guidelines.

Python

Written in Python 3.8.8.

License

This repository is open-source software available under the MIT License.

Contact

Please raise an issue for code changes. To reach out, please send an email to richard@baltrusch.net.

Metadata

Release files for bach-generator 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bach-generator 1.2.1
File Size Uploaded
bach_generator-1.2.1.tar.gz 26.0 kB Details

Release files / bach_generator-1.2.1.tar.gz

Download URL bach_generator-1.2.1.tar.gz
Size 26.0 kB
Tags Source
SHA-256 checksum
How to use checksums
989b661fb6da2bdd8c61ecc2e246d50ca888f509cc2aab68826e017f3c3ee5e4
BLAKE2b-256 checksum
How to use checksums
4b14acbd7455a8574a8fa50708444722bd7e79f419165488afcf1fdc3edc90c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.8

Release history Release notifications | RSS feed

This release

1.2.1 This release

1 release file

1.2.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page