FantasyTalking 安裝指南
環境要求
在安裝 FantasyTalking 之前,需要確保開發環境滿足以下要求。作業系統方面,項目支援 Windows、macOS 和 Linux 等主流作業系統,強烈建議使用 64 位元系統以獲得最佳效能。Node.js 版本需要 14.0 或更高版本,Python 版本需要 3.7 或更高版本。如果使用 Python 後端,還需要安裝 FFmpeg 音訊處理工具。
硬體方面,為了獲得流暢的語音合成體驗,系統至少需要 4GB 記憶體和雙核心處理器。對於需要即時語音合成的應用場景,建議使用 SSD 儲存以降低 I/O 延遲。如果計劃進行模型訓練或自定義聲音開發,需要配備支援 CUDA 的顯示卡。
安裝方式
FantasyTalking 支援多種安裝方式,開發者可以根據自己的需求和技術棧選擇最合適的方法。
使用 npm 安裝(Node.js)
# 安裝核心庫
npm install fantasy-talking
# 安裝可選元件
npm install fantasy-talking-ui # Web UI 元件
npm install fantasy-talking-node # Node.js 後端支援
使用 pip 安裝(Python)
# 安裝核心庫
pip install fantasy-talking
# 安裝可選元件
pip install fantasy-talking[ui] # Web UI 元件
pip install fantasy-talking[all] # 所有可選元件
從原始碼編譯
# 複製倉庫
git clone https://github.com/fantasytalking/fantasy-talking.git
cd fantasy-talking
# 安裝 Node.js 依賴
npm install
# 安裝 Python 依賴(可選)
pip install -r requirements.txt
# 構建項目
npm run build
快速開始
JavaScript/TypeScript 使用範例
const FantasyTalking = require('fantasy-talking');
// 初始化客戶端
const client = new FantasyTalking({
apiKey: 'your-api-key',
// 可選:指定語音風格
voice: {
style: 'neutral',
language: 'zh-TW',
speed: 1.0,
pitch: 1.0
}
});
// 合成語音
async function synthesize() {
const audio = await client.speak('你好,這是 FantasyTalking 的測試語音');
console.log('語音合成成功');
// 保存到檔案
const fs = require('fs');
fs.writeFileSync('output.wav', audio);
// 或直接播放
client.play(audio);
}
synthesize();
Python 使用範例
from fantasy_talking import FantasyTalking
# 初始化客戶端
client = FantasyTalking(
api_key='your-api-key',
voice={
'style': 'neutral',
'language': 'zh-TW',
'speed': 1.0,
'pitch': 1.0
}
)
# 合成語音
audio = client.speak('你好,這是 FantasyTalking 的測試語音')
# 保存到檔案
with open('output.wav', 'wb') as f:
f.write(audio)
print('語音合成成功')
配置選項
FantasyTalking 提供了豐富的配置選項,可以根據具體需求進行調整。
基本配置
| 參數 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| apiKey | string | - | API 密鑰,必填 |
| apiUrl | string | 預設地址 | API 伺服器位址 |
| timeout | number | 30000 | 請求超時時間(毫秒) |
| retries | number | 3 | 請求失敗重試次數 |
語音配置
| 參數 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| voice.style | string | neutral | 語音風格:neutral, happy, sad, angry, excited |
| voice.language | string | zh-TW | 語言代碼 |
| voice.speed | number | 1.0 | 語速,範圍 0.5-2.0 |
| voice.pitch | number | 1.0 | 音調,範圍 0.5-2.0 |
| voice.volume | number | 1.0 | 音量,範圍 0-1.0 |
進階配置
對於進階使用者,可以配置更多的參數來實現更精細的控制:
const client = new FantasyTalking({
apiKey: 'your-api-key',
// 音訊輸出格式
audio: {
format: 'wav', // wav, mp3, ogg
sampleRate: 16000, // 8000, 16000, 24000, 48000
bitDepth: 16 // 8, 16, 24
},
// 對話配置
dialogue: {
enable: true,
contextWindow: 5, // 保留的對話上下文輪數
systemPrompt: '你是一個友善的語音助手'
},
// 串流輸出
stream: {
enable: true,
chunkSize: 1024 # 串流輸出的區塊大小
}
});
服務部署
Docker 部署
項目提供了 Docker 鏡像,方便快速部署:
# 拉取鏡像
docker pull fantasytalking/server:latest
# 執行容器
docker run -d \
--name fantasy-talking \
-p 8080:8080 \
-e API_KEY=your-api-key \
-e PORT=8080 \
fantasytalking/server:latest
自建服務
如果需要完全控制服務,可以自行部署後端服務:
# 複製後端代碼
git clone https://github.com/fantasytalking/server.git
cd server
# 配置環境變數
cp .env.example .env
# 編輯 .env 檔案,填入 API 密鑰等配置
# 啟動服務
npm run start
服務啟動後,可以通過 RESTful API 進行語音合成請求。詳細的 API 文檔請參考官方文檔。
驗證安裝
安裝完成後,可以通過以下方式驗證 FantasyTalking 是否正常運作:
# 使用命令列工具測試
fantasy-talking --text "測試語音" --output test.wav
# 或者執行範例代碼
npm run example
如果一切正常,應該能看到語音檔案生成成功。常見的安裝問題及解決方案請參考常見問題頁面。
