Showing
1 changed file
with
66 additions
and
80 deletions
| 1 | -const recorder = require('node-record-lpcm16'); | 1 | +const express = require('express'); |
| 2 | +const multer = require('multer'); | ||
| 3 | +const fs = require('fs'); | ||
| 4 | +const upload = multer(); | ||
| 5 | +const app = express(); | ||
| 6 | +const port = 5501; | ||
| 7 | +app.use(express.static('./')); | ||
| 8 | +async function testGoogleTextToSpeech(audioBuffer) { | ||
| 9 | + const speech = require('@google-cloud/speech'); | ||
| 10 | + const client = new speech.SpeechClient(); | ||
| 11 | + const audio = { | ||
| 12 | + content: audioBuffer.toString('base64'), | ||
| 13 | + }; | ||
| 14 | + const encoding = 'LINEAR16'; | ||
| 15 | + const sampleRateHertz = 48000; | ||
| 16 | + const languageCode = 'ko-KR'; | ||
| 17 | + const speechContexts = [{ | ||
| 18 | + phrases: [ | ||
| 19 | + '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE', | ||
| 20 | + '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 크다', | ||
| 21 | + '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 작다', | ||
| 22 | + 'for OOV_CLASS_ALPHA_SEQUENCE in range $OOV_CLASS_ALPHANUMERIC_SEQUENCE', | ||
| 23 | + 'if', | ||
| 24 | + '이프', | ||
| 25 | + 'else if', | ||
| 26 | + '엘스 이프', | ||
| 27 | + 'else', | ||
| 28 | + '엘스', | ||
| 29 | + 'while', | ||
| 30 | + '와일', | ||
| 31 | + '함수', | ||
| 32 | + '파라미터', | ||
| 33 | + 'parameter', | ||
| 34 | + '변수 선언', | ||
| 35 | + '함수 선언', | ||
| 36 | + '반복문 선언', | ||
| 37 | + '조건문 선언' | ||
| 38 | + ], | ||
| 39 | + boost: 20 | ||
| 40 | + }] | ||
| 2 | 41 | ||
| 3 | -// Imports the Google Cloud client library | 42 | + const request = { |
| 4 | -const speech = require('@google-cloud/speech'); | 43 | + audio: audio, |
| 5 | - | 44 | + config: { |
| 6 | - | 45 | + encoding: encoding, |
| 7 | -// Creates a client | 46 | + sampleRateHertz: sampleRateHertz, |
| 8 | -const client = new speech.SpeechClient(); | 47 | + languageCode: languageCode, |
| 9 | - | 48 | + alternativeLanguageCodes: ['en-US'], |
| 10 | -/** | 49 | + speechContexts: speechContexts |
| 11 | - * TODO(developer): Uncomment the following lines before running the sample. | 50 | + }, |
| 12 | - */ | 51 | + interimResults: false, // If you want interim results, set this to true |
| 13 | -const encoding = 'LINEAR16'; | 52 | + }; |
| 14 | -const sampleRateHertz = 16000; | 53 | + const [response] = await client.recognize(request); |
| 15 | -const languageCode = 'ko-KR'; | 54 | + const transcription = response.results |
| 16 | -const speechContexts = [{ | 55 | + .map(result => result.alternatives[0].transcript) |
| 17 | - phrases: [ | 56 | + .join('\n'); |
| 18 | - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_ALPHA_SEQUENCE', '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_DIGIT_SEQUENCE', | 57 | + return transcription; |
| 19 | - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_ALPHA_SEQUENCE 보다 크다', | 58 | +} |
| 20 | - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_ALPHA_SEQUENCE 보다 작다', | 59 | +app.post('/upload_sound', upload.any(), async (req, res) => { |
| 21 | - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_DIGIT_SEQUENCE 보다 크다', | 60 | + console.log("Getting text transcription.."); |
| 22 | - '$OOV_CLASS_ALPHA_SEQUENCE 는 $OOV_CLASS_DIGIT_SEQUENCE 보다 작다', | 61 | + let transcription = await testGoogleTextToSpeech(req.files[0].buffer); |
| 23 | - 'for OOV_CLASS_ALPHA_SEQUENCE in range $OOV_CLASS_DIGIT_SEQUENCE', | 62 | + console.log("Text transcription: " + transcription); |
| 24 | - 'if', | 63 | + res.status(200).send(transcription); |
| 25 | - '이프', | 64 | +}); |
| 26 | - 'else if', | 65 | +app.listen(port, () => { |
| 27 | - '엘스 이프', | 66 | + console.log(`Express server listening on port: ${port}...`); |
| 28 | - 'else', | 67 | +}); |
| 29 | - '엘스', | ||
| 30 | - 'while', | ||
| 31 | - '와일', | ||
| 32 | - '함수', | ||
| 33 | - '파라미터', | ||
| 34 | - 'parameter', | ||
| 35 | - '변수 선언', | ||
| 36 | - '함수 선언', | ||
| 37 | - '반복문 선언', | ||
| 38 | - '조건문 선언' | ||
| 39 | - ], | ||
| 40 | - boost: 20 | ||
| 41 | -}] | ||
| 42 | - | ||
| 43 | -const request = { | ||
| 44 | - config: { | ||
| 45 | - encoding: encoding, | ||
| 46 | - sampleRateHertz: sampleRateHertz, | ||
| 47 | - languageCode: languageCode, | ||
| 48 | - alternativeLanguageCodes: ['en-US'], | ||
| 49 | - speechContexts: speechContexts | ||
| 50 | - }, | ||
| 51 | - interimResults: false, // If you want interim results, set this to true | ||
| 52 | -}; | ||
| 53 | - | ||
| 54 | -// Create a recognize stream | ||
| 55 | -const recognizeStream = client | ||
| 56 | - .streamingRecognize(request) | ||
| 57 | - .on('error', console.error) | ||
| 58 | - .on('data', data => | ||
| 59 | - process.stdout.write( | ||
| 60 | - data.results[0] && data.results[0].alternatives[0] | ||
| 61 | - ? `Transcription: ${data.results[0].alternatives[0].transcript}\n` | ||
| 62 | - : '\n\nReached transcription time limit, press Ctrl+C\n' | ||
| 63 | - ) | ||
| 64 | - ); | ||
| 65 | - | ||
| 66 | -// Start recording and send the microphone input to the Speech API. | ||
| 67 | -// Ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies | ||
| 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 | ... | ... |
-
Please register or login to post a comment