| 1 | import fetch from 'node-fetch'; |
| 2 | import { Router } from 'express'; |
| 3 | |
| 4 | import { readSecret, SECRET_KEYS } from './secrets.js'; |
| 5 | |
| 6 | export const router = Router(); |
| 7 | |
| 8 | router.post('/list', async (req, res) => { |
| 9 | try { |
| 10 | const key = readSecret(req.user.directories, SECRET_KEYS.AZURE_TTS); |
| 11 | |
| 12 | if (!key) { |
| 13 | console.warn('Azure TTS API Key not set'); |
| 14 | return res.sendStatus(403); |
| 15 | } |
| 16 | |
| 17 | const region = req.body.region; |
| 18 | |
| 19 | if (!region) { |
| 20 | console.warn('Azure TTS region not set'); |
| 21 | return res.sendStatus(400); |
| 22 | } |
| 23 | |
| 24 | const url = `https://${region}.tts.speech.microsoft.com/cognitiveservices/voices/list`; |
| 25 | |
| 26 | const response = await fetch(url, { |
| 27 | method: 'GET', |
| 28 | headers: { |
| 29 | 'Ocp-Apim-Subscription-Key': key, |
| 30 | }, |
| 31 | }); |
| 32 | |
| 33 | if (!response.ok) { |
| 34 | console.warn('Azure Request failed', response.status, response.statusText); |
| 35 | return res.sendStatus(500); |
| 36 | } |
| 37 | |
| 38 | const voices = await response.json(); |
| 39 | return res.json(voices); |
| 40 | } catch (error) { |
| 41 | console.error('Azure Request failed', error); |
| 42 | return res.sendStatus(500); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | router.post('/generate', async (req, res) => { |
| 47 | try { |
| 48 | const key = readSecret(req.user.directories, SECRET_KEYS.AZURE_TTS); |
| 49 | |
| 50 | if (!key) { |
| 51 | console.warn('Azure TTS API Key not set'); |
| 52 | return res.sendStatus(403); |
| 53 | } |
| 54 | |
| 55 | const { text, voice, region } = req.body; |
| 56 | if (!text || !voice || !region) { |
| 57 | console.warn('Missing required parameters'); |
| 58 | return res.sendStatus(400); |
| 59 | } |
| 60 | |
| 61 | const url = `https://${region}.tts.speech.microsoft.com/cognitiveservices/v1`; |
| 62 | const lang = String(voice).split('-').slice(0, 2).join('-'); |
| 63 | const escapedText = String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); |
| 64 | const ssml = `<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='${lang}'><voice xml:lang='${lang}' name='${voice}'>${escapedText}</voice></speak>`; |
| 65 | |
| 66 | const response = await fetch(url, { |
| 67 | method: 'POST', |
| 68 | headers: { |
| 69 | 'Ocp-Apim-Subscription-Key': key, |
| 70 | 'Content-Type': 'application/ssml+xml', |
| 71 | 'X-Microsoft-OutputFormat': 'webm-24khz-16bit-mono-opus', |
| 72 | }, |
| 73 | body: ssml, |
| 74 | }); |
| 75 | |
| 76 | if (!response.ok) { |
| 77 | console.warn('Azure Request failed', response.status, response.statusText); |
| 78 | return res.sendStatus(500); |
| 79 | } |
| 80 | |
| 81 | const audio = Buffer.from(await response.arrayBuffer()); |
| 82 | res.set('Content-Type', 'audio/ogg'); |
| 83 | return res.send(audio); |
| 84 | } catch (error) { |
| 85 | console.error('Azure Request failed', error); |
| 86 | return res.sendStatus(500); |
| 87 | } |
| 88 | }); |