UNPKG

coze-plugin-utils

Version:

Comprehensive utility library for Coze plugins with multimedia processing, browser automation, cloud storage integration, and AI-powered video/audio generation capabilities

120 lines 5.22 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tts = tts; const core_1 = require("../core"); const sdk = __importStar(require("microsoft-cognitiveservices-speech-sdk")); const node_path_1 = __importDefault(require("node:path")); const promises_1 = __importDefault(require("node:fs/promises")); async function tts({ voiceName, text, withFrontend = false, speed = 1.0, pitch = 0, volumn = 1.0, }) { const azureConfig = (0, core_1.getGlobalConfig)('azure'); if (!azureConfig || !azureConfig.speech) { throw new Error('Azure Speech Service is not configured'); } const { key, region } = azureConfig.speech; const speechConfig = sdk.SpeechConfig.fromSubscription(key, region); const matches = /^\w+-\w+/.exec(voiceName); const lang = matches ? matches[0] : 'en-US'; speechConfig.speechSynthesisVoiceName = voiceName; speechConfig.speechRecognitionLanguage = lang; speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Audio48Khz192KBitRateMonoMp3; const synthesizer = new sdk.SpeechSynthesizer(speechConfig); function formatProsody(speed, volumn, pitch) { const format = (v) => { let ret = `${Math.round((v - 1) * 100)}%`; if (!ret.startsWith('-')) ret = `+${ret}`; return ret; }; return `speed="${format(speed)}" pitch="${format(pitch)}" volumn="${format(volumn)}"`; } let xml = `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="${lang}"> <voice name="${voiceName}"><lang xml:lang="${lang}"> <prosody ${formatProsody(speed, volumn, pitch)}>${text}</prosody> </lang></voice></speak> `; if (lang !== 'zh-CN') { const chineseCharactersReg = /[\u4e00-\u9fa5]+/; xml = xml.replace(chineseCharactersReg, '<lang xml:lang="zh-CN">“$&”</lang>'); } let frontend; if (withFrontend) { frontend = { words: [], }; let index = 0; synthesizer.wordBoundary = (sender, event) => { let _text = event.text; _text = _text.replace(/<[^>]*?>/g, '').trim(); // 忽略可能多余的标点符号 const _index = text.slice(index).indexOf(_text); if (_index === -1) return; const textOffset = _index + index; index = textOffset + _text.length; frontend.words.push({ audioOffset: event.audioOffset / 10000, text: event.text, textOffset, duration: event.duration / 10000, boundaryType: event.boundaryType, }); }; } const audioData = await new Promise((resolve, reject) => { synthesizer.speakSsmlAsync(xml, (result) => { if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) { // console.log('synthesis finished.'); // console.log(result); resolve({ data: result.audioData, duration: result.privAudioDuration / 1e7 }); } else { // console.error(result); reject(new Error('SynthesizingAudioFailed')); } }, (_error) => { // console.error('Error:', error); reject(new Error('SynthesizingAudioFailed')); }); }); const tmpDir = await (0, core_1.createTempDir)(); const audioPath = node_path_1.default.join(tmpDir, 'speech.mp3'); await promises_1.default.writeFile(audioPath, Buffer.from(audioData.data)); return { audio: audioPath, duration: audioData.duration, frontend }; } //# sourceMappingURL=azure.speech.js.map