สร้างเพลงด้วย Lyria 3

Lyria 3 คือกลุ่มโมเดลการสร้างเพลงของ Google ซึ่งพร้อมให้บริการ ผ่าน Gemini API Lyria 3 ช่วยให้คุณสร้างเสียงสเตอริโอ 44.1 kHz คุณภาพสูงจากพรอมต์ข้อความหรือจากรูปภาพได้ โมเดลเหล่านี้ให้ความสอดคล้องเชิงโครงสร้าง ซึ่งรวมถึงเสียงร้อง เนื้อเพลงแบบจับเวลา และการเรียบเรียงดนตรีบรรเลงทั้งหมด

ตระกูล Lyria 3 มี 2 โมเดล ได้แก่

รุ่น รหัสโมเดล เหมาะสำหรับ ระยะเวลา เอาต์พุต
คลิป Lyria 3 lyria-3-clip-preview คลิปสั้น วิดีโอวนซ้ำ ตัวอย่าง 30 วินาที MP3
Lyria 3 Pro lyria-3-pro-preview เพลงเต็มที่มีท่อนร้อง คอรัส และบริดจ์ 2-3 นาที (ควบคุมได้โดยใช้พรอมต์) MP3

ทั้ง 2 โมเดลสามารถใช้ได้โดยใช้ Interactions API ใหม่ ซึ่งรองรับอินพุตแบบหลายโมดัล (ข้อความและรูปภาพ) และสร้างเสียงสเตอริโอที่มีความเที่ยงตรงสูง 44.1 kHz

สร้างมิวสิกคลิป

โมเดลคลิป Lyria 3 จะสร้างคลิปความยาว 30 วินาทีเสมอ หากต้องการสร้าง คลิป ให้เรียกใช้เมธอด interactions.create ด้วยพรอมต์ข้อความ คำตอบ จะมีเนื้อเพลงและโครงสร้างเพลงที่สร้างขึ้นพร้อมกับเสียงในสคีมา steps เสมอ

Python

import base64
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="lyria-3-clip-preview",
    input="Create a 30-second cheerful acoustic folk song with guitar and harmonica.",
)

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "audio":
                print(f"Generated audio with mime_type: {content_block.mime_type}")
                with open("music.mp3", "wb") as f:
                    f.write(base64.b64decode(content_block.data))
            elif content_block.type == "text":
                print(f"Lyrics: {content_block.text}")

JavaScript

import { GoogleGenAI } from '@google/genai';
import * as fs from 'fs';

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: 'lyria-3-clip-preview',
    input: 'Create a 30-second cheerful acoustic folk song with ' +
           'guitar and harmonica.',
});

for (const step of interaction.steps) {
    if (step.type === 'model_output') {
        for (const contentBlock of step.content) {
            if (contentBlock.type === 'audio') {
                console.log(`Generated audio with mime_type: ${contentBlock.mimeType}`);
                fs.writeFileSync('music.mp3', Buffer.from(contentBlock.data, 'base64'));
            } else if (contentBlock.type === 'text') {
                console.log(`Lyrics: ${contentBlock.text}`);
            }
        }
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
    "model": "lyria-3-clip-preview",
    "input": "Create a 30-second cheerful acoustic folk song with guitar and harmonica."
}'

สร้างเพลงแบบเต็มความยาว

ใช้โมเดล lyria-3-pro-preview เพื่อสร้างเพลงแบบเต็มความยาว 2-3 นาที โมเดล Pro เข้าใจโครงสร้างดนตรีและสามารถสร้าง ผลงานที่มีท่อนร้อง คอรัส และบริดจ์ที่แตกต่างกัน คุณสามารถกำหนดระยะเวลาได้โดยระบุในพรอมต์ (เช่น "สร้างเพลง 2 นาที") หรือโดยใช้การประทับเวลาเพื่อกำหนดโครงสร้าง

Python

interaction = client.interactions.create(
    model="lyria-3-pro-preview",
    input="An epic cinematic orchestral piece about a journey home. Starts with a solo piano intro, builds through sweeping strings, and climaxes with a massive wall of sound.",
)

JavaScript

const interaction = await client.interactions.create({
    model: 'lyria-3-pro-preview',
    input: 'An epic cinematic orchestral piece about a journey home. ' +
           'Starts with a solo piano intro, builds through sweeping ' +
           'strings, and climaxes with a massive wall of sound.',
});

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
    "model": "lyria-3-pro-preview",
    "input": "An epic cinematic orchestral piece about a journey home. Starts with a solo piano intro, builds through sweeping strings, and climaxes with a massive wall of sound."
}'

