feat: add extend api

- Added extend API
- Added documentation for extend API
This commit is contained in:
blueeon
2024-04-27 17:58:27 +08:00
parent fc3d59a4e9
commit dd5a5208de
7 changed files with 177 additions and 6 deletions
+70
View File
@@ -0,0 +1,70 @@
import { NextResponse, NextRequest } from "next/server";
import { sunoApi } from "@/lib/SunoApi";
import { corsHeaders } from "@/lib/utils";
export const dynamic = "force-dynamic";
export async function POST(req: NextRequest) {
if (req.method === 'POST') {
try {
const body = await req.json();
const { audio_id, prompt, continue_at, tags, title } = body;
console.log(body)
if (!audio_id) {
return new NextResponse(JSON.stringify({ error: 'Audio ID is required' }), {
status: 400,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
const audioInfo = await (await sunoApi)
.extendAudio(audio_id, prompt, continue_at, tags, title);
return new NextResponse(JSON.stringify(audioInfo), {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
} catch (error: any) {
console.error('Error extend audio:', JSON.stringify(error.response.data));
if (error.response.status === 402) {
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
status: 402,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
status: 500,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: {
Allow: 'POST',
...corsHeaders
},
status: 405
});
}
}
export async function OPTIONS(request: Request) {
return new Response(null, {
status: 200,
headers: corsHeaders
});
}
+1
View File
@@ -28,6 +28,7 @@ export default function Docs() {
- \`/api/get\`: Get music information based on the id. Use “,” to separate multiple
ids. If no IDs are provided, all music will be returned.
- \`/api/get_limit\`: Get quota Info
- \`/api/extend_audio\`: Extend audio length
\`\`\`
Feel free to explore the detailed API parameters and conduct tests on this page.
+44
View File
@@ -179,6 +179,50 @@
}
}
},
"/api/extend_audio": {
"post": {
"summary": "Extend audio length.",
"description": "Extend audio length.",
"tags": ["default"],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": ["audio_id"],
"properties": {
"audio_id": {
"type": "string",
"description": "The ID of the audio clip to extend.",
"example": "e76498dc-6ab4-4a10-a19f-8a095790e28d"
},
"prompt": {
"type": "string",
"description": "Detailed prompt, including information such as music lyrics.",
"example": ""
},
"continue_at": {
"type": "string",
"description": "Extend a new clip from a song at mm:ss(e.g. 00:30). Default extends from the end of the song.",
"example": "109.96"
},
"title": {
"type": "string",
"description": "Music title",
"example": ""
},
"tags": {
"type": "string",
"description": "Music genre",
"example": ""
}
}
}
}
}
}
}
},
"/api/generate_lyrics": {
"post": {
"summary": "Generate lyrics based on Prompt.",
+1
View File
@@ -105,6 +105,7 @@ Suno API currently mainly implements the following APIs:
- \`/api/get\`: Get music list
- \`/api/get?ids=\`: Get music Info by id, separate multiple id with ",".
- \`/api/get_limit\`: Get quota Info
- \`/api/extend_audio\`: Extend audio length
\`\`\`
For more detailed documentation, please check out the demo site:
+29 -1
View File
@@ -255,6 +255,34 @@ class SunoApi {
return lyricsResponse.data;
}
/**
* Extends an existing audio clip by generating additional content based on the provided prompt.
*
* @param audioId The ID of the audio clip to extend.
* @param prompt The prompt for generating additional content.
* @param continueAt Extend a new clip from a song at mm:ss(e.g. 00:30). Default extends from the end of the song.
* @param tags Style of Music.
* @param title Title of the song.
* @returns A promise that resolves to an AudioInfo object representing the extended audio clip.
*/
public async extendAudio(
audioId: string,
prompt: string = "",
continueAt: string = "0",
tags: string = "",
title: string = ""
): Promise<AudioInfo> {
const response = await this.client.post(`${SunoApi.BASE_URL}/api/generate/v2/`, {
continue_clip_id: audioId,
continue_at: continueAt,
mv: "chirp-v3-0",
prompt: prompt,
tags: tags,
title: ""
});
return response.data;
}
/**
* Processes the lyrics (prompt) from the audio metadata into a more readable format.
* @param prompt The original lyrics text.
@@ -327,7 +355,7 @@ const newSunoApi = async (cookie: string) => {
return await sunoApi.init();
}
if (! process.env.SUNO_COOKIE) {
if (!process.env.SUNO_COOKIE) {
console.log("Environment does not contain SUNO_COOKIE.", process.env)
}