Transcribe streaming audio from a microphone in Java
Learn how to transcribe streaming audio in Java.
Overview
By the end of this tutorial, you’ll be able to transcribe audio from your microphone in Java.
Supported languages
Streaming Speech-to-Text is only available for English.
Before you begin
To complete this tutorial, you need:
- Java 8 or above.
- An AssemblyAI account with credit card set up.
Here’s the full sample code for what you’ll build in this tutorial:
Step 1: Install the SDK
Include the latest version of AssemblyAI’s Java SDK in your project dependencies:
Maven
Gradle
Step 2: Create a real-time transcriber
In this step, you’ll create a real-time transcriber and configure it to use your API key.
Browse to Account, and then click the text under Your API key to copy it.
Use the builder to create a new real-time transcriber with your API key, a sample rate of 16 kHz, and lambdas to log the different events.
Replace YOUR_API_KEY
with your copied API key.
The real-time transcriber returns two types of transcripts: partial and final.
- Partial transcripts are returned as the audio is being streamed to AssemblyAI.
- Final transcripts are returned when the service detects a pause in speech.
End of utterance controls
You can configure the silence threshold for automatic utterance detection and programmatically force the end of an utterance to immediately get a Final transcript.
Sample rate
The sample_rate
is the number of audio samples per second, measured in hertz (Hz). Higher sample rates result in higher quality audio, which may lead to better transcripts, but also more data being sent over the network.
We recommend the following sample rates:
- Minimum quality:
8_000
(8 kHz) - Medium quality:
16_000
(16 kHz) - Maximum quality:
48_000
(48 kHz)
If you don’t set a sample rate on the real-time transcriber, it defaults to 16 kHz.
Step 3: Connect the streaming service
Connect to the streaming service so you can send audio to it.
Step 4: Record audio from microphone
In this step, you’ll use Java’s built-in APIs for recording audio.
Create the audio format that the real-time service expects, which is single channel, pcm_s16le
(PCM signed 16-bit little-endian) encoded, with a sample rate of 16_000
.
The sample rate needs to be the same value as you configured on the real-time transcriber.
Audio data format
By default, transcriptions expect PCM16-encoded audio. If you want to use mu-law encoding, see Specifying the encoding.
Step 5: Disconnect the real-time service
When you are done, close the transcriber.
Step 6: Run the code in a thread
To be able to listen for user input while the recording is happening, you need to run the code in a separate thread. When the user hits enter, interrupt the thread and exit the program.
Next steps
To learn more about Streaming Speech-to-Text, see the following resources:
Need some help?
If you get stuck, or have any other questions, we’d love to help you out. Contact our support team at support@assemblyai.com or create a support ticket.