เลือกรูปแบบเอาต์พุต

โดยค่าเริ่มต้น โมเดล Lyria 3 จะสร้างเสียงในรูปแบบ MP3 สำหรับ Lyria 3 Pro คุณยังขอเอาต์พุตในรูปแบบ WAV ได้ด้วยโดยการตั้งค่า response_mime_type

Python

interaction = client.interactions.create(
    model="lyria-3-pro-preview",
    input="An atmospheric ambient track.",
    response_modalities=["audio", "text"],
    response_mime_type="audio/wav",
)

JavaScript

const interaction = await client.interactions.create({
    model: 'lyria-3-pro-preview',
    input: 'An atmospheric ambient track.',
    responseModalities: ["audio", "text"],
    responseMimeType: "audio/wav",
});

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "lyria-3-pro-preview",
    "input": "An atmospheric ambient track.",
    "responseModalities": ["audio", "text"],
    "responseMimeType": "audio/wav"
  }'

แยกวิเคราะห์คำตอบ

การตอบกลับจาก Lyria 3 มีบล็อกเนื้อหาหลายรายการภายในสคีมา steps การโต้ตอบจะแสดงลำดับขั้นตอน โดยขั้นตอน model_output จะมี เนื้อหาที่สร้างขึ้น บล็อกเนื้อหาข้อความมีเนื้อเพลงที่สร้างขึ้นหรือคำอธิบาย JSON ของโครงสร้างเพลง บล็อกเนื้อหาที่มีประเภท audio จะมีข้อมูลเสียงที่เข้ารหัส Base64

Python

lyrics = []
audio_data = None

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "audio":
                audio_data = base64.b64decode(content_block.data)
            elif content_block.type == "text":
                lyrics.append(content_block.text)

if lyrics:
    print("Lyrics:\n" + "\n".join(lyrics))

if audio_data:
    with open("output.mp3", "wb") as f:
        f.write(audio_data)

JavaScript

const lyrics = [];
let audioData = null;

for (const step of interaction.steps) {
    if (step.type === 'model_output') {
        for (const contentBlock of step.content) {
            if (contentBlock.type === 'audio') {
                audioData = Buffer.from(contentBlock.data, 'base64');
            } else if (contentBlock.type === 'text') {
                lyrics.push(contentBlock.text);
            }
        }
    }
}

if (lyrics.length) {
    console.log("Lyrics:\n" + lyrics.join("\n"));
}

if (audioData) {
    fs.writeFileSync("output.mp3", audioData);
}

REST

# The output from the REST API is a JSON object containing base64 encoded data.
# You can extract the text or the audio data using a tool like jq.
# To extract the audio and save it to a file:
curl ... | jq -r '.steps[] | select(.type=="model_output") | .content[] | select(.type=="audio") | .data' | base64 -d > output.mp3

สร้างเพลงจากรูปภาพ

Lyria 3 รองรับอินพุตหลายรูปแบบ คุณสามารถระบุรูปภาพได้สูงสุด 10 ภาพ พร้อมกับพรอมต์ข้อความในรายการ input และโมเดลจะแต่งเพลง โดยได้แรงบันดาลใจจากเนื้อหาภาพ

Python

uploaded_image = client.files.upload(file="desert_sunset.jpg")

response = client.interactions.create(
    model="lyria-3-pro-preview",
    input=[
        {"type": "text", "text": "An atmospheric ambient track inspired by the mood and colors in this image."},
        {
            "type": "image",
            "uri": uploaded_image.uri,
            "mime_type": uploaded_image.mime_type
        }
    ],
)

