Skip to main content

Library to help store audio data in a circular buffer.

Project description

This library was created to help store audio data in a numpy array. It allows for writing lists and numpy arrays into a circular buffer. You can read the data from the buffer with overlap to perform smooth FFTs. The buffer is a wrapper around a numpy ndarray. It contains a start and end position to keep track of where to read and write the data.

Main Functions:
  • clear() - Clear the length, start, and end indexes

  • get_data() - Return a copy of the data without moving indexes

  • set_data(data) - Set the data and change the shape of the buffer to this data shape

  • write(data, error) - Write data into the buffer and move the end index

  • read(amount) - Read data from the buffer and move the start index. If the amount is greater that what is in the buffer return a 0 length buffer

Extra Functions to help with the start and end pointers:
  • expanding_write(data, error) - Write data into the buffer. If the data is larger than the buffer expand the buffer

  • growing_write(data) - Write data into the buffer if there is not enough space make the buffer larger

  • write_value(value, length, error=True, move_start=True) - Write a value into the buffer for the given length. This is more efficient then creating and writing an array of a single value.

  • write_zeros(length, error=True, move_start=True) - Write Zeros for the given length. This is more efficient then creating and writing an array of zeros.

  • read_remaining(amount) - Read the amount or read all of the data available to read

  • read_overlap(amount, increment) - Read the amount of data given, but only increment the start index by the increment amount. This makes the next read, read some duplicate data (hence overlap)

Buffer Control Functions:
  • maxsize - (property) change the amount of samples that can be held

  • columns - (property) Number of columns that the array contains (shape[1])

  • shape - (property) Change the shape of the buffer

  • dtype - (property) Change the data type for the numpy buffer

  • get_indexes(start, length) - Return a list of indexes for reading and writing (this makes the buffer circular)

  • move_start(amount, error) - Move the start index (read)

  • move_end(amount, error) - Move the end index (write)

  • get_available_space() - return the amount of data that the buffer can still hold

Example - simple example

Simple reading and writing. See test_buffer for tests and usage.

import numpy as np
import np_rw_buffer

buffer = np_rw_buffer.RingBuffer(10)

buffer.write(np.arange(5))
r = buffer.read(4)
assert np.all(r == np.arange(4).reshape((-1, 1)))

# Not enough data, don't read anything (use read_remaining or get_data)
d = np.arange(5).reshape((-1, 1))
buffer.write(d)
r = buffer.read(10)
assert len(r) == 0
assert len(buffer) == 6

r = buffer.read()
assert len(r) == 6
assert np.all(r == np.vstack((d[-1:], d)))

buffer.write(np.arange(6))
# buffer.write(np.arange(5))  # Raises an OverflowError
buffer.write(np.arange(5), False)

Example - AudioFramingBuffer

The AudioFramingBuffer is slightly different from the RingBuffer. It has a sample_rate, seconds, and buffer_delay.

It’s main differences are how it reads and writes. The start and end pointers are completely different and decoupled. The start pointer can underrun the end pointer and back fills with 0’s. The end pointer can overrun the start pointer.

import numpy as np
from np_rw_buffer import AudioFramingBuffer

buffer = AudioFramingBuffer(2000, 1)
buffer.write(np.array([(i,) for i in range(10)]))
# Buffer: [(read ptr)0, 1, 2, 3, 4, 5, 6, 7, 8, 9, (write ptr) 0, 0, 0, 0, 0]
assert buffer._end == 10
assert buffer._start == 0

# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, (write ptr) 0, 0, 0, 0, 0] (read ptr at end)
assert np.all(buffer.read(15) == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0]).reshape((-1, 1)))
assert buffer._start == 15
assert buffer._end == 10

buffer.write(np.array([(i,) for i in range(10)])) # This will write in the position after 19
# Buffer: [0, 0, 0, 0, 0, 0, 0, 0, 0, (was 9) 0, 0, 1, 2, 3, 4, (read ptr) 5, 6, 7, 8, 9] (write ptr at end)
assert buffer._end == 20
assert buffer._start == 15

# [5, 6, 7, 8, 9, (write ptr) 0, 0, 0, 0, 0] (read ptr at end)
assert np.all(buffer.read(10) == np.array([5, 6, 7, 8, 9, 0, 0, 0, 0, 0]).reshape((-1, 1)))

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

np_rw_buffer-1.1.10.tar.gz (13.6 kB view details)

Uploaded Source

Built Distributions

