Skip to main content

Machine learning based Bach music generator

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bach_generator-1.2.0.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

bach_generator-1.2.0-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file bach_generator-1.2.0.tar.gz.

File metadata

  • Download URL: bach_generator-1.2.0.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for bach_generator-1.2.0.tar.gz
Algorithm Hash digest
SHA256 cde9daddb2ac79206de6a2edebc56a9cc5ee60516a6fd7749ed4d9d301977389
MD5 90cb80dd35f3211a83a7a6e4df315fe2
BLAKE2b-256 f0cecba919db74a518d145fc09e9cc53d0efab6ae1ace0612b54eae2c3e89c24

See more details on using hashes here.

File details

Details for the file bach_generator-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bach_generator-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 119e308970b90f02218a97b1116fd0b2230d4fa5666350a6392394a1edaa137b
MD5 f2714979261f4f2f5020f442ae7ed8dc
BLAKE2b-256 eca755600112d959e11494473cab6ece67cbb6a0dc4a210dc2bac8197148d0a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page