JavaScript

const uploadedImage = await client.files.upload({
    file: "desert_sunset.jpg",
    config: { mimeType: "image/jpeg" }
});

const interaction = await client.interactions.create({
    model: 'lyria-3-pro-preview',
    input: [
        { type: 'text', text: 'An atmospheric ambient track inspired by the mood and colors in this image.' },
        {
            type: 'image',
            uri: uploadedImage.uri,
            mimeType: uploadedImage.mimeType
        }
    ],
});

REST

# First upload the image using the Files API, then use the URI:
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "lyria-3-pro-preview",
    "input": [
      {"type": "text", "text": "An atmospheric ambient track inspired by the mood and colors in this image."},
      {"type": "image", "uri": "YOUR_FILE_URI", "mime_type": "image/jpeg"}
    ]
  }'

ระบุเนื้อเพลงที่กำหนดเอง

คุณสามารถเขียนเนื้อเพลงของคุณเองและใส่ไว้ในพรอมต์ได้ ใช้แท็กส่วน เช่น [Verse], [Chorus] และ [Bridge] เพื่อช่วยให้โมเดลเข้าใจโครงสร้างเพลง

Python

prompt = """
Create a dreamy indie pop song with the following lyrics:

[Verse 1]
Walking through the neon glow,
city lights reflect below,
every shadow tells a story,
every corner, fading glory.

[Chorus]
We are the echoes in the night,
burning brighter than the light,
hold on tight, don't let me go,
we are the echoes down below.

[Verse 2]
Footsteps lost on empty streets,
rhythms sync to heartbeats,
whispers carried by the breeze,
dancing through the autumn leaves.
"""

interaction = client.interactions.create(
    model="lyria-3-pro-preview",
    input=prompt,
)

JavaScript

const prompt = `
Create a dreamy indie pop song with the following lyrics:

[Verse 1]
Walking through the neon glow,
city lights reflect below,
every shadow tells a story,
every corner, fading glory.

[Chorus]
We are the echoes in the night,
burning brighter than the light,
hold on tight, don't let me go,
we are the echoes down below.

[Verse 2]
Footsteps lost on empty streets,
rhythms sync to heartbeats,
whispers carried by the breeze,
dancing through the autumn leaves.
`;

const interaction = await client.interactions.create({
    model: 'lyria-3-pro-preview',
    input: prompt,
});

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "lyria-3-pro-preview",
    "input": "Create a dreamy indie pop song with the following lyrics: ..."
  }'

ควบคุมเวลาและโครงสร้าง

คุณสามารถระบุสิ่งที่เกิดขึ้นในช่วงเวลาที่เฉพาะเจาะจงในเพลงได้โดยใช้ การประทับเวลา ซึ่งมีประโยชน์ในการควบคุมเวลาที่เครื่องดนตรีเริ่มเล่น เวลาที่เนื้อเพลง ปรากฏ และวิธีที่เพลงดำเนินไป

Python

prompt = """
[0:00 - 0:10] Intro: Begin with a soft lo-fi beat and muffled
              vinyl crackle.
[0:10 - 0:30] Verse 1: Add a warm Fender Rhodes piano melody
              and gentle vocals singing about a rainy morning.
[0:30 - 0:50] Chorus: Full band with upbeat drums and soaring
              synth leads. The lyrics are hopeful and uplifting.
[0:50 - 1:00] Outro: Fade out with the piano melody alone.
"""

interaction = client.interactions.create(
    model="lyria-3-pro-preview",
    input=prompt,
)

JavaScript

const prompt = `
[0:00 - 0:10] Intro: Begin with a soft lo-fi beat and muffled
              vinyl crackle.
[0:10 - 0:30] Verse 1: Add a warm Fender Rhodes piano melody
              and gentle vocals singing about a rainy morning.
[0:30 - 0:50] Chorus: Full band with upbeat drums and soaring
              synth leads. The lyrics are hopeful and uplifting.
[0:50 - 1:00] Outro: Fade out with the piano melody alone.
`;

