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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file np_rw_buffer-1.1.8.tar.gz
.
File metadata
- Download URL: np_rw_buffer-1.1.8.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aea1ffdd3cb2ce2f4214c185d8b55d738a35a480d0d88974dc3237ab768cdc23 |
|
MD5 | 028ffd0e176462cbf1b68353fec7af15 |
|
BLAKE2b-256 | bda03845f00d785bc87a688f0cde17c2189007372a6bca3aec5f0ea1497e3720 |
File details
Details for the file np_rw_buffer-1.1.8-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 56.8 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e010aa3d83668794a764b73db677b2ec54b843f47f8835dd5b88be104aca6e12 |
|
MD5 | 0468d87bab0fb829eb769bf728177a83 |
|
BLAKE2b-256 | 221fe4db2e707dce979f4188c1b9ef417f844aa413d447f3a9c0d9a310df9462 |
File details
Details for the file np_rw_buffer-1.1.8-cp38-cp38-win32.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp38-cp38-win32.whl
- Upload date:
- Size: 56.1 kB
- Tags: CPython 3.8, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40b31a41fa3966a003b5dae03f245752bb69d122da50a326ba24ad8cc995fbc6 |
|
MD5 | 925294ef50222913e0acf27988123a36 |
|
BLAKE2b-256 | e740f886e7e27616847d176d6079cf3272f05a86ecaf3be5e14d100d839102b9 |
File details
Details for the file np_rw_buffer-1.1.8-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 56.7 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41b719a629a0cf2ee85005b9a340da0d2d9500e64858acda37d7d7906df10242 |
|
MD5 | 014ea14e29f2603edcecafd04cc01141 |
|
BLAKE2b-256 | 57726da21c3cd10b7bc85b50233aa9800b2bcfbffd3603d577ffa3367b5bea2a |
File details
Details for the file np_rw_buffer-1.1.8-cp37-cp37m-win32.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp37-cp37m-win32.whl
- Upload date:
- Size: 50.6 kB
- Tags: CPython 3.7m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b55ae4e9079d000b6fe9718ee9cd509859d2d920c2917a68ea954c0622f0e358 |
|
MD5 | 4fba13c63b8392d061775d23a2f78333 |
|
BLAKE2b-256 | db87266295f894384d2c0f3da02ec81b0df2582df66dc36d51a71e3396e0ce1b |
File details
Details for the file np_rw_buffer-1.1.8-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 39.7 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | efa14d91fde3c518ff38686a55cd9ef59bbfbd2fb0b1ff7e0002ba418f9fd77c |
|
MD5 | 327b146aa953c41755a4ab23d0e9f676 |
|
BLAKE2b-256 | 3d12cfb64709b698535633565675f325f5b5e2c229d02f26936f432ba067121c |
File details
Details for the file np_rw_buffer-1.1.8-cp36-cp36m-win32.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp36-cp36m-win32.whl
- Upload date:
- Size: 45.2 kB
- Tags: CPython 3.6m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d8cb0592b746809ee2b857e385082ae50895b2588c8936315697d5bf085125d |
|
MD5 | 89e6507871e02fddbfb373af10b290a7 |
|
BLAKE2b-256 | af065f17aab8e3ced92ebc690f0ca42e8c7e50b45a17433166c5e83597359426 |
File details
Details for the file np_rw_buffer-1.1.8-cp34-cp34m-win_amd64.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp34-cp34m-win_amd64.whl
- Upload date:
- Size: 54.7 kB
- Tags: CPython 3.4m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48d53e5bcb5bff2b610948d66d2aad633899c6da9c46432ef94b333349eb2415 |
|
MD5 | da8190f53e575afc825482020619ff3d |
|
BLAKE2b-256 | a0e695002367ca280544364a1368fb9f6c11a45e4600314574ea8093019abed7 |
File details
Details for the file np_rw_buffer-1.1.8-cp34-cp34m-win32.whl
.
File metadata
- Download URL: np_rw_buffer-1.1.8-cp34-cp34m-win32.whl
- Upload date:
- Size: 54.9 kB
- Tags: CPython 3.4m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26d8793ad1871a29243772fac16fc0206a2e37a1abdf414039a7a32d4052e108 |
|
MD5 | 8b37f41ec5afef1621ff63d9d2ff8964 |
|
BLAKE2b-256 | 3967de7d5a9aaa567c0b5be0ffa434e54f3e6b99bf370c18125bf632430e4da6 |