apm

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Go WebRTC Audio Processing Module (APM)

Go bindings for WebRTC's AudioProcessing module, providing production-quality audio processing for voice applications. Built on the extensively tested and optimized WebRTC audio engine, this library is ideal for real-time voice communication scenarios.

✨ Key Features

🎯 Audio Processing Capabilities
  • Echo Cancellation (AEC) - Effectively removes speaker audio that leaks into the microphone
  • Noise Suppression (NS) - Significantly reduces background noise while preserving speech clarity
  • Automatic Gain Control (AGC) - Intelligently normalizes audio levels automatically
  • High-Pass Filter (HPF) - Removes DC offset and low-frequency noise
🚀 Performance Advantages
  • ✅ Powered by industry-leading WebRTC audio processing engine
  • ✅ Optimized specifically for real-time voice applications
  • ✅ Low-latency, high-performance processing

📦 Installation

System Requirements
  • Operating System: Linux, macOS, Windows
  • Go Version: 1.19 or higher
  • Dependencies: Self-contained WebRTC library (based on commit: d8dd1409d661), no additional system dependencies required.
Installation Steps
  1. Install the Go package
go get github.com/CoyAce/apm
  1. Import in your project
import "github.com/CoyAce/apm"
  1. Example
config := apm.Config{
		CaptureChannels:       1,
		RenderChannels:        1,
		HighPassFilterEnabled: true,
		EchoCancellation:      apm.EchoCancellationConfig{Enabled: true, MobileMode: mobile, StreamDelayMs: 54},
		NoiseSuppression:      apm.NoiseSuppressionConfig{Enabled: true, SuppressionLevel: apm.NsLevelModerate},
		GainControl: apm.GainControlConfig{
			Enabled:                      true,
			InputVolumeControllerEnabled: true,
			HeadroomDB:                   15,
			MaxGainDb:                    50,
		},
	}
processor, _ := apm.New(config)
processor.SetStreamAnalogLevel(180)

// add far end
_ = ae.processor.ProcessRenderInt16(farEnd)

// process near end
ae.processor.SetStreamDelay(config.EchoCancellation.StreamDelayMs)
_ := ae.processor.ProcessCapture(output)

Documentation

Overview

Package apm provides a Go wrapper for WebRTC's AudioProcessing module.

This package enables high-quality audio processing including echo cancellation, noise suppression, automatic gain control, and voice activity detection.

Example usage:

processor, err := apm.New(apm.Config{
    SampleRateHz: 48000,
    NumChannels:  1,
    EchoCancellation: &apm.EchoCancellationConfig{
        Enabled:          true,
        SuppressionLevel: apm.SuppressionHigh,
    },
    NoiseSuppression: &apm.NoiseSuppressionConfig{
        Enabled: true,
        Level:   apm.NoiseSuppressionHigh,
    },
})
if err != nil {
    log.Fatal(err)
}
defer processor.Close()

// Process audio frames...
cleanSamples, hasVoice, err := processor.ProcessCapture(micSamples)

Package cgo provides low-level CGO bindings to the WebRTC AudioProcessing module.

Index

Constants

View Source
const (
	SampleRateHz       = C.APM_SAMPLE_RATE_HZ
	FrameMs            = C.APM_FRAME_MS
	NumSamplesPerFrame = C.APM_NUM_SAMPLES_PER_FRAME
)

Constants exported from the C library

Variables

This section is empty.

Functions

func GetNumSamplesPerFrame

func GetNumSamplesPerFrame() int

GetNumSamplesPerFrame returns the number of samples per frame

func GetSampleRateHz

func GetSampleRateHz() int

GetSampleRateHz returns the sample rate in Hz

Types

type AnalogMicGainEmulationConfig added in v0.1.1

type AnalogMicGainEmulationConfig struct {
	Enabled bool
	// Initial analog gain level to use for the emulated analog gain. Must
	// be in the range [0...255].
	InitialLevel int
}

type CaptureLevelAdjustmentConfig added in v0.1.1

type CaptureLevelAdjustmentConfig struct {
	Enabled                bool
	PreGainFactor          float32
	PostGainFactor         float32
	AnalogMicGainEmulation AnalogMicGainEmulationConfig
}

type Config

type Config struct {
	CaptureLevelAdjustment CaptureLevelAdjustmentConfig
	EchoCancellation       EchoCancellationConfig
	GainControl            GainControlConfig
	NoiseSuppression       NoiseSuppressionConfig
	HighPassFilterEnabled  bool
	CaptureChannels        int
	RenderChannels         int
}

Config holds all runtime configuration options

type EchoCancellationConfig

type EchoCancellationConfig struct {
	Enabled       bool
	MobileMode    bool
	StreamDelayMs int // nil means use delay-agnostic mode
}

EchoCancellationConfig holds echo cancellation settings

type GainControlConfig

type GainControlConfig struct {
	Enabled                      bool
	InputVolumeControllerEnabled bool
	HeadroomDB                   float32
	MaxGainDB                    float32
	GainDB                       float32
}

GainControlConfig holds automatic gain control settings

type Handle

type Handle struct {
	// contains filtered or unexported fields
}

Handle represents an opaque handle to the audio processor

func Create

func Create(config Config) (*Handle, error)

Create creates a new audio processor with the given initialization config

func (*Handle) ApplyConfig

func (h *Handle) ApplyConfig(config Config)

ApplyConfig updates the runtime configuration