np_rw_buffer-1.1.10-cp39-cp39-win_amd64.whl (77.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

np_rw_buffer-1.1.10-cp39-cp39-win32.whl (77.1 kB view details)

Uploaded CPython 3.9 Windows x86

np_rw_buffer-1.1.10-cp38-cp38-win_amd64.whl (71.6 kB view details)

Uploaded CPython 3.8 Windows x86-64

np_rw_buffer-1.1.10-cp38-cp38-win32.whl (77.1 kB view details)

Uploaded CPython 3.8 Windows x86

np_rw_buffer-1.1.10-cp37-cp37m-win_amd64.whl (77.7 kB view details)

Uploaded CPython 3.7m Windows x86-64

np_rw_buffer-1.1.10-cp37-cp37m-win32.whl (71.6 kB view details)

Uploaded CPython 3.7m Windows x86

np_rw_buffer-1.1.10-cp36-cp36m-win_amd64.whl (71.6 kB view details)

Uploaded CPython 3.6m Windows x86-64

np_rw_buffer-1.1.10-cp36-cp36m-win32.whl (71.6 kB view details)

Uploaded CPython 3.6m Windows x86

np_rw_buffer-1.1.10-cp34-cp34m-win_amd64.whl (75.7 kB view details)

Uploaded CPython 3.4m Windows x86-64

np_rw_buffer-1.1.10-cp34-cp34m-win32.whl (75.9 kB view details)

Uploaded CPython 3.4m Windows x86

File details

Details for the file np_rw_buffer-1.1.10.tar.gz.

File metadata

  • Download URL: np_rw_buffer-1.1.10.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10.tar.gz
Algorithm Hash digest
SHA256 660026be6835cef90b93bdac9486722e21c97ad20ef549f49048a8bd84bf0d73
MD5 80f29461df5dfedb9af090c2bb09f538
BLAKE2b-256 020e76a93031023677d16e42c144af6822dca762df3b5d8d368c4400c0ae0e17

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 77.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8fca05889b79cefdd598371b8fdddcfdbd193d1ee3f797f6c9464471862b6ce0
MD5 a4e0eadc82df75d3dfc927ca41a6b8f2
BLAKE2b-256 f0f96726e16c9268b22ffde5217d571cb495ff38ca4ffa5b7e88bfc717b1ef87

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp39-cp39-win32.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp39-cp39-win32.whl
  • Upload date:
  • Size: 77.1 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 ca7da71584f79541bad8d51121bc4c07129b9e063e19440026e051bd30b1de3b
MD5 765395dbfb4517295d1e4d9ece4e2f1b
BLAKE2b-256 8a10ba159d2a2effdecccf26859ae7db2630ba209971d42a2c6aab203ea2b6f8

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 71.6 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f189a84ba3b6878eb11c00a8927a96fef9ae658faa4607230388b5126e94b6bd
MD5 020a27f7fce82a774811f760424e4b29
BLAKE2b-256 1e9c2c8197841ffd20967977f587bad2c8329ff9bf3f2ab5a7be40452c98b6d6

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp38-cp38-win32.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp38-cp38-win32.whl
  • Upload date:
  • Size: 77.1 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 ad0fcf4cf80e99d2b566690aa494803f5f02ecdac56677da7dc6c77dc0a4eca8
MD5 d7c7b6e62ca8d25ee029f02503a63ab6
BLAKE2b-256 bd5bd75e2ddd78e92c84f5b862a6d5ce40d6aa9ef7ceb3470dfac73fb9c5860d

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 77.7 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 12d1b5e29d50791f4c333eb1f00feb20be23f1a145aa21d9de1eaf354a256225
MD5 06d26a3ffc983bbf0c6bbea7a2084092
BLAKE2b-256 50ff037cdf9097ea250110459dafb067ee8c710506178588c595d55617a4b70a

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp37-cp37m-win32.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 71.6 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 dddc004c93f45eb1882d94e394307efa9e9f79a8785a43ed3902789481bf47e3
MD5 3fa800039a1370d4528de878de648e0a
BLAKE2b-256 c3a662a96fec94f0c9baa952f8c83962f644523338f794c8d18dfd6a892e5034

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 71.6 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 549ff33a045c0435bbf5e38519a8a45ce72fe75dbdb383daea955b947baa7f0e
MD5 bafa29e1279ecdf5573ae45f5ed947a0
BLAKE2b-256 dd5ba8d43cb79e853b61fee02fe0aa58422980273730def173be8a957777f528

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp36-cp36m-win32.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 71.6 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 4ecc2ab125c01eacddf22c7a7dfec29eeecac19a609aede3f9d2162d7ad9f356
MD5 63f536d044e586101f1bfd85a310a91d
BLAKE2b-256 4b553b442638fc0ca844a895ab49081814c6f26cb5b4431b60ac36f5570e499d

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp34-cp34m-win_amd64.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp34-cp34m-win_amd64.whl
  • Upload date:
  • Size: 75.7 kB
  • Tags: CPython 3.4m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 7d389951df349eb6ab466780b6cbcdc38e2f714050836b01f099dbae0670c8d4
MD5 78280ba9ce2ea89d1d29ae23ad222a9f
BLAKE2b-256 6ae66c706400a0522f2a351d39609b2e552535626f47df90f0135524af1cc88f

See more details on using hashes here.

File details

Details for the file np_rw_buffer-1.1.10-cp34-cp34m-win32.whl.

File metadata

  • Download URL: np_rw_buffer-1.1.10-cp34-cp34m-win32.whl
  • Upload date:
  • Size: 75.9 kB
  • Tags: CPython 3.4m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for np_rw_buffer-1.1.10-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 15afffe4cec948475cbf194bef3e05905b4f31dd5f2cbe8aa165dc0fffe8f61c
MD5 b183aeeac4dde676dd6eaf09c9768ef8
BLAKE2b-256 8d800b20e524d70bb8459cff8c96e76a4c02a9a9fcba4cf1327f038f2043092d

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