Skip to main content

Buffer audio samples into fixed-sized blocks, with overlap

Project description

Python: Buffer audio samples into fixed-sized blocks, with overlap

This small utility package encapsulates a single-consumer, single-producer ringbuffer.

  • Populate the buffer with arrays of arbitrary-length samples
  • Query the buffer, and it returns arrays of a specified fixed length, optionally with overlap between successive blocks

It is designed primarily for applying the short-time Fourier transform (STFT) to successive blocks of an input audio stream (see below for example).

It is safe for usage in real-time audio applications, as no memory allocation or system I/O is done within the extend method.

Usage

To do block-sized buffering with overlap in conjunction with sounddevice:

import sounddevice as sd
import numpy as np
import blockbuffer

block_size = 1024
hop_size = 128

bb = blockbuffer.BlockBuffer(block_size=block_size,
                             hop_size=hop_size)

def input_callback(data, frames, time, status):
    global bb
    bb.extend(data.flatten())
    for block in bb:
        block_windowed = block * np.hanning(block_size)
        block_spectrum = np.fft.rfft(block_windowed)

stream = sd.InputStream(callback=input_callback, channels=1)
stream.start()

Source code

Source code is available on GitHub.

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

blockbuffer-0.0.3.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file blockbuffer-0.0.3.tar.gz.

File metadata

  • Download URL: blockbuffer-0.0.3.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for blockbuffer-0.0.3.tar.gz
Algorithm Hash digest
SHA256 0ca005895105ce0dc409a0d64050a3e39ad0228c5218a82e01116266ee0fba30
MD5 30a1926f11e766f01a78207c02a1e69c
BLAKE2b-256 b5af0ddee3659b5b0f3b59035c69867014c02ba75c9566e534c8d84889fc011e

See more details on using hashes here.

Supported by

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