|
| 1 | +/** |
| 2 | + * Copyright 2017, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * This application demonstrates how to perform basic recognize operations with |
| 18 | + * with the Google Cloud Speech API. |
| 19 | + * |
| 20 | + * For more information, see the README.md under /speech and the documentation |
| 21 | + * at https://cloud.google.com/speech/docs. |
| 22 | + */ |
| 23 | + |
| 24 | +'use strict'; |
| 25 | + |
| 26 | +function syncRecognizeModelSelection( |
| 27 | + filename, |
| 28 | + model, |
| 29 | + encoding, |
| 30 | + sampleRateHertz, |
| 31 | + languageCode |
| 32 | +) { |
| 33 | + // [START speech_transcribe_model_selection] |
| 34 | + // Imports the Google Cloud client library |
| 35 | + const fs = require('fs'); |
| 36 | + const speech = require('@google-cloud/speech').v1p1beta1; |
| 37 | + |
| 38 | + // Creates a client |
| 39 | + const client = new speech.SpeechClient(); |
| 40 | + |
| 41 | + /** |
| 42 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 43 | + */ |
| 44 | + // const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; |
| 45 | + // const model = 'Model to use, e.g. phone_call, video, default'; |
| 46 | + // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; |
| 47 | + // const sampleRateHertz = 16000; |
| 48 | + // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 49 | + |
| 50 | + const config = { |
| 51 | + encoding: encoding, |
| 52 | + sampleRateHertz: sampleRateHertz, |
| 53 | + languageCode: languageCode, |
| 54 | + model: model, |
| 55 | + }; |
| 56 | + const audio = { |
| 57 | + content: fs.readFileSync(filename).toString('base64'), |
| 58 | + }; |
| 59 | + |
| 60 | + const request = { |
| 61 | + config: config, |
| 62 | + audio: audio, |
| 63 | + }; |
| 64 | + |
| 65 | + // Detects speech in the audio file |
| 66 | + client |
| 67 | + .recognize(request) |
| 68 | + .then(data => { |
| 69 | + const response = data[0]; |
| 70 | + const transcription = response.results |
| 71 | + .map(result => result.alternatives[0].transcript) |
| 72 | + .join('\n'); |
| 73 | + console.log(`Transcription: `, transcription); |
| 74 | + }) |
| 75 | + .catch(err => { |
| 76 | + console.error('ERROR:', err); |
| 77 | + }); |
| 78 | + // [END speech_transcribe_model_selection] |
| 79 | +} |
| 80 | + |
| 81 | +function syncRecognizeModelSelectionGCS( |
| 82 | + gcsUri, |
| 83 | + model, |
| 84 | + encoding, |
| 85 | + sampleRateHertz, |
| 86 | + languageCode |
| 87 | +) { |
| 88 | + // [START speech_transcribe_model_selection_gcs] |
| 89 | + // Imports the Google Cloud client library |
| 90 | + const speech = require('@google-cloud/speech').v1p1beta1; |
| 91 | + |
| 92 | + // Creates a client |
| 93 | + const client = new speech.SpeechClient(); |
| 94 | + |
| 95 | + /** |
| 96 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 97 | + */ |
| 98 | + // const gcsUri = 'gs://my-bucket/audio.raw'; |
| 99 | + // const model = 'Model to use, e.g. phone_call, video, default'; |
| 100 | + // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; |
| 101 | + // const sampleRateHertz = 16000; |
| 102 | + // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 103 | + |
| 104 | + const config = { |
| 105 | + encoding: encoding, |
| 106 | + sampleRateHertz: sampleRateHertz, |
| 107 | + languageCode: languageCode, |
| 108 | + model: model, |
| 109 | + }; |
| 110 | + const audio = { |
| 111 | + uri: gcsUri, |
| 112 | + }; |
| 113 | + |
| 114 | + const request = { |
| 115 | + config: config, |
| 116 | + audio: audio, |
| 117 | + }; |
| 118 | + |
| 119 | + // Detects speech in the audio file |
| 120 | + client |
| 121 | + .recognize(request) |
| 122 | + .then(data => { |
| 123 | + const response = data[0]; |
| 124 | + const transcription = response.results |
| 125 | + .map(result => result.alternatives[0].transcript) |
| 126 | + .join('\n'); |
| 127 | + console.log(`Transcription: `, transcription); |
| 128 | + }) |
| 129 | + .catch(err => { |
| 130 | + console.error('ERROR:', err); |
| 131 | + }); |
| 132 | + // [END speech_transcribe_model_selection_gcs] |
| 133 | +} |
| 134 | + |
| 135 | +require(`yargs`) |
| 136 | + .demand(1) |
| 137 | + .command( |
| 138 | + `sync-model <filename> <model>`, |
| 139 | + `Detects speech in a local audio file using provided model.`, |
| 140 | + {}, |
| 141 | + opts => |
| 142 | + syncRecognizeModelSelection( |
| 143 | + opts.filename, |
| 144 | + opts.model, |
| 145 | + opts.encoding, |
| 146 | + opts.sampleRateHertz, |
| 147 | + opts.languageCode |
| 148 | + ) |
| 149 | + ) |
| 150 | + .command( |
| 151 | + `sync-model-gcs <gcsUri> <model>`, |
| 152 | + `Detects speech in an audio file located in a Google Cloud Storage bucket using provided model.`, |
| 153 | + {}, |
| 154 | + opts => |
| 155 | + syncRecognizeModelSelectionGCS( |
| 156 | + opts.gcsUri, |
| 157 | + opts.model, |
| 158 | + opts.encoding, |
| 159 | + opts.sampleRateHertz, |
| 160 | + opts.languageCode |
| 161 | + ) |
| 162 | + ) |
| 163 | + .options({ |
| 164 | + encoding: { |
| 165 | + alias: 'e', |
| 166 | + default: 'LINEAR16', |
| 167 | + global: true, |
| 168 | + requiresArg: true, |
| 169 | + type: 'string', |
| 170 | + }, |
| 171 | + sampleRateHertz: { |
| 172 | + alias: 'r', |
| 173 | + default: 16000, |
| 174 | + global: true, |
| 175 | + requiresArg: true, |
| 176 | + type: 'number', |
| 177 | + }, |
| 178 | + languageCode: { |
| 179 | + alias: 'l', |
| 180 | + default: 'en-US', |
| 181 | + global: true, |
| 182 | + requiresArg: true, |
| 183 | + type: 'string', |
| 184 | + }, |
| 185 | + }) |
| 186 | + .example( |
| 187 | + `node $0 sync-model ./resources/Google_Gnome.wav video -e LINEAR16 -r 16000` |
| 188 | + ) |
| 189 | + .example( |
| 190 | + `node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000` |
| 191 | + ) |
| 192 | + .wrap(120) |
| 193 | + .recommendCommands() |
| 194 | + .epilogue(`For more information, see https://cloud.google.com/speech/docs`) |
| 195 | + .help() |
| 196 | + .strict().argv; |
0 commit comments