Skip to main content

IMPSY is the Interactive Musical Prediction SYstem, a tool for creative interactive intelligent musical instruments using a recurrent mixture density neural network.

Project description

IMPSY: The Interactive Musical Predictive System

MIT License DOI Install and run IMPSY Coverage Status PyPI - Version

Predictive Musical Interaction

IMPSY is a system for predicting musical control data in live performance. It uses a mixture density recurrent neural network (MDRNN) to observe control inputs over multiple time steps, predicting the next value of each step, and the time that expects the next value to occur. It provides an input and output interface over OSC and can work with musical interfaces with any number of real valued inputs (we've tried from 1-8). Several interactive paradigms are supported for call-response improvisation, as well as independent operation, and "filtering" of the performer's input. Whenever you use IMPSY, your input data is logged to build up a training corpus and a script is provided to train new versions of your model.

Here's a demonstration video showing how IMPSY can be used with different musical interfaces:

Installation

IMPSY is written in Python with Keras and TensorFlow Probability, so it should work on any platform where Tensorflow can be installed. Python 3 is required and we use Poetry for managing dependencies. IMPSY currently relies on Python 3.11, TensorFlow 2.15.0, TensorFlow Probability 0.23.0, and keras-mdn-layer 0.3.0. You can see the dependencies in pyproject.toml.

To install IMPSY, first ensure that you have a Python 3.11 installation available, you might want to use pyenv to manage different Python versions. Then you need to install Poetry. The poetry install instructions vary depending on your preferences for a python setup this is likely to work on Linux, macOS or Windows (WSL):

curl -sSL https://install.python-poetry.org | python3 -

Then you should clone this repository or download it to your computer:

git clone https://github.com/cpmpercussion/impsy.git
cd impsy

Then you can install the dependencies using Poetry:

poetry install

Finally, you can test that IMPSY works:

poetry run ./start_impsy.py --help

How to use

There are four steps for using IMPSY. First, you'll need to setup your musical interface to send it OSC data and receive predictions the same way. Then you can log data, train the MDRNN, and make predictions using our provided scripts.

1. Connect music interface and synthesis software

You'll need:

  • A music interface that can output data as OSC.
  • Some synthesiser software that can take OSC as input.

These could be the same piece of software or hardware!

You need to decide on the number of inputs (or dimension) for your predictive model. This is the number of continuous outputs from your interface plus one (for time). So for an interface with 8 faders, the dimension will be 9.

Now you need your music interface to send messages to IMPSY over OSC. The default address for IMPSY is: localhost:5001. The messages to IMPSY should have the OSC address /interface, and then a float between 0 and 1 for each continuous output on your interface, e.g.:

/interface 0 0.5 0.23 0.87 0.9 0.7 0.45 0.654

For an 8-dimensional interface.

Your synthesiser software or interface needs to listen for messages from the IMPSY system as well. These have the same format with the OSC address /prediction. You can interpret these as interactions predicted to occur right when the message is sent.

Here's an example diagram for our 8-controller example, the xtouch mini controller.

Predictive Musical Interaction

In this example we've used Pd to connect the xtouch mini to IMPSY and to synthesis sounds. Our Pd mapping patch takes data from the xtouch and sends /interface OSC messages to IMPSY, it also receives /prediction OSC message back from IMPSY whenever they occur. Of course, whenever the user performs with the controller, the mapping patch sends commands to the synthesiser patch to make sound. Whenever /prediction messages are received, these also trigger changes in the synth patch, and we also send MIDI messages back to the xtouch controller to update its lights so that the performer knows what IMPSY is predicting.

So what happens if IMPSY and the performer play at the same time? In this example, it doesn't make sense for both to control the synthesiser at the same time, so we set IMPSY to run in "call and response" mode, so that it only makes predictions when the human has stopped performing. We could also set up our mapping patch to use prediction messages for a different synth and use one of the simultaneous performance modes of IMPS.

2. Log some training data

You use the run command to log training data. If your interface has N inputs the dimension is N+1:

poetry run ./start_impsy run --dimension (N+1) --log

This command creates files in the logs directory with data like this:

2019-01-17T12:37:38.109979,interface,0.3359375,0.296875,0.5078125
2019-01-17T12:37:38.137938,interface,0.359375,0.296875,0.53125
2019-01-17T12:37:38.160842,interface,0.375,0.3046875,0.1953125

These CSV files have the format: timestamp, source of message (interface or rnn), x_1, x_2, ..., x_N.

You can log training data without using the RNN with the mode option to select "user" if you like, or use a partially trained RNN and then collect more data.

poetry run ./start_impsy run --dimension (N+1) --log -mode user

Every time you use IMPS' "run" command, a new log file is created so that you can build up a significant dataset!

3. Train an MDRNN

There's two steps for training: Generate a dataset file, and train the predictive model.

Use the dataset command:

poetry run ./start_impsy dataset --dimension (N+1)

This command collates all logs of dimension N+1 from the logs directory and saves the data in a compressed .npz file in the datasets directory. It will also print out some information about your dataset, in particular the total number of individual interactions. To have a useful dataset, it's good to start with more than 10,000 individual interactions but YMMV.

To train the model, use the train command---this can take a while on a normal computer, so be prepared to let your computer sit and think for a few hours! You'll have to decide what size model to try to train: xs, s, m, l, xl. The size refers to the number of LSTM units in each layer of your model and roughly corresponds to "learning capacity" at a cost of slower training and predictions. It's a good idea to start with an xs or s model, and the larger models are more relevant for quite large datasets (e.g., >1M individual interactions).

poetry run ./start_impsy train --dimension (N+1) --modelsize s

It's a good idea to use the earlystopping option to stop training after the model stops improving for 10 epochs.

4. Perform with your predictive model

Now that you have a trained model, you can run this command to start making predictions:

poetry run ./start_impsy run --dimension (N+1) --modelsize xs --log

The --log switch logs all of your interactions as well as predictions for later re-training. (The dataset generator filters out RNN records so that you only train on human sourced data).

PS: all the IMPSY commands respond to the --help switch to show command line options. If there's something not documented or working, it would be great if you add an issue above to let me know.

Using Docker to run IMPSy

We provide the docker image charlepm/impsy which includes IMPSY with Poetry and required libraries installed.

You can use the docker image to try out impsy or even use it in production if you are using OSC communication to a sound source or musical interface. MIDI doesn't work in the docker container (not sure how this could be achieved but if someone has a good idea...). The docker container is defined at examples/Dockerfile.

We also have a docker compose file to start IMPSY as well as the web user interface: docker-compose.yml

Docker compose configuration

From the IMPSY main directory run:

docker compose -f docker-compose.yml up

Then you can navigate to http://127.0.0.1:4000 to view the web interface. OSC communication happens through ports 5000 and 5000. The local config.toml, and datasets, logs and models directories are mapped into the docker containers.

Using docker to create datasets or train models

You can run a docker container and use different impsy commands from the command line as well:

docker run -d -v $(pwd)/datasets:/code/datasets -v $(pwd)/logs:/code/logs -v $(pwd)/models:/code/models -v $(pwd)/config.toml:/code/config.toml charlepm/impsy:0.5.3 poetry run ./start_impsy.py --help

This can be useful to use the dataset or train commands to generate new datasets and models.

More about Mixture Density Recurrent Neural Networks

IMPSY uses a mixture density recurrent neural network MDRNN to make predictions. This machine learning architecture is set up to predict the next in a sequence of multi-valued elements. The recurrent neural network uses LSTM units to remember information about past inputs and use this to help make decisions. The mixture density model at the end of the network allows continuous multi-valued elements to be sampled from a rich probability distribution.

The network is illustrated here---every time IMPSY receives an interaction message from your interface, it is sent to thorugh the LSTM layers to produce the parameters of a Gaussian mixture model. The predicted next interaction is sampled from this probability model.

A Musical MDRNN

The MDRNN is written in Keras and uses the keras-mdn-layer package. There's more info and tutorials about MDNs on that github repo.

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

impsy-0.5.4.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

impsy-0.5.4-py3-none-any.whl (39.2 kB view details)

Uploaded Python 3

File details

Details for the file impsy-0.5.4.tar.gz.

File metadata

  • Download URL: impsy-0.5.4.tar.gz
  • Upload date:
  • Size: 35.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.3 Darwin/23.6.0

File hashes

Hashes for impsy-0.5.4.tar.gz
Algorithm Hash digest
SHA256 4aeddb78bf12fc8ff8124e822ed1a7508b5990c5b3c9f4b361ed3224d62e42f3
MD5 c2468b4052ec090f7f0f2b6c879760cc
BLAKE2b-256 bbaca1d5244e17b38528ed182cfb74dfd59428cdce33c43a8b1d127ce3469449

See more details on using hashes here.

File details

Details for the file impsy-0.5.4-py3-none-any.whl.

File metadata

  • Download URL: impsy-0.5.4-py3-none-any.whl
  • Upload date:
  • Size: 39.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.3 Darwin/23.6.0

File hashes

Hashes for impsy-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7dc94b52eb7bc6c526b4a51b6ad9f057d0ea968122f011ec98bd73f08ea616b0
MD5 73e1d64ee0f5644a65e5d0ef8141c635
BLAKE2b-256 0a135d73b7d6aef516612783d4c8ec6307affb09e872fcf55c2d981955e20a4d

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