Song of the Machines (1): Sampling musical sections
Published: 2022-06-30 13:00:52

---
title: Song of the Machines (1): Sampling musical sections
published: true
date: 2022-06-30 06:52:23 UTC
tags: 
canonical_url: https://dataroots.io/research/contributions/song-of-the-machine-1
---

Can 4 Dataroots colleagues without professional music production experience write hit songs with AI? In this blogpost series the Beatroots team members uncover how they wrote their latest song, _Song of the Machines_, which they submitted for the 2022 AI Song Contest.

<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&amp;url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1279304755&amp;show_artwork=true"></iframe>

## AI Song Contest

The [AI Song Contest](https://www.aisongcontest.com/) is an international music competition exploring the use of AI in the songwriting process. We have participated with Beatroots since its inception in 2020.

The premise for the contest is to write songs with, but not limited to, AI. The winner is voted in 2 phases. First, a panel of experts decides on a top 15 based on a diverse set of criteria (eg. overall song quality, use of AI tools, creativity, ...). Next, the voting very much resembles the procedure at the Eurovision song contest: 50% jury and 50% public. Unfortunately, we didn't make it to the final. You can listen to the finalists and cast your votes [on the official website](https://www.aisongcontest.com/the-2022-finalists) by the 30th of June.

For the past two years, we have had a particular focus to keep humanoid interference to a bare minimum. Our 2020 submission _Violent Delights Have Violent Ends_ was generated entirely by a notebook script and our 2021 song _[Robotic Love](https://www.youtube.com/watch?v=K4vx_Y4c6WY)_ was a remixed version of a cherry picked sample of a finetuned [Jukebox](https://openai.com/blog/jukebox/) model.

This year, our vision was to develop and fine-tune AI tools as "creative partners" in music-making. Again, we have used as much AI-generated output as possible, from lyrics to melodies to beats. Meanwhile, the sole responsibility of the humble humanoid was rearranging and producing the generated samples in a DAW (_Digital Audio Workstation_). Once our code is cleaned and peer reviewed, we will maintain it as an open source repository on Github. Stay tuned!

<iframe style="border-radius: 12px" width="100%" height="380" title="Spotify Embed: Beatroots AI" frameborder="0" allowfullscreen allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" src="https://open.spotify.com/embed/playlist/6aFhKJVgbRBmTPPkG3dVTu?si=d104260cc7e64203&amp;utm_source=oembed"></iframe>

## Workflow

At first, we have researched promising models in the open source community. It is truly astounding to see the progress and variety of AI music generation tools over the last two years. One day we could take the time to write up a survey on this, but for now it suffices to say that Google (with their Magenta project) and OpenAI (with Jukebox, MuseNet, GPT-3, ...) take the cake.

Next, we decided on which of these tools are the most promising and could realistically be mastered and optionally finetuned within the time constraints of the competition.

Once the deadline for submission came creeping in (read: 1 week before handing in) we put an abrupt halt to the development phase and entered the most fun part of the project: songwriting.

![](https://lh3.googleusercontent.com/E_XD1s1FCzXZhYLf6qel1zLnJgXJX6bXSLnOkiyNMi30mJ9WBy0vwNi4V2EOGzXUSKdr-hqrUYceobTqD8ScNRGlHQRn6Pe7f9Wb01F4Z0xQGKdobs8XMs4zsRPS2RSqtiYdR5k)
_Song of the Machines: Songwriting workflow_

The final workflow to create our hit song is described in the flowchart above.

We didn’t know what to call our song, which is why we asked [GPT-3](https://openai.com/api/) to give us cool names instead. The question we asked our dear Android friend was ‘Generate multiple creative titles for a song which would win the AI song contest’. GPT-3 gave us quite a lot of candidate titles ('Beyond the Binary' comes to mind), but we liked ‘Song of the Machines’ the best.

Subsequently, we combined inputs and outputs from our designated tools:

1. Generating musical sections with **DoriVAE** as a starting point
2. Generating harmonies on DoriVAE sampled melodies with CocoNet and **CocoZoë**
3. Generating pop lyrics and chords with **GPT-Sandy**
4. Enriching our outputs with the free **Magenta plugin for Ableton**

In the remainder of this article, we zoom in on (1). (2), (3) and (4) are subjects for upcoming posts!

## MusicVAE: A Variational Auto Encoder (_VAE_) for music

With MusicVAE, an open source model published by Magenta, one can:

- randomly sample music note sequences
- interpolate between sequences ('eg. get a mixture of two melodies')
- manipulate existing sequences with attribute vectors ('eg. make beats less complex')

The core idea to achieve these feats is to train a Neural Network architecture to compress music (encoder) and rebuild (decoder) the original input with the additional constraint that the representations in the the compressed space (also called _latent space_) should be similar if the music is similar as well (and different if the music is different). This is what we call a Variational Auto Encoder (VAE).

If you want to learn more about the subject, Magenta has written an [excellent blog post](https://magenta.tensorflow.org/music-vae) about this.

One of the models Magenta has pre-trained is for 16-bar music with three tracks: melody, bass and drums. Since this generates the most tracks in one go it's the one we have chosen to interact with.

Below you can listen to trio sample generated for this blogpost with MusicVAE:

 ![audio-thumbnail](https://dataroots.io/research/contributions/song-of-the-machine-1)
<svg width="24" height="24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M7.5 15.33a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm-2.25.75a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0ZM15 13.83a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm-2.25.75a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.486 6.81A2.25 2.25 0 0 1 17.25 9v5.579a.75.75 0 0 1-1.5 0v-5.58a.75.75 0 0 0-.932-.727.755.755 0 0 1-.059.013l-4.465.744a.75.75 0 0 0-.544.72v6.33a.75.75 0 0 1-1.5 0v-6.33a2.25 2.25 0 0 1 1.763-2.194l4.473-.746Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M3 1.5a.75.75 0 0 0-.75.75v19.5a.75.75 0 0 0 .75.75h18a.75.75 0 0 0 .75-.75V5.133a.75.75 0 0 0-.225-.535l-.002-.002-3-2.883A.75.75 0 0 0 18 1.5H3ZM1.409.659A2.25 2.25 0 0 1 3 0h15a2.25 2.25 0 0 1 1.568.637l.003.002 3 2.883a2.25 2.25 0 0 1 .679 1.61V21.75A2.25 2.25 0 0 1 21 24H3a2.25 2.25 0 0 1-2.25-2.25V2.25c0-.597.237-1.169.659-1.591Z"></path></svg>

<audio src="https://dataroots.ghost.io/content/media/2022/06/musicvae-trio-sample.wav" preload="metadata"></audio>
MusicVAE trio sample

<button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"></path></svg></button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><rect x="3" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect><rect x="14" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect></svg></button>0:00
/0:33
<input type="range" max="100" value="0"><button>1×</button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z"></path></svg></button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z"></path></svg></button><input type="range" max="100" value="100">

Although remarkable in its capability to generate short musical sequences the entire sample lacks musical coherence across its full length. A famous music design principle popularised by Youtube star Adam Neely is that '[Repetition legitimises](https://www.youtube.com/watch?v=LlmTWlaWs_o)'. This generated sample feels lawless to me.

Ironic due to its apparent simplicity, repetition is one of the fundamental challenges for current music generation machine learning algorithms, especially for long term structures. The model does not know when to repeat or only slightly adapt an earlier motif. This is why the outputs from state of the art models sound more like a 'fluid hallucinations' instead of actual 'songs'.

Most radio hit songs also have a repetitive and predictable structure, for example 'verse-chorus-verse-chorus-bridge-chorus' is a well known success formula handled by countless songwriters and producers. Each of these section types have their own characteristics and style traits, for example the best melody should be in the chorus, the verse tells the story, the bridge connects verse to chorus, ... MusicVAE is indifferent to these subtleties (although one could argue to use attribute vectors).

Currently, there is no publicly available dataset that aligns musical sections to theirs scores. Luckily, we were able to reuse such a dataset on 200 Eurovision songs which we received for the 2020 AI song contest. This is a small dataset, which poses additional challenges on allowed model complexity.

To deal with small datasets, generate more cohesive musical ideas and introduce the notion of sections into MusicVAE, we first developed MiniVAE in 2020. For Song of the Machines, we refined and rebranded MiniVAE to DoriVAE.

## DoriVAE: Nested Latent Spaces

![](https://dataroots.ghost.io/content/images/2022/06/dorivae.png)
_DoriVAE model architecture_

The main idea is to train smaller VAEs that are fed the encodings from MusicVAE as input, and yield their reconstructions as output. Each new VAE is specialised and trained to one specific music type, genre, artist, ... In our use case each VAE is specialised in musical sections and represents a 16-bar trio (melody, bass and drums). This is an idea inspired on [MidiMe](https://magenta.tensorflow.org/midi-me).

We opted to reduce the existing latent space (which has a dimension of 512) by encoding this into a nested, smaller latent space with a dimension of 16. This nested latent space is optimised to contain only the musical structures we request (eg. all samples in the mini latent space represent a catchy chorus).

To generate new samples, we simply need to sample from our nested space and then decode twice: first with our custom trained section decoder, finally with the pretrained MusicVAE decoder. In total, we trained 6 additional VAEs This is how we generated the melody for the chorus in _Song of the Machines:_

 ![audio-thumbnail](https://dataroots.io/research/contributions/song-of-the-machine-1)
<svg width="24" height="24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M7.5 15.33a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm-2.25.75a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0ZM15 13.83a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm-2.25.75a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.486 6.81A2.25 2.25 0 0 1 17.25 9v5.579a.75.75 0 0 1-1.5 0v-5.58a.75.75 0 0 0-.932-.727.755.755 0 0 1-.059.013l-4.465.744a.75.75 0 0 0-.544.72v6.33a.75.75 0 0 1-1.5 0v-6.33a2.25 2.25 0 0 1 1.763-2.194l4.473-.746Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M3 1.5a.75.75 0 0 0-.75.75v19.5a.75.75 0 0 0 .75.75h18a.75.75 0 0 0 .75-.75V5.133a.75.75 0 0 0-.225-.535l-.002-.002-3-2.883A.75.75 0 0 0 18 1.5H3ZM1.409.659A2.25 2.25 0 0 1 3 0h15a2.25 2.25 0 0 1 1.568.637l.003.002 3 2.883a2.25 2.25 0 0 1 .679 1.61V21.75A2.25 2.25 0 0 1 21 24H3a2.25 2.25 0 0 1-2.25-2.25V2.25c0-.597.237-1.169.659-1.591Z"></path></svg>

<audio src="https://dataroots.ghost.io/content/media/2022/06/very_nice_chorus_1.wav" preload="metadata"></audio>
Very nice chorus

<button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"></path></svg></button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><rect x="3" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect><rect x="14" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect></svg></button>0:00
/0:16
<input type="range" max="100" value="0"><button>1×</button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z"></path></svg></button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z"></path></svg></button><input type="range" max="100" value="100">

or the intro melody (which we slightly modified in the DAW):

 ![audio-thumbnail](https://dataroots.io/research/contributions/song-of-the-machine-1)
<svg width="24" height="24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M7.5 15.33a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm-2.25.75a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0ZM15 13.83a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm-2.25.75a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.486 6.81A2.25 2.25 0 0 1 17.25 9v5.579a.75.75 0 0 1-1.5 0v-5.58a.75.75 0 0 0-.932-.727.755.755 0 0 1-.059.013l-4.465.744a.75.75 0 0 0-.544.72v6.33a.75.75 0 0 1-1.5 0v-6.33a2.25 2.25 0 0 1 1.763-2.194l4.473-.746Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M3 1.5a.75.75 0 0 0-.75.75v19.5a.75.75 0 0 0 .75.75h18a.75.75 0 0 0 .75-.75V5.133a.75.75 0 0 0-.225-.535l-.002-.002-3-2.883A.75.75 0 0 0 18 1.5H3ZM1.409.659A2.25 2.25 0 0 1 3 0h15a2.25 2.25 0 0 1 1.568.637l.003.002 3 2.883a2.25 2.25 0 0 1 .679 1.61V21.75A2.25 2.25 0 0 1 21 24H3a2.25 2.25 0 0 1-2.25-2.25V2.25c0-.597.237-1.169.659-1.591Z"></path></svg>

<audio src="https://dataroots.ghost.io/content/media/2022/06/minivae_real_intro.wav" preload="metadata"></audio>
DoriVAE real intro

<button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"></path></svg></button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><rect x="3" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect><rect x="14" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect></svg></button>0:00
/0:18
<input type="range" max="100" value="0"><button>1×</button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z"></path></svg></button><button><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path d="M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z"></path></svg></button><input type="range" max="100" value="100">

## Conclusion

Creating nested latent spaces with MusicVAE is an effective method to create specialised models to sample music the way you want. DoriVAE fed the team with 3 tracks (melody, bass and drum) and 16 bars (32 seconds at 120BPM) of inspiration per generated sample, which we then cherry picked and adjusted to our liking.

The journey didn't end here. Stay tuned for the upcoming blogposts by our team on how we enriched our trio samples with contemporary harmonisations, finetuned transformers to generate lyrics with a musical context and put it all together within a DAW.