-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
57 lines (50 loc) · 1.47 KB
/
server.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
const express = require('express')
var cors = require('cors')
const speech = require('@google-cloud/speech');
// Creates a client
const client = new speech.SpeechClient();
const app = express()
const port = 8080
app.use(cors())
app.use(express.json()) // for parsing application/json
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/scoreVoiceRecording', async (req, res) => {
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
let gcs_uri = req.body.gcs_uri;
const audio = {
uri: gcs_uri,
};
const config = {
encoding: 'WEBM_OPUS',
audioChannelCount: 2,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// Send recording GCP bucket link to GCP NLP API
// Detects speech in the audio file
let returnObj = {
score: 0,
transcription: '',
};
try {
const [response] = await client.recognize(request);
const score = response.results["0"].alternatives["0"].confidence;
const transcription = response.results["0"].alternatives["0"].transcript;
returnObj.score = score;
returnObj.transcription = transcription;
} catch (error) {
console.log(error);
throw error;
}
// Return score to client
console.log(returnObj);
return res.send(returnObj);
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})