|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// [START app] |
| 17 | +// [START import_libraries] |
| 18 | +var google = require('googleapis'); |
| 19 | +var async = require('async'); |
| 20 | +var fs = require('fs'); |
| 21 | +// [END import_libraries] |
| 22 | + |
| 23 | +// Url to discovery doc file |
| 24 | +// [START discovery_doc] |
| 25 | +var url = 'https://speech.googleapis.com/$discovery/rest'; |
| 26 | +// [END discovery_doc] |
| 27 | + |
| 28 | +// [START authenticating] |
| 29 | +function getSpeechService(callback) { |
| 30 | + // Acquire credentials |
| 31 | + google.auth.getApplicationDefault(function (err, authClient) { |
| 32 | + if (err) { |
| 33 | + return callback(err); |
| 34 | + } |
| 35 | + |
| 36 | + // The createScopedRequired method returns true when running on GAE or a |
| 37 | + // local developer machine. In that case, the desired scopes must be passed |
| 38 | + // in manually. When the code is running in GCE or a Managed VM, the scopes |
| 39 | + // are pulled from the GCE metadata server. |
| 40 | + // See https://cloud.google.com/compute/docs/authentication for more |
| 41 | + // information. |
| 42 | + if (authClient.createScopedRequired && authClient.createScopedRequired()) { |
| 43 | + // Scopes can be specified either as an array or as a single, |
| 44 | + // space-delimited string. |
| 45 | + authClient = authClient.createScoped([ |
| 46 | + 'https://www.googleapis.com/auth/cloud-platform' |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + // Load the speach service using acquired credentials |
| 51 | + console.log('Loading speech service...'); |
| 52 | + google.discoverAPI({ |
| 53 | + url: url, |
| 54 | + version: 'v1', |
| 55 | + auth: authClient |
| 56 | + }, function (err, speechService) { |
| 57 | + if (err) { |
| 58 | + return callback(err); |
| 59 | + } |
| 60 | + callback(null, speechService, authClient); |
| 61 | + }); |
| 62 | + }); |
| 63 | +} |
| 64 | +// [END authenticating] |
| 65 | + |
| 66 | +// [START construct_request] |
| 67 | +function prepareRequest(inputFile, callback) { |
| 68 | + fs.readFile(inputFile, function (err, audioFile) { |
| 69 | + if (err) { |
| 70 | + return callback(err); |
| 71 | + } |
| 72 | + console.log('Got audio file!'); |
| 73 | + var encoded = new Buffer(audioFile).toString('base64'); |
| 74 | + var payload = { |
| 75 | + initialRequest: { |
| 76 | + encoding: 'LINEAR16', |
| 77 | + sampleRate: 16000 |
| 78 | + }, |
| 79 | + audioRequest: { |
| 80 | + content: encoded |
| 81 | + } |
| 82 | + }; |
| 83 | + return callback(null, payload); |
| 84 | + }); |
| 85 | +} |
| 86 | +// [END construct_request] |
| 87 | + |
| 88 | +// [START send_request] |
| 89 | +function main(inputFile, callback) { |
| 90 | + var requestPayload; |
| 91 | + |
| 92 | + async.waterfall([ |
| 93 | + function (cb) { |
| 94 | + prepareRequest(inputFile, cb); |
| 95 | + }, |
| 96 | + function (payload, cb) { |
| 97 | + requestPayload = payload; |
| 98 | + getSpeechService(cb); |
| 99 | + }, |
| 100 | + function (speechService, authClient, cb) { |
| 101 | + console.log('Analyzing speech...'); |
| 102 | + speechService.speech.recognize({ |
| 103 | + auth: authClient, |
| 104 | + resource: requestPayload |
| 105 | + }, cb); |
| 106 | + } |
| 107 | + ], function (err, result) { |
| 108 | + if (err) { |
| 109 | + return callback(err); |
| 110 | + } |
| 111 | + console.log('result:', JSON.stringify(result, null, 2)); |
| 112 | + callback(null, result); |
| 113 | + }); |
| 114 | +} |
| 115 | +// [END send_request] |
| 116 | + |
| 117 | +// [START run_application] |
| 118 | +if (module === require.main) { |
| 119 | + if (process.argv.length < 3) { |
| 120 | + console.log('Usage: node recognize <inputFile>'); |
| 121 | + process.exit(); |
| 122 | + } |
| 123 | + var inputFile = process.argv[2]; |
| 124 | + main(inputFile, console.log); |
| 125 | +} |
| 126 | +// [END run_application] |
| 127 | +// [END app] |
| 128 | + |
| 129 | +exports.main = main; |
0 commit comments