轻松创作高质量的AI音乐——Suno API

创造优雅高质量 AI 音乐的轻松之道——Suno API

伴随人工智能的广泛应用,各类 AI 程序正逐步融入日常生活的方方面面。如今,人工智能不仅已深入人类的工作与生活中,更是创新了多个行业,从最初的文本创作、医疗教育,延展至音乐的创作领域。

Suno 是一个专注于高质量 AI 音乐与歌曲创作的平台,用户只需输入简约的文本提示,便可根据不同的风格和歌词生成富含人声的乐曲。此音乐生成器是由来自 Meta、TikTok、Kensho 等知名科技公司的团队开发,旨在无需任何乐器的辅助,让每个人都能自由创造动人的旋律。

最新的 Suno 已将音乐生成模型升级至 V3 版本,现可生成长度达两分钟的旋律。

然而,Suno 官方未提供 API 接入。AceDataCloud 则提供了一套与 Suno 官方接口模拟对接的 API,使得音乐生成的过程变得更加便捷高效。

申请与使用

若想使用 Suno Audios API,首先访问 Suno Audios Generation API 页面,点击「Acquire」按钮,以获取进行请求所需的凭证:

若您尚未登录或注册,将自动跳转至登录页面以邀请您进行注册与登录,完成后会返回当前页面。

在首次申请时,系统将赠送部分免费额度,供您无偿使用该 API。

基本使用

只需随心输入一段想要表达的歌曲主题文字,例如若您希望生成一首关于圣诞节的歌曲,则可以输入 a song for Christmas,如图所示:

生成的代码如下所示:

您可以点击「Try」按钮直接测试 API,稍等一至两分钟,生成结果如下:

{
  "success": true,
  "data": [
    {
      "id": "2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5",
      "title": "Winter Wonderland",
      "image_url": "https://cdn1.suno.ai/image_2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.png",
      "lyric": "[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near",
      "audio_url": "https://cdn1.suno.ai/2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.mp3",
      "video_url": "https://cdn1.suno.ai/2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.mp4",
      "created_at": "2024-05-10T16:21:37.624Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "holiday"
    },
    {
      "id": "5dca232b-17cc-4896-a2d1-4b59178bf410",
      "title": "Winter Wonderland",
      "image_url": "https://cdn1.suno.ai/image_5dca232b-17cc-4896-a2d1-4b59178bf410.png",
      "lyric": "[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near",
      "audio_url": "https://cdn1.suno.ai/5dca232b-17cc-4896-a2d1-4b59178bf410.mp3",
      "video_url": "https://cdn1.suno.ai/5dca232b-17cc-4896-a2d1-4b59178bf410.mp4",
      "created_at": "2024-05-10T16:21:37.624Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "holiday"
    }
  ]
}

在此时,您将看到生成了两首歌曲的内容,包含标题、预览图、歌词、音频与视频等信息。

字段说明如下:

  • success:生成是否成功,若成功则为 true,否则为 false
  • data:一个列表,详述了生成的歌曲信息。
    • id:歌曲的识别 ID
    • title:歌曲的名称
    • image_url:歌曲封面图像
    • lyric:歌曲的歌词
    • audio_url:歌曲的音频文件,点击可播放 mp3 音频。
    • video_url:歌曲的视频文件,点击可播放 mp4 视频。
    • created_at:创建的时间
    • model:使用的模型,通常为最新的 v3 版本
    • style:歌曲的风格

自定义生成

若想个性化生成歌词,可以直接输入您所期望的歌词内容:

此时 lyric 字段可传入类似以下内容:

[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near

注意,此处的歌词中 \n 为换行符。若您不知如何生成歌词,可使用 AceDataCloud 提供的歌词生成 API 以通过提示生成歌词,该 API 的地址为 Suno Lyrics Generation API

接下来,您可根据歌词、标题与风格自定义生成歌曲,配置如下内容:

  • lyric:歌词文本
  • custom:设置为 true,表示自定义生成,默认为 false,表示使用 prompt 来生成。
  • instrumental: 无歌词选项,默认为 false,当其为 true 时,将忽略上述输入的lyric 参数。
  • file:歌曲的名称。
  • style:歌曲的风格,选填项。

填写示例:

填写完成后,系统将自动生成的代码如下:

所对应的代码为:

curl -X POST 'https://api.acedata.cloud/suno/audios' \
-H 'authorization: Bearer {token}' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-d '{
"lyric": "[Verse]\\nSnowflakes falling all around\\nGlistening white\\nCovering the ground\\nChildren laughing\\nFull of delight\\nIn this winter wonderland tonight\\nSanta's sleigh\\nUp in the sky\\nRudolph's nose shining bright\\nOh my\\nHear the jingle bells\\nRinging so clear\\nBringing joy and holiday cheer\\n[Verse 2]\\nRoasting chestnuts by the fire's glow\\nChristmas lights\\nThey twinkle and show\\nFamilies gathering with love and cheer\\nSpreading warmth to everyone near",
"custom": true
}'

