Skip to content

Commit e5ccae6

Browse files
committed
Split read() method into read() and readinto()
1 parent 61b8c2a commit e5ccae6

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/pa_ringbuffer.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,51 @@ def write(self, data, size=-1):
122122
raise ValueError('data size must be multiple of elementsize')
123123
return self._lib.PaUtil_WriteRingBuffer(self._ptr, data, size)
124124

125-
def read(self, data, size=-1):
126-
"""Read data from the ring buffer.
125+
def read(self, size=-1):
126+
"""Read data from the ring buffer into a new buffer.
127127
128128
Parameters
129129
----------
130-
data : CData pointer or buffer
131-
The memory where the data should be stored.
132130
size : int, optional
133131
The number of elements to be read.
132+
If not specified, all available elements are read.
133+
134+
Returns
135+
-------
136+
buffer
137+
A new buffer containing the read data.
138+
Its size may be less than the requested *size*.
139+
140+
"""
141+
if size < 0:
142+
size = self.read_available
143+
data = self._ffi.new(
144+
'unsigned char[]', size * self._ptr.elementSizeBytes)
145+
size = self.readinto(data)
146+
return self._ffi.buffer(data, size * self._ptr.elementSizeBytes)
147+
148+
def readinto(self, data):
149+
"""Read data from the ring buffer into a user-provided buffer.
150+
151+
Parameters
152+
----------
153+
data : CData pointer or buffer
154+
The memory where the data should be stored.
134155
135156
Returns
136157
-------
137158
int
138-
The number of elements read.
159+
The number of elements read, which may be less than the size
160+
of *data*.
139161
140162
"""
141163
try:
142164
data = self._ffi.from_buffer(data)
143165
except TypeError:
144166
pass # input is not a buffer
145-
if size < 0:
146-
size, rest = divmod(self._ffi.sizeof(data),
147-
self._ptr.elementSizeBytes)
148-
if rest:
149-
raise ValueError('data size must be multiple of elementsize')
167+
size, rest = divmod(self._ffi.sizeof(data), self._ptr.elementSizeBytes)
168+
if rest:
169+
raise ValueError('data size must be multiple of elementsize')
150170
return self._lib.PaUtil_ReadRingBuffer(self._ptr, data, size)
151171

152172
def get_write_buffers(self, size):

0 commit comments

Comments
 (0)