const interaction = await client.interactions.create({
    model: 'lyria-3-pro-preview',
    input: prompt,
});

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "lyria-3-pro-preview",
    "input": "[0:00 - 0:10] Intro: ..."
  }'

สร้างแทร็กบรรเลง

สำหรับเพลงประกอบ เกมซาวด์แทร็ก หรือกรณีการใช้งานใดๆ ที่ไม่จำเป็นต้องมีเสียงร้อง คุณสามารถแจ้งให้โมเดลสร้างแทร็กเฉพาะดนตรีได้โดยทำดังนี้

Python

interaction = client.interactions.create(
    model="lyria-3-clip-preview",
    input="A bright chiptune melody in C Major, retro 8-bit video game style. Instrumental only, no vocals.",
)

JavaScript

const interaction = await client.interactions.create({
    model: 'lyria-3-clip-preview',
    input: 'A bright chiptune melody in C Major, retro 8-bit video game style. Instrumental only, no vocals.',
});

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "lyria-3-clip-preview",
    "input": "A bright chiptune melody in C Major, retro 8-bit video game style. Instrumental only, no vocals."
  }'

สร้างเพลงในภาษาต่างๆ

Lyria 3 จะสร้างเนื้อเพลงในภาษาของพรอมต์ หากต้องการสร้างเพลง ที่มีเนื้อร้องเป็นภาษาฝรั่งเศส ให้เขียนพรอมต์เป็นภาษาฝรั่งเศส โมเดลจะปรับรูปแบบการร้อง และการออกเสียงให้ตรงกับภาษา

Python

interaction = client.interactions.create(
    model="lyria-3-pro-preview",
    input="Crée une chanson pop romantique en français sur un coucher de soleil à Paris. Utilise du piano et de la guitare acoustique.",
)

JavaScript

const interaction = await client.interactions.create({
    model: 'lyria-3-pro-preview',
    input: 'Crée une chanson pop romantique en français sur un coucher de soleil à Paris. Utilise du piano et de la guitare acoustique.',
});

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "lyria-3-pro-preview",
    "input": "Crée une chanson pop romantique en français sur un coucher de soleil à Paris. Utilise du piano et de la guitare acoustique."
  }'

ความสามารถของโมเดล

Lyria 3 จะวิเคราะห์กระบวนการพรอมต์ของคุณ โดยโมเดลจะพิจารณาโครงสร้างดนตรี (ท่อนนำ ท่อนร้อง คอรัส บริดจ์ ฯลฯ) ตามพรอมต์ของคุณ ซึ่งจะเกิดขึ้นก่อนที่จะสร้างเสียง และช่วยให้มั่นใจได้ถึงความสอดคล้องเชิงโครงสร้างและความเป็นดนตรี

คำแนะนำในการเขียนพรอมต์

ยิ่งพรอมต์ของคุณเจาะจงมากเท่าไหร่ ผลลัพธ์ก็จะยิ่งดีเท่านั้น สิ่งที่คุณระบุเพื่อเป็นแนวทางในการสร้างมีดังนี้

  • แนวเพลง: ระบุแนวเพลงหรือแนวเพลงผสม (เช่น "ฮิปฮอปโลไฟ" "แจ๊สฟิวชัน" "ออร์เคสตราแบบภาพยนตร์")
  • เครื่องดนตรี: ระบุชื่อเครื่องดนตรี (เช่น "เปียโน Fender Rhodes" "กีตาร์สไลด์" "ดรัมแมชชีน TR-808")
  • BPM: ตั้งค่าเทมโป (เช่น "120 BPM", "เทมโปช้าประมาณ 70 BPM")
  • คีย์/สเกล: ระบุคีย์เพลง (เช่น "ในคีย์ G เมเจอร์" "ในคีย์ D ไมเนอร์")
  • อารมณ์และบรรยากาศ: ใช้คำคุณศัพท์ที่อธิบายรายละเอียด (เช่น "หวนรำลึก" "ดุดัน" "เหนือจริง" "เหมือนฝัน")
  • โครงสร้าง: ใช้แท็ก เช่น [Verse], [Chorus], [Bridge], [Intro], [Outro] หรือการประทับเวลาเพื่อควบคุมการดำเนินเพลง
  • ระยะเวลา: โมเดลคลิปจะสร้างคลิปความยาว 30 วินาทีเสมอ สำหรับโมเดล Pro ให้ระบุความยาวที่ต้องการในพรอมต์ (เช่น "สร้างเพลงยาว 2 นาที") หรือใช้การประทับเวลาเพื่อควบคุมระยะเวลา

