Skip to content

Commit 9e58877

Browse files
committed
RingBuffer docs update
1 parent 015bb74 commit 9e58877

1 file changed

Lines changed: 90 additions & 57 deletions

File tree

‎jack.py‎

Lines changed: 90 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,28 +1908,36 @@ def clear(self):
19081908

19091909

19101910
class RingBuffer(object):
1911-
"""JACK's lock-free ringbuffer.
19121911

1913-
These are a good way to pass data between threads, when streaming
1914-
realtime data to slower media, like audio file playback or recording
1915-
1916-
"""
1912+
"""JACK's lock-free ringbuffer."""
19171913

19181914
def __init__(self, size):
1919-
"""Allocates a ringbuffer data structure of a specified size.
1915+
"""Create a lock-free ringbuffer.
1916+
1917+
A ringbuffer is a good way to pass data between threads
1918+
(e.g. between the main program and the process callback),
1919+
when streaming realtime data to slower media, like audio file
1920+
playback or recording.
1921+
1922+
The key attribute of a ringbuffer is that it can be safely
1923+
accessed by two threads simultaneously -- one reading from the
1924+
buffer and the other writing to it -- without using any
1925+
synchronization or mutual exclusion primitives. For this to
1926+
work correctly, there can only be a single reader and a single
1927+
writer thread. Their identities cannot be interchanged.
19201928
19211929
Parameters
19221930
----------
19231931
size : int
1924-
Size in bytes of the ringbuffer. JACK will declare a buffer of at
1925-
least this size, but one of the spaces is reserved. Use
1926-
:meth:`write_space` to determine the actual size available
1927-
for writing.
1932+
Size in bytes. JACK will allocate a buffer of at least this
1933+
size (rounded up to the next power of 2), but one byte is
1934+
reserved for internal use. Use :attr:`write_space` to
1935+
determine the actual size available for writing.
19281936
19291937
"""
19301938
ptr = _lib.jack_ringbuffer_create(size)
19311939
if not ptr:
1932-
raise JackError("Could not create ringbuffer")
1940+
raise JackError("Could not create RingBuffer")
19331941
self._ptr = _ffi.gc(ptr, _lib.jack_ringbuffer_free)
19341942

19351943
def read(self, size):
@@ -1943,9 +1951,13 @@ def read(self, size):
19431951
Returns
19441952
-------
19451953
buffer
1946-
A python buffer object containing the data requested.
1947-
Note that the buffer might contain less bytes than requested
1948-
if the buffer has none left.
1954+
A buffer object containing the requested data.
1955+
If no more data is left (see :attr:`read_space`), a smaller
1956+
(or even empty) buffer is returned.
1957+
1958+
See Also
1959+
--------
1960+
peek, :attr:`read_space`, :attr:`read_buffers`
19491961
19501962
"""
19511963
data = _ffi.new("unsigned char[]", size)
@@ -1955,10 +1967,11 @@ def read(self, size):
19551967
def peek(self, size):
19561968
"""Peek at data from the ringbuffer.
19571969
1958-
Opposed to :meth:`read` this function does not move the read pointer.
1959-
Thus it's a convenient way to inspect data in the ringbuffer in a
1960-
continous fashion. The price is that the data is copied into a user
1961-
provided buffer. For "raw" non-copy inspection of the data in the
1970+
Opposed to :meth:`read` this function does not move the read
1971+
pointer. Thus it's a convenient way to inspect data in the
1972+
ringbuffer in a continuous fashion.
1973+
The price is that the data is copied into a newly allocated
1974+
buffer. For "raw" non-copy inspection of the data in the
19621975
ringbuffer use :attr:`read_buffers`.
19631976
19641977
Parameters
@@ -1968,27 +1981,38 @@ def peek(self, size):
19681981
19691982
Returns
19701983
-------
1971-
A python buffer object containing at the data requested.
1972-
Note that the buffer might contain less bytes than requested
1973-
if the buffer has none left.
1984+
buffer
1985+
A buffer object containing the requested data.
1986+
If no more data is left (see :attr:`read_space`), a smaller
1987+
(or even empty) buffer is returned.
1988+
1989+
See Also
1990+
--------
1991+
read, :attr:`read_space`, :attr:`read_buffers`
19741992
19751993
"""
19761994
data = _ffi.new("unsigned char[]", size)
19771995
size = _lib.jack_ringbuffer_peek(self._ptr, data, size)
19781996
return _ffi.buffer(data, size)
19791997

