Showing
1 changed file
with
52 additions
and
0 deletions
| 1 | +const recorder = require('node-record-lpcm16'); | ||
| 2 | + | ||
| 3 | +// Imports the Google Cloud client library | ||
| 4 | +const speech = require('@google-cloud/speech'); | ||
| 5 | + | ||
| 6 | +// Creates a client | ||
| 7 | +const client = new speech.SpeechClient(); | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * TODO(developer): Uncomment the following lines before running the sample. | ||
| 11 | + */ | ||
| 12 | +const encoding = 'LINEAR16'; | ||
| 13 | +const sampleRateHertz = 16000; | ||
| 14 | +const languageCode = 'ko-KR'; | ||
| 15 | + | ||
| 16 | +const request = { | ||
| 17 | + config: { | ||
| 18 | + encoding: encoding, | ||
| 19 | + sampleRateHertz: sampleRateHertz, | ||
| 20 | + languageCode: languageCode | ||
| 21 | + }, | ||
| 22 | + interimResults: false, // If you want interim results, set this to true | ||
| 23 | +}; | ||
| 24 | + | ||
| 25 | +// Create a recognize stream | ||
| 26 | +const recognizeStream = client | ||
| 27 | + .streamingRecognize(request) | ||
| 28 | + .on('error', console.error) | ||
| 29 | + .on('data', data => | ||
| 30 | + process.stdout.write( | ||
| 31 | + data.results[0] && data.results[0].alternatives[0] | ||
| 32 | + ? `Transcription: ${data.results[0].alternatives[0].transcript}\n` | ||
| 33 | + : '\n\nReached transcription time limit, press Ctrl+C\n' | ||
| 34 | + ) | ||
| 35 | + ); | ||
| 36 | + | ||
| 37 | +// Start recording and send the microphone input to the Speech API. | ||
| 38 | +// Ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies | ||
| 39 | +recorder | ||
| 40 | + .record({ | ||
| 41 | + sampleRateHertz: sampleRateHertz, | ||
| 42 | + threshold: 0, | ||
| 43 | + // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options | ||
| 44 | + verbose: false, | ||
| 45 | + recordProgram: 'sox', // Try also "arecord" or "sox" | ||
| 46 | + silence: '1.0', | ||
| 47 | + }) | ||
| 48 | + .stream() | ||
| 49 | + .on('error', console.error) | ||
| 50 | + .pipe(recognizeStream); | ||
| 51 | + | ||
| 52 | +console.log('Listening, press Ctrl+C to stop.'); | ... | ... |
-
Please register or login to post a comment