ตัวอย่างพรอมต์

ตัวอย่างพรอมต์ที่มีประสิทธิภาพมีดังนี้

  • "A 30-second lofi hip hop beat with dusty vinyl crackle, mellow Rhodes piano chords, a slow boom-bap drum pattern at 85 BPM, and a jazzy upright bass line. Instrumental only."
  • "An upbeat, feel-good pop song in G major at 120 BPM with bright acoustic guitar strumming, claps, and warm vocal harmonies about a summer road trip."
  • "A dark, atmospheric trap beat at 140 BPM with heavy 808 bass, eerie synth pads, sharp hi-hats, and a haunting vocal sample. In D minor."

แนวทางปฏิบัติแนะนำ

  • ทำซ้ำด้วยคลิปก่อน ใช้lyria-3-clip-previewโมเดลที่เร็วกว่าเพื่อ ทดลองใช้พรอมต์ก่อนที่จะสร้างวิดีโอแบบเต็มด้วย lyria-3-pro-preview
  • ใช้คำที่เฉพาะเจาะจง พรอมต์ที่คลุมเครือจะให้ผลลัพธ์ทั่วไป ระบุเครื่องดนตรี BPM, คีย์, อารมณ์ และโครงสร้างเพื่อให้ได้ผลลัพธ์ที่ดีที่สุด
  • เลือกภาษาที่ตรงกัน ป้อนพรอมต์ในภาษาที่คุณต้องการให้เนื้อเพลงเป็น
  • ใช้แท็กส่วน แท็ก [Verse], [Chorus], [Bridge] ช่วยให้โมเดลมี โครงสร้างที่ชัดเจนในการปฏิบัติตาม
  • แยกเนื้อเพลงออกจากวิธีการ เมื่อระบุเนื้อเพลงที่กำหนดเอง ให้แยกเนื้อเพลงออกจากคำสั่งเกี่ยวกับทิศทางดนตรีอย่างชัดเจน

ข้อจำกัด

  • ความปลอดภัย: ตัวกรองความปลอดภัยจะตรวจสอบพรอมต์ทั้งหมด ระบบจะบล็อกพรอมต์ที่ทริกเกอร์ ตัวกรอง ซึ่งรวมถึงพรอมต์ที่ขอเสียงของศิลปินที่เฉพาะเจาะจง หรือการสร้างเนื้อเพลงที่มีลิขสิทธิ์
  • การทำลายน้ำ: เสียงที่สร้างขึ้นทั้งหมดจะมีลายน้ำที่เป็นเสียง SynthID สำหรับการระบุ ลายน้ำนี้จะมองไม่เห็นด้วยตาเปล่าและ ไม่มีผลต่อประสบการณ์การฟัง
  • การแก้ไขหลายรอบ: การสร้างเพลงเป็นกระบวนการแบบรอบเดียว การแก้ไขซ้ำหรือการปรับแต่งคลิปที่สร้างขึ้นผ่านพรอมต์หลายรายการไม่รองรับใน Lyria 3 เวอร์ชันปัจจุบัน
  • ความยาว: โมเดลคลิปจะสร้างคลิปความยาว 30 วินาทีเสมอ โมเดล Pro สร้างเพลงที่มีความยาว 2-3 นาที โดยระยะเวลาที่แน่นอนจะ ขึ้นอยู่กับพรอมต์ของคุณ
  • ความแน่นอน: ผลลัพธ์อาจแตกต่างกันไปในแต่ละการเรียกใช้ แม้จะใช้พรอมต์เดียวกันก็ตาม

ขั้นตอนถัดไป