19801998
def write(self, data):
1981-
"""Write data to the ringbuffer.
1999+
"""Write data into the ringbuffer.
19822000
19832001
Parameters
19842002
----------
1985-
data : buffer
2003+
data : buffer or bytes or iterable of int
19862004
Bytes to be written to the ringbuffer.
19872005
19882006
Returns
19892007
-------
1990-
The number of bytes writen, which could be less than the length
1991-
of `data` if there was no space left.
2008+
int
2009+
The number of bytes written, which could be less than the
2010+
length of `data` if there was no more space left
2011+
(see :attr:`write_space`).
2012+
2013+
See Also
2014+
--------
2015+
:attr:`write_space`, :attr:`write_buffers`
19922016
19932017
"""
19942018
try:
@@ -2002,10 +2026,10 @@ def write(self, data):
20022026
def read_advance(self, size):
20032027
"""Advance the read pointer.
20042028
2005-
After data has been read from the ringbuffer using the python buffer
2006-
objects returned by :attr:`read_buffers` or :meth:`peek`, use this
2007-
method to advance the buffer pointers, making that space available for
2008-
future write operations.
2029+
After data has been read from the ringbuffer using
2030+
:attr:`read_buffers` or :meth:`peek`, use this method to advance
2031+
the buffer pointers, making that space available for future
2032+
write operations.
20092033
20102034
Parameters
20112035
----------
@@ -2018,10 +2042,9 @@ def read_advance(self, size):
20182042
def write_advance(self, size):
20192043
"""Advance the write pointer.
20202044
2021-
After data has been written the ringbuffer using the python buffer
2022-
objects returned by :attr:`write_buffers`, use this method to
2023-
advance the buffer pointer, making the data available for future
2024-
read operations.
2045+
After data has been written to the ringbuffer using
2046+
:attr:`write_buffers`, use this method to advance the buffer
2047+
pointer, making the data available for future read operations.
20252048
20262049
Parameters
20272050
----------
@@ -2034,10 +2057,10 @@ def write_advance(self, size):
20342057
def mlock(self):
20352058
"""Lock a ringbuffer data block into memory.
20362059
2037-
Uses the mlock() system call.
2038-
This prevents the RingBuffer from being paged to swap.
2060+
Uses the ``mlock()`` system call. This prevents the
2061+
ringbuffer's memory from being paged to the swap area.
20392062
2040-
This is not a realtime operation.
2063+
.. note:: This is not a realtime operation.
20412064
20422065
"""
20432066
_check(_lib.jack_ringbuffer_mlock(self._ptr),
@@ -2046,13 +2069,13 @@ def mlock(self):
20462069
def reset(self, size=None):
20472070
"""Reset the read and write pointers, making an empty buffer.
20482071
2049-
This is not thread safe.
2072+
.. note:: This is not thread safe.
20502073
2051-
Other Parameters
2052-
----------------
2053-
size : int
2054-
Optional new size for the ringbuffer,
2055-
that must be less than allocated size.
2074+
Parameters
2075+
----------
2076+
size : int, optional
2077+
The new size for the ringbuffer.
2078+
Must be less than allocated size.
20562079
20572080
"""
20582081
if size is None:
@@ -2064,17 +2087,18 @@ def reset(self, size=None):
20642087
def read_buffers(self):
20652088
"""Contains two buffer objects that can be read directly.
20662089
2067-
Two are needed because the data to be read may be split across the
2068-
end of the ringbuffer. Either of them could be 0 length.
2090+
Two are needed because the data to be read may be split across
2091+
the end of the ringbuffer. Either of them could be 0 length.
20692092
2070-
This can be used as a no-copy version of :meth:`peek` or :meth:`read`.
2093+
This can be used as a no-copy version of :meth:`peek` or
2094+
:meth:`read`.
20712095
2072-
After the operation :meth:`read_advance` can be used to advance the
2073-
buffer pointers, making that space available for future write
2074-
operations.
2096+
When finished with reading, :meth:`read_advance` should be used.
20752097
2076-
Note: After a read pointer moving operation these buffers are no
2077-
longer valid and one should use this property to get new ones.
2098+
.. note:: After an operation that changes the read pointer
2099+
(:meth:`read`, :meth:`read_advance`, :meth:`reset`), the
2100+
buffers are no longer valid and one should use this property
2101+
again to get new ones.
20782102
20792103
"""
20802104
vectors = _ffi.new("jack_ringbuffer_data_t[2]")
@@ -2088,16 +2112,19 @@ def read_buffers(self):
20882112
def write_buffers(self):
20892113
"""Contains two buffer objects that can be written to directly.
20902114
2091-
Two are needed because the space available for writing may be split
2092-
across the end of the ringbuffer. Either of them could be 0 length.
2115+
Two are needed because the space available for writing may be
2116+
split across the end of the ringbuffer. Either of them could be
2117+
0 length.
20932118
20942119
This can be used as a no-copy version of :meth:`write`.
20952120
2096-
After the operation :meth:`write_advance` can be used to advance the
2097-
buffer pointer, making the data available for future read operations.
2121+
When finished with writing, :meth:`write_advance` should be
2122+
used.
20982123
2099-
Note: After a write pointer moving operation these buffers are no
2100-
longer valid and one should use this property to get new ones.
2124+
.. note:: After an operation that changes the write pointer
2125+
(:meth:`write`, :meth:`write_advance`, :meth:`reset`), the
2126+
buffers are no longer valid and one should use this property
2127+
again to get new ones.
21012128
21022129
"""
21032130
vectors = _ffi.new("jack_ringbuffer_data_t[2]")
@@ -2119,7 +2146,13 @@ def write_space(self):
21192146

21202147
@property
21212148
def size(self):
2122-
"""The number of bytes in total used by the buffer."""
2149+
"""The number of bytes in total used by the buffer.
2150+
2151+
See Also
2152+
--------
2153+
:attr:`read_space`, :attr:`write_space`
2154+
2155+
"""
21232156
return self._ptr.size
21242157

21252158

0 commit comments

Comments
 (0)