Skip to main content

FastPeaks

FastPeaks is a small Python library and command-line tool that generates normalized waveform peaks from audio files.

It is designed for supplying precomputed waveform data to browser waveform libraries such as WaveSurfer.js.

FastPeaks uses FFmpeg to decode audio directly to low-sample-rate mono PCM and NumPy to calculate waveform peaks efficiently. A 77-minute MP3 can be processed in approximately four seconds on typical desktop hardware, while cached results can be loaded in milliseconds.

Features

  • Generate normalized RMS waveform peaks
  • Process MP3 and other FFmpeg-supported audio formats
  • Decode directly to low-sample-rate mono audio
  • Choose the number of returned peaks
  • Optional JSON caching
  • Automatic cache invalidation when the source file changes
  • Force cache regeneration when required
  • Python API
  • Command-line interface
  • JSON output suitable for WaveSurfer.js

Requirements

FastPeaks requires:

  • Python 3.10 or newer
  • FFmpeg
  • NumPy

NumPy is installed automatically with FastPeaks. FFmpeg must be installed separately and available through your system's PATH.

Check whether FFmpeg is installed:

ffmpeg -version

Installing FFmpeg on macOS

Using Homebrew:

brew install ffmpeg

Installing FFmpeg on Ubuntu or Debian

sudo apt update
sudo apt install ffmpeg

Installing FFmpeg on Windows

FFmpeg can be installed using Windows Package Manager:

winget install Gyan.FFmpeg

Restart your terminal after installation and confirm that this works:

ffmpeg -version

Installation

Once FastPeaks is available from PyPI:

pip install fastpeaks

For local development, clone the repository and install it in editable mode:

git clone https://github.com/stayml/fastpeaks.git
cd fastpeaks

python -m venv .venv
source .venv/bin/activate

python -m pip install --upgrade pip
python -m pip install -e ".[dev]"

On Windows, activate the virtual environment with:

.venv\Scripts\activate

Python usage

Generate 1,000 waveform peaks:

import fastpeaks

peaks = fastpeaks.generate_peaks("audio.mp3")

Generate 2,000 peaks:

import fastpeaks

peaks = fastpeaks.generate_peaks(
    "audio.mp3",
    num_peaks=2000,
)

Enable caching:

import fastpeaks

peaks = fastpeaks.generate_peaks(
    "audio.mp3",
    num_peaks=2000,
    cache=True,
)

Force cache regeneration:

import fastpeaks

peaks = fastpeaks.generate_peaks(
    "audio.mp3",
    num_peaks=2000,
    cache=True,
    refresh_cache=True,
)

Use a custom cache directory:

import fastpeaks

peaks = fastpeaks.generate_peaks(
    "audio.mp3",
    cache=True,
    cache_dir="./peak_cache",
)

Display performance timings:

import fastpeaks

peaks = fastpeaks.generate_peaks(
    "audio.mp3",
    num_peaks=2000,
    cache=True,
    show_timings=True,
)

Timing information is written to stderr.

Function parameters

fastpeaks.generate_peaks(
    audio_path,
    num_peaks=1000,
    decode_sample_rate=2000,
    decimals=4,
    cache=False,
    cache_dir=None,
    refresh_cache=False,
    show_timings=False,
)
Parameter Description
audio_path Path to the input audio file.
num_peaks Number of peaks to return. Defaults to 1000.
decode_sample_rate FFmpeg output sample rate used for analysis. Defaults to 2000.
decimals Decimal places in returned values. Defaults to 4.
cache Load and save JSON cache data when True.
cache_dir Optional cache directory. Defaults to .fastpeaks_cache.
refresh_cache Ignore existing cache data and regenerate it.
show_timings Print performance information to stderr.

refresh_cache=True automatically enables caching.

The returned value is a normal Python list:

[0.4835, 0.6149, 0.2875, 0.7414, 1.0]

Values are normalized between 0.0 and 1.0.

Command-line usage

Generate peaks and print JSON to the terminal:

