bwe

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bwe implements the libopus OSCE blind-bandwidth-extension network (BBWENet), which extends 16 kHz decoded speech up to 48 kHz.

LoadModel binds the model weights from a validated dnnblob.Blob into typed Go layers. The model dimensions and required weight-record names mirror libopus 1.6.1 dnn/bbwenet_data.{h,c} and dnn/nndsp.c.

State.Process runs the BBWENet forward pass from libopus dnn/osce.c: the feature network produces per-subframe latent conditioning vectors, which drive a cascade of adaptive-convolution (AF1/AF2/AF3) and adaptive-shaping (TDShape) stages plus learned upsampling, transforming a 16 kHz input frame into a 48 kHz output frame.

Index

Constants

View Source
const (
	FeatureDim  = 114
	FrameSize16 = 80
	CondDim     = 128

	FNetConv1In  = 342 // BBWENET_FNET_CONV1_IN_SIZE * 3 (libopus conv1d_init)
	FNetConv1Out = 128
	FNetConv2In  = 384 // BBWENET_FNET_CONV2_IN_SIZE * 3
	FNetConv2Out = 128
	FNetGRUIn    = 128
	FNetGRUOut   = 128 // 3 * 128 outputs for gru-zrh
	FNetTConvIn  = 128
	FNetTConvOut = 256 // 2 * 128 (kernel_size 2)

	TDShape1Alpha1FIn  = 256
	TDShape1Alpha1FOut = 80
	TDShape1Alpha1TIn  = 42
	TDShape1Alpha1TOut = 80
	TDShape1Alpha2In   = 160
	TDShape1Alpha2Out  = 80

	TDShape2Alpha1FIn  = 256
	TDShape2Alpha1FOut = 120
	TDShape2Alpha1TIn  = 42
	TDShape2Alpha1TOut = 120
	TDShape2Alpha2In   = 240
	TDShape2Alpha2Out  = 120

	AF1KernelIn  = 128
	AF1KernelOut = 48
	AF1GainIn    = 128
	AF1GainOut   = 3

	AF2KernelIn  = 128
	AF2KernelOut = 288
	AF2GainIn    = 128
	AF2GainOut   = 3

	AF3KernelIn  = 128
	AF3KernelOut = 48
	AF3GainIn    = 128
	AF3GainOut   = 1
)

Layer dimensions are copied verbatim from libopus `dnn/bbwenet_data.h` and the `init_bbwenetlayers` call sites in `dnn/bbwenet_data.c`. The loader validates each bound layer against these dimensions.

View Source
const (
	// Sample-rate ratios. The BBWENet pipeline is a 3x upsampler:
	// 16 kHz lowband -> 48 kHz wideband.
	InputSampleRate  = 16000
	OutputSampleRate = 48000
)

Per-frame and per-subframe dimensions for the libopus 1.6.1 BBWENet pipeline, copied verbatim from the dnn/bbwenet_data.h layer geometry and the dnn/nndsp.c adaconv/adashape constants.

Variables

This section is empty.

Functions

func EvaluateLayerInt8

func EvaluateLayerInt8(layer *LinearLayer, out, in []float32)

EvaluateLayerInt8 runs the supplied LinearLayer using only its int8 quantised weight path (cgemv8x4 + scale). Used by parity tests to verify the int8 kernel agrees with a reference computation on real libopus weights. Writes zeros to `out` when the layer has no int8 weights.

Types

type FeatureState

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

FeatureState mirrors libopus `OSCEBWEFeatureState` (dnn/osce_structs.h): the 50%-overlap analysis maintains a 160-sample input history plus the previous frame's first 82 spectral values for the instantaneous-frequency cross-power computation.

func (*FeatureState) CalculateFeatures

func (s *FeatureState) CalculateFeatures(features []float32, xq16k []int16)

CalculateFeatures populates `features` with libopus-compatible BBWENet input vectors derived from `xq16k`. The input is interpreted as signed int16 PCM at 16 kHz (the SILK lowband output). `xq16k` length must be a multiple of 160 (one 10 ms hop). `features` must hold `(len(xq16k)/160) * FeatureDim` floats; existing contents are overwritten.

func (*FeatureState) Reset

func (s *FeatureState) Reset()

Reset clears the persistent buffers and primes the previous spectrum to match libopus `osce_bwe_reset`.

type LinearLayer

type LinearLayer struct {
	Bias         dnnblob.Float32View
	Subias       dnnblob.Float32View
	Weights      dnnblob.Int8View
	FloatWeights dnnblob.Float32View
	Scale        dnnblob.Float32View
	NbInputs     int
	NbOutputs    int
}

