Skip to main content

wavesurfer

PyPI License

A Python package for audio visualization and playback in Jupyter notebooks.

Features

  • Visualize audio waveforms in Jupyter notebooks
  • Support for various audio formats (WAV, MP3, FLAC, etc.)
  • Streaming audio playback for real-time applications
  • Bounded, throttled waveform previews for long-running streams
  • Programmatic control with play/pause functionality
  • Performance monitoring with latency and RTF metrics
  • Display alignment information on waveforms

Installation

pip install wavesurfer

Usage

Basic Playback

Play a wave file directly:

from wavesurfer import play

play("assets/test_16k.wav")

Play waveform data:

from audiolab import load_audio
from wavesurfer import play

audio, rate = load_audio("assets/test_16k.wav")
player = play(audio, sample_rate=rate)

Displaying Alignments

Display alignment information on the waveform:

from wavesurfer import play

# Play with alignment information from a TextGrid file
player = play(
    "assets/test_16k.wav",
    alignments="assets/test_16k.TextGrid",
    config={"options": {"normalize": True}},
)

You can also provide alignments as a list of alignment items:

from wavesurfer import play

# Create alignment items
alignments = [
    {"start": 0.0, "end": 0.5, "content": "hello"},
    {"start": 0.5, "end": 1.0, "content": "world"},
]

# Play with alignment information
player = play("assets/test_16k.wav", alignments=alignments)

Streaming Playback

Play streaming waveform data:

import time
from audiolab import load_audio
from wavesurfer import play

def audio_generator():
    frame_size = int(0.3 * 16000)
    for frame, _ in load_audio("assets/test_16k.wav", frame_size=frame_size):
        time.sleep(0.1)  # RTF: 0.1 / 0.3 < 1
        yield frame

player = play(audio_generator(), sample_rate=16000)

Streams may also yield (chunk, sample_rate) pairs, which is useful when the rate is discovered while producing the audio. A stream must keep the same sample rate throughout.

Async generators can be observed, cancelled, and cleaned up explicitly:

player = play(async_audio_generator(), sample_rate=16000)
await player.wait()

# Or stop ingestion early:
player.cancel()
player.close()

Streaming keeps only a bounded window for waveform previews. Full audio is retained for download by default but generated as a WAV only when requested. This behavior is configurable:

player = play(
    audio_generator(),
    sample_rate=16000,
    config={
        "streaming": {
            "previewSeconds": 20,
            "waveformRefreshInterval": 1000,
            "retainAudio": False,
        }
    },
)

Programmatic Control

For more advanced usage, you can use the Player class directly to have programmatic control over playback:

from wavesurfer import Player

# Create a player instance
player = Player()

# Load audio
player.load("assets/test_16k.wav")

# Programmatically control playback
player.play()   # Start playback
player.pause()  # Pause playback
player.close()  # Release timers, audio contexts, URLs, and browser objects

The Player class also supports all the audio formats that the play function supports, including file paths, waveform data, and streaming generators.

play() returns the Player, so the shorter API can also be used with programmatic controls. File inputs detect their sample rate automatically; NumPy arrays require sample_rate.

Player is also a context manager when deterministic cleanup is convenient:

with Player() as player:
    player.load("assets/test_16k.wav")

Alignment Models

Dictionary alignments accept either start/end/content or start/duration/symbol. You can also use the explicit Region model:

from wavesurfer import Region, play

regions = [Region(start=0.0, end=0.5, content="hello")]
play("assets/test_16k.wav", alignments=regions)

Overlapping regions can be combined with concatenate_overlaps=True, or matching labels can be merged with merge_matching=True.

For TextGrid files with multiple tiers, select one by name or index:

play("audio.wav", alignments="alignment.TextGrid", alignment_tier="words")

Development

Install the test dependencies and run the suite with:

pip install -e ".[test]"
pytest
node --test tests/js/*.test.js

WaveSurfer.js is vendored so a fresh clone works without a network download. Maintainers can refresh the pinned browser assets with bash scripts/update_vendor_assets.sh.

License

BSD 2-Clause License

Metadata

Release files for wavesurfer 0.3.9

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

Source distribution (sdist)

Source distribution for wavesurfer 0.3.9
File Size Uploaded
wavesurfer-0.3.9.tar.gz 96.3 kB Details

Built distribution (wheel)

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

Total release size: 191.8 kB

Release files / wavesurfer-0.3.9.tar.gz

Download URL wavesurfer-0.3.9.tar.gz
Size 96.3 kB
Tags Source
SHA-256 checksum
How to use checksums
df663f305abd437efd570df79cf0b46ca1cb86b54d71c7af4559024d8576ca07
BLAKE2b-256 checksum
How to use checksums
d4bce34ab540d6b683197a0297adb094d7ec019e47e3c3b398fb07a0d05b1558
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release files / wavesurfer-0.3.9-py3-none-any.whl

Download URL wavesurfer-0.3.9-py3-none-any.whl
Size 95.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17fe610b68d4a64f65ca153046a668445b81d19ca90e64c0214be83a078dc6bf
BLAKE2b-256 checksum
How to use checksums
a634dbabba9f1f43abe8995ed4c9b28a686a5c81f34c472ebbe53fbba66971e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release history Release notifications | RSS feed

This release

0.3.9 This release

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.2.9

1 release file

0.2.8

1 release file

0.2.7

1 release file

0.2.6

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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