Manual Pengguna FantasyTalking
Fitur Dasar
FantasyTalking menyediakan fungsi sintesis ucapan dan dialog yang komprehensif. Manual ini akan memperkenalkan cara menggunakan berbagai fitur secara terperinci.
Sintesis Ucapan
Sintesis ucapan adalah fitur inti FantasyTalking, mengonversi teks menjadi ucapan yang alami dan mulus. Berikut adalah penggunaan dasarnya:
// 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 Markup SSML
FantasyTalking mendukung bahasa markup SSML untuk kontrol yang lebih halus atas keluaran ucapan:
<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>
Kode JavaScript yang sesuai:
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
Selama pemutaran ucapan, Anda dapat mendengarkan berbagai 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 Batch
Ketika perlu menyintesis teks dalam jumlah besar, Anda dapat menggunakan sintesis batch untuk efisiensi 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);
}
Fitur Dialog
Dialog Dasar
FantasyTalking menyediakan fungsi manajemen dialog yang kuat, mendukung percakapan multi-giliran dan memori 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 dapat mengonfigurasi untuk berbagai skenario dialog:
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
}
});
Manajemen Riwayat 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);
Fitur Lanjutan
Sintesis Ucapan Streaming
Untuk skenario umpan balik waktu nyata, Anda dapat menggunakan sintesis ucapan streaming:
// 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 Kustom
Jika Anda memerlukan suara eksklusif merek, Anda dapat melatih model kustom:
// 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-Pemrosesan Audio
FantasyTalking menyediakan berbagai fungsi pasca-pemrosesan 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
});
Praktik Terbaik
Optimasi Performa
Dalam aplikasi nyata, perhatikan hal-hal berikut untuk optimasi performa:
- Penggunaan ulang koneksi: Hindari sering membuat instans klien; menggunakan ulang koneksi dapat secara signifikan mengurangi latensi
- Pemrosesan batch: Saat memproses teks dalam jumlah besar, gunakan sintesis batch
- Caching: Untuk teks yang identik, gunakan caching untuk menghindari sintesis duplikat
- Keluaran streaming: Gunakan keluaran streaming untuk teks panjang agar pemutaran dapat dimulai lebih awal dan meningkatkan pengalaman pengguna
Penanganan Kesalahan
Disarankan untuk mengimplementasikan penanganan kesalahan yang komprehensif:
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, disarankan untuk:
- Pra-pemuatan: Sintesis ucapan terlebih dahulu sebelum pengguna mungkin membutuhkannya
- Umpan balik: Berikan umpan balik visual selama pemutaran ucapan
- Penanganan interupsi: Dukung pengguna menginterupsi pemutaran ucapan
- Dukungan offline: Implementasikan dukungan offline untuk fitur dasar
