An OpenAI Gym style toolkit for music generation experiments.
Project description
RaveForce
RaveForce is a Python package that allows you to define your musical task in Python with Glicol syntax, and train an agent to do the task with APIs similar to the OpenAI Gym.
Here is an interactive example on the Google Colab.
Why RaveForce
Let's consider a simple example: you want to train an agent to play the synth sequencer for you. The goal is to mimic a famous bass line. Therefore, in each step
, the agent
needs to make a decision on which note to play and what kind of timbre to make. The agent can have an observation
of what has been synthesised, and the reward
is calculated by comparing the similarity between the synthesised audio and the target at the moment.
Yet it can be very difficult and time-consuming to build a real-world environment (such as a music robot) to cover all the needs for electronic music. Another option is to use some built-in Python function to compose our music tasks
, but still, for each task, you need to write some DSP function chains which will be unlikely for these codes to be used again in the real world. A better way is to find a commonplace between our simulation and real-world music practices. Live coding is exactly such a practice where the artist performs improvised algorithmic music by writing program code in real-time. What if we train a virtual agent to write (part of the) code to synthesis a loop for us?
The architecture looks like this:
Agent
-> Play around the live coding code
-> Live coding engine does the non-real-time synthesis
-> Get the reward, observation space, etc.
This process should involve some deep neural network as the synthesised audio is much more difficult to process than the symbolic sequences.
Previously, SuperCollider is used for RaveForce. See paper:
Lan, Qichao, Jim Tørresen, and Alexander Refsum Jensenius. "RaveForce: A Deep Reinforcement Learning Environment for Music Generation." (2019).
But due to the speed limit of non-real-time synthesis on hard disk from SuperCollider, we switch to Glicol.
Glicol is a new live coding language that can be accessed in the browsers:
The syntax of Glicol is very similar to synth or sequencers, which perfectly fits our needs. Plus, Glicol is written in Rust and can be called in Python via WebAssembly (there are other methods but wasm is used since it shares the same format with Glicol js bindings).
How to use RaveForce
Install
This is quite straightforward:
pip install raveforce
Be familiar with Glicol syntax.
Visit Glicol website to get familiar with its syntax and concept:
Python
Since we are going to define our own musical task, we should make some changes to the make
method.
Let's consider the simplest example: just let the agent to play for 1 step, tweaking attack
, decay
and freq
of a sine wave synth to simulate a kick drum.
import raveforce
import librosa
target, sr = librosa.load("YOUR_KICK_DRUM_SAMPLE", sr=None)
dur = len(target) / sr
env = gym.make(
"""
~env: imp 0.1 >> envperc {} {}
kick_drum: sin {} >> mul ~env
""",
total_step=1,
step_len=dur,
target = target,
action_space=[
("lin", 0.0001, dur-0.0001),
("rel", 0, lambda x: dur-0.0001-x), # related to para 0
("exp", 10, 10000)
]
)
Then, use as a normal Gym
env:
observation = env.reset()
action = env.action_space.sample()
print(action)
observation, reward, done, info = env.step(action)
plt.plot(observation) # make your own import matplotlib
print(reward, done, info)
License
Glicol branch (main):
MIT License
SuperCollider branch:
GPL-3.0 License
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
Built Distribution
File details
Details for the file raveforce-0.2.4.tar.gz
.
File metadata
- Download URL: raveforce-0.2.4.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 696e7da38a93dfd42344cfb8ab304e1fcdf191a18d6af065aca88980e5d30261 |
|
MD5 | 295538d9c78abf71eded67322b76a596 |
|
BLAKE2b-256 | 0e88c13f606db6052ef9fef16437f401ee49dbf53ee9313336e9e2ee580c450c |
File details
Details for the file raveforce-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: raveforce-0.2.4-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d0fe60ab6a4a408a6533b9805973de488c1544a4befa8ccb18ddaef12034c4b |
|
MD5 | 1251a2560c3bd4e24f38b5c440495fd4 |
|
BLAKE2b-256 | f30891406b1c4798d9420d26427c393efbb19c90b08cac3639747a921d01206a |