/
index.js
204 lines (177 loc) · 6.41 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// Check for valid SSML
//
const convert = require('xml-js');
const https = require('https');
const mm = require('music-metadata');
const ssmlCheckCore = require('ssml-check-core');
function getAudioFiles(element) {
let files = [];
if ((element.name === 'audio') && (element.attributes.src)) {
files.push(element.attributes.src);
}
if (element.elements) {
element.elements.forEach((item) => {
files = files.concat(getAudioFiles(item));
});
}
return files;
}
function validateAudio(src, platform) {
const errors = [];
// It can be one of the built-in Amazon sounds (from the soundbank)
// We'll check if it has the appropriate structure
// soundbank://soundlibrary/category/sound
if (platform === 'amazon') {
const prefix = 'soundbank://soundlibrary/';
if (src.indexOf(prefix) === 0) {
// Parse out the category and sound
const path = src.slice(prefix.length - src.length).split('/');
// Does it have a valid category?
if ((path.length === 2) && (['ambience', 'animals', 'battle',
'cartoon', 'foley', 'gameshow', 'home', 'human', 'impact',
'magic', 'musical', 'nature', 'office', 'scifi', 'transportation'].indexOf(path[0]) !== -1)) {
// We'll say this is good
return Promise.resolve(errors);
} else {
errors.push({type: 'audio', value: src, detail: `Invalid soundbank category ${path[0]}`});
return Promise.resolve(errors);
}
}
}
// Alexa - Must be MP3 at HTTPS endpoint
// Google - Must be MP3 or OGG at HTTPS endpoint
if (!src.match(/^https(.)+\.mp3/gi)) {
// It can be OGG if Google platform
if ((platform !== 'google') || !src.match(/^https(.)+\.ogg/gi)) {
errors.push({type: 'audio', value: src, detail: 'Not correct audio format on HTTPS'});
return Promise.resolve(errors);
}
}
// Make sure we can access the audio file
// The sample rate must be 22050Hz, 24000Hz, or 16000Hz (24000Hz on google)
// and the bit rate must be 48kbps on amazon or 24-96kpbs on google
// audio file length cannot be more than 240 seconds (120 seconds on google)
return new Promise((resolve) => {
const request = https.get(src, (resp) => {
const bufs = [];
resp.on('data', (d) => { bufs.push(d); });
resp.on('end', () => {
const buf = Buffer.concat(bufs);
return mm.parseBuffer(buf, 'audio/mpeg')
.then((metadata) => {
metadata.format = metadata.format || {};
if ([22050, 24000, 16000].indexOf(parseInt(metadata.format.sampleRate)) === -1) {
errors.push({type: 'audio', value: src, detail: `Invalid sample rate ${metadata.format.sampleRate} Hz`})
}
if (((platform !== 'google') || (metadata.format.bitrate < 24000) || (metadata.format.bitrate > 96000))
&& (metadata.format.bitrate != 48000)) {
errors.push({type: 'audio', value: src, detail: `Invalid bit rate ${metadata.format.bitrate}`})
}
if (((platform !== 'amazon') || (metadata.format.duration > 240000))
&& (metadata.format.duration > 120000)) {
errors.push({type: 'audio', value: src, detail: `Invalid duration ${metadata.format.duration} ms`})
}
resolve(errors);
});
});
});
request.on('error', () => {
errors.push({type: 'audio', value: src, detail: 'Can\'t access file'});
resolve(errors);
});
});
}
function removeAudioFiles(parent, index, badAudio, element) {
let removedTag;
if ((element.name === 'audio') && (element.attributes.src)
&& (badAudio.indexOf(element.attributes.src) > -1)) {
// Remove this please
parent.elements.splice(index, 1);
removedTag = true;
}
if (element.elements) {
let i;
let removed;
for (i = 0; i < element.elements.length; i++) {
removed = removeAudioFiles(element, i, badAudio, element.elements[i]);
if (removed) {
// Decrement i since an item was removed
i--;
}
}
}
return removedTag;
}
function getAudioErrors(ssml, platform) {
const promises = [];
let errors = [];
let result;
// The input is either a string or a JSON object - convert it if
// it is a string into a JSON object
try {
result = JSON.parse(convert.xml2json(ssml, {compact: false}));
const audio = getAudioFiles(result.elements[0]);
audio.forEach((file) => {
promises.push(validateAudio(file, platform));
});
return Promise.all(promises).then((audioErrors) => {
audioErrors.forEach((audioError) => {
errors = errors.concat(audioError);
});
return errors;
}).then((errors) => {
removeAudioFiles(result, 0, errors.map((x) => x.value), result.elements[0]);
return {json: result, errors: errors};
});
} catch (err) {
// Just return the errors we already have
if (result) {
removeAudioFiles(result, 0, errors.map((x) => x.value), result.elements[0]);
}
return Promise.resolve({json: result, errors: errors});
}
}
module.exports = {
check: function(ssml, options) {
const userOptions = options || {};
userOptions.platform = userOptions.platform || 'all';
let errors;
return ssmlCheckCore.check(ssml, options)
.then((coreErrors) => {
errors = coreErrors || [];
// If they asked to validate audio files, do that now
if (userOptions.validateAudioFiles) {
return getAudioErrors(ssml, userOptions.platform);
} else {
return {errors: []};
}
}).then((audioErrors) => {
errors = errors.concat(audioErrors.errors);
return (errors.length ? errors : undefined);
});
},
verifyAndFix: function(ssml, options) {
const userOptions = options || {};
userOptions.platform = userOptions.platform || 'all';
let retVal;
return ssmlCheckCore.verifyAndFix(ssml, options)
.then((result) => {
retVal = result;
if (userOptions.validateAudioFiles) {
const fixedSSML = (result && result.fixedSSML) ? result.fixedSSML : ssml;
return getAudioErrors(fixedSSML, userOptions.platform);
} else {
return {errors: []};
}
}).then((audioErrors) => {
if (audioErrors.errors.length) {
// OK, there were additional errors to report
retVal.errors = retVal.errors || [];
retVal.errors = retVal.errors.concat(audioErrors.errors);
retVal.fixedSSML = convert.json2xml(audioErrors.json, {compact: false});
}
return retVal;
});
},
};