音楽クイックスタート

Music APIで音楽を生成する方法を学びます。

このガイドでは、Music APIを使って音楽を生成する方法を紹介します。

ElevenLabs music skillを使うと、AIコーディングアシスタントから音楽トラックを生成できます。

npx skills add elevenlabs/skills --skill music
Music APIは有料ユーザーのみご利用いただけます。

Music APIの使い方

1

APIキーを作成

ダッシュボードでAPIキーを作成し、安全にAPIへアクセスするために使用します。

キーは管理されたシークレットとして保存し、好みに応じて.envファイルによる環境変数として、またはアプリの設定で直接SDKに渡してください。

.env
ELEVENLABS_API_KEY=<your_api_key_here>
2

SDKをインストール

dotenvライブラリも使用して、環境変数からAPIキーを読み込みます。

pip install elevenlabs
pip install python-dotenv
3

APIリクエストを実行

使用する言語に応じて、example.pyまたはexample.mtsという名前の新しいファイルを作成し、次のコードを追加します。

# example.py
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
import os
from dotenv import load_dotenv
load_dotenv()
elevenlabs = ElevenLabs(
api_key=os.getenv("ELEVENLABS_API_KEY"),
)
track = elevenlabs.music.compose(
prompt="Create an intense, fast-paced electronic track for a high-adrenaline video game scene. Use driving synth arpeggios, punchy drums, distorted bass, glitch effects, and aggressive rhythmic textures. The tempo should be fast, 130–150 bpm, with rising tension, quick transitions, and dynamic energy bursts.",
music_length_ms=10000,
model_id="music_v2_5",
)
# Save the track to a file
with open("path/to/music.mp3", "wb") as f:
for chunk in track:
f.write(chunk)

次に、実行します。

python example.py

生成された音楽が再生されます。

コンポジションプラン

コンポジションプランは、生成したい音楽をより詳しく記述するJSONオブジェクトです。簡単なプロトタイピングにはテキストプロンプトを使用し、チャンク構造の指定、歌詞タイミングの正確な調整、複雑なアレンジが必要な場合はコンポジションプランを使用します。

コンポジションプランを生成する

APIを使用して、プロンプトからコンポジションプランを生成できます。

from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
import os
from dotenv import load_dotenv
load_dotenv()
elevenlabs = ElevenLabs(
api_key=os.getenv("ELEVENLABS_API_KEY"),
)
composition_plan = elevenlabs.music.composition_plan.create(
prompt="Create an intense, fast-paced electronic track for a high-adrenaline video game scene. Use driving synth arpeggios, punchy drums, distorted bass, glitch effects, and aggressive rhythmic textures. The tempo should be fast, 130–150 bpm, with rising tension, quick transitions, and dynamic energy bursts.",
music_length_ms=10000,
model_id="music_v2_5",
)
print(composition_plan)

上記により、次のようなコンポジションプランが生成されます。

{
"chunks": [
{
"text": "[Intro]",
"durationMs": 3000,
"positiveStyles": [
"electronic",
"fast-paced",
"rising synth arpeggio",
"glitch fx",
"filtered noise sweep",
"soft punchy kick building tension",
"high adrenaline"
],
"negativeStyles": ["soft pads", "melodic vocals", "ambient textures"],
"contextAdherence": "high"
},
{
"text": "[Peak Drop]",
"durationMs": 4000,
"positiveStyles": [
"full punchy drums",
"distorted bass stab",
"aggressive rhythmic hits",
"rapid arpeggio sequences"
],
"negativeStyles": ["smooth transitions", "clean bass", "slow buildup"],
"contextAdherence": "high"
},
{
"text": "[Final Burst]",
"durationMs": 3000,
"positiveStyles": [
"glitch stutter",
"energy burst vox chopped sample",
"quick transitions",
"snare rolls"
],
"negativeStyles": ["long reverb tails", "fadeout", "gentle melodies"],
"contextAdherence": "high"
}
]
}

コンポジションプランを使う

コンポジションプランをcomposeメソッドに渡すことで、音楽を生成できます。

# You can pass in composition_plan or prompt, but not both.
composition = elevenlabs.music.compose(
composition_plan=composition_plan,
model_id="music_v2_5",
)
play(composition)

詳細情報付きで音楽を生成する

音楽を生成するたびに、プロンプトからコンポジションプランが作成されます。詳細レスポンスエンドポイントを使用すると、このプランを取得できます。

track_details = elevenlabs.music.compose_detailed(
prompt="Create an intense, fast-paced electronic track for a high-adrenaline video game scene. Use driving synth arpeggios, punchy drums, distorted bass, glitch effects, and aggressive rhythmic textures. The tempo should be fast, 130–150 bpm, with rising tension, quick transitions, and dynamic energy bursts.",
music_length_ms=10000,
model_id="music_v2_5",
)
print(track_details.json) # json contains composition_plan and song_metadata. The composition plan will include lyrics (if applicable)
print(track_details.filename)
# track_details.audio contains the audio bytes

著作権で保護された素材

著作権で保護された素材を含む音楽またはコンポジションプランを生成しようとすると、エラーになります。これには、バンドやミュージシャンを名前で指定することや、著作権で保護された歌詞を使用することが含まれます。

著作権で保護された素材を含むプロンプト

この場合、APIは代わりに使用できるプロンプトの候補を含むbad_promptエラーを返します。

try:
# This will result in a bad_prompt error
track = elevenlabs.music.compose(
prompt="A song that sounds like 'Bohemian Rhapsody'",
music_length_ms=10000,
model_id="music_v2_5",
)
except Exception as e:
if e.body['detail']['status'] == 'bad_prompt':
prompt_suggestion = e.body['detail']['data']['prompt_suggestion']
print(prompt_suggestion) # Prints: An epic rock ballad with dramatic tempo changes, operatic harmonies, and a narrative structure that blends melancholy with bursts of theatrical intensity.
# Use the prompt suggestion to generate the track instead

著作権で保護された素材を含むコンポジションプラン

著作権で保護された素材を使用するスタイルでコンポジションプランを生成した場合、bad_composition_planエラーが返されます。音楽プロンプトと同様に、提案されたコンポジションプランcomposition_plan_suggestionがエラー内で返されます。

有害な素材を含むコンポジションプランまたはプロンプトの場合、プロンプトの候補は 返されません。

次のステップ