
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create a virtual machine
/ 20
Install necessary software and create a firewall rule
/ 40
Run the sample solution (hello-https)
/ 20
Run the sample solution to capture audio on webpage
/ 20
The Google Cloud Speech streaming API enables developers to turn spoken language into text in real time. Using the API in combination with Javascript's Web Audio API and Websockets, a Java servlet can accept streamed speech from a webpage and provide text transcripts of it, enabling any web page to use the spoken word as an additional user interface.
This lab is split into multiple sections, each section introduces a component of the final web application.
The webapp you create takes audio from the client's microphone and streams it to a Java servlet. The Java servlet passes the data to the Cloud Speech API, which streams transcriptions of any speech it detects back to the servlet. The servlet then passes the transcription results to the client, which then displays it on the page.
To accomplish this, you need to create several components:
This lab assumes familiarity with:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Compute Engine is a service that allows you to start VMs on Google's infrastructure. In this lab, you create and use a VM to run a servlet written in Java 8 that hosts the website and use the Cloud Speech API to provide dynamic transcriptions to the client. For the purposes of this lab, you also use the VM to run your code.
To create a new VM, click the Navigation menu () > Compute Engine > VM instances.
To create a new instance, click Create Instance.
In the Machine configuration.
Name the new instance speaking-with-a-webpage
.
Choose a zone as
Leave the other values as default.
Click OS and storage.
Click Change to begin configuring your boot disk and select the following values:
For Operating system select Debian.
For Version select Debian GNU/Linux 11 (bullseye).
Leave the Boot disk type set to default.
At the bottom of the window, click Select.
Click Networking.
Click Security.
For Identity and API access > Service Account leave as default.
For Access scopes select Allow full access to all Cloud APIs.
Click Create.
After a few minutes, your VM is up and running! View your VM and its details in the VM instances list. Notice the SSH button to use in the next step, and the External IP to use later in this lab.
A new window opens and connects you to your VM, providing a command prompt. Use this interface for the rest of this lab.
Click Check my progress to verify your performed task.
Read more about Compute Engine and its different capabilities in the Compute Engine documentation reference.
This creates the directory, speaking-with-a-webpage
, which contains subdirectories for each of the following sections. Each subdirectory builds on the one before it, incrementally adding new functionality:
01-hello-https
- contains a minimal Jetty servlet with static files and a handler served over HTTPS02-webaudio
- fills out the client-side Javascript to record audio from the client's microphone and display a visualization to confirm it works03-websockets
- modifies both the client and the server to communicate with each other through a websocket04-speech
- modifies the server to send audio to the Cloud Speech API, and send subsequent transcriptions to the Javascript clientThe example used for this lab does not use the normal HTTPS port - instead, they use the non-privileged port 8443
, for development purposes.
cd ~/speaking-with-a-webpage
git diff --no-index 01-hello-https/ 02-webaudio/
02-https
and 03-webaudio
directories / steps.
Use the arrow keys, PgUp / PgDn to navigate, and q
to quit.
Click Check my progress to verify your performed task.
The Java Servlet is the backbone that supports this webapp, as it serves the required client-side HTML, CSS, and Javascript code, and connects to the Cloud Speech API to provide transcriptions.
When accessing a user's microphone from a webpage, browsers require the webpage to communicate over a secure channel to prevent eavesdropping. Because of this, set up your servlet to serve webpages over HTTPS. Since configuring and serving secure web pages is a topic in itself, for this lab use the self-signed certificate and Jetty configuration files in the provided sample solution, which is sufficient for a development environment.
For this section, simply read through and run the provided Maven project in 01-hello-https
. Take particular note of the files within the src/
directory, as those are the primary files that are built on in subsequent steps:
src/main/webapp
include the Javascript, CSS, and HTML files, that are served statically by JettyTranscribeServlet.java
defines the servlet that handles requests to the path /transcribe
The 01-hello-https subdirectory of the provided speaking-with-a-webpage
repository contains a Maven servlet project configured for HTTPS. This servlet uses the Jetty servlet framework to serve both static files and a dynamic endpoint. It also uses the blog post above to generate a self-signed certificate using the Key Tool command, and adds Jetty configuration to support HTTPS.
01-hello-https.
https://<your-external-ip>:8443
When you first access the webapp using the HTTPS URL, your browser will likely warn you that the connection is not private. This is because the sample app uses a self-signed SSL certificate for development. In a production environment, you would need an SSL certificate signed by a Certificate Authority, but for the purposes of this lab, a self-signed SSL certificate suffices. Just be sure not to speak of any secrets with your web page. 😁
Click Check my progress to verify your performed task.
The Web Audio API allows a webpage to capture audio data from a user's microphone, given their consent. The Cloud Speech API needs this raw data in a certain form, and needs to know the rate at which it's sampled.
The 02-webaudio
subdirectory of the provided speaking-with-a-webpage
repository builds on the 01-hello-https
sample code by adding the Web Audio getUserMedia function to connect the user's microphone to a visualization of the audio. It then adds a ScriptProcessorNode to the audio pipeline to retrieve the raw audio bytes, in preparation for sending it to the server. Since the Cloud Speech API will also eventually need the sampleRate, it retrieves that as well. Start the 02-webaudio
app as follows:
Press CTRL+C to stop the server.
Navigate to the directory that contains 02-webaudio
:
https://<your-external-ip>:8443
01-hello-https
) and the current one (02-webaudio
) by running:
cd ~/speaking-with-a-webpage
git diff --no-index 01-hello-https/ 02-webaudio/
Click Check my progress to verify your performed task.
A normal HTTP connection is not ideal for realtime streaming of audio to a server, while receiving transcriptions as they become available. In this section, you create a Web Socket connection from the client to the server, and use it to send along the audio metadata (i.e.,the sample rate) and data to the server, while listening for a response (i.e., the transcript of the data).
The provided example changes the TranscribeServlet to extend from WebSocketServlet
in order to register a WebSocketAdapter
. The WebSocketAdapter
it defines simply takes the message it's received and sends it back to the client.
On the client, the sample replaces the scriptNode from the previous step with one that sends the data to a socket to be defined later. It then creates that secure Websocket connection to the server. Once both the server and the microphone have connected, it starts listening for messages from the server; then it sends the server the sample rate. When the server echos back the sample rate, the client replaces the listener with the more permanent transcription handler, and connects the scriptNode
to begin streaming audio bytes to the server.
03-websockets
:To access your running webapp, look for the External IP address in your Cloud Console VM Instances page, and point your browser to: https://<your-external-ip>:8443
Press CTRL+C to stop the server.
The Google Cloud Speech streaming API allows you to send audio bytes to the API in real time and asynchronously receive transcriptions of any speech it detects. The API expects the bytes to be in a specific format, as determined by the configuration that is sent in the beginning of a request. For this webapp, you send the API raw audio samples in the LINEAR16
format - that is, each sample is a 16-bit signed integer - sent at the sample rate obtained by the client.
The 04-speech
subdirectory of the provided speaking-with-a-webpage
repository fills out the server code from the 03-websockets
step. It incorporates the code from the StreamingRecognizeClient sample code above to connect with, pass along audio bytes to, and receive transcripts from the Cloud Speech API. When it asynchronously receives the transcripts, it uses its connection to the Javascript client to pass those along. The Javascript client simply outputs it to the web page.
https://<your-external-ip>:8443
Below are multiple-choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.
You have learned how to use gcloud to create a VM and start a Java servlet to capture audio and transcribe to text on a webpage!
Think about software improvements you could add:
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated December 11, 2024
Lab Last Tested October 03, 2024
Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one