Skip to content

Commit

Permalink
Merge remote-tracking branch 'dpe/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer committed Nov 2, 2017
2 parents 164102f + 9580282 commit ab8e88a
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
85 changes: 85 additions & 0 deletions packages/google-cloud-node/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# Google Cloud Speech API Node.js Samples

[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-speech.svg)]()

The [Cloud Speech API](https://cloud.google.com/speech/docs) enables easy integration of Google speech recognition technologies into developer applications. Send audio and receive a text transcription from the Cloud Speech API service.

## Table of Contents

* [Setup](#setup)
* [Samples](#samples)
* [Speech Recognition](#speech-recognition)
* [Running the tests](#running-the-tests)

## Setup

1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
1. Install dependencies:

With **npm**:

npm install

With **yarn**:

yarn install

[prereq]: ../README.md#prerequisites
[run]: ../README.md#how-to-run-a-sample

## Samples

### Speech Recognition

View the [documentation][recognize_0_docs] or the [source code][recognize_0_code].

__Usage:__ `node recognize.js --help`

```
Commands:
sync <filename> Detects speech in a local audio file.
sync-gcs <gcsUri> Detects speech in an audio file located in a Google Cloud Storage bucket.
sync-words <filename> Detects speech in a local audio file with word time offset.
async <filename> Creates a job to detect speech in a local audio file, and waits for the job to complete.
async-gcs <gcsUri> Creates a job to detect speech in an audio file located in a Google Cloud Storage bucket,
and waits for the job to complete.
async-gcs-words <gcsUri> Creates a job to detect speech with word time offset in an audio file located in a Google
Cloud Storage bucket, and waits for the job to complete.
stream <filename> Detects speech in a local audio file by streaming it to the Speech API.
listen Detects speech in a microphone input stream. This command requires that you have SoX
installed and available in your $PATH. See
https://www.npmjs.com/package/node-record-lpcm16#dependencies
Options:
--help Show help [boolean]
--encoding, -e [string] [default: "LINEAR16"]
--sampleRateHertz, -r [number] [default: 16000]
--languageCode, -l [string] [default: "en-US"]
Examples:
node recognize.js sync ./resources/audio.raw -e LINEAR16 -r 16000
node recognize.js async-gcs gs://gcs-test-data/vr.flac -e FLAC -r 16000
node recognize.js stream ./resources/audio.raw -e LINEAR16 -r 16000
node recognize.js listen
For more information, see https://cloud.google.com/speech/docs
```

[recognize_0_docs]: https://cloud.google.com/speech/docs
[recognize_0_code]: recognize.js

## Running the tests

1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables.

1. Run the tests:

With **npm**:

npm test

With **yarn**:

yarn test
45 changes: 45 additions & 0 deletions packages/google-cloud-node/samples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "nodejs-docs-samples-speech",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"test": "samples test run --cmd ava -- -T 20s --verbose system-test/*.test.js"
},
"dependencies": {
"@google-cloud/speech": "0.10.2",
"@google-cloud/storage": "1.2.1",
"node-record-lpcm16": "0.3.0",
"yargs": "8.0.2"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.17",
"ava": "0.21.0",
"proxyquire": "1.8.0",
"sinon": "3.2.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"product": "speech",
"samples": [
{
"id": "recognize",
"name": "Speech Recognition",
"file": "recognize.js",
"docs_link": "https://cloud.google.com/speech/docs",
"usage": "node recognize.js --help"
}
]
}
}
63 changes: 63 additions & 0 deletions packages/google-cloud-node/samples/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.
*/

'use strict';

// [START speech_quickstart]
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech');
const fs = require('fs');

// Your Google Cloud Platform project ID
const projectId = 'your-project-id';

// Instantiates a client
const speechClient = Speech({
projectId: projectId
});

// The name of the audio file to transcribe
const fileName = './resources/audio.raw';

// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');

// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US'
};
const request = {
audio: audio,
config: config
};

// Detects speech in the audio file
speechClient.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_quickstart]

0 comments on commit ab8e88a

Please sign in to comment.