Bảng điểm của thành viên Phan Tuấn Hải
Chuyển văn bản thành giọng nói
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const fs = require("fs");
/**
* Hàm chuyển đổi văn bản thành file âm thanh chuyên nghiệp
* @param {string} text - Nội dung cần chuyển đổi
* @param {string} fileName - Tên file đầu ra (vd: output.mp3)
*/
async function textToSpeech(text, fileName) {
// 1. Cấu hình thông tin kết nối
const speechConfig = sdk.SpeechConfig.fromSubscription(
"YOUR_AZURE_SPEECH_KEY",
"YOUR_AZURE_REGION"
);
// 2. Thiết lập giọng đọc (Clipchamp thường dùng các giọng Neural này)
// Giọng nữ: vi-VN-HoaiMyNeural | Giọng nam: vi-VN-NamMinhNeural
speechConfig.speechSynthesisVoiceName = "vi-VN-HoaiMyNeural";
// Thiết lập định dạng đầu ra là MP3
speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Audio16Khz128KBitrateMonoMp3;
// 3. Cấu hình file đầu ra
const audioConfig = sdk.AudioConfig.fromAudioFileOutput(fileName);
// 4. Khởi tạo bộ tổng hợp
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
console.log(`Đang xử lý giọng nói: "${text.substring(0, 30)}..."`);
return new Promise((resolve, reject) => {
synthesizer.speakTextAsync(
text,
result => {
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
console.log(`Thành công! File đã lưu tại: ${fileName}`);
resolve(result);
} else {
console.error("Lỗi chi tiết: " + result.errorDetails);
reject(result.errorDetails);
}
synthesizer.close();
},
err => {
console.trace("Lỗi hệ thống: " + err);
synthesizer.close();
reject(err);
}
);
});
}
// --- CÁCH SỬ DỤNG ---
const myText = "Chào bạn, đây là đoạn mã chuyển đổi văn bản thành giọng nói chuẩn Microsoft.";
textToSpeech(myText, "output.mp3");
Các ý kiến mới nhất