跳到主要内容

TTS ​快速​开始

本​指南​将​使用​ S​hisa TTS 生​成​您​的​第一​段​语音​音频。​您​需要​一​个​ A​PI 密钥 — ​在​ Shisa 平台创建。​新账户​包含​ $10 ​的​免费额​度

1. ​设置​您​的​端点​和​密钥

使用​标准​ Bearer ​令牌​对​每​个​请​求​进行​身份验证:

Endpoint: https://api.shisa.ai/tts
Auth: Authorization: Bearer YOUR_API_KEY
提示

请​勿​将​ A​PI 密钥​纳入源​代码​管理。​在​实际​应用​程序​中,​应从​环境​变量​(例如​ SHISA_API_KEY)​中​读取。

2. 生成语​音

发送​包含​您​所​需 voice_idtext ​和​ format ​的​ POST ​请​求。​以​下示例​使用​声音​ 61ba1141-60aa-4bc3-a3b3-be1ec20700b3

curl
# List available voices
curl -s -X GET "https://api.shisa.ai/tts/voices" \
-H "Authorization: Bearer YOUR_API_KEY" | jq .

# Generate speech
curl -s -X POST "https://api.shisa.ai/tts" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"voice_id": "61ba1141-60aa-4bc3-a3b3-be1ec20700b3",
"format": "mp3",
"stream": false,
"text": "こんにちは。Shisa APIへようこそ。"
}' \
--output speech.mp3

# Stream audio directly to a player
curl -s -X POST "https://api.shisa.ai/tts" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"voice_id": "61ba1141-60aa-4bc3-a3b3-be1ec20700b3",
"format": "mp3",
"stream": true,
"text": "ストリーミングテストです。"
}' \
--output - | ffplay -nodisp -autoexit -
Python
import requests

API_URL = "https://api.shisa.ai"
API_KEY = "YOUR_API_KEY"
HEADERS = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}

# List available voices
def list_voices():
response = requests.get(f"{API_URL}/tts/voices", headers=HEADERS)
return response.json()

# Generate speech
def generate_speech(text, voice_id="61ba1141-60aa-4bc3-a3b3-be1ec20700b3", format="mp3", stream=False):
response = requests.post(
f"{API_URL}/tts",
headers=HEADERS,
json={
"voice_id": voice_id,
"format": format,
"stream": stream,
"text": text
},
stream=stream
)

output_file = f"output.{format}"
with open(output_file, "wb") as f:
if stream:
for chunk in response.iter_content():
f.write(chunk)
else:
f.write(response.content)

return output_file

# Example usage
voices = list_voices()
print(voices)

audio_file = generate_speech(
"お客様の声を大切にしています。",
voice_id="61ba1141-60aa-4bc3-a3b3-be1ec20700b3"
)
JavaScript
const API_URL = 'https://api.shisa.ai';
const API_KEY = 'YOUR_API_KEY';
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
};

// List available voices
const listVoices = async () => {
const response = await fetch(`${API_URL}/tts/voices`, { headers });
return response.json();
};

// Generate speech
const generateSpeech = async (text, voiceId = '61ba1141-60aa-4bc3-a3b3-be1ec20700b3', format = 'mp3') => {
const response = await fetch(`${API_URL}/tts`, {
method: 'POST',
headers,
body: JSON.stringify({
voice_id: voiceId,
format,
stream: true,
text,
}),
});

// Handle streaming response
const reader = response.body.getReader();
const chunks = [];

while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}

// Combine chunks and create audio blob
const blob = new Blob(chunks, { type: `audio/${format}` });
const url = URL.createObjectURL(blob);

// Play audio
const audio = new Audio(url);
audio.play();
};

// Example usage
const voices = await listVoices();
console.log(voices);

await generateSpeech('ようこそ、Shisa APIへ。', '61ba1141-60aa-4bc3-a3b3-be1ec20700b3');

3.​ ​播放音频

响应​是​ 二​进制​音频,​而​非 JSON — ​请​求会​将​其​直接​写入​文件​(speech.mp3)。​使用​任意​音​频​工​具​播放:

# Play the generated audio
ffplay -nodisp -autoexit speech.mp3

# Or stream directly
curl -s -X POST "https://api.shisa.ai/tts" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"voice_id": "61ba1141-60aa-4bc3-a3b3-be1ec20700b3", "format": "mp3", "stream": true, "text": "ストリーミングテストです。"}' \
--output - | ffplay -nodisp -autoexit -
备注

流式​传输​("stream": true)​仅​适用​于​ streaming: true ​的​声音。​请​在声​音目录​中查​看​声音​的​功能。

后续​步​骤

  • 声​音中​浏览​所有​可用​的​声音。
  • 在​ API ​参考中查​看​所有​参数、​响应​格式​和​错误​代码。
  • 身份验证中​了解​各​服务​通用​的​认证​头​约定。