This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 273
/
app.js
executable file
·73 lines (64 loc) · 1.73 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require('express');
const app = express();
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1.js');
const { IamAuthenticator } = require('ibm-watson/auth');
const textToSpeech = new TextToSpeechV1({
version: '2018-04-05',
authenticator: new IamAuthenticator({
apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY || 'type-key-here',
}),
url: process.env.TEXT_TO_SPEECH_URL,
});
// Bootstrap application settings
require('./config/express')(app);
const getFileExtension = (acceptQuery) => {
const accept = acceptQuery || '';
switch (accept) {
case 'audio/ogg;codecs=opus':
case 'audio/ogg;codecs=vorbis':
return 'ogg';
case 'audio/wav':
return 'wav';
case 'audio/mpeg':
return 'mpeg';
case 'audio/webm':
return 'webm';
case 'audio/flac':
return 'flac';
default:
return 'mp3';
}
};
app.get('/', (req, res) => {
res.render('index');
});
/**
* Pipe the synthesize method
*/
app.get('/api/v3/synthesize', async (req, res, next) => {
try {
const { result } = await textToSpeech.synthesize(req.query);
const transcript = result;
transcript.on('response', (response) => {
if (req.query.download) {
response.headers['content-disposition'] = `attachment; filename=transcript.${getFileExtension(req.query.accept)}`;
}
});
transcript.on('error', next);
transcript.pipe(res);
} catch (error) {
res.send(error);
}
});
// Return the list of voices
app.get('/api/v2/voices', async (req, res, next) => {
try {
const { result } = textToSpeech.listVoices();
res.json(result);
} catch (error) {
next(error);
}
});
// error-handler settings
require('./config/error-handler')(app);
module.exports = app;