( msg: Api.Message, text: string, p0: string, replyMsg?: Api.Message | null)
| 1141 | } |
| 1142 | |
| 1143 | async function processAudioGeneration( |
| 1144 | msg: Api.Message, text: string, p0: string, |
| 1145 | replyMsg?: Api.Message | null): Promise<void> { |
| 1146 | const client = await getGeminiClient(); |
| 1147 | const modelName = ConfigManager.get(CONFIG_KEYS.GEMINI_TTS_MODEL, DEFAULT_CONFIG[CONFIG_KEYS.GEMINI_TTS_MODEL]); |
| 1148 | const voiceName = ConfigManager.get(CONFIG_KEYS.GEMINI_TTS_VOICE, DEFAULT_CONFIG[CONFIG_KEYS.GEMINI_TTS_VOICE]); |
| 1149 | |
| 1150 | const response = await client.generateTTS({ |
| 1151 | model: modelName, |
| 1152 | contents: [{ parts: [{ text }] }], |
| 1153 | voiceName |
| 1154 | }); |
| 1155 | |
| 1156 | if (!response.audioData?.length) { |
| 1157 | throw new Error('没有收到音频数据'); |
| 1158 | } |
| 1159 | |
| 1160 | const combinedAudio = Buffer.concat(response.audioData); |
| 1161 | if (combinedAudio.length === 0) { |
| 1162 | throw new Error('合并后的音频数据为空'); |
| 1163 | } |
| 1164 | |
| 1165 | if (replyMsg) { |
| 1166 | let processedAudio: any = combinedAudio; |
| 1167 | |
| 1168 | if (response.audioMimeType && response.audioMimeType.includes('L16') && response.audioMimeType.includes('pcm')) { |
| 1169 | processedAudio = Utils.convertToWav(combinedAudio, response.audioMimeType) as any; |
| 1170 | } |
| 1171 | |
| 1172 | const audioFile = Object.assign(processedAudio as any, { |
| 1173 | name: 'gemini.ogg' |
| 1174 | }); |
| 1175 | |
| 1176 | await msg.client?.sendFile(msg.peerId, { |
| 1177 | file: audioFile, |
| 1178 | caption: `<b>文本:</b> ${Utils.escapeHtml(text)}\n\n<i>Powered by Gemini TTS (${voiceName})</i>`, |
| 1179 | parseMode: "html", |
| 1180 | replyTo: replyMsg.id, |
| 1181 | attributes: [new Api.DocumentAttributeAudio({ |
| 1182 | duration: 0, |
| 1183 | voice: true |
| 1184 | })] |
| 1185 | }); |
| 1186 | |
| 1187 | try { |
| 1188 | await msg.delete(); |
| 1189 | } catch {} |
| 1190 | } else { |
| 1191 | let processedAudio: any = combinedAudio; |
| 1192 | |
| 1193 | if (response.audioMimeType && response.audioMimeType.includes('L16') && response.audioMimeType.includes('pcm')) { |
| 1194 | processedAudio = Utils.convertToWav(combinedAudio, response.audioMimeType) as any; |
| 1195 | } |
| 1196 | |
| 1197 | const audioFile = Object.assign(processedAudio as any, { |
| 1198 | name: 'gemini.ogg' |
| 1199 | }); |
| 1200 |
no test coverage detected