Music generation

更新时间:
复制 MD 格式

Fun-Music generates complete songs with male or female vocals in Chinese or English from a text prompt describing the music style and scene, or from custom lyrics. It also supports instrumental music generation.

Important

This model is currently in limited preview. Apply for access on the Model Gallery before use. This model is available only in the China (Beijing) region.

Overview

Fun-Music is an end-to-end music generation model. Provide a natural language description or custom lyrics, and it returns a complete song or instrumental track:

  • prompt: Describe the music style, scene, mood, and instrument preferences. The model writes lyrics and generates a song automatically.

  • lyrics: Provide custom lyrics. The model composes and performs a song based on your lyrics.

  • gender: Select male or female vocals (fun-music-v1 only).

  • is_instrumental=true: Generate instrumental music (no vocals).

  • Streaming and non-streaming output

  • MP3 and WAV audio output

Differences between the two models

Fun-Music provides two models with the following differences:

Feature

fun-music-v1

fun-music-preview

prompt

Required (at least one of prompt or lyrics)

Required

lyrics

Optional if prompt is provided; if both are supplied, lyrics takes effect

Optional; if provided, lyrics takes priority

Instrumental music

Supported (is_instrumental=true)

Supported (is_instrumental=true)

Vocal gender (gender)

Supported

Not supported

Audio output formats

Set the format parameter to specify the output format:

Format

Characteristics

Use cases

mp3

Lossy compression, small file size

Streaming, online playback, storage

wav

Lossless format, larger file size

Post-production, high-quality playback

Prerequisites

  • An API key. For more information, see Get an API key.

  • The API key configured as an environment variable (recommended):

    export DASHSCOPE_API_KEY="sk-xxx"
Note

Replace {WorkspaceId} in the sample code with your actual workspace ID. For more information, see Workspace management.

Quick start

The following examples demonstrate three typical use cases.

Generate a song from a prompt

Pass the prompt parameter with a description of the music style and scene. The model writes the lyrics and composes the song.

curl

curl -X POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "fun-music-v1",
    "input": {
        "prompt": "Fresh summer folk song, acoustic guitar and harmonica accompaniment, upbeat tempo, suitable as background music for travel vlogs",
        "gender": "female"
    }
}'

Python

import requests
import os
import json

api_key = os.getenv("DASHSCOPE_API_KEY")
url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation"

response = requests.post(url,
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    },
    json={
        "model": "fun-music-v1",
        "input": {
            "prompt": "Fresh summer folk song, accompanied by acoustic guitar and harmonica, upbeat tempo, suitable as travel vlog background music",
            "gender": "female"
        }
    }
)

result = response.json()
audio_url = result["output"]["audio"]["url"]
print(f"Music generated successfully! Download URL: {audio_url}")

Java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class FunMusicDemo {
    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        String endpoint = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation";

        HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + apiKey);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        String jsonBody = "{\"model\":\"fun-music-v1\","
            + "\"input\":{\"prompt\":\"Fresh summer folk song, accompanied by acoustic guitar and harmonica, upbeat tempo, suitable as travel vlog background music\","
            + "\"gender\":\"female\"}}";

        try (OutputStream os = conn.getOutputStream()) {
            os.write(jsonBody.getBytes("UTF-8"));
        }

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            System.out.println(sb.toString());
        }
    }
}

Generate a song from lyrics

Pass the lyrics parameter with your lyrics. The model composes and generates a song based on them.

curl

curl -X POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "fun-music-v1",
    "input": {
        "lyrics": "[verse]\nMorning sunlight streams through the curtains,\nThe aroma of coffee fills the room.\nOpening a book left unfinished yesterday,\nTime quietly slips away like this.\n\n[chorus]\nTake it slow, no need to rush,\nLife should be this easygoing.\nToss all the worries into the wind,\nEmbrace every sunny day and rainy season.",
        "gender": "female"
    }
}'

Python

import requests
import os
import json

api_key = os.getenv("DASHSCOPE_API_KEY")
url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation"

lyrics = """[verse]
Morning sunlight filters through the curtains,
The aroma of coffee fills the room.
Opening the book left unfinished yesterday,
Time quietly slips away.

[chorus]
Take it slow, no rush,
Life should be this easy.
Toss your worries into the wind,
Embrace every sunny day and rainy season."""

response = requests.post(url,
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    },
    json={
        "model": "fun-music-v1",
        "input": {
            "lyrics": lyrics,
            "gender": "female"
        }
    }
)

result = response.json()
audio_url = result["output"]["audio"]["url"]
print(f"Music generated successfully! Download URL: {audio_url}")

Java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class FunMusicLyricsDemo {
    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        String endpoint = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation";

        HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + apiKey);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        String lyrics = "[verse]\\nMorning sunlight filters through the curtains,\\n"
            + "The aroma of coffee fills the room.\\n"
            + "Opening the book left unfinished yesterday,\\n"
            + "Time quietly slips away.\\n\\n"
            + "[chorus]\\nTake it slow, no rush,\\n"
            + "Life should be this easy.\\n"
            + "Toss your worries into the wind,\\n"
            + "Embrace every sunny day and rainy season.";

        String jsonBody = "{\"model\":\"fun-music-v1\","
            + "\"input\":{\"lyrics\":\"" + lyrics + "\","
            + "\"gender\":\"female\"}}";

        try (OutputStream os = conn.getOutputStream()) {
            os.write(jsonBody.getBytes("UTF-8"));
        }

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            System.out.println(sb.toString());
        }
    }
}

