DEV Community

Cover image for How to turn text into music with Facebook's MusicGen
Mike Young
Mike Young

Posted on • Originally published at notes.aimodels.fyi

How to turn text into music with Facebook's MusicGen

MusicGen allows anyone to generate original music with just a text prompt. In this guide, I'll walk you through how to use this creative AI model to enhance your musical workflow.

Subscribe or follow me on Twitter for more content like this!

Have you ever struggled with writer's block when composing a new song? Or do you want an AI assistant to help you brainstorm new melodies and harmonies? Facebook's MusicGen model makes musical ideation and experimentation quick and easy.

We'll look at:

  • MusicGen's capabilities for generating music from text

  • Step-by-step instructions for using MusicGen via Replicate's API

  • Finding similar music composition models with AIModels.fyi

Let's see how MusicGen can unlock new creative possibilities for musicians, composers, and anyone looking to generate unique, production-ready music.

Generate Original Music with Text Prompts

MusicGen allows you to generate musical ideas just by describing the mood, genre, instruments, etc. in a text prompt. Here are some of the creative ways you can use text-to-music generation:

  • Overcome writer's block - Get new melodic or harmonic ideas based on a text description when you're stuck.

  • Experiment and iterate - Easily try variations by tweaking the text prompt.

  • Explore new genres - Generate music in styles you're less familiar with.

  • Produce background music - Create custom background music for videos, podcasts, games, and more.

  • Remix melodies - Prime the model with an existing melody to generate variations.

MusicGen delivers production-ready clips, in your choice of .mp3 or .wav format up to 8 seconds long. The samples can be used as inspirational sketches or incorporated directly into a composition.

About the MusicGen Model

MusicGen was created by Facebook's AI Research team in 2023. It's an auto-regressive transformer model trained on licensed music data.

The model generates 4 parallel melodic streams representing different musical elements like bass, drums, harmony, etc. This structure allows it to produce musically coherent compositions in a variety of genres and styles.

MusicGen offers a few different model sizes. The base model used on Replicate is "Melody" which is optimized for text-to-audio generation. There is also a larger model optimized for melody continuation.

You can learn more details about the model architecture in the Facebook Research paper and on the project GitHub page.

Model Limitations

Like all AI models, MusicGen has a few limitations:

  • MusicGen's output is influenced by the provided prompts and melodies. Creative inputs can greatly impact the quality and uniqueness of generated compositions.

  • The model's training data affects its musical style and output. It may not perfectly replicate every musical genre or style.

  • MusicGen's generated music might require post-processing to achieve the desired level of polish and refinement.

Understanding these limitations will help you make the most of MusicGen while also managing your expectations (or those of your customers).

Understanding the Inputs and Outputs of MusicGen

Inputs

  • model_version: Choose the model version for generation (e.g., "melody," "large," "encode-decode").

  • prompt: Provide a description of the music you want to generate.

  • input_audio: Influence the generated music by providing an audio file.

  • duration: Specify the duration of the generated audio.

  • continuation: Choose whether the generated music should continue the melody of the input audio.

  • Other parameters like top_k, top_p, temperature, and more allow you to fine-tune the output.

Outputs

The output schema is a string representing a URI that points to the generated audio file.

Step-by-Step Guide to Using the MusicGen Model

In this section, we'll walk through a detailed step-by-step process to effectively use the MusicGen model for generating music compositions. Each step is accompanied by specific code snippets and explanations of what is happening.

Step 1: Install the Node.js Client

To begin, you'll need to install the Node.js client for Replicate. This client will enable you to interact with the Replicate API and run the MusicGen model.

npm install replicate
Enter fullscreen mode Exit fullscreen mode

This command installs the necessary Node.js package named "replicate."

Step 2: Set Up API Token

Before you can access the Replicate API, you need to set up your API token as an environment variable. This token will authenticate your requests to the API.

export REPLICATE_API_TOKEN=your_api_token
Enter fullscreen mode Exit fullscreen mode

Replace your_api_token with your actual Replicate API token.

Step 3: Run the Model and Generate Music

Now, let's run the MusicGen model to generate music compositions based on specified inputs. We'll use the Node.js client to make API requests.

import Replicate from "replicate";

// Create a Replicate client instance
const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,
});

// Define input parameters for the model
const modelVersion = "melody";
const prompt = "Expressive piano melody";
const duration = 10; // Duration of the generated audio in seconds

// Run the MusicGen model
const output = await replicate.run(
  "facebookresearch/musicgen:7a76a8258b23fae65c5a22debb8841d1d7e816b75c2f24218cd2bd8573787906",
  {
    input: {
      model_version: modelVersion,
      prompt: prompt,
      duration: duration,
      // Other input parameters here
    },
  }
);

console.log("Generated audio URI:", output);
Enter fullscreen mode Exit fullscreen mode

In this code snippet:

  • We import the Replicate class from the installed Node.js package.

  • We create an instance of the Replicate client using your API token.

  • We define the modelVersion, prompt, and duration for the music generation.

  • We use the replicate.run() method to run the MusicGen model with the specified inputs.

  • The generated audio URI is logged to the console.

Step 4: Exploring Generated Audio

After running the model, you'll receive an audio URI pointing to the generated music composition. You can use this URI to access and explore the generated audio.

That's it! At this point, you have successfully utilized the MusicGen model to create a music composition based on your inputs.

Conclusion

Congratulations! You've successfully completed the step-by-step guide to using the MusicGen model for music composition. By following these instructions, you've harnessed the power of AI to generate unique and creative musical compositions. As you continue your journey into the world of AI-driven music, don't hesitate to experiment with different inputs and parameters to explore a wide range of musical possibilities. If you have any questions or need further assistance, feel free to reach out or refer to the resources mentioned in this guide. Happy music composition with AI!

Subscribe or follow me on Twitter for more content like this!

Further Reading

If you're interested in exploring audio-related topics, here are some relevant articles that delve into AI applications for audio generation, manipulation, and analysis:

  1. Audioldm: Text-to-Audio Generation with Latent Diffusion Models

  2. Bark Tortoise TTS: Generating Text-to-Speech with AI

  3. Converting Speech into Text with OpenAI's Whisper Model

  4. Learn How to Harness the Power of AI for Lip-Syncing Videos with This Comprehensive Guide

  5. Audio LDM: AI Text-to-Audio Generation with Latent Diffusion Models

Top comments (0)