-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
67 lines (56 loc) · 1.69 KB
/
main.js
File metadata and controls
67 lines (56 loc) · 1.69 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
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
const http = require('http');
// Your Google Cloud Platform project ID
const projectId = <project id>;
// Creates a client
const client = new speech.SpeechClient({
projectId: projectId,
});
// The name of the audio file to transcribe
const fileName = './HowMuch.wav';
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
//sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
str = encodeURIComponent(transcription)//.replace(/\s/N/);
console.log(`Transcription: ${transcription}`);
console.log(`str ${str}`)
http.get('http://127.0.0.1:9090/translate?ph=' + str, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(data);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
})
.catch(err => {
console.error('ERROR:', err);
});