천현우

changed looks like server

1 -const recorder = require('node-record-lpcm16'); 1 +const express = require('express');
2 - 2 +const multer = require('multer');
3 -// Imports the Google Cloud client library 3 +const fs = require('fs');
4 -const speech = require('@google-cloud/speech'); 4 +const upload = multer();
5 - 5 +const app = express();
6 - 6 +const port = 5501;
7 -// Creates a client 7 +app.use(express.static('./'));
8 -const client = new speech.SpeechClient(); 8 +async function testGoogleTextToSpeech(audioBuffer) {
9 - 9 + const speech = require('@google-cloud/speech');
10 -/** 10 + const client = new speech.SpeechClient();
11 - * TODO(developer): Uncomment the following lines before running the sample. 11 + const audio = {
12 - */ 12 + content: audioBuffer.toString('base64'),
13 -const encoding = 'LINEAR16'; 13 + };
14 -const sampleRateHertz = 16000; 14 + const encoding = 'LINEAR16';
15 -const languageCode = 'ko-KR'; 15 + const sampleRateHertz = 48000;
16 -const speechContexts = [{ 16 + const languageCode = 'ko-KR';
17 + const speechContexts = [{
17 phrases: [ 18 phrases: [
18 - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_ALPHA_SEQUENCE', '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_DIGIT_SEQUENCE', 19 + '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
19 - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_ALPHA_SEQUENCE 보다 크다', 20 + '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 크다',
20 - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_ALPHA_SEQUENCE 보다 작다', 21 + '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 작다',
21 - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_DIGIT_SEQUENCE 보다 크다', 22 + 'for OOV_CLASS_ALPHA_SEQUENCE in range $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
22 - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_DIGIT_SEQUENCE 보다 작다',
23 - 'for OOV_CLASS_ALPHA_SEQUENCE in range $OOV_CLASS_DIGIT_SEQUENCE',
24 'if', 23 'if',
25 '이프', 24 '이프',
26 'else if', 25 'else if',
...@@ -38,9 +37,10 @@ const speechContexts = [{ ...@@ -38,9 +37,10 @@ const speechContexts = [{
38 '조건문 선언' 37 '조건문 선언'
39 ], 38 ],
40 boost: 20 39 boost: 20
41 -}] 40 + }]
42 41
43 -const request = { 42 + const request = {
43 + audio: audio,
44 config: { 44 config: {
45 encoding: encoding, 45 encoding: encoding,
46 sampleRateHertz: sampleRateHertz, 46 sampleRateHertz: sampleRateHertz,
...@@ -49,33 +49,19 @@ const request = { ...@@ -49,33 +49,19 @@ const request = {
49 speechContexts: speechContexts 49 speechContexts: speechContexts
50 }, 50 },
51 interimResults: false, // If you want interim results, set this to true 51 interimResults: false, // If you want interim results, set this to true
52 -}; 52 + };
53 - 53 + const [response] = await client.recognize(request);
54 -// Create a recognize stream 54 + const transcription = response.results
55 -const recognizeStream = client 55 + .map(result => result.alternatives[0].transcript)
56 - .streamingRecognize(request) 56 + .join('\n');
57 - .on('error', console.error) 57 + return transcription;
58 - .on('data', data => 58 +}
59 - process.stdout.write( 59 +app.post('/upload_sound', upload.any(), async (req, res) => {
60 - data.results[0] && data.results[0].alternatives[0] 60 + console.log("Getting text transcription..");
61 - ? `Transcription: ${data.results[0].alternatives[0].transcript}\n` 61 + let transcription = await testGoogleTextToSpeech(req.files[0].buffer);
62 - : '\n\nReached transcription time limit, press Ctrl+C\n' 62 + console.log("Text transcription: " + transcription);
63 - ) 63 + res.status(200).send(transcription);
64 - ); 64 +});
65 - 65 +app.listen(port, () => {
66 -// Start recording and send the microphone input to the Speech API. 66 + console.log(`Express server listening on port: ${port}...`);
67 -// Ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies 67 +});
68 -recorder
69 - .record({
70 - sampleRateHertz: sampleRateHertz,
71 - threshold: 0,
72 - // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
73 - verbose: false,
74 - recordProgram: 'sox', // Try also "arecord" or "sox"
75 - silence: '1.0',
76 - })
77 - .stream()
78 - .on('error', console.error)
79 - .pipe(recognizeStream);
80 -
81 -console.log('Listening, press Ctrl+C to stop.');
...\ No newline at end of file ...\ No newline at end of file
......