SPEAKER: To get started with the Speech API for Node.js,
you'll need a GCP project, which you can set up in the console.
Enable the Speech API for that project,
then, create a service account and download the private key
as Jason.
You're going to use these credentials
to access Google Cloud APIs, so keep this file secure and out
of your code and public repos.
To access the credential securely from your project,
go to the terminal and set an environment variable
to the path of your service account.
Now, prepare your environment Node.js development.
Install the client library.
If you use Cloud Shell on the console,
you can skip this step, since the required dependencies
are already included.
For this example, you will need some audio which
contains speech to transcribe.
There's a sample audio you can download
linked in the notes below.
Move your audio sample to the project in a resources folder,
and change the name of the file to make
a generic for example purposes.
Now, you don't actually have to change the name of your file,
but just remember to write it correctly
when prompted to write the file name to the code.
Make a new project file or open an existing file.
Open the Google Cloud client library and the file system
module.
Start with an async function called main,
then instantiate a speech client.
Provide the file name of the audio file
you want to transcribe, then use file system to read
the local audio file and convert it to Base64 encoding.
Create a document called audio with a field called
content, set to that base64 string of the audio.
Provide some details about configuration
in an object called config.
You must include the type of encoding,
the sample rate in Hertz, and the language
of the speech in the audio, but there are also optional fields.
You can find out more about those fields in the API
documentation link below.
Instantiate an object called request,
which is made up of the audio and config objects.
Call recognize, passing the request object.
This returns a promise that resolves
to contain the result at zero or more sequential speech
recognition result messages.
Each speech recognition result has a property
called alternatives, which may contain one or more recognition
hypotheses.
An alternative has three properties, transcript,
confidence and words.
Transcript is a string representing the words
that the user spoke.
Confidence is a number between 0.0 and 1.0,
where a higher number indicates an estimated greater
likelihood that the recognized words are correct.
Words is an array of word info objects.
I won't talk more about word info,
since it goes beyond the scope of this quick start.
But if you're curious, check out the API reference
to find out more.
To print out the transcription, get the first alternative
from each result, then join them in a single string.
Log that transcription.
Run the main function and include a catch block
to handle any errors.
Save your file and run the code.
If everything worked properly, you
should see the transcription of the text.
If not, take a look back at the code for any syntax errors.
Congratulations.
You've just sent your first request to the Speech
to Text API.
To find out more about the client libraries
or consult speech basics, check out the Cloud documentation.