func (*Handle) Destroy

func (h *Handle) Destroy()

Destroy releases the audio processor resources

func (*Handle) GetStats

func (h *Handle) GetStats() Stats

GetStats returns statistics from the last capture frame processing

func (*Handle) Initialize

func (h *Handle) Initialize()

func (*Handle) ProcessCaptureFrame

func (h *Handle) ProcessCaptureFrame(samples []float32, numChannels int) error

ProcessCaptureFrame processes a capture (microphone) frame samples should be interleaved float32 samples with length = numChannels * NumSamplesPerFrame

func (*Handle) ProcessCaptureIntFrame added in v0.1.0

func (h *Handle) ProcessCaptureIntFrame(samples []int16, numChannels int) error

func (*Handle) ProcessRenderFrame

func (h *Handle) ProcessRenderFrame(samples []float32, numChannels int) error

ProcessRenderFrame processes a render (speaker) frame for echo cancellation samples should be interleaved float32 samples with length = numChannels * NumSamplesPerFrame

func (*Handle) ProcessRenderIntFrame added in v0.1.0

func (h *Handle) ProcessRenderIntFrame(samples []int16, numChannels int) error

func (*Handle) RecommendedStreamAnalogLevel added in v0.1.1

func (h *Handle) RecommendedStreamAnalogLevel() int

func (*Handle) SetOutputWillBeMuted

func (h *Handle) SetOutputWillBeMuted(muted bool)

SetOutputWillBeMuted signals that output will be muted (hint for AEC/AGC)

func (*Handle) SetStreamAnalogLevel added in v0.1.1

func (h *Handle) SetStreamAnalogLevel(level int)

func (*Handle) SetStreamDelayMs

func (h *Handle) SetStreamDelayMs(delayMs int)

SetStreamDelayMs sets the delay between render and capture streams

func (*Handle) SetStreamKeyPressed

func (h *Handle) SetStreamKeyPressed(pressed bool)

SetStreamKeyPressed signals that a key is being pressed (hint for AEC)

func (*Handle) StreamDelayMs

func (h *Handle) StreamDelayMs() int

type NoiseSuppressionConfig

type NoiseSuppressionConfig struct {
	Enabled          bool
	SuppressionLevel NsLevel
}

NoiseSuppressionConfig holds noise suppression settings

type NsLevel

type NsLevel int

NsLevel represents noise suppression levels

const (
	NsLevelLow      NsLevel = C.NS_LEVEL_LOW
	NsLevelModerate NsLevel = C.NS_LEVEL_MODERATE
	NsLevelHigh     NsLevel = C.NS_LEVEL_HIGH
	NsLevelVeryHigh NsLevel = C.NS_LEVEL_VERY_HIGH
)

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

Processor is the main APM instance

func New

func New(config Config) (*Processor, error)

New creates a new audio processor with the given configuration

func (*Processor) Close

func (p *Processor) Close() error

Close releases resources associated with the processor

func (*Processor) GetStats

func (p *Processor) GetStats() Stats

GetStats returns statistics from the last ProcessCapture call

func (*Processor) Initialize added in v0.0.2

func (p *Processor) Initialize()

func (*Processor) ProcessCapture

func (p *Processor) ProcessCapture(samples []float32) error

ProcessCapture processes microphone input (near-end signal) Returns processed audio and voice activity detection result Input samples should be float32 in range [-1.0, 1.0]

func (*Processor) ProcessCaptureInt16

func (p *Processor) ProcessCaptureInt16(samples []int16) error

ProcessCaptureInt16 processes microphone input with int16 samples

func (*Processor) ProcessRender

func (p *Processor) ProcessRender(samples []float32) error

ProcessRender provides speaker output (far-end signal) for echo cancellation Must be called with speaker audio BEFORE ProcessCapture for the corresponding frame

func (*Processor) ProcessRenderInt16

func (p *Processor) ProcessRenderInt16(samples []int16) error

ProcessRenderInt16 provides speaker output with int16 samples

func (*Processor) RecommendedStreamAnalogLevel added in v0.1.1

func (p *Processor) RecommendedStreamAnalogLevel() int

func (*Processor) SetKeyPressed

func (p *Processor) SetKeyPressed(pressed bool)

SetKeyPressed signals that a key is being pressed (hint for AEC)

func (*Processor) SetOutputMuted

func (p *Processor) SetOutputMuted(muted bool)

SetOutputMuted signals that output will be muted (hint for AEC/AGC)

func (*Processor) SetStreamAnalogLevel added in v0.1.1

func (p *Processor) SetStreamAnalogLevel(level int)

func (*Processor) SetStreamDelay

func (p *Processor) SetStreamDelay(delayMs int)

SetStreamDelay updates the estimated delay between render and capture

func (*Processor) StreamDelay added in v0.0.2

func (p *Processor) StreamDelay() int

type Stats

type Stats struct {
	ResidualEchoLikelihood    float64
	DivergentFilterFraction   float64
	EchoReturnLoss            float64
	EchoReturnLossEnhancement float64
	DelayMedianMs             int
	DelayStdMs                int
	DelayMs                   int
}

Stats holds statistics from the audio processor

Directories

Path Synopsis
examples
echo_cancellation command
Example: Echo cancellation
Example: Echo cancellation
full_pipeline command
Example: Full audio processing pipeline
Example: Full audio processing pipeline
noise_suppression command
Example: Noise suppression
Example: Noise suppression
google.com

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL