Skip to content

Commit 7def7df

Browse files
amstanmgeier
authored andcommitted
Add RingBuffer class
See spatialaudio#3.
1 parent 2e7c292 commit 7def7df

1 file changed

Lines changed: 245 additions & 0 deletions

File tree

‎jack.py‎

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,35 @@
200200
void jack_set_info_function(void (*func)(const char*));
201201
void jack_free(void* ptr);
202202
203+
/* ringbuffer.h */
204+
205+
typedef struct {
206+
char* buf;
207+
size_t len;
208+
} jack_ringbuffer_data_t;
209+
typedef struct {
210+
char* buf;
211+
volatile size_t write_ptr;
212+
volatile size_t read_ptr;
213+
size_t size;
214+
size_t size_mask;
215+
int mlocked;
216+
} jack_ringbuffer_t;
217+
jack_ringbuffer_t* jack_ringbuffer_create(size_t sz);
218+
void jack_ringbuffer_free(jack_ringbuffer_t* rb);
219+
void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t* rb, jack_ringbuffer_data_t* vec);
220+
void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t* rb, jack_ringbuffer_data_t* vec);
221+
size_t jack_ringbuffer_read(jack_ringbuffer_t* rb, char* dest, size_t cnt);
222+
size_t jack_ringbuffer_peek(jack_ringbuffer_t* rb, char* dest, size_t cnt);
223+
void jack_ringbuffer_read_advance(jack_ringbuffer_t* rb, size_t cnt);
224+
size_t jack_ringbuffer_read_space(const jack_ringbuffer_t* rb);
225+
int jack_ringbuffer_mlock(jack_ringbuffer_t* rb);
226+
void jack_ringbuffer_reset(jack_ringbuffer_t* rb);
227+
void jack_ringbuffer_reset_size (jack_ringbuffer_t* rb, size_t sz);
228+
size_t jack_ringbuffer_write(jack_ringbuffer_t* rb, const unsigned char* src, size_t cnt);
229+
void jack_ringbuffer_write_advance(jack_ringbuffer_t* rb, size_t cnt);
230+
size_t jack_ringbuffer_write_space(const jack_ringbuffer_t* rb);
231+
203232
/* transport.h */
204233
205234
/* TODO: jack_release_timebase */
@@ -1877,6 +1906,222 @@ def clear(self):
18771906
self._portlist[0].unregister()
18781907

18791908

