-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapiHelpers.js
More file actions
145 lines (118 loc) · 3.43 KB
/
Copy pathapiHelpers.js
File metadata and controls
145 lines (118 loc) · 3.43 KB
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
const axios = require('axios');
const fs = require('fs');
const util = require('util');
const textToSpeech = require('@google-cloud/text-to-speech');
const AWS = require('aws-sdk');
const path = require('path');
const client = new textToSpeech.TextToSpeechClient();
const speech = require('@google-cloud/speech');
const { getLanguageById } = require('./database/models.js')
const { Language, User } = require('./database/config.js')
//Configuring AWS environment
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
var S3 = new AWS.S3();
/**
*
* @param {string} word - word to be translated
* @param {string} from - language code for the language the word is in
* @param {string} to - language code for the langage to translate the word to
* @returns tranlated text
*/
const googleTranslate = (word, from, to) => {
const translatePromise = new Promise((res, rej) => {
axios.get(`https://www.googleapis.com/language/translate/v2?key=${process.env.GOOGLE_TRANS_API}&source=${from}&q=${word}&target=${to}`)
.then((result) => {
const translation = result.data.data.translations[0].translatedText
res(translation)
})
.catch((err) => {
rej(err)
})
})
return Promise.resolve(translatePromise);
};
const googleTextToSpeech = (word, languageCode = 'en') => {
return Promise.resolve(new Promise(async (res, rej) => {
await util.promisify(fs.writeFile)(`${word}.mp3`, (await client.synthesizeSpeech({
input: { text: word },
voice: { languageCode, ssmlGender: 'NEUTRAL' },
audioConfig: { audioEncoding: 'MP3' },
}))[0].audioContent, 'binary');
S3.upload({
Bucket: 'vocapp-bucket',
Body: fs.createReadStream(`./${word}.mp3`),
Key: "words/" + path.basename(`./${word}.mp3`),
ACL: 'public-read'
}, function (err, data) {
if (err) {
console.log("Error", err);
rej(err);
}
if (data) {
fs.exists(`${word}.mp3`, (exists) => {
if(exists) {
fs.unlinkSync(`${word}.mp3`);
}
})
console.log("Uploaded in:", data.Location);
res(data.Location);
}
})
}))
}
const googleSpeechToText = async (base64, currentLanguageId, word, userId) => {
const client = new speech.SpeechClient();
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
let language = await Language.findOne({
where: {
id: currentLanguageId
}
})
if(!language.transSTT) {
return null;
};
const audio = {
content: base64,
};
const config = {
encoding: 'AMR_WB',
sampleRateHertz: 16000,
languageCode: language.lang_code,
};
const request = {
audio: audio,
config: config,
};
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
if (transcription.includes(word)){
let points = await User.findOne({
where:{
id: userId
}
})
points = points.points + 1;
User.update({
points: points
}, {
where: {
id: userId
},
})
return true
}
else{
return false
}
// Detects speech in the audio file
}
module.exports = {
googleTranslate,
googleTextToSpeech,
googleSpeechToText,
}