Book Image

Hands-On Music Generation with Magenta

By : Alexandre DuBreuil
Book Image

Hands-On Music Generation with Magenta

By: Alexandre DuBreuil

Overview of this book

The importance of machine learning (ML) in art is growing at a rapid pace due to recent advancements in the field, and Magenta is at the forefront of this innovation. With this book, you’ll follow a hands-on approach to using ML models for music generation, learning how to integrate them into an existing music production workflow. Complete with practical examples and explanations of the theoretical background required to understand the underlying technologies, this book is the perfect starting point to begin exploring music generation. The book will help you learn how to use the models in Magenta for generating percussion sequences, monophonic and polyphonic melodies in MIDI, and instrument sounds in raw audio. Through practical examples and in-depth explanations, you’ll understand ML models such as RNNs, VAEs, and GANs. Using this knowledge, you’ll create and train your own models for advanced music generation use cases, along with preparing new datasets. Finally, you’ll get to grips with integrating Magenta with other technologies, such as digital audio workstations (DAWs), and using Magenta.js to distribute music generation apps in the browser. By the end of this book, you'll be well-versed with Magenta and have developed the skills you need to use ML models for music generation in your own style.
Table of Contents (16 chapters)
Section 1: Introduction to Artwork Generation
Section 2: Music Generation with Machine Learning
Section 3: Training, Learning, and Generating a Specific Style
Section 4: Making Your Models Interact with Other Applications

To get the most out of this book

This book doesn't require any specific knowledge about music or machine learning to enjoy, as we'll be covering all the technical aspects regarding those two subjects throughout the book. However, we do assume that you have some programming knowledge using Python. The code we provide is thoroughly commented and explained, though, which makes it easy for newcomers to use and understand.

The provided code and content works on all platforms, including Linux, macOS, and Windows. We'll be setting up the development environment as we go along, so you don't need any specific setup before we start. If you already are using an Integrated Development Environment (IDE) and a DAW, you'll be able to use them during the course of this book.

Download the example code files

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Each time the step operation is called, the RNN needs to update its state, the hidden vector, h."

A block of code is set as follows:

import os
import as mm

mm.notebook_utils.download_bundle("drum_kit_rnn.mag", "bundles")
bundle = mm.sequence_generator_bundle.read_bundle_file(
os.path.join("bundles", "drum_kit_rnn.mag"))

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

> drums_rnn_generate --helpfull

USAGE: drums_rnn_generate [flags]



Any command-line input or output is written as follows:

> drums_rnn_generate --bundle_file=bundles/drum_kit_rnn.mag --output_dir output

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "The main reason to stick with bar throughout this book is to follow Magenta's code convention, where bar is used more consistently than measure."

Warnings or important notes appear like this.
Tips and tricks appear like this.