|
| 1 | +# Cloud Speech API gRPC samples for Java |
| 2 | + |
| 3 | +This is a sample repo for accessing the [Google Cloud Speech API](http://cloud.google.com/speech) with |
| 4 | +[gRPC](http://www.grpc.io/) client library. |
| 5 | + |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +### Enable the Speech API |
| 10 | + |
| 11 | +If you have not already done so, [enable the Google Cloud Speech API for your project](https://console.developers.google.com/apis/api/speech.googleapis.com/overview). |
| 12 | +You must be whitelisted to do this. |
| 13 | + |
| 14 | + |
| 15 | +### Download and install Java and Maven |
| 16 | + |
| 17 | +Install [Java7 or |
| 18 | +higher](http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html). |
| 19 | + |
| 20 | +This sample uses the [Apache Maven][maven] build system. Before getting started, be |
| 21 | +sure to [download][maven-download] and [install][maven-install] it. When you use |
| 22 | +Maven as described here, it will automatically download the needed client |
| 23 | +libraries. |
| 24 | + |
| 25 | +[maven]: https://maven.apache.org |
| 26 | +[maven-download]: https://maven.apache.org/download.cgi |
| 27 | +[maven-install]: https://maven.apache.org/install.html |
| 28 | + |
| 29 | + |
| 30 | +### Set Up to Authenticate With Your Project's Credentials |
| 31 | + |
| 32 | +The example uses a service account for OAuth2 authentication. |
| 33 | +So next, set up to authenticate with the Speech API using your project's |
| 34 | +service account credentials. |
| 35 | + |
| 36 | +Visit the [Cloud Console](https://console.developers.google.com), and navigate to: |
| 37 | +`API Manager > Credentials > Create credentials > |
| 38 | +Service account key > New service account`. |
| 39 | +Create a new service account, and download the json credentials file. |
| 40 | + |
| 41 | +Then, set |
| 42 | +the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your |
| 43 | +downloaded service account credentials before running this example: |
| 44 | + |
| 45 | + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json |
| 46 | + |
| 47 | +If you do not do this, you will see an error that looks something like this when |
| 48 | +you run the example scripts: |
| 49 | +`WARNING: RPC failed: Status{code=PERMISSION_DENIED, description=Request had insufficient authentication scopes., cause=null}`. |
| 50 | +See the |
| 51 | +[Cloud Platform Auth Guide](https://cloud.google.com/docs/authentication#developer_workflow) |
| 52 | +for more information. |
| 53 | + |
| 54 | +## Build the application |
| 55 | + |
| 56 | +Then, build the program: |
| 57 | + |
| 58 | +```sh |
| 59 | +$ mvn package |
| 60 | +``` |
| 61 | + |
| 62 | +or |
| 63 | + |
| 64 | +```sh |
| 65 | +$ mvn compile |
| 66 | +$ mvn assembly:single |
| 67 | +``` |
| 68 | + |
| 69 | +## Run the clients |
| 70 | + |
| 71 | +These programs return the transcription of the audio file you provided. Please |
| 72 | +note that the audio file must be in RAW format. You can use `sox` |
| 73 | +(available, e.g. via [http://sox.sourceforge.net/](http://sox.sourceforge.net/) |
| 74 | +or [homebrew](http://brew.sh/)) to convert audio files to raw format. |
| 75 | + |
| 76 | +### Run the non-streaming client |
| 77 | + |
| 78 | +You can run the batch client like this: |
| 79 | + |
| 80 | +```sh |
| 81 | +$ bin/speech-sample-nonstreaming.sh --host=speech.googleapis.com --port=443 \ |
| 82 | +--file=<audio file path> --sampling=<sample rate> |
| 83 | +``` |
| 84 | + |
| 85 | +Try a streaming rate of 16000 and the included sample audio file, as follows: |
| 86 | + |
| 87 | +```sh |
| 88 | +$ bin/speech-sample-nonstreaming.sh --host=speech.googleapis.com --port=443 \ |
| 89 | +--file=resources/audio.raw --sampling=16000 |
| 90 | +``` |
| 91 | + |
| 92 | +### Run the streaming client |
| 93 | + |
| 94 | +You can run the streaming client as follows: |
| 95 | + |
| 96 | +```sh |
| 97 | +$ bin/speech-sample-streaming.sh --host=speech.googleapis.com --port=443 \ |
| 98 | +--file=resources/audio.raw --sampling=16000 |
| 99 | +``` |
| 100 | + |
0 commit comments