Skip to content

Commit 80a434f

Browse files
authored
Merge pull request #189 from tmirkovic/add-routes
Add routes for generating stems and getting lyrics timestamps
2 parents c82e4c3 + 15bab02 commit 80a434f

File tree

8 files changed

+231
-0
lines changed

8 files changed

+231
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ 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)
130+
- `/api/get_aligned_lyrics`: Get list of timestamps for each word in the lyrics
129131
- `/api/clip`: Get clip information based on ID passed as query parameter `id`
130132
- `/api/concat`: Generate the whole song from extensions
131133
```

README_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Suno API 目前主要实现了以下 API:
123123
- `/api/get`: 根据id获取音乐信息。获取多个请用","分隔,不传ids则返回所有音乐
124124
- `/api/get_limit`: 获取配额信息
125125
- `/api/extend_audio`: 在一首音乐的基础上,扩展音乐长度
126+
- `/api/generate_stems`: 制作主干轨道(单独的音频和音乐轨道
127+
- `/api/get_aligned_lyrics`: 获取歌词中每个单词的时间戳列表
126128
- `/api/clip`: 检索特定音乐的信息
127129
- `/api/concat`: 合并音乐,将扩展后的音乐和原始音乐合并
128130
```
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { NextResponse, NextRequest } from "next/server";
2+
import { sunoApi } from "@/lib/SunoApi";
3+
import { corsHeaders } from "@/lib/utils";
4+
5+
export const dynamic = "force-dynamic";
6+
7+
export async function GET(req: NextRequest) {
8+
if (req.method === 'GET') {
9+
try {
10+
const url = new URL(req.url);
11+
const song_id = url.searchParams.get('song_id');
12+
13+
if (!song_id) {
14+
return new NextResponse(JSON.stringify({ error: 'Song ID is required' }), {
15+
status: 400,
16+
headers: {
17+
'Content-Type': 'application/json',
18+
...corsHeaders
19+
}
20+
});
21+
}
22+
23+
const lyricAlignment = await (await sunoApi).getLyricAlignment(song_id);
24+
25+
26+
return new NextResponse(JSON.stringify(lyricAlignment), {
27+
status: 200,
28+
headers: {
29+
'Content-Type': 'application/json',
30+
...corsHeaders
31+
}
32+
});
33+
} catch (error) {
34+
console.error('Error fetching lyric alignment:', error);
35+
36+
return new NextResponse(JSON.stringify({ error: 'Internal server error. ' + error }), {
37+
status: 500,
38+
headers: {
39+
'Content-Type': 'application/json',
40+
...corsHeaders
41+
}
42+
});
43+
}
44+
} else {
45+
return new NextResponse('Method Not Allowed', {
46+
headers: {
47+
Allow: 'GET',
48+
...corsHeaders
49+
},
50+
status: 405
51+
});
52+
}
53+
}
54+
55+
export async function OPTIONS(request: Request) {
56+
return new Response(null, {
57+
status: 200,
58+
headers: corsHeaders
59+
});
60+
}

src/app/docs/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ 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)
33+
- \`/api/get_aligned_lyrics\`: Get list of timestamps for each word in the lyrics
3234
- \`/api/clip\`: Get clip information based on ID passed as query parameter \`id\`
3335
- \`/api/concat\`: Generate the whole song from extensions
3436
\`\`\`

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

Lines changed: 52 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.",
@@ -387,6 +416,29 @@
387416
}
388417
}
389418
},
419+
"/api/get_aligned_lyrics": {
420+
"get": {
421+
"summary": "Get lyric alignment.",
422+
"description": "Get lyric alignment.",
423+
"tags": ["default"],
424+
"parameters": [
425+
{
426+
"name": "song_id",
427+
"in": "query",
428+
"required": true,
429+
"description": "Song ID",
430+
"schema": {
431+
"type": "string"
432+
}
433+
}
434+
],
435+
"responses": {
436+
"200": {
437+
"$ref": "#/components/schemas/audio_info"
438+
}
439+
}
440+
}
441+
},
390442
"/api/clip": {
391443
"get": {
392444
"summary": "Get clip information based on ID.",

src/app/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ 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)
110+
- \`/api/get_aligned_lyrics\`: Get list of timestamps for each word in the lyrics
109111
- \`/api/concat\`: Generate the whole song from extensions
110112
\`\`\`
111113

src/lib/SunoApi.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,48 @@ class SunoApi {
385385
return response.data;
386386
}
387387

388+
/**
389+
* Generate stems for a song.
390+
* @param song_id The ID of the song to generate stems for.
391+
* @returns A promise that resolves to an AudioInfo object representing the generated stems.
392+
*/
393+
public async generateStems(song_id: string): Promise<AudioInfo[]> {
394+
await this.keepAlive(false);
395+
const response = await this.client.post(
396+
`${SunoApi.BASE_URL}/api/edit/stems/${song_id}`, {}
397+
);
398+
399+
console.log('generateStems response:\n', response?.data);
400+
return response.data.clips.map((clip: any) => ({
401+
id: clip.id,
402+
status: clip.status,
403+
created_at: clip.created_at,
404+
title: clip.title,
405+
stem_from_id: clip.metadata.stem_from_id,
406+
duration: clip.metadata.duration
407+
}));
408+
}
409+
410+
411+
/**
412+
* Get the lyric alignment for a song.
413+
* @param song_id The ID of the song to get the lyric alignment for.
414+
* @returns A promise that resolves to an object containing the lyric alignment.
415+
*/
416+
public async getLyricAlignment(song_id: string): Promise<object> {
417+
await this.keepAlive(false);
418+
const response = await this.client.get(`${SunoApi.BASE_URL}/api/gen/${song_id}/aligned_lyrics/v2/`);
419+
420+
console.log(`getLyricAlignment ~ response:`, response.data);
421+
return response.data?.aligned_words.map((transcribedWord: any) => ({
422+
word: transcribedWord.word,
423+
start_s: transcribedWord.start_s,
424+
end_s: transcribedWord.end_s,
425+
success: transcribedWord.success,
426+
p_align: transcribedWord.p_align
427+
}));
428+
}
429+
388430
/**
389431
* Processes the lyrics (prompt) from the audio metadata into a more readable format.
390432
* @param prompt The original lyrics text.

0 commit comments

Comments
 (0)