测试成功后,生成效果将相似。

继续生成

若您希望对已生成的歌曲进行延续创作,可以将参数 action 设置为 extend,并输入需要延续生成的歌曲 ID,歌曲 ID 可通过基本使用流程获取,如下图所示:

此时,您可以看到属于歌曲的 ID 如下:

"id": "b9e9fa11-0bf3-47cd-a3d7-85735aee3e07"

注意,此处的 id 为已生成歌曲的 ID。若您需要创建新歌曲,可参考上文的基本使用过程。

接下来,请务必填写歌词与风格以自定义生成歌曲,配置如下内容:

  • lyric:歌词文本
  • custom:设置为 true,表示自定义生成,默认为 false,使用 prompt 生成。
  • style:歌曲的风格,选填。
  • continue_at:以秒为单位续写当前音频的时间。例如,213.5 表示音频续写到 3 分 33.5 秒。

填写示例如下:

填完后系统会自动生成代码如下:

所对应的 Python 代码为:

import requests

url = "https://api.acedata.cloud/suno/audios"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "action": "extend",
    "prompt": "A song for Christmas",
    "audio_id": "b9e9fa11-0bf3-47cd-a3d7-85735aee3e07",
    "continue_at": 10,
    "style": "cute",
    "lyric": "la la la"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

点击运行,即可看到返回结果,如下:

{
  "success": true,
  "task_id": "baf55c32-1207-4bdf-bd00-32a864f0474d",
  "data": [
    {
      "id": "5a3d0054-a6c5-43a9-a348-eae22d1f0efe",
      "title": "",
      "image_url": "https://cdn2.suno.ai/image_5a3d0054-a6c5-43a9-a348-eae22d1f0efe.jpeg",
      "lyric": "la la la",
      "audio_url": "https://cdn1.suno.ai/5a3d0054-a6c5-43a9-a348-eae22d1f0efe.mp3",
      "video_url": "https://cdn1.suno.ai/5a3d0054-a6c5-43a9-a348-eae22d1f0efe.mp4",
      "created_at": "2024-07-25T11:15:49.320Z",
      "model": "chirp-v3.5",
      "prompt": null,
      "style": "cute",
      "duration": 7.96
    },
    {
      "id": "9ae90c96-adcf-4aad-a591-361485168f13",
      "title": "",
      "image_url": "https://cdn2.suno.ai/image_9ae90c96-adcf-4aad-a591-361485168f13.jpeg",
      "lyric": "la la la",
      "audio_url": "https://cdn1.suno.ai/9ae90c96-adcf-4aad-a591-361485168f13.mp3",
      "video_url": "https://cdn1.suno.ai/9ae90c96-adcf-4aad-a591-361485168f13.mp3",
      "created_at": "2024-07-25T11:15:49.321Z",
      "model": "chirp-v3.5",
      "prompt": null,
      "style": "cute",
      "duration": 4.2
    }
  ]
}

可以看出,结果内容与前文一致,这便成功实现了歌曲的续生功能。

异步回调

鉴于 Suno 音乐生成所需的时间相对较长,通常为一至两分钟,如果 API 长时间未响应,HTTP 请求会一直维持连接,造成系统资源的额外消耗,因此本 API 也提供了异步回调的支持。

整体流程为:当客户端发起请求时,需额外指定一个 callback_url 字段。在客户端提交 API 请求后,系统会立刻返回一项结果,内含一个 task_id 字段,此字段代表当前的任务识别 ID。当任务完成后,生成的音乐结果将以 POST JSON 的形式发送至客户端指定的 callback_url,同时也包括 task_id 字段,这样任务结果可通过 ID 关联起来。

接下来,我们通过一个示例来详细了解如何操作。

首先,为了实现 Webhook 回调,开发者应当替换为自己搭建的 HTTP 服务器的 URL。为方便演示,这里使用一个公开的 Webhook 示例网站 https://webhook.site/,打开此网站即可生成一个 Webhook URL,如图所示:

将该 URL 复制下来,即可作为 Webhook 使用,此处的示例 URL 为 https://webhook.site/03e60575-3d96-4132-b681-b713d78116e2。

