Overview
By the end of this tutorial, you’ll be able to transcribe audio from your microphone in Go.Streaming Speech-to-Text is only available for English. See Supported languages.
Before you begin
To complete this tutorial, you need:- Go installed.
- An with a credit card set up.
Step 1: Install dependencies
1
PortAudio is a cross-platform library for streaming audio. The Go SDK uses PortAudio to stream audio from your microphone.
2
Install the AssemblyAI Go module using
go get
.Step 2: Create a transcriber
In this step, you’ll define a transcriber to handle real-time events.1
Create a new file called
main.go
that imports the AssemblyAI Go module.2
Create a type that implements RealtimeHandler.
3
Browse to , and then click Copy API key under Copy your API key.
4
Create a new RealTimeClient using the function you created. Replace
YOUR_API_KEY
with your copied API key.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. By default, the SDK uses a sample rate of 16 kHz. You can set your own sample rate using the We recommend the following sample rates:
WithSampleRate
option.- Minimum quality:
8_000
(8 kHz) - Medium quality:
16_000
(16 kHz) - Maximum quality:
48_000
(48 kHz)
Step 3: Connect the transcriber
To stream audio to AssemblyAI, you first need to establish a connection to the API usingclient.Connect()
.
Step 4: Record audio from microphone
In this step, you’ll configure your Go app to record audio from your microphone. You’ll use the gordonklaus/portaudio module to make this easier.1
Install the portaudio module for Go.
2
Create a new file called
recorder.go
with the following code:3
In
main.go
, open a microphone stream. The sampleRate
must be the same value as the one you passed to RealTimeClient
(16_000
by default).The recorder formats the audio data for you. If you want to stream data from elsewhere, make sure that your audio data is in the following format:
- Single channel
- 16-bit signed integer PCM or mu-law encoding
4
Read data from the microphone stream, and send it to AssemblyAI for transcription using
client.Send()
.Step 5: Disconnect the transcriber
In this step, you’ll clean up resources by stopping the recorder and disconnecting the transcriber. To disconnect the transcriber onCtrl+C
, use client.Disconnect()
. Disconnect()
accepts a boolean parameter that allows you to wait for any remaining transcriptions before closing the connection.