Skip to main content
This guide demonstrates how to implement a noise reduction system for real-time audio transcription using AssemblyAI’s Real-time STT and the noisereduce library. You’ll learn how to create a custom audio pipeline that preprocesses incoming audio to remove background noise before it reaches the transcription service. This solution is particularly valuable for:
  • Voice assistants operating in noisy environments
  • Customer service applications processing calls
  • Meeting transcription tools
  • Voice-enabled applications requiring high accuracy
The implementation uses Python and combines proven audio processing techniques with AssemblyAI’s powerful transcription capabilities. While our example focuses on microphone input, the principles can be applied to any real-time audio stream.

Quickstart

Step-by-step guide

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for an AssemblyAI account and get your API key from your dashboard. Please note that Streaming Speech-to-text is available for upgraded accounts only. If you’re on the free plan, you’ll need to upgrade your account by adding a credit card.

Install and import packages

Install the required packages:
Import packages and set your API key.
Make sure not to share this token with anyone - it is a private key associated uniquely to your account.

Audio configuration and global variables

Set all of your audio configurations and global variables. The NOISE_BUFFER_SIZE controls how much audio is buffered before applying noise reduction — 0.5 seconds provides a good balance between latency and noise reduction quality.

WebSocket event handlers

Open WebSocket

When the connection opens, we start a background thread that reads audio from the microphone, buffers it, applies noise reduction using noisereduce, and sends the denoised audio to AssemblyAI. The noise reduction works by:
  1. Accumulating raw audio samples into a buffer
  2. Once the buffer reaches 0.5 seconds, converting to float and applying nr.reduce_noise()
  3. Converting back to int16 and sending over the WebSocket
  4. Keeping the last 1024 samples as overlap for continuity, and only sending the non-overlapping portion to avoid duplicate audio

Handle WebSocket messages

WebSocket error and close handlers

Begin real-time STT transcription

Open the microphone, connect the WebSocket, and start streaming noise-reduced audio.
You can press Ctrl+C to stop the transcription.