接下来,我们可以设置字段 callback_url 为上述 Webhook URL,并填写 prompt,如下展示:

点击运行,很快即可得到如下结果:

{
  "task_id": "44472ab8-783b-4054-b861-5bf14e462f60"
}

稍等片刻,您便可在 https://webhook.site/03e60575-3d96-4132-b681-b713d78116e2 观察到生成歌曲的结果,如图展示:

内容如下:

{
  "success": true,
  "task_id": "44472ab8-783b-4054-b861-5bf14e462f60",
  "data": [
    {
      "id": "da4324e5-84b2-484b-b0e9-dd261381c594",
      "title": "Winter Whispers",
      "image_url": "https://cdn1.suno.ai/image_da4324e5-84b2-484b-b0e9-dd261381c594.png",
      "lyric": "[Verse]\nSnow falling gently from the sky\nChildren giggling as they pass by\nFire crackling\nCozy and warm\nChristmas spirit begins to swarm\n[Verse 2]\nTwinkling lights\nA sight to behold\nStockings hung\nWaiting to be filled with gold\nGifts wrapped with love\nPiled high\nExcitement in the air\nYou can't deny\n[Chorus]\nWinter whispers in the wind\nJoy and love it brings\nLet's celebrate this season\nWith the ones we're missing",
      "audio_url": "https://cdn1.suno.ai/da4324e5-84b2-484b-b0e9-dd261381c594.mp3",
      "video_url": "https://cdn1.suno.ai/da4324e5-84b2-484b-b0e9-dd261381c594.mp4",
      "created_at": "2024-05-11T07:33:05.430Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "pop"
    },
    {
      "id": "b878a87b-a0db-4046-8ccd-ecd2fb3d4372",
      "title": "Winter Whispers",
      "image_url": "https://cdn1.suno.ai/image_b878a87b-a0db-4046-8ccd-ecd2fb3d4372.png",
      "lyric": "[Verse]\nSnow falling gently from the sky\nChildren giggling as they pass by\nFire crackling\nCozy and warm\nChristmas spirit begins to swarm\n[Verse 2]\nTwinkling lights\nA sight to behold\nStockings hung\nWaiting to be filled with gold\nGifts wrapped with love\nPiled high\nExcitement in the air\nYou can't deny\n[Chorus]\nWinter whispers in the wind\nJoy and love it brings\nLet's celebrate this season\nWith the ones we're missing",
      "audio_url": "https://cdn1.suno.ai/b878a87b-a0db-4046-8ccd-ecd2fb3d4372.mp3",
      "video_url": "https://cdn1.suno.ai/b878a87b-a0db-4046-8ccd-ecd2fb3d4372.mp4",
      "created_at": "2024-05-11T07:33:05.430Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "pop"
    }
  ]
}

在此结果中包含了一个 task_id 字段,其余字段与前文所述相似,因此通过该字段可以实现任务的关联。

PyCharm是一个集成开发环境(IDE),主要用于Python语言开发。如果你想利用它创建一个程序来通过Suno API生成音乐并将其下载,你可以按照以下步骤操作: 1. **安装所需库**: 首先,你需要安装`requests`库来发送HTTP请求以及处理API响应,如果尚未安装,可以在命令行中输入 `pip install requests`. 2. **设置Suno API**: 确保你有Suno API的访问权限,并获取到必要的访问密钥或令牌。通常,这涉及到注册一个开发者账号并在文档中找到对应的API端点和下载音乐的URL。 3. **编写代码**: 使用PyCharm编写一个脚本,示例如下: ```python import requests import os def download_sunny_music(api_key, song_id): # 创建请求头 headers = { 'Authorization': f'Token {api_key}', 'Content-Type': 'application/json' } # 构造下载URL base_url = "https://api.suno.com/music/download" url = f"{base_url}/{song_id}" # 发送GET请求 response = requests.get(url, headers=headers) # 检查请求状态码 if response.status_code == 200: # 下载文件 file_path = f'music/{song_id}.mp3' # 根据需求定制保存路径 with open(file_path, 'wb') as file: file.write(response.content) print(f"Music downloaded: {file_path}") else: print(f"Error downloading music: {response.text}") # 使用API密钥和歌单ID替换这里的变量 api_key = "your_api_key" song_id = "your_song_id" download_sunny_music(api_key, song_id) ``` 4. **运行和调试**: 在PyCharm中设置好项目结构,将上述代码放在适当的位置,然后运行这段代码。记得将`api_key`和`song_id`替换为你实际的API密钥和要下载的歌曲ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值