Skip to content

Commit 96b675a

Browse files
committed
Add route for generating stems
1 parent 74d34fb commit 96b675a

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Suno API currently mainly implements the following APIs:
126126
If no IDs are provided, all music will be returned.
127127
- `/api/get_limit`: Get quota Info
128128
- `/api/extend_audio`: Extend audio length
129+
- `/api/generate_stems`: Make stem tracks (separate audio and music track)
129130
- `/api/clip`: Get clip information based on ID passed as query parameter `id`
130131
- `/api/concat`: Generate the whole song from extensions
131132
```

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Suno API 目前主要实现了以下 API:
123123
- `/api/get`: 根据id获取音乐信息。获取多个请用","分隔,不传ids则返回所有音乐
124124
- `/api/get_limit`: 获取配额信息
125125
- `/api/extend_audio`: 在一首音乐的基础上,扩展音乐长度
126+
- `/api/generate_stems`: 制作主干轨道(单独的音频和音乐轨道
126127
- `/api/clip`: 检索特定音乐的信息
127128
- `/api/concat`: 合并音乐,将扩展后的音乐和原始音乐合并
128129
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { NextResponse, NextRequest } from "next/server";
2+
import { DEFAULT_MODEL, sunoApi } from "@/lib/SunoApi";
3+
import { corsHeaders } from "@/lib/utils";
4+
5+
export const dynamic = "force-dynamic";
6+
7+
export async function POST(req: NextRequest) {
8+
if (req.method === 'POST') {
9+
try {
10+
const body = await req.json();
11+
const { audio_id } = body;
12+
13+
if (!audio_id) {
14+
return new NextResponse(JSON.stringify({ error: 'Audio ID is required' }), {
15+
status: 400,
16+
headers: {
17+
'Content-Type': 'application/json',
18+
...corsHeaders
19+
}
20+
});
21+
}
22+
23+
const audioInfo = await (await sunoApi)
24+
.generateStems(audio_id);
25+
26+
return new NextResponse(JSON.stringify(audioInfo), {
27+
status: 200,
28+
headers: {
29+
'Content-Type': 'application/json',
30+
...corsHeaders
31+
}
32+
});
33+
} catch (error: any) {
34+
console.error('Error generating stems:', JSON.stringify(error.response.data));
35+
if (error.response.status === 402) {
36+
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
37+
status: 402,
38+
headers: {
39+
'Content-Type': 'application/json',
40+
...corsHeaders
41+
}
42+
});
43+
}
44+
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
45+
status: 500,
46+
headers: {
47+
'Content-Type': 'application/json',
48+
...corsHeaders
49+
}
50+
});
51+
}
52+
} else {
53+
return new NextResponse('Method Not Allowed', {
54+
headers: {
55+
Allow: 'POST',
56+
...corsHeaders
57+
},
58+
status: 405
59+
});
60+
}
61+
}
62+
63+
64+
export async function OPTIONS(request: Request) {
65+
return new Response(null, {
66+
status: 200,
67+
headers: corsHeaders
68+
});
69+
}

src/app/docs/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default function Docs() {
2929
ids. If no IDs are provided, all music will be returned.
3030
- \`/api/get_limit\`: Get quota Info
3131
- \`/api/extend_audio\`: Extend audio length
32+
- \`/api/generate_stems\`: Make stem tracks (separate audio and music track)
3233
- \`/api/clip\`: Get clip information based on ID passed as query parameter \`id\`
3334
- \`/api/concat\`: Generate the whole song from extensions
3435
\`\`\`

src/app/docs/swagger-suno-api.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,35 @@
243243
}
244244
}
245245
},
246+
"/api/generate_stems": {
247+
"post": {
248+
"summary": "Make stem tracks (separate audio and music track).",
249+
"description": "Make stem tracks (separate audio and music track).",
250+
"tags": ["default"],
251+
"requestBody": {
252+
"content": {
253+
"application/json": {
254+
"schema": {
255+
"type": "object",
256+
"required": ["audio_id"],
257+
"properties": {
258+
"audio_id": {
259+
"type": "string",
260+
"description": "The ID of the song to generate stems for.",
261+
"example": "e76498dc-6ab4-4a10-a19f-8a095790e28d"
262+
}
263+
}
264+
}
265+
}
266+
}
267+
},
268+
"responses": {
269+
"200": {
270+
"$ref": "#/components/schemas/audio_info"
271+
}
272+
}
273+
}
274+
},
246275
"/api/generate_lyrics": {
247276
"post": {
248277
"summary": "Generate lyrics based on Prompt.",

src/app/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Suno API currently mainly implements the following APIs:
106106
- \`/api/get?ids=\`: Get music Info by id, separate multiple id with ",".
107107
- \`/api/get_limit\`: Get quota Info
108108
- \`/api/extend_audio\`: Extend audio length
109+
- \`/api/generate_stems\`: Make stem tracks (separate audio and music track)
109110
- \`/api/concat\`: Generate the whole song from extensions
110111
\`\`\`
111112

src/lib/SunoApi.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,27 @@ class SunoApi {
383383
return response.data;
384384
}
385385

386+
/**
387+
* Generate stems for a song.
388+
* @param song_id The ID of the song to generate stems for.
389+
* @returns A promise that resolves to an AudioInfo object representing the generated stems.
390+
*/
391+
public async generateStems(song_id: string): Promise<AudioInfo[]> {
392+
const response = await this.client.post(
393+
`${SunoApi.BASE_URL}/api/edit/stems/${song_id}`, {}
394+
);
395+
console.log('generateStems response:\n', response?.data);
396+
return response.data.clips.map((clip: any) => ({
397+
id: clip.id,
398+
status: clip.status,
399+
metadata: clip.metadata,
400+
created_at: clip.created_at,
401+
title: clip.title,
402+
stem_from_id: clip.metadata.stem_from_id,
403+
duration: clip.metadata.duration
404+
}));
405+
}
406+
386407
/**
387408
* Processes the lyrics (prompt) from the audio metadata into a more readable format.
388409
* @param prompt The original lyrics text.

0 commit comments

Comments
 (0)