fastpeaks "audio.mp3"

Generate 2,000 peaks:

fastpeaks "audio.mp3" --num-peaks 2000

Enable caching:

fastpeaks "audio.mp3" --cache

Force cache regeneration:

fastpeaks "audio.mp3" --refresh-cache

Write the peaks to a file:

fastpeaks "audio.mp3" --output peaks.json

Generate formatted JSON:

fastpeaks "audio.mp3" --pretty --output peaks.json

Use a custom cache directory:

fastpeaks "audio.mp3" \
    --cache-dir "./peak_cache" \
    --output peaks.json

Display timings:

fastpeaks "audio.mp3" \
    --cache \
    --timings \
    --output peaks.json

See all available options:

fastpeaks --help

Using peaks with WaveSurfer.js

Generate the peak file:

fastpeaks "audio.mp3" \
    --num-peaks 2000 \
    --output peaks.json

Load both the peaks and audio in JavaScript:

async function createWaveform() {
    const response = await fetch("/peaks.json");

    if (!response.ok) {
        throw new Error(
            `Could not load waveform peaks: ${response.status}`
        );
    }

    const peaks = await response.json();

    const wavesurfer = WaveSurfer.create({
        container: "#waveform",
        url: "/audio.mp3",
        peaks: [peaks],
        duration: 300,
    });

    return wavesurfer;
}

createWaveform().catch((error) => {
    console.error(error);
});

Replace duration: 300 with the duration of your audio in seconds.

Caching

Caching is disabled by default.

When enabled, FastPeaks stores JSON cache files under:

.fastpeaks_cache/

A custom directory can be supplied using cache_dir or --cache-dir.

Cache entries account for:

  • Resolved audio path
  • Audio file size
  • Audio modification time
  • Number of requested peaks
  • Decode sample rate
  • Decimal precision
  • FastPeaks algorithm version

If the source file changes, its existing cache entry is treated as stale and regenerated.

Cache files are written atomically to reduce the risk of incomplete files.

How it works

FastPeaks asks FFmpeg to:

  1. Decode the source audio
  2. Select the first audio stream
  3. Downmix it to mono
  4. Resample it to a low analysis sample rate
  5. Return signed 16-bit PCM to Python

NumPy then:

  1. Converts the PCM data to normalized floating-point samples
  2. Divides the samples into equally sized sections
  3. Calculates the RMS amplitude of every section
  4. Normalizes the largest result to 1.0
  5. Returns the requested number of peaks

The low decode sample rate dramatically reduces memory usage and Python processing time. FFmpeg decoding remains the largest part of first-time processing.

Development

Install the development dependencies:

python -m pip install -e ".[dev]"

Run the tests:

pytest

Build the package:

python -m build

Check the generated distributions:

python -m twine check dist/*

Licence

FastPeaks is released under the MIT License.

Metadata

Release files for fastpeaks 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastpeaks 0.1.0
File Size Uploaded Provenance
fastpeaks-0.1.0.tar.gz 17.1 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastpeaks 0.1.0
File Interpreter ABI Platform
fastpeaks-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.0 kB

Release files / fastpeaks-0.1.0.tar.gz

Download URL fastpeaks-0.1.0.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5d11f6c9ae5ba2e3f2f5a0f3a9386327336ee6a3d4c0719710c3be9e0d9d1902
BLAKE2b-256 checksum
How to use checksums
88e4b366bb28910a25ef6707479b6b09f931c69974764f7bef0838ce5394558f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.

Transparency log

Release files / fastpeaks-0.1.0-py3-none-any.whl

Download URL fastpeaks-0.1.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a64e38ce16b71d8301f959a93dbe7d7fc06eaa2032635392fbfe644c8720bdee
BLAKE2b-256 checksum
How to use checksums
c9ec94cafa15e39e0d3690bfe47c229f3654298b491f9db536ef24ffbff52285
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.

Transparency log

Security for fastpeaks 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastpeaks-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page