Generate instrumental music

Set is_instrumental to true to generate vocal-free instrumental music. When is_instrumental is set to true, the lyrics and gender parameters are ignored.

curl

curl -X POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "fun-music-preview",
    "input": {
        "prompt": "Serene piano piece, suitable as background music for late-night reading, soothing and elegant",
        "is_instrumental": true
    }
}'

Python

import requests
import os
import json

api_key = os.getenv("DASHSCOPE_API_KEY")
url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation"

response = requests.post(url,
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    },
    json={
        "model": "fun-music-preview",
        "input": {
            "prompt": "Serene piano piece, suitable as background music for late-night reading, soothing and elegant",
            "is_instrumental": true
        }
    }
)

result = response.json()
audio_url = result["output"]["audio"]["url"]
print(f"Instrumental music generated successfully! Download URL: {audio_url}")

Java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class FunMusicInstrumentalDemo {
    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        String endpoint = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation";

        HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + apiKey);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        String jsonBody = "{\"model\":\"fun-music-preview\","
            + "\"input\":{\"prompt\":\"Serene piano piece, suitable as background music for late-night reading, soothing and elegant\","
            + "\"is_instrumental\":true}}";

        try (OutputStream os = conn.getOutputStream()) {
            os.write(jsonBody.getBytes("UTF-8"));
        }

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            System.out.println(sb.toString());
        }
    }
}

Composition guide

Prompt

prompt is the primary parameter for describing your music creation intent. The model writes lyrics, arranges music, and generates a song based on your description.

Writing tip: Use specific descriptions of mood, scene, and instrument preferences for better results.

  • Recommended: Melancholic piano, rainy night yearning

  • Not recommended: Melancholic music (too vague)

Note

In your prompt, specify instruments (such as "piano accompaniment", "saxophone solo", or "guzheng and bamboo flute"), tempo (such as "upbeat", "slow", or "dense drum beats"), and emotional tone (such as "warm", "melancholic", "intense", or "laid-back"). The model follows these descriptions as closely as possible. More specific prompts produce more accurate results.

Prompt examples by style

Style

Prompt example

Folk

A warm, healing folk song with acoustic guitar, telling a story of a lazy afternoon in a cafe

Traditional Chinese

A traditional Chinese song with guzheng and bamboo flute, evoking misty landscapes and a farewell between wanderers

Rock

Intense rock with distorted electric guitar, dense drum beats, singing about youthful rebellion and freedom

Ballad

A slow ballad with piano accompaniment, quiet and deep, with a touch of melancholy, expressing longing and memories

Rap

A hip-hop rap with a sharp beat, 808 bass drums, full of street energy, telling stories of city life

Children's song

A cheerful children's song with xylophone and hand drums, simple and catchy rhythm, teaching kids about nature

Lyrics

The lyrics parameter accepts your lyrics. The model composes a song that follows them closely. Use song section tags to control the arrangement of song sections.

Song section tags

Tag

Description

[intro]

Intro, sets the mood

[verse]

Verse, tells the story

[chorus]

Chorus, emotional climax

[bridge]

Bridge, shifts perspective

[outro]

Outro, gradual fade-out

Lyrics example

[intro]
Piano keys fall gently, the evening breeze is cool.
That summer, heartbeats quietly grew warm.

[verse]
By the classroom window, sunlight slants across your profile.
Borrowing half an eraser, fingertips spark an electric line.
On the way home from school, bicycle bells chase the clouds.
You said the future is far away, but I wanted to walk to the end.

[chorus]
Youth is an unopened letter, filled with brave promises.
Even if the world flickers bright and dark, with you I see the light.
Love is like the sweet rain of early summer, soaking dreams without fear of distance.
We run toward tomorrow with laughter, hand in hand, never looking back.

[bridge]
Later, wind and rain scattered the paper umbrella, silence replaced the answers.
But the song in my heart is unfinished, still waiting for "don't drift apart."

[chorus]
Youth is an unopened letter, filled with brave promises.
Even if the world flickers bright and dark, with you I see the light.
Love is like the sweet rain of early summer, soaking dreams without fear of distance.
We run toward tomorrow with laughter, hand in hand, never looking back.

[outro]
The piano fades, starlight paves the long street.
The story is unfinished, the next page is still passionate.

Writing requirements

  • Originality: Don't copy or imitate lyrics, rhyme schemes, or signature phrases from published songs.

  • Content safety: Don't include content related to politics, violence, pornography, vulgarity, horror, or drugs. Keep the content positive and emotionally genuine.

  • Language: Only Chinese and English lyrics are supported. Japanese, Korean, and other languages aren't supported.

Vocal gender (gender)

The gender parameter selects the vocal gender. Only supported by the fun-music-v1 model. The default is female.

  • female: Female vocals (default)

  • male: Male vocals

