mp3

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package mp3 provides a pure-Go MP3 encoder used to compress completed call audio before it is streamed to broadcast aggregators (Broadcastify Calls, RdioScanner, OpenMHz, Icecast).

It wraps the fixed-point Shine encoder, so the daemon keeps its zero-CGO single-binary guarantee — no libmp3lame / libshine at build or runtime. Shine supports the MPEG-1, MPEG-2 and MPEG-2.5 sample-rate families, so 8 kHz digital-voice audio encodes directly with no resample stage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BitrateFor added in v0.6.6

func BitrateFor(sampleRate int) int

BitrateFor returns the CBR bitrate, in bits per second, the encoder uses for audio at sampleRate. Shine hard-codes 128 kbps, which is wrong on two counts for the low sample rates GopherTrunk records at:

  • 128 kbps is not even a legal Layer-III bitrate for the MPEG-2.5 family (8/11.025/12 kHz), so Shine writes an out-of-range bitrate index that corrupts every frame header.
  • More subtly, Shine's single-granule frames (every rate below 32 kHz) desync their bit-reservoir stuffing once the per-frame budget grows past roughly 3300 bits (~420 bytes/frame), emitting oversized frames that no demuxer can follow. This is why an 8 kHz call uploaded to Rdio Scanner transcodes to silence / "Invalid data found".

So each single-granule family gets the highest standard bitrate that keeps a frame comfortably under that limit — 32 kbps for MPEG-2.5, 64 kbps for MPEG-2 — both well above transparent for 8 kHz mono voice. The MPEG-1 rates (≥32 kHz) split each frame into two granules, halving the per-granule budget, so they keep Shine's 128 kbps default. Callers that pace a stream by bitrate (Icecast) use this so their timing tracks the real encoder output.

func Encode

func Encode(samples []int16, sampleRate int) ([]byte, error)

Encode compresses mono 16-bit PCM sampled at sampleRate Hz into a complete MP3 byte stream. The input is treated as a single channel; callers with stereo audio must downmix first.

func EncodeWAVFile

func EncodeWAVFile(path string) ([]byte, int, error)

EncodeWAVFile reads a 16-bit PCM mono WAV file from path and returns the equivalent MP3 byte stream plus the source sample rate.

func ReadWAV

func ReadWAV(path string) ([]int16, int, error)

ReadWAV parses a canonical 16-bit PCM mono WAV file and returns its samples and sample rate. It tolerates extra chunks between `fmt ` and `data` (e.g. LIST/INFO) by walking the chunk list. Only mono 16-bit PCM is supported — the format the recorder writes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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