Skip to content

Commit 15bab02

Browse files
committed
Add route for getting aligned lyrics
1 parent 96b675a commit 15bab02

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Suno API currently mainly implements the following APIs:
127127
- `/api/get_limit`: Get quota Info
128128
- `/api/extend_audio`: Extend audio length
129129
- `/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
130131
- `/api/clip`: Get clip information based on ID passed as query parameter `id`
131132
- `/api/concat`: Generate the whole song from extensions
132133
```

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Suno API 目前主要实现了以下 API:
124124
- `/api/get_limit`: 获取配额信息
125125
- `/api/extend_audio`: 在一首音乐的基础上,扩展音乐长度
126126
- `/api/generate_stems`: 制作主干轨道(单独的音频和音乐轨道
127+
- `/api/get_aligned_lyrics`: 获取歌词中每个单词的时间戳列表
127128
- `/api/clip`: 检索特定音乐的信息
128129
- `/api/concat`: 合并音乐,将扩展后的音乐和原始音乐合并
129130
```
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function Docs() {
3030
- \`/api/get_limit\`: Get quota Info
3131
- \`/api/extend_audio\`: Extend audio length
3232
- \`/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
3334
- \`/api/clip\`: Get clip information based on ID passed as query parameter \`id\`
3435
- \`/api/concat\`: Generate the whole song from extensions
3536
\`\`\`

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,29 @@
416416
}
417417
}
418418
},
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+
},
419442
"/api/clip": {
420443
"get": {
421444
"summary": "Get clip information based on ID.",

src/app/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Suno API currently mainly implements the following APIs:
107107
- \`/api/get_limit\`: Get quota Info
108108
- \`/api/extend_audio\`: Extend audio length
109109
- \`/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
110111
- \`/api/concat\`: Generate the whole song from extensions
111112
\`\`\`
112113

src/lib/SunoApi.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,21 +389,42 @@ class SunoApi {
389389
* @returns A promise that resolves to an AudioInfo object representing the generated stems.
390390
*/
391391
public async generateStems(song_id: string): Promise<AudioInfo[]> {
392+
await this.keepAlive(false);
392393
const response = await this.client.post(
393394
`${SunoApi.BASE_URL}/api/edit/stems/${song_id}`, {}
394395
);
396+
395397
console.log('generateStems response:\n', response?.data);
396398
return response.data.clips.map((clip: any) => ({
397399
id: clip.id,
398400
status: clip.status,
399-
metadata: clip.metadata,
400401
created_at: clip.created_at,
401402
title: clip.title,
402403
stem_from_id: clip.metadata.stem_from_id,
403404
duration: clip.metadata.duration
404405
}));
405406
}
406407

408+
409+
/**
410+
* Get the lyric alignment for a song.
411+
* @param song_id The ID of the song to get the lyric alignment for.
412+
* @returns A promise that resolves to an object containing the lyric alignment.
413+
*/
414+
public async getLyricAlignment(song_id: string): Promise<object> {
415+
await this.keepAlive(false);
416+
const response = await this.client.get(`${SunoApi.BASE_URL}/api/gen/${song_id}/aligned_lyrics/v2/`);
417+
418+
console.log(`getLyricAlignment ~ response:`, response.data);
419+
return response.data?.aligned_words.map((transcribedWord: any) => ({
420+
word: transcribedWord.word,
421+
start_s: transcribedWord.start_s,
422+
end_s: transcribedWord.end_s,
423+
success: transcribedWord.success,
424+
p_align: transcribedWord.p_align
425+
}));
426+
}
427+
407428
/**
408429
* Processes the lyrics (prompt) from the audio metadata into a more readable format.
409430
* @param prompt The original lyrics text.

0 commit comments

Comments
 (0)