|
| 1 | +# @google-cloud/speech |
| 2 | +> Google Cloud Speech Client Library for Node.js |
| 3 | +
|
| 4 | +> **This is a Beta release of Google Cloud Speech.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes. |
| 5 | +
|
| 6 | +*Looking for more Google APIs than just Speech? You might want to check out [`google-cloud`][google-cloud].* |
| 7 | + |
| 8 | +- [API Documentation][gcloud-speech-docs] |
| 9 | +- [Official Documentation][cloud-speech-docs] |
| 10 | + |
| 11 | + |
| 12 | +```sh |
| 13 | +$ npm install --save @google-cloud/speech |
| 14 | +``` |
| 15 | +```js |
| 16 | +var speech = require('@google-cloud/speech')({ |
| 17 | + projectId: 'grape-spaceship-123', |
| 18 | + keyFilename: '/path/to/keyfile.json' |
| 19 | +}); |
| 20 | + |
| 21 | +// Detect the speech in an audio file. |
| 22 | +speechClient.recognize('./audio.raw', { |
| 23 | + encoding: 'LINEAR16', |
| 24 | + sampleRate: 16000 |
| 25 | +}, function(err, transcript) { |
| 26 | + // transcript = 'how old is the Brooklyn Bridge' |
| 27 | +}); |
| 28 | + |
| 29 | +// Detect the speech in an audio file stream. |
| 30 | +fs.createReadStream('./audio.raw') |
| 31 | + .on('error', console.error) |
| 32 | + .pipe(speech.createRecognizeStream({ |
| 33 | + config: { |
| 34 | + encoding: 'LINEAR16', |
| 35 | + sampleRate: 16000 |
| 36 | + }, |
| 37 | + singleUtterance: false, |
| 38 | + interimResults: false |
| 39 | + })) |
| 40 | + .on('error', console.error) |
| 41 | + .on('data', function(data) { |
| 42 | + // The first "data" event emitted might look like: |
| 43 | + // data = { |
| 44 | + // endpointerType: Speech.endpointerTypes.START_OF_SPEECH, |
| 45 | + // ... |
| 46 | + // } |
| 47 | + // |
| 48 | + // A later "data" event emitted might look like: |
| 49 | + // data = { |
| 50 | + // endpointerType: Speech.endpointerTypes.END_OF_AUDIO, |
| 51 | + // ... |
| 52 | + // } |
| 53 | + // |
| 54 | + // A final "data" event emitted might look like: |
| 55 | + // data = { |
| 56 | + // endpointerType: Speech.endpointerTypes.END_OF_AUDIO, |
| 57 | + // results: "how old is the Brooklyn Bridge", |
| 58 | + // ... |
| 59 | + // } |
| 60 | + }); |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | +## Authentication |
| 65 | + |
| 66 | +It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Google Cloud services. |
| 67 | + |
| 68 | +### On Google Compute Engine |
| 69 | + |
| 70 | +If you are running this client on Google Compute Engine, we handle authentication for you with no configuration. You just need to make sure that when you [set up the GCE instance][gce-how-to], you add the correct scopes for the APIs you want to access. |
| 71 | + |
| 72 | +``` js |
| 73 | +// Authenticating on a global basis. |
| 74 | +var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' |
| 75 | + |
| 76 | +var speech = require('@google-cloud/speech')({ |
| 77 | + projectId: projectId |
| 78 | +}); |
| 79 | + |
| 80 | +// ...you're good to go! |
| 81 | +``` |
| 82 | + |
| 83 | +### Elsewhere |
| 84 | + |
| 85 | +If you are not running this client on Google Compute Engine, you need a Google Developers service account. To create a service account: |
| 86 | + |
| 87 | +1. Visit the [Google Developers Console][dev-console]. |
| 88 | +2. Create a new project or click on an existing project. |
| 89 | +3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services): |
| 90 | + * Google Cloud Speech API |
| 91 | +4. Navigate to **APIs & auth** > **Credentials** and then: |
| 92 | + * If you want to use a new service account, click on **Create new Client ID** and select **Service account**. After the account is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests. |
| 93 | + * If you want to generate a new key for an existing service account, click on **Generate new JSON key** and download the JSON key file. |
| 94 | + |
| 95 | +``` js |
| 96 | +var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' |
| 97 | + |
| 98 | +var speech = require('@google-cloud/speech')({ |
| 99 | + projectId: projectId, |
| 100 | + |
| 101 | + // The path to your key file: |
| 102 | + keyFilename: '/path/to/keyfile.json' |
| 103 | + |
| 104 | + // Or the contents of the key file: |
| 105 | + credentials: require('./path/to/keyfile.json') |
| 106 | +}); |
| 107 | + |
| 108 | +// ...you're good to go! |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node/ |
| 113 | +[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using |
| 114 | +[dev-console]: https://console.developers.google.com/project |
| 115 | +[gcloud-speech-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/speech |
| 116 | +[cloud-speech-docs]: https://cloud.google.com/speech |
0 commit comments