LinearLayer is the typed Go projection of one libopus `LinearLayer` weight bundle. Bias/Subias/Scale always come as float32 records; the weight payload may be either int8 (with scale) or float32, matching the libopus `linear_init` selection rules.

type LinearLayerSpec

type LinearLayerSpec struct {
	Name         string
	Bias         string
	Subias       string
	Weights      string
	FloatWeights string
	Scale        string
	NbInputs     int
	NbOutputs    int
}

LinearLayerSpec mirrors the libopus `linear_init(...)` argument tuple, with optional record names left empty when the layer does not carry that component. The Name selects the destination field on the Model.

type Model

type Model struct {
	FNetConv1        LinearLayer
	FNetConv2        LinearLayer
	FNetGRUInput     LinearLayer
	FNetGRURecurrent LinearLayer
	FNetTConv        LinearLayer
	TDShape1Alpha1F  LinearLayer
	TDShape1Alpha1T  LinearLayer
	TDShape1Alpha2   LinearLayer
	TDShape2Alpha1F  LinearLayer
	TDShape2Alpha1T  LinearLayer
	TDShape2Alpha2   LinearLayer
	AF1Kernel        LinearLayer
	AF1Gain          LinearLayer
	AF2Kernel        LinearLayer
	AF2Gain          LinearLayer
	AF3Kernel        LinearLayer
	AF3Gain          LinearLayer
}

Model holds every BBWENet weight layer libopus binds from a USE_WEIGHTS_FILE blob. Order matches the `BBWENETLayers` struct in dnn/bbwenet_data.h.

func LoadModel

func LoadModel(blob *dnnblob.Blob) (*Model, error)

LoadModel binds a libopus-style OSCE BWE model blob into typed Go layers. The blob must satisfy `dnnblob.Blob.SupportsOSCEBWE` (i.e. every required `bbwenet_*` record name is present); missing records or size mismatches return errInvalidBWEModel.

type State

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

State carries the persistent BBWENet runtime state libopus keeps inside BBWENetState (dnn/osce_structs.h): the bound model plus the conv / GRU / tdshape working buffers and overlap windows reused across forward passes.

func (*State) Loaded

func (s *State) Loaded() bool

Loaded reports whether the BBWENet runtime has a valid model binding.

func (*State) Model

func (s *State) Model() *Model

Model returns the bound BBWENet model, or nil until a valid weights blob is bound.

func (*State) Process

func (s *State) Process(in16k, out48k, features []float32) error

Process runs the BBWENet 16 kHz -> 48 kHz upsampler for one decoder frame. in16k must be 160 (10 ms) or 320 (20 ms) samples of normalised float32 PCM at 16 kHz; features must hold num_frames=len(in16k)/160 vectors of FeatureDim floats each. out48k must hold 3 * len(in16k) float32 samples.

The forward pass mirrors libopus bbwe_feature_net / bbwenet_process_frames (dnn/osce.c): the feature net (conv1 / conv2 / tconv / GRU) produces the per-subframe conditioning, which drives three AdaConv stages, two AdaShape blocks, the interleaved upsamp_2x and interpol_3_2 resamplers and the Valin activation, in that order. Output agrees with libopus to within float32 roundoff, gated by the BWE forward-pass parity tests.

func (*State) ProcessDelayed

func (s *State) ProcessDelayed(in16k, out48k, features []float32) error

ProcessDelayed runs the BBWENet forward pass and then applies libopus' public osce_bwe wrapper semantics: 21 samples of int16-domain output delay plus int16 scaling/clipping. The output remains normalised float32 PCM for the Go decoder, but its samples mirror the libopus int16 wrapper boundary.

func (*State) Reset

func (s *State) Reset()

Reset clears the per-stream working buffers libopus zero-initialises in `reset_bbwenet_state`. The model binding survives so libopus-style reset semantics are preserved (USE_WEIGHTS_FILE lifetime).

func (*State) SetModel

func (s *State) SetModel(blob *dnnblob.Blob) error

SetModel binds (or clears) the BBWENet model on the runtime state. Passing a nil blob clears the binding so subsequent Loaded() calls return false.

func (*State) SetModelPreservingState added in v0.2.0

func (s *State) SetModelPreservingState(blob *dnnblob.Blob) error

SetModelPreservingState replaces the BBWENet weights without resetting the recurrent and filter history. libopus's decoder model control reloads OSCE weights while retaining each SILK channel's OSCE BWE state.

Jump to

Keyboard shortcuts

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