Skip to content

Commit 626fcb0

Browse files
committed
Less obscure, slightly more complicated but maybe faster implementation
1 parent 8a8ccec commit 626fcb0

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎src/sounddevice.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def callback_ptr(iptr, optr, frames, time, status, _):
859859
def callback_ptr(iptr, optr, frames, time, status, _):
860860
data = _array(
861861
_buffer(iptr, frames, self._channels, self._samplesize),
862-
self._channels, self._dtype)
862+
self._channels, self._dtype, self._samplesize)
863863
return _wrap_callback(callback, data, frames, time, status)
864864

865865
elif kind == 'output' and wrap_callback == 'buffer':
@@ -875,7 +875,7 @@ def callback_ptr(iptr, optr, frames, time, status, _):
875875
def callback_ptr(iptr, optr, frames, time, status, _):
876876
data = _array(
877877
_buffer(optr, frames, self._channels, self._samplesize),
878-
self._channels, self._dtype)
878+
self._channels, self._dtype, self._samplesize)
879879
return _wrap_callback(callback, data, frames, time, status)
880880

881881
elif kind == 'duplex' and wrap_callback == 'buffer':
@@ -896,10 +896,12 @@ def callback_ptr(iptr, optr, frames, time, status, _):
896896
ichannels, ochannels = self._channels
897897
idtype, odtype = self._dtype
898898
isize, osize = self._samplesize
899-
idata = _array(_buffer(iptr, frames, ichannels, isize),
900-
ichannels, idtype)
901-
odata = _array(_buffer(optr, frames, ochannels, osize),
902-
ochannels, odtype)
899+
idata = _array(
900+
_buffer(iptr, frames, ichannels, isize),
901+
ichannels, idtype, isize)
902+
odata = _array(
903+
_buffer(optr, frames, ochannels, osize),
904+
ochannels, odtype, osize)
903905
return _wrap_callback(
904906
callback, idata, odata, frames, time, status)
905907

@@ -1490,8 +1492,9 @@ def read(self, frames):
14901492
"""
14911493
dtype, _ = _split(self._dtype)
14921494
channels, _ = _split(self._channels)
1495+
samplesize, _ = _split(self._samplesize)
14931496
data, overflowed = _InputStreamBase._raw_read(self, frames)
1494-
data = _array(data, channels, dtype)
1497+
data = _array(data, channels, dtype, samplesize)
14951498
return data, overflowed
14961499

14971500

@@ -2788,10 +2791,11 @@ def _buffer(ptr, frames, channels, samplesize):
27882791
return _ffi.buffer(ptr, frames * channels * samplesize)
27892792

27902793

2791-
def _array(buffer, channels, dtype):
2794+
def _array(buffer, channels, dtype, samplesize):
27922795
"""Create NumPy array from a buffer object."""
27932796
import numpy as np
2794-
return np.frombuffer(buffer, dtype=np.dtype((dtype, (channels,))))
2797+
shape = len(buffer) // (samplesize * channels), channels
2798+
return np.ndarray(shape, dtype=dtype, buffer=buffer)
27952799

27962800

27972801
def _split(value):

0 commit comments

Comments
 (0)