Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions Bass4Py/bass/stream.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from libc.string cimport memmove

from ..bindings.bass cimport (
_BASS_ATTRIB_BITRATE,
_BASS_ATTRIB_NET_RESUME,
Expand Down Expand Up @@ -97,18 +96,15 @@ cdef QWORD __stdcall CFILELENPROC_STD(void *user) with gil:

cdef DWORD CFILEREADPROC(void *buffer, DWORD length, void *user) with gil:
cdef Stream strm = <Stream?>user
cdef bytes data
cdef DWORD blen

cdef unsigned char[:] buffer_memoryview = <unsigned char[:length]>buffer # make memoryview, using buffer as the memory
cdef const unsigned char[:] bytes_memoryview # if readinto() is not available, we'll put the result of read() in this variable and copy the data
try:
data = strm._file.read(length)
blen = len(data)

if blen > length:
data = data[:length]
blen = length

memmove(buffer, <char *>data, blen)
if hasattr(strm._file, "readinto"): blen = strm._file.readinto(buffer_memoryview) # read data directly from file into the buffer
elif hasattr(strm._file, "read"):
bytes_memoryview = strm._file.read(length)
len = bytes_memoryview.shape[0] # we might not've gotten as much data as requested
buffer_memoryview[:len] = bytes_memoryview[:len] # let Cython copy the data for us
return blen
except Exception:
warnings.warn(traceback.format_exc(), RuntimeWarning)
Expand Down