Instrumental mode (is_instrumental)

Set is_instrumental to true to generate instrumental music (no vocals). When is_instrumental is set to true, lyrics and gender are ignored. The default value is false.

Advanced features

Streaming output

Streaming mode returns audio data progressively as it's generated, which is ideal for real-time playback. Both fun-music-v1 and fun-music-preview support this mode. To enable streaming output, add X-DashScope-SSE: enable to the request header.

Note

Streaming and non-streaming modes have different character limits:

  • Non-streaming mode: lyrics accepts 5-350 Chinese characters or 5-2,000 English characters; prompt accepts 1-2,000 characters.

  • Streaming mode: lyrics accepts 300-350 Chinese characters or 200-250 English words; prompt accepts 5-1,000 characters (Chinese or English).

curl

curl -X POST 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
    "model": "fun-music-v1",
    "input": {
        "prompt": "High-energy electronic dance music, synthesizer effects, full of energy, suitable for workout scenes",
        "gender": "male"
    }
}'

Python

import requests
import os
import json
import base64

api_key = os.getenv("DASHSCOPE_API_KEY")
url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation"

response = requests.post(url,
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "X-DashScope-SSE": "enable"
    },
    json={
        "model": "fun-music-v1",
        "input": {
            "prompt": "High-energy electronic dance music, synthesizer effects, full of energy, suitable for workout scenes",
            "gender": "male"
        }
    },
    stream=True
)

output_file = "output.mp3"
with open(output_file, "wb") as f:
    for line in response.iter_lines():
        if not line:
            continue
        decoded = line.decode("utf-8")
        if decoded.startswith("data:"):
            data = json.loads(decoded[5:])
            finish_reason = data.get("output", {}).get("finish_reason")
            if finish_reason == "null":
                audio_data = data["output"]["audio"].get("data", "")
                if audio_data:
                    f.write(base64.b64decode(audio_data))
            elif finish_reason == "stop":
                print(f"Music generation complete! Saved to {output_file}")

Java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class FunMusicStreamDemo {
    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        String endpoint = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/audio/music/generation";

        HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + apiKey);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("X-DashScope-SSE", "enable");
        conn.setDoOutput(true);

        String jsonBody = "{\"model\":\"fun-music-v1\","
            + "\"input\":{\"prompt\":\"High-energy electronic dance music, synthesizer effects, full of energy, suitable for workout scenes\","
            + "\"gender\":\"male\"}}";

        try (OutputStream os = conn.getOutputStream()) {
            os.write(jsonBody.getBytes("UTF-8"));
        }

        String outputFile = "output.mp3";
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "UTF-8"));
             FileOutputStream fos = new FileOutputStream(outputFile)) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("data:")) {
                    String data = line.substring(5);
                    if (data.contains("\"finish_reason\":\"null\"")) {
                        int start = data.indexOf("\"data\":\"") + 8;
                        int end = data.indexOf("\"", start);
                        if (start > 8 && end > start) {
                            byte[] chunk = Base64.getDecoder().decode(
                                data.substring(start, end));
                            fos.write(chunk);
                        }
                    } else if (data.contains("\"finish_reason\":\"stop\"")) {
                        System.out.println("Music generation complete! Saved to " + outputFile);
                    }
                }
            }
        }
    }
}

Supported models and regions

China (Beijing)

To call the following models, use an API key from the Beijing region:

  • fun-music-v1

  • fun-music-preview

API reference

Music generation API reference

FAQ

Can I specify instruments, tempo, or mood?

Yes. Describe them directly in your prompt, such as "piano accompaniment, slow tempo, melancholic." The model follows these descriptions as closely as possible. More specific prompts produce more accurate results. For detailed prompt writing tips, see Prompt.

Which model should I choose?

fun-music-v1 supports vocal gender selection and generates higher quality audio. fun-music-preview supports custom lyrics and prompt-based generation. For a detailed comparison, see Differences between the two models.

How long are audio download URLs valid?

Audio file download URLs are valid for 24 hours. Download the files within this period. After the URL expires, call the API again to generate a new one.

What's the difference between lyrics and prompt?

The lyrics parameter takes your lyrics, and the model composes a song based on them. The prompt parameter takes a natural language description of the music style and scene, and the model writes lyrics and generates music automatically. Parameter requirements differ by model: for fun-music-v1, at least one of the two parameters is required; if both are provided, lyrics takes effect. For fun-music-preview, prompt is required; lyrics is optional and takes priority over prompt when provided.

When should I use streaming vs. non-streaming mode?

Use non-streaming mode if you only need the final audio file. The API call is simpler. Use streaming mode if you need audio data delivered progressively during generation, such as for real-time playback.

What are the parameter limits for streaming vs. non-streaming mode?

Character limits for lyrics and prompt differ between the two modes. In non-streaming mode, lyrics accepts 5-350 Chinese characters or 5-2,000 English characters, and prompt accepts 1-2,000 characters. In streaming mode, lyrics accepts 300-350 Chinese characters or 200-250 English words, and prompt accepts 5-1,000 characters (Chinese or English).