diff --git a/.cloud-repo-tools.json b/.cloud-repo-tools.json index cbb3042d..4a3b56f7 100644 --- a/.cloud-repo-tools.json +++ b/.cloud-repo-tools.json @@ -11,6 +11,13 @@ "file": "recognize.js", "docs_link": "https://cloud.google.com/speech/docs", "usage": "node recognize.js --help" + }, + { + "id": "recognize.v1p1beta1", + "name": "Speech Recognition v1p1beta1", + "file": "recognize.v1p1beta1.js", + "docs_link": "https://cloud.google.com/speech/docs", + "usage": "node recognize.v1p1beta1.js --help" } ] } diff --git a/README.md b/README.md index 85236594..8aed0bfa 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ has instructions for running the samples. | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | | Speech Recognition | [source code](https://github.com/googleapis/nodejs-speech/blob/master/samples/recognize.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/recognize.js,samples/README.md) | +| Speech Recognition v1p1beta1 | [source code](https://github.com/googleapis/nodejs-speech/blob/master/samples/recognize.v1p1beta1.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/recognize.v1p1beta1.js,samples/README.md) | The [Speech API Node.js Client API Reference][client-docs] documentation also contains samples. diff --git a/package.json b/package.json index b0de8a95..14b7835b 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,7 @@ "eslint": "^4.10.0", "eslint-config-prettier": "^2.7.0", "eslint-plugin-node": "^6.0.0", - "eslint-plugin-prettier": "^2.3.1", + "eslint-plugin-prettier": "^2.6.0", "ink-docstrap": "^1.3.0", "intelli-espower-loader": "^1.0.1", "jsdoc": "^3.5.5", diff --git a/samples/README.md b/samples/README.md index 5b5995cf..7728ffb5 100644 --- a/samples/README.md +++ b/samples/README.md @@ -11,6 +11,7 @@ The [Cloud Speech API](https://cloud.google.com/speech/docs) enables easy integr * [Before you begin](#before-you-begin) * [Samples](#samples) * [Speech Recognition](#speech-recognition) + * [Speech Recognition v1p1beta1](#speech-recognition-v1p1beta1) ## Before you begin @@ -65,5 +66,38 @@ For more information, see https://cloud.google.com/speech/docs [recognize_0_docs]: https://cloud.google.com/speech/docs [recognize_0_code]: recognize.js +### Speech Recognition v1p1beta1 + +View the [source code][recognize.v1p1beta1_1_code]. + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/recognize.v1p1beta1.js,samples/README.md) + +__Usage:__ `node recognize.v1p1beta1.js --help` + +``` +recognize.v1p1beta1.js + +Commands: + recognize.v1p1beta1.js sync-model Detects speech in a local audio file using provided model. + recognize.v1p1beta1.js sync-model-gcs Detects speech in an audio file located in a Google Cloud + Storage bucket using provided model. + +Options: + --version Show version number [boolean] + --encoding, -e [string] [default: "LINEAR16"] + --sampleRateHertz, -r [number] [default: 16000] + --languageCode, -l [string] [default: "en-US"] + --help Show help [boolean] + +Examples: + node recognize.v1p1beta1.js sync-model ./resources/Google_Gnome.wav video -e LINEAR16 -r 16000 + node recognize.v1p1beta1.js sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000 + +For more information, see https://cloud.google.com/speech/docs +``` + +[recognize.v1p1beta1_1_docs]: https://cloud.google.com/speech/docs +[recognize.v1p1beta1_1_code]: recognize.v1p1beta1.js + [shell_img]: http://gstatic.com/cloudssh/images/open-btn.png [shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/README.md diff --git a/samples/recognize.v1p1beta1.js b/samples/recognize.v1p1beta1.js new file mode 100644 index 00000000..8c9c1b2a --- /dev/null +++ b/samples/recognize.v1p1beta1.js @@ -0,0 +1,196 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This application demonstrates how to perform basic recognize operations with + * with the Google Cloud Speech API. + * + * For more information, see the README.md under /speech and the documentation + * at https://cloud.google.com/speech/docs. + */ + +'use strict'; + +function syncRecognizeModelSelection( + filename, + model, + encoding, + sampleRateHertz, + languageCode +) { + // [START speech_transcribe_model_selection] + // Imports the Google Cloud client library + const fs = require('fs'); + const speech = require('@google-cloud/speech').v1p1beta1; + + // Creates a client + const client = new speech.SpeechClient(); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; + // const model = 'Model to use, e.g. phone_call, video, default'; + // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; + // const sampleRateHertz = 16000; + // const languageCode = 'BCP-47 language code, e.g. en-US'; + + const config = { + encoding: encoding, + sampleRateHertz: sampleRateHertz, + languageCode: languageCode, + model: model, + }; + const audio = { + content: fs.readFileSync(filename).toString('base64'), + }; + + const request = { + config: config, + audio: audio, + }; + + // Detects speech in the audio file + client + .recognize(request) + .then(data => { + const response = data[0]; + const transcription = response.results + .map(result => result.alternatives[0].transcript) + .join('\n'); + console.log(`Transcription: `, transcription); + }) + .catch(err => { + console.error('ERROR:', err); + }); + // [END speech_transcribe_model_selection] +} + +function syncRecognizeModelSelectionGCS( + gcsUri, + model, + encoding, + sampleRateHertz, + languageCode +) { + // [START speech_transcribe_model_selection_gcs] + // Imports the Google Cloud client library + const speech = require('@google-cloud/speech').v1p1beta1; + + // Creates a client + const client = new speech.SpeechClient(); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const gcsUri = 'gs://my-bucket/audio.raw'; + // const model = 'Model to use, e.g. phone_call, video, default'; + // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; + // const sampleRateHertz = 16000; + // const languageCode = 'BCP-47 language code, e.g. en-US'; + + const config = { + encoding: encoding, + sampleRateHertz: sampleRateHertz, + languageCode: languageCode, + model: model, + }; + const audio = { + uri: gcsUri, + }; + + const request = { + config: config, + audio: audio, + }; + + // Detects speech in the audio file + client + .recognize(request) + .then(data => { + const response = data[0]; + const transcription = response.results + .map(result => result.alternatives[0].transcript) + .join('\n'); + console.log(`Transcription: `, transcription); + }) + .catch(err => { + console.error('ERROR:', err); + }); + // [END speech_transcribe_model_selection_gcs] +} + +require(`yargs`) + .demand(1) + .command( + `sync-model `, + `Detects speech in a local audio file using provided model.`, + {}, + opts => + syncRecognizeModelSelection( + opts.filename, + opts.model, + opts.encoding, + opts.sampleRateHertz, + opts.languageCode + ) + ) + .command( + `sync-model-gcs `, + `Detects speech in an audio file located in a Google Cloud Storage bucket using provided model.`, + {}, + opts => + syncRecognizeModelSelectionGCS( + opts.gcsUri, + opts.model, + opts.encoding, + opts.sampleRateHertz, + opts.languageCode + ) + ) + .options({ + encoding: { + alias: 'e', + default: 'LINEAR16', + global: true, + requiresArg: true, + type: 'string', + }, + sampleRateHertz: { + alias: 'r', + default: 16000, + global: true, + requiresArg: true, + type: 'number', + }, + languageCode: { + alias: 'l', + default: 'en-US', + global: true, + requiresArg: true, + type: 'string', + }, + }) + .example( + `node $0 sync-model ./resources/Google_Gnome.wav video -e LINEAR16 -r 16000` + ) + .example( + `node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000` + ) + .wrap(120) + .recommendCommands() + .epilogue(`For more information, see https://cloud.google.com/speech/docs`) + .help() + .strict().argv; diff --git a/samples/resources/Google_Gnome.wav b/samples/resources/Google_Gnome.wav new file mode 100644 index 00000000..2f497b7f Binary files /dev/null and b/samples/resources/Google_Gnome.wav differ diff --git a/samples/system-test/recognize.v1p1beta1.test.js b/samples/system-test/recognize.v1p1beta1.test.js new file mode 100644 index 00000000..984b4a11 --- /dev/null +++ b/samples/system-test/recognize.v1p1beta1.test.js @@ -0,0 +1,59 @@ +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const path = require(`path`); +const storage = require(`@google-cloud/storage`)(); +const test = require(`ava`); +const uuid = require(`uuid`); + +const {runAsync} = require(`@google-cloud/nodejs-repo-tools`); + +const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; +const cmd = `node recognize.v1p1beta1.js`; +const cwd = path.join(__dirname, `..`); +const filename = `Google_Gnome.wav`; +const filepath = path.join(__dirname, `../resources/${filename}`); +const text = `the weather outside is sunny`; + +test.before(async () => { + const [bucket] = await storage.createBucket(bucketName); + await bucket.upload(filepath); +}); + +test.after.always(async () => { + const bucket = storage.bucket(bucketName); + await bucket.deleteFiles({force: true}); + await bucket.deleteFiles({force: true}); // Try a second time... + await bucket.delete(); +}); + +test(`should run sync recognize with model selection`, async t => { + const model = `video`; + const output = await runAsync(`${cmd} sync-model ${filepath} ${model}`, cwd); + t.true(output.includes(`Transcription:`)); + t.true(output.includes(text)); +}); + +test(`should run sync recognize on a GCS file with model selection`, async t => { + const model = `video`; + const output = await runAsync( + `${cmd} sync-model-gcs gs://${bucketName}/${filename} ${model}`, + cwd + ); + t.true(output.includes(`Transcription:`)); + t.true(output.includes(text)); +});