> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/stable-audio.md).

# Stable Audio

在 Clore.ai 上使用 Stable Audio 生成音乐和音效

在 CLORE.AI GPU 上使用 Stability AI 的 Stable Audio 生成音乐和音效。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 为什么选择 Stable Audio？

* **高质量** - 44.1kHz 立体声音频生成
* **可变长度** - 可生成最长 95 秒
* **多用途** - 音乐、音效、环境音
* **文本转音频** - 描述你想听到的内容
* **开源权重** - 可用 Stable Audio Open

## 模型变体

| 模型                | 时长   | 质量 | 显存   | 许可证 |
| ----------------- | ---- | -- | ---- | --- |
| Stable Audio Open | 47 秒 | 好  | 8GB  | 打开  |
| Stable Audio 2.0  | 3 分钟 | 优秀 | 12GB | 商用  |

## 在 CLORE.AI 上快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel
```

**端口：**

```
22/tcp
7860/http
```

**命令：**

```bash
pip install stable-audio-tools gradio && \
python -c "
import gradio as gr
import torch
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond
import soundfile as sf
import tempfile

model, model_config = get_pretrained_model('stabilityai/stable-audio-open-1.0')
model = model.to('cuda')

def generate(prompt, duration, steps, seed):
    conditioning = [{
        'prompt': prompt,
        'seconds_start': 0,
        'seconds_total': duration
    }]

    generator = torch.Generator('cuda').manual_seed(seed) if seed > 0 else None

    output = generate_diffusion_cond(
        model,
        conditioning=conditioning,
        steps=steps,
        cfg_scale=7,
        sample_size=model_config['sample_size'],
        sample_rate=model_config['sample_rate'],
        device='cuda',
        seed=seed if seed > 0 else None
    )

    audio = output[0].T.cpu().numpy()

    with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
        sf.write(f.name, audio, model_config['sample_rate'])
        return f.name

gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label='提示词'),
        gr.Slider(1, 47, value=10, label='时长（秒）'),
        gr.Slider(10, 150, value=100, label='步数'),
        gr.Number(value=-1, label='种子')
    ],
    outputs=gr.Audio(label='生成的音频'),
    title='Stable Audio Open'
).launch(server_name='0.0.0.0', server_port=7860)
"
```

## 访问你的服务

部署后，找到你的 `http_pub` URL 在 **我的订单**:

1. 前往 **我的订单** 页面
2. 点击你的订单
3. 找到 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 替代 `localhost` 在下面的示例中。

## 硬件要求

| 模型                | 最低 GPU        | 推荐            |
| ----------------- | ------------- | ------------- |
| Stable Audio Open | RTX 3070 8GB  | RTX 3090 24GB |
| Stable Audio 2.0  | RTX 3090 12GB | RTX 4090 24GB |

## 安装

```bash
pip install stable-audio-tools torch torchaudio
```

## 基础用法

### 文本生成音乐

```python
import torch
import torchaudio
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond

# 加载模型
model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
model = model.to("cuda")

sample_rate = model_config["sample_rate"]
sample_size = model_config["sample_size"]

# 定义你想要的内容
conditioning = [{
    "prompt": "欢快的电子舞曲，带有朗朗上口的合成器旋律，128 BPM",
    "seconds_start": 0,
    "seconds_total": 30
}]

# 生成
output = generate_diffusion_cond(
    model,
    conditioning=conditioning,
    steps=100,
    cfg_scale=7,
    sample_size=sample_size,
    sample_rate=sample_rate,
    device="cuda"
)

# 保存
audio = output[0].T
torchaudio.save("music.wav", audio.cpu(), sample_rate)
```

### 音效

```python
conditioning = [{
    "prompt": "雷暴伴随大雨和远处雷鸣",
    "seconds_start": 0,
    "seconds_total": 20
}]

output = generate_diffusion_cond(
    model,
    conditioning=conditioning,
    steps=100,
    cfg_scale=7,
    sample_size=sample_size,
    sample_rate=sample_rate,
    device="cuda"
)

torchaudio.save("thunderstorm.wav", output[0].T.cpu(), sample_rate)
```

### 环境音

```python
conditioning = [{
    "prompt": "宁静的森林氛围，鸟鸣和轻柔微风",
    "seconds_start": 0,
    "seconds_total": 45
}]

output = generate_diffusion_cond(
    model,
    conditioning=conditioning,
    steps=100,
    cfg_scale=7,
    sample_size=sample_size,
    sample_rate=sample_rate,
    device="cuda"
)