1909+
class RingBuffer(object):
1910+
"""JACK's lock-free ringbuffer.
1911+
1912+
These are a good way to pass data between threads, when streaming
1913+
realtime data to slower media, like audio file playback or recording
1914+
1915+
"""
1916+
1917+
def __init__(self, size):
1918+
"""Allocates a ringbuffer data structure of a specified size.
1919+
1920+
Parameters
1921+
----------
1922+
size : int
1923+
Size in bytes of the ringbuffer. JACK will declare a buffer of at
1924+
least this size, but one of the spaces is reserved. Use
1925+
:meth:`write_space` to determine the actual size available
1926+
for writing.
1927+
1928+
"""
1929+
ptr = _lib.jack_ringbuffer_create(size)
1930+
if not ptr:
1931+
raise JackError("Could not create ringbuffer")
1932+
self._ptr = _ffi.gc(ptr, _lib.jack_ringbuffer_free)
1933+
1934+
def read(self, size):
1935+
"""Read data from the ringbuffer.
1936+
1937+
Parameters
1938+
----------
1939+
size : int
1940+
Number of bytes to read.
1941+
1942+
Returns
1943+
-------
1944+
buffer
1945+
A python buffer object containing the data requested.
1946+
Note that the buffer might contain less bytes than requested
1947+
if the buffer has none left.
1948+
1949+
"""
1950+
data = _ffi.new("unsigned char[]", size)
1951+
size = _lib.jack_ringbuffer_read(self._ptr, data, size)
1952+
return _ffi.buffer(data, size)
1953+
1954+
def peek(self, size):
1955+
"""Peek at data from the ringbuffer.
1956+
1957+
Opposed to :meth:`read` this function does not move the read pointer.
1958+
Thus it's a convenient way to inspect data in the ringbuffer in a
1959+
continous fashion. The price is that the data is copied into a user
1960+
provided buffer. For "raw" non-copy inspection of the data in the
1961+
ringbuffer use :attr:`read_buffers`.
1962+
1963+
Parameters
1964+
----------
1965+
size : int
1966+
Number of bytes to peek.
1967+
1968+
Returns
1969+
-------
1970+
A python buffer object containing at the data requested.
1971+
Note that the buffer might contain less bytes than requested
1972+
if the buffer has none left.
1973+
1974+
"""
1975+
data = _ffi.new("unsigned char[]", size)
1976+
size = _lib.jack_ringbuffer_peek(self._ptr, data, size)
1977+
return _ffi.buffer(data, size)
1978+
1979+
def write(self, data):
1980+
"""Write data to the ringbuffer.
1981+
1982+
Parameters
1983+
----------
1984+
data : buffer
1985+
Bytes to be written to the ringbuffer.
1986+
1987+
Returns
1988+
-------
1989+
The number of bytes writen, which could be less than the length
1990+
of `data` if there was no space left.
1991+
1992+
"""
1993+
try:
1994+
data = _ffi.from_buffer(data)
1995+
except AttributeError:
1996+
pass # from_buffer() not supported
1997+
except TypeError:
1998+
pass # input is not a buffer
1999+
return _lib.jack_ringbuffer_write(self._ptr, data, len(data))
2000+
2001+
def read_advance(self, size):
2002+
"""Advance the read pointer.
2003+
2004+
After data has been read from the ringbuffer using the python buffer
2005+
objects returned by :attr:`read_buffers` or :meth:`peek`, use this
2006+
method to advance the buffer pointers, making that space available for
2007+
future write operations.
2008+
2009+
Parameters
2010+
----------
2011+
size : int
2012+
The number of bytes to advance.
2013+
2014+
"""
2015+
_lib.jack_ringbuffer_read_advance(self._ptr, size)
2016+
2017+
def write_advance(self, size):
2018+
"""Advance the write pointer.
2019+
2020+
After data has been written the ringbuffer using the python buffer
2021+
objects returned by :attr:`write_buffers`, use this method to
2022+
advance the buffer pointer, making the data available for future
2023+
read operations.
2024+
2025+
Parameters
2026+
----------
2027+
size : int
2028+
The number of bytes to advance.
2029+
2030+
"""
2031+
_lib.jack_ringbuffer_write_advance(self._ptr, size)
2032+
2033+
def mlock(self):
2034+
"""Lock a ringbuffer data block into memory.
2035+
2036+
Uses the mlock() system call.
2037+
This prevents the RingBuffer from being paged to swap.
2038+
2039+
This is not a realtime operation.
2040+
2041+
"""
2042+
_check(_lib.jack_ringbuffer_mlock(self._ptr),
2043+
"Error mlocking the RingBuffer data")
2044+
2045+
def reset(self, size=None):
2046+
"""Reset the read and write pointers, making an empty buffer.
2047+
2048+
This is not thread safe.
2049+
2050+
Other Parameters
2051+
----------------
2052+
size : int
2053+
Optional new size for the ringbuffer,
2054+
that must be less than allocated size.
2055+
2056+
"""
2057+
if size is None:
2058+
_lib.jack_ringbuffer_reset(self._ptr)
2059+
else:
2060+
_lib.jack_ringbuffer_reset_size(self._ptr, size)
2061+
2062+
@property
2063+
def read_buffers(self):
2064+
"""Contains two buffer objects that can be read directly.
2065+
2066+
Two are needed because the data to be read may be split across the
2067+
end of the ringbuffer. Either of them could be 0 length.
2068+
2069+
This can be used as a no-copy version of :meth:`peek` or :meth:`read`.
2070+
2071+
After the operation :meth:`read_advance` can be used to advance the
2072+
buffer pointers, making that space available for future write
2073+
operations.
2074+
2075+
Note: After a read pointer moving operation these buffers are no
2076+
longer valid and one should use this property to get new ones.
2077+
2078+
"""
2079+
vectors = _ffi.new("jack_ringbuffer_data_t[2]")
2080+
_lib.jack_ringbuffer_get_read_vector(self._ptr, vectors)
2081+
return (
2082+
_ffi.buffer(vectors[0].buf,vectors[0].len),
2083+
_ffi.buffer(vectors[1].buf,vectors[1].len)
2084+
)
2085+
2086+
@property
2087+
def write_buffers(self):
2088+
"""Contains two buffer objects that can be written to directly.
2089+
2090+
Two are needed because the space available for writing may be split
2091+
across the end of the ringbuffer. Either of them could be 0 length.
2092+
2093+
This can be used as a no-copy version of :meth:`write`.
2094+
2095+
After the operation :meth:`write_advance` can be used to advance the
2096+
buffer pointer, making the data available for future read operations.
2097+
2098+
Note: After a write pointer moving operation these buffers are no
2099+
longer valid and one should use this property to get new ones.
2100+
2101+
"""
2102+
vectors = _ffi.new("jack_ringbuffer_data_t[2]")
2103+
_lib.jack_ringbuffer_get_write_vector(self._ptr, vectors)
2104+
return (
2105+
_ffi.buffer(vectors[0].buf,vectors[0].len),
2106+
_ffi.buffer(vectors[1].buf,vectors[1].len)
2107+
)
2108+
2109+
@property
2110+
def read_space(self):
2111+
"""The number of bytes available for reading."""
2112+
return _lib.jack_ringbuffer_read_space(self._ptr)
2113+
2114+
@property
2115+
def write_space(self):
2116+
"""The number of bytes available for writing."""
2117+
return _lib.jack_ringbuffer_write_space(self._ptr)
2118+
2119+
@property
2120+
def size(self):
2121+
"""The number of bytes in total used by the buffer."""
2122+
return self._ptr.size
2123+
2124+
18802125
def _make_statusbase():
18812126
"""Create base class for Status."""
18822127
import numbers

0 commit comments

Comments
 (0)