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.9.tar.gz (15.4 kB view details)

Uploaded Source

Built Distributions

np_rw_buffer-1.1.9-cp39-cp39-win_amd64.whl (77.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.9 Windows x86

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.8 Windows x86

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

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.7m Windows x86

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

Uploaded CPython 3.6m Windows x86-64

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

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.4m Windows x86-64

np_rw_buffer-1.1.9-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.9.tar.gz.

File metadata

  • Download URL: np_rw_buffer-1.1.9.tar.gz
  • Upload date:
  • Size: 15.4 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.9.tar.gz
Algorithm Hash digest
SHA256 f985b71de0a8c31d3e09942abdf5656a51edf24459971e28da37a8bc3515463e
MD5 80a64661b439ddd873f1008dd5457850
BLAKE2b-256 8bef37af1ff4b88c7c255ff2d15cdc5d82511d48d95ec131b1549be5eb3eba8f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 77.8 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.9-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ee3124577a8ae9785a297f1cc2e1cd2420368d32ef55408fc658a70486c78b99
MD5 7ed003bc8c708300a2e3672f29ba0891
BLAKE2b-256 b91d408a932159ee5c07c1e963b90230cb99ea2b72ba20be1454d8d0fc8f07fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 78da7420b4960493897244fedb5c63cb1239b00141ff21dabfe4e2ff259ab457
MD5 4a8fbc5a49515c0a735ad6de322ce6a9
BLAKE2b-256 16230868601d6b25db6e3edba2b53afcb20dd01f5ef9481c6a46c0a6732dc0a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 112c66b9fb990cbab0edb870556dc3e48fcd375e1e238c781d2464de96a76d68
MD5 54c023f113bf2529ab2293a560274a01
BLAKE2b-256 7796c8e87ba7dc7a1c82e92bf893b3a3ed3838a6f774a40887624bfd2cca2f00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 cf4689a82ee2988851cf78fc8b945473e02ef570ca50407bafceb88576f24f2b
MD5 941057ed9fa76feb0114b2f321b3fdae
BLAKE2b-256 574fcb7f1b6c1d5be5456b5d011a1f85aacdb0250ea7a3f9f9bf3f92db536775

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 b63d8980c2a455e1678247972a1ecdaf2165df8adc1c6b74d137a2698d3885a7
MD5 8218316d335e6a8c33311c61966cf208
BLAKE2b-256 9a1bbda50a6d924e603c03ac3afb7e09a008b0b426d707048275f2789d035c40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 3a1da6837acd2fb78eeb3ab54843627ec71c2f4bb9104502af3830cc794ff7e2
MD5 ad776ac84ab03386ecfb0d33001cf638
BLAKE2b-256 a31891d350472576c3a85840db31c30bc68880c1a2421d930a948964115a4b53

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 b9bd7ee1f4ce5a069f091fe1f6ec5db4cb9b331bdf12dfda79e09243dcfbe154
MD5 3c580d8d0acfe560c7edf6c7c14d06f4
BLAKE2b-256 b6f0d375f2aeebd40a45157f30f80ebcc610fc71a76e4c45b26d5d1236b966e0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 4db6266a2fe3fc3b02aea3d56e6474e962fee18a35053066dba019ffde7f9ac6
MD5 4f9f8855238115bd5b245cca084338b2
BLAKE2b-256 87f5b98daec31539e262f6da30d3f4f3fe345251bc33c59a4c928518fe2ca24b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 1765f6d439e1d6ead19368e5a77b44b251e3451154019bf8c6b6d8b95e13a9d9
MD5 0953775f6b6450634252a29f9164b577
BLAKE2b-256 5c9bfb028c2ef8fe815a06b09c23cf0cd7a2a9c14e0367c32597bef09b0c492c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: np_rw_buffer-1.1.9-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.9-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 1f8650f492517b2ce9c2dc1c5f8cabc6c09116643907317d16ee7d426c513f43
MD5 ff2bca356bfd5099221165d35f741451
BLAKE2b-256 d4419add839a7df85bc80e3ffd30999dce6225ed5245214a710a8ce5971bbbba

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