torchaudio.save("forest.wav", output[0].T.cpu(), sample_rate)
```

## 提示词示例

### 音乐类型

```python
prompts = {
    "electronic": "充满活力的 EDM 曲目，低沉贝斯、合成器琶音和推进感强的节拍，130 BPM",
    "jazz": "平滑爵士钢琴三重奏，立式贝斯和刷鼓，节奏舒缓",
    "rock": "带有失真效果的重型摇滚吉他 riff、鼓和贝斯，强劲而充满能量",
    "classical": "管弦乐作品，带有弦乐和木管乐器，戏剧性且富有电影感",
    "ambient": "具有氛围感的环境音景，铺底音色与细腻纹理，梦幻般",
    "hiphop": "带有黑胶噼啪声的 lo-fi 嘻哈节拍，柔和钢琴和轻松鼓点，85 BPM"
}
```

### 音效

```python
prompts = {
    "explosion": "巨大的爆炸，伴随碎片和火焰，电影感",
    "footsteps": "碎石上的脚步声，缓慢行走",
    "car": "跑车引擎轰鸣并加速",
    "water": "洞穴中的水花与滴水声",
    "wind": "强风在群山间呼啸",
    "fire": "噼啪作响的篝火，木头爆裂声"
}
```

### 环境/背景

```python
prompts = {
    "cafe": "咖啡店氛围，轻声交谈和浓缩咖啡机",
    "ocean": "沙滩上的海浪，远处海鸥鸣叫",
    "city": "繁忙的城市街道，车流、喇叭声和行人",
    "rain": "窗外细雨，偶尔雷声",
    "space": "科幻飞船内部的嗡鸣和哔哔声"
}
```

## 高级选项

### 生成控制

```python
output = generate_diffusion_cond(
    model,
    conditioning=conditioning,
    steps=150,              # 步数越多 = 质量越高
    cfg_scale=7,            # 提示词遵循度（5-10）
    sample_size=sample_size,
    sample_rate=sample_rate,
    device="cuda",
    seed=42                 # 可复现结果
)
```

### 可变长度

```python
# 短音效（5 秒）
conditioning = [{
    "prompt": "门缓慢吱呀打开",
    "seconds_start": 0,
    "seconds_total": 5
}]

# 中等片段（30 秒）
conditioning = [{
    "prompt": "欢快的摇滚音乐",
    "seconds_start": 0,
    "seconds_total": 30
}]

# 最长长度（Open 版为 47 秒）
conditioning = [{
    "prompt": "氛围电子音乐，纹理逐渐演变",
    "seconds_start": 0,
    "seconds_total": 47
}]
```

## 批量生成

```python
import os

prompts = [
    "充满活力的鼓打贝斯曲目",
    "宁静的钢琴旋律",
    "科幻激光音效",
    "雨打铁皮屋顶"
]

output_dir = "./audio_output"
os.makedirs(output_dir, exist_ok=True)

for i, prompt in enumerate(prompts):
    conditioning = [{
        "prompt": prompt,
        "seconds_start": 0,
        "seconds_total": 15
    }]

    output = generate_diffusion_cond(
        model,
        conditioning=conditioning,
        steps=100,
        cfg_scale=7,
        sample_size=sample_size,
        sample_rate=sample_rate,
        device="cuda"
    )

    torchaudio.save(f"{output_dir}/audio_{i}.wav", output[0].T.cpu(), sample_rate)
    print(f"Generated: {prompt[:30]}...")

    torch.cuda.empty_cache()
```

## Gradio 网页界面

```python
import gradio as gr
import torch
import torchaudio
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond
import tempfile

model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
model = model.to("cuda")

sample_rate = model_config["sample_rate"]
sample_size = model_config["sample_size"]

def generate_audio(prompt, duration, steps, cfg_scale, seed):
    conditioning = [{
        "prompt": prompt,
        "seconds_start": 0,
        "seconds_total": duration
    }]

    generator_seed = seed if seed > 0 else None

    output = generate_diffusion_cond(
        model,
        conditioning=conditioning,
        steps=steps,
        cfg_scale=cfg_scale,
        sample_size=sample_size,
        sample_rate=sample_rate,
        device="cuda",
        seed=generator_seed
    )

    audio = output[0].T.cpu()

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
        torchaudio.save(f.name, audio, sample_rate)
        return f.name

demo = gr.Interface(
    fn=generate_audio,
    inputs=[
        gr.Textbox(label="提示词", placeholder="描述你想要的音频..."),
        gr.Slider(1, 47, value=15, step=1, label="时长（秒）"),
        gr.Slider(20, 200, value=100, step=10, label="步数"),
        gr.Slider(1, 15, value=7, step=0.5, label="CFG 比例"),
        gr.Number(value=-1, label="种子（-1 表示随机）")
    ],
    outputs=gr.Audio(label="生成的音频", type="filepath"),
    title="Stable Audio Open - 文本转音频",
    description="根据文本描述生成音乐和音效。运行于 CLORE.AI。",
    examples=[
        ["欢快的电子舞曲，带有合成器，128 BPM", 20, 100, 7, 42],
        ["雷暴伴随大雨", 15, 100, 7, 123],
        ["宁静而富有情感的钢琴旋律", 30, 100, 7, 456]
    ]
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## 性能

| 时长   | 步数  | GPU      | 时间     |
| ---- | --- | -------- | ------ |
| 10 秒 | 100 | RTX 3090 | \~15秒  |
| 10 秒 | 100 | RTX 4090 | \~10 秒 |
| 30 秒 | 100 | RTX 3090 | \~40 秒 |
| 30 秒 | 100 | RTX 4090 | \~25 秒 |
| 47 秒 | 100 | RTX 4090 | \~40 秒 |

## 质量提示

### 更好的音乐

```python
# 包括速度和风格
prompt = "充满活力的摇滚音乐，电吉他、鼓、贝斯，140 BPM，高能量"

# 具体说明乐器
prompt = "独奏原声吉他指弹，民谣风格，温暖而亲密"

# 描述情绪
prompt = "忧郁的钢琴曲，小调，慢速，充满情感和悲伤"
```

### 更好的音效

```python
# 具体说明
prompt = "步枪单发枪声，户外，带回声"

# 包括环境
prompt = "木地板上的脚步声，室内，慢速，吱嘎作响"

# 描述质感
prompt = "火焰噼啪作响，大篝火，木头爆裂声，火花"
```

## 成本估算

CLORE.AI 市场的典型费率：

| GPU           | 小时费率    | \~30 秒片段/小时 |
| ------------- | ------- | ----------- |
| RTX 3060 12GB | \~$0.03 | \~50        |
| RTX 3090 24GB | \~$0.06 | \~90        |
| RTX 4090 24GB | \~$0.10 | \~140       |
| A100 40GB     | \~$0.17 | \~200       |

*价格会有所不同。请查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前费率。*

## 故障排查

### 内存不足

```python
# 缩短时长
conditioning = [{
    "prompt": prompt,
    "seconds_total": 15  # 而不是 47
}]

# 或启用 CPU 卸载
model.enable_model_cpu_offload()
```

### 输出质量差

* 增加步数（150-200）
* 调整 CFG 比例（尝试 5-10）
* 在提示词中更具体
* 尝试不同的种子

### 无声音 / 静音

* 检查提示词是否足够具体
* 避免过于抽象的描述
* 先尝试已知有效的提示词

### 音频瑕疵

* 增加步数
* 降低 CFG 比例
* 缩短时长
* 检查 GPU 是否因过热降频

## Stable Audio 与其他工具对比

| 功能 | Stable Audio | AudioCraft | Bark  |
| -- | ------------ | ---------- | ----- |
| 音乐 | 优秀           | 优秀         | 较差    |
| 音效 | 很高           | 好          | 较差    |
| 语音 | 否            | 否          | 是     |
| 时长 | 47秒 / 3分钟    | 30秒        | 15秒   |
| 质量 | 44.1kHz      | 32kHz      | 24kHz |
| 打开 | 部分           | 是          | 是     |

**在以下情况下使用 Stable Audio：**

* 高质量音乐生成
* 用于游戏/视频的音效
* 背景音乐
* 环境音景

## 下一步

* [AudioCraft](/guides/guides_v2-zh/yin-pin-yu-yu-yin/audiocraft-music.md) - Meta 的音乐生成
* [Bark TTS](/guides/guides_v2-zh/yin-pin-yu-yu-yin/bark-tts.md) - 语音合成
* [Demucs](/guides/guides_v2-zh/yin-pin-yu-yu-yin/demucs-separation.md) - 音频分离
* [Whisper](/guides/guides_v2-zh/yin-pin-yu-yu-yin/whisper-transcription.md) - 转录


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/stable-audio.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
