Manual Pengguna FantasyTalking
Ciri Asas
FantasyTalking menyediakan fungsi sintesis pertuturan dan dialog yang menyeluruh. Manual ini akan memperkenalkan cara menggunakan pelbagai ciri dengan terperinci.
Sintesis Pertuturan
Sintesis pertuturan ialah ciri teras FantasyTalking, menukar teks menjadi pertuturan yang semula jadi dan lancar. Berikut ialah penggunaan asas:
// Simple text-to-speech
const audio = await client.speak('Hello, welcome to FantasyTalking');
// Specify voice style
const audio = await client.speak('The weather is great today', {
voice: {
style: 'happy',
language: 'en-US'
}
});
// Specify speed and pitch
const audio = await client.speak('Please wait a moment', {
voice: {
speed: 1.2,
pitch: 0.9
}
});
Bahasa Penanda SSML
FantasyTalking menyokong bahasa penanda SSML untuk kawalan yang lebih halus ke atas output pertuturan:
<speak>
<p>
<s>First sentence.</s>
<s>Second sentence.</s>
</p>
<break time="500ms"/>
<p>
<emphasis level="moderate">This is important content</emphasis>
</p>
</speak>
Kod JavaScript yang sepadan:
const ssml = `
<speak>
<p>
<s>First sentence.</s>
<s>Second sentence.</s>
</p>
<break time="500ms"/>
<p>
<emphasis level="moderate">This is important content</emphasis>
</p>
</speak>
`;
const audio = await client.speak(ssml, { ssml: true });
Peristiwa Suara
Semasa main balik pertuturan, anda boleh mendengar pelbagai peristiwa untuk interaksi:
// Listen for speech start
client.on('speech-start', (event) => {
console.log('Speech started playing');
});
// Listen for word boundary (for subtitle sync)
client.on('word-boundary', (event) => {
console.log(`Current word: ${event.word}`);
console.log(`Start time: ${event.start}ms`);
console.log(`End time: ${event.end}ms`);
});
// Listen for speech end
client.on('speech-end', (event) => {
console.log('Speech playback completed');
});
// Listen for errors
client.on('error', (event) => {
console.error('Error:', event.error);
});
Sintesis Berkelompok
Apabila perlu mensintesis sejumlah besar teks, anda boleh menggunakan sintesis berkelompok untuk kecekapan yang lebih baik:
const texts = [
'This is the first voice',
'This is the second voice',
'This is the third voice'
];
// Parallel synthesis
const results = await Promise.all(
texts.map(text => client.speak(text))
);
// Sequential synthesis (preserving order)
const results = [];
for (const text of texts) {
const audio = await client.speak(text);
results.push(audio);
}
Ciri Dialog
Dialog Asas
FantasyTalking menyediakan fungsi pengurusan dialog yang berkuasa, menyokong perbualan berbilang pusingan dan ingatan konteks:
// Create dialogue session
const session = client.createSession({
// Optional: set system prompt
systemPrompt: 'You are a friendly assistant, good at answering questions',
// Optional: set language
language: 'en-US'
});
// First turn
const response1 = await session.chat('How is the weather today?');
console.log(response1.text); // AI response text
console.log(response1.audio); // AI response audio
// Second turn (automatically includes context)
const response2 = await session.chat('What about tomorrow?');
console.log(response2.text);
Konfigurasi Dialog
Anda boleh mengkonfigurasi untuk senario dialog yang berbeza:
const session = client.createSession({
// Dialogue configuration
dialogue: {
// Temperature, controls response creativity
temperature: 0.7,
// Max response tokens
maxTokens: 500,
// Topic tags, for distinguishing scenarios
tags: ['customer-service', 'pre-sales']
},
// Voice configuration
voice: {
// AI response voice style
style: 'professional',
// Speech rate
speed: 1.0
}
});
Pengurusan Sejarah Dialog
// Get dialogue history
const history = session.getHistory();
console.log(history);
// Save dialogue history
const savedHistory = session.save();
fs.writeFileSync('conversation.json', JSON.stringify(savedHistory));
// Load dialogue history
const loadedSession = client.loadSession(savedHistory);
Ciri Lanjutan
Sintesis Pertuturan Penstriman
Untuk senario maklum balas masa nyata, anda boleh menggunakan sintesis pertuturan penstriman:
// Enable streaming output
const stream = await client.speakStream('This is a long piece of text');
// Listen for data chunks
stream.on('data', (chunk) => {
// Play audio chunk
audioPlayer.play(chunk);
});
stream.on('end', () => {
console.log('Speech synthesis completed');
});
stream.on('error', (error) => {
console.error('Error:', error);
});
Model Suara Tersuai
Jika anda memerlukan suara eksklusif jenama, anda boleh melatih model tersuai:
// Prepare training data
const trainingData = {
name: 'Brand Voice',
description: 'Voice for enterprise brand',
samples: [
{ text: 'Sample text 1', audio: 'base64 encoded audio 1' },
{ text: 'Sample text 2', audio: 'base64 encoded audio 2' },
// More samples...
]
};
// Submit training job
const job = await client.trainVoice(trainingData);
// Poll training status
while (job.status !== 'completed') {
await sleep(5000);
const status = await client.getJobStatus(job.id);
console.log(`Training progress: ${status.progress}%`);
}
// After training, use custom voice
const audio = await client.speak('Using custom voice', {
voice: {
modelId: job.modelId
}
});
Pasca-Pemprosesan Audio
FantasyTalking menyediakan pelbagai fungsi pasca-pemprosesan audio:
const audio = await client.speak('Original speech');
// Volume adjustment
const normalized = client.process(audio, {
normalize: true,
targetLevel: -20 // dB
});
// Noise reduction
const denoised = client.process(audio, {
denoise: true,
noiseThreshold: 0.5
});
// Speed adjustment
const spedUp = client.process(audio, {
speed: 1.5 // 1.5x speed
});
Amalan Terbaik
Pengoptimuman Prestasi
Dalam aplikasi sebenar, ambil perhatian perkara berikut untuk pengoptimuman prestasi:
- Penggunaan semula sambungan: Elakkan mencipta instans klien dengan kerap; menggunakan semula sambungan boleh mengurangkan kependaman dengan ketara
- Pemprosesan berkelompok: Apabila memproses sejumlah besar teks, gunakan sintesis berkelompok
- Caching: Untuk teks yang sama, gunakan caching untuk mengelakkan sintesis berulang
- Output penstriman: Gunakan output penstriman untuk teks panjang supaya main balik boleh bermula lebih awal dan meningkatkan pengalaman pengguna
Pengendalian Ralat
Adalah disyorkan untuk melaksanakan pengendalian ralat yang menyeluruh:
try {
const audio = await client.speak('Test text');
} catch (error) {
if (error.code === 'RATE_LIMIT') {
// Handle rate limit
console.log('Too many requests, please try again later');
} else if (error.code === 'INVALID_API_KEY') {
// Handle authentication error
console.log('Invalid API key');
} else {
// Other errors
console.error('Unknown error:', error);
}
}
Pengalaman Pengguna
Untuk pengalaman pengguna yang lebih baik, adalah disyorkan untuk:
- Pramuat: Pra-sintesis pertuturan sebelum pengguna mungkin memerlukannya
- Maklum balas: Berikan maklum balas visual semasa main balik pertuturan
- Pengendalian gangguan: Sokong pengguna mengganggu main balik pertuturan
- Sokongan luar talian: Laksanakan sokongan luar talian untuk ciri asas
