Skip to main content

Lightweight audio seeking across common formats: O(1) for uncompressed PCM (WAV/AIFF), best-effort frame-level seek for compressed formats (MP3/FLAC/OGG).

Project description

Audio Seek

PyPI version Python Version License

A lightweight Python library for reading arbitrary time slices out of common audio files without loading them fully — with minimal dependencies (numpy + soundfile).

Key Features

  • Zero Waste: Reads and decodes only the requested time slice, not the whole file.
  • Usual Formats: Supports WAV, AIFF, FLAC, OGG/Vorbis, and MP3 via soundfile/libsndfile.
  • O(1) Where Possible: Seeking into uncompressed PCM (WAV/AIFF) is a direct sample-offset seek, O(1). Compressed formats (MP3/FLAC/OGG) are frame/entropy coded, so seeking there is best-effort at frame granularity, not sample-exact.
  • Complexity Introspection: AudioSeek.seek_complexity() tells you which regime a given file falls into.
  • Minimal Dependencies: Just numpy and soundfile — no bundled codecs, no JIT toolchains.

Installation

pip install audio-seek

Quick Start

Read a Specific Audio Segment

from audio_seek import read_audio_segment

# Read 5 seconds starting at 2 minutes
segment = read_audio_segment(
    file_path="long_audio.wav",
    start_sec=120.0,
    duration_sec=5.0,
)
# Returns a numpy float32 array of shape (sample_rate * duration_sec,)

Get Duration Without Loading Audio

from audio_seek import AudioSeek

# Only reads the header, doesn't decode audio data
duration = AudioSeek.get_duration("audio.mp3")
print(f"Duration: {duration:.2f}s")

Check Seek Complexity

from audio_seek import AudioSeek

AudioSeek.seek_complexity("audio.wav")  # "O(1)"  — uncompressed PCM
AudioSeek.seek_complexity("audio.mp3")  # "O(n)"  — frame/entropy coded

Extract a Segment to a New WAV File

from audio_seek import AudioSeek

AudioSeek.read_segment_to_file(
    file_path="input.mp3",
    start_sec=10.0,
    duration_sec=15.0,
    output_path="clip.wav",  # written as PCM_16 WAV
)

Mix Multi-Channel Audio Down to Mono

from audio_seek import ensure_mono

mono = ensure_mono(stereo_data, style="soundfile")  # (samples, channels) -> (samples,)

How It Works

audio-seek wraps soundfile/libsndfile and seeks directly via SoundFile.seek():

  • For uncompressed PCM (WAV, AIFF), each sample occupies a fixed number of bytes, so seeking is a direct file-offset computation — true O(1), sample-accurate.
  • For compressed formats (MP3, FLAC, OGG/Vorbis), audio is encoded in frames/blocks of varying or fixed sample counts, sometimes with decoder warm-up requirements. Seeking there lands on the nearest frame boundary and is not guaranteed sample-exact — closer to O(n) in the worst case.

Use Cases

Ideal for applications requiring low-latency access to specific segments of long audio recordings:

  • Machine Learning: Dataset slicing without loading entire files
  • Web Services: Real-time audio segment delivery
  • Audio Processing: Random access pipelines

Requirements

  • Python >= 3.11
  • numpy
  • soundfile

Testing

pytest tests/ -v

License

Apache License 2.0 - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

audio_seek-0.2.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

audio_seek-0.2.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file audio_seek-0.2.0.tar.gz.

File metadata

  • Download URL: audio_seek-0.2.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Darwin/25.3.0

File hashes

Hashes for audio_seek-0.2.0.tar.gz
Algorithm Hash digest
SHA256 04c716c05e84e246915fd669c09864a3019bde3f014e5c4acee715293eac5f34
MD5 129bf08e63285554579da01b792dadd4
BLAKE2b-256 fc8344bbad005513cbc85f1876b9c38aff74d60cd0fc62ab1ea8fd76c805b655

See more details on using hashes here.

File details

Details for the file audio_seek-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: audio_seek-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Darwin/25.3.0

File hashes

Hashes for audio_seek-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10ac0b407203b27d91f9b07067c942daf1b69cdffd3b3a819b6a7b94ba257f84
MD5 f456876bee991fd823884362a7c4088a
BLAKE2b-256 ea5d4a4b71fed3e378376da93e4f9061571acccdf5028456ce3bcbbff022f9c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page