Problems with string conversion and buffer storage
Toni Barth <hihohaia <at> googlemail.com>
2015-01-27 18:18:43 GMT
i've got a kind of unresolvable problem, at least for me. I've got a c
library which defines functions which accept callbacks which take a
buffer of a given length as parameter and are ment to fill the buffer
with relevant data, in a given format. Here the documentation excerpt
relevant for the parameters:
The stream that needs writing.
Pointer to the buffer to write the sample data in. The data should be as
follows: 8-bit samples are unsigned, 16-bit samples are signed, 32-bit
floating-point samples range from -1 to +1.
The maximum number of bytes to write.
The user instance data given when BASS_StreamCreate was called.
I don't know if these information are relevant, but anyway.
At the moment i'm that far that I wrote a c callback function which
wraps a python function, the actual callback function, which the
python-user can define. This function should return the requested data.
It should work just like reading from a file, passing these data as
return value and the c function should fill the c buffer correctly and
yeah... all should work fine. My callback python function looks like this:
print 'cb fired!'
So, the c callback takes the python return value and converts it to the
type 'bytes', which should be fine, since a pythonic string is returned.
It then checks the string length and shortens it if it is too long. It
then casts the bytes object into a char * and memmoves the data to the
buffer. It just looks like this:
cdef DWORD CSTREAMPROC(HSTREAM handle,void *buffer,DWORD length,void *user):
cdef object cb,pythonf
cdef char *cbuf
cdef int pos,i
cdef bytes pythonbuf
cdef STREAM stream
print 'got buf'
print 'length is too large'
print 'copying around'
Yep, the c function must return the written bytes as DWORD. So, my
current state looks like this. Calling the callback works one time. Then
the program, which is a audio playback library as obvious, plays a
whitenoise and the program crashes, indicating that something went wrong
when filling the buffer with the relevant data, so i'm now starting to
guess, since any debug attempts fail (Visual Studio 2012 doesn't return
much and gdb under debian 8 just says 'warning: .cygdbinit: no such file
or directory' when calling cygdb and returning some errors in the plain
One thing is that file.read() doesn't return the data as expected/the
right way, at least not as the c equivalent fread() does, because with
this function it all works fine. Another idea is that I'll have to
convert the data into the concerned bit formats before filling the buffer.
So anyway, I don't have any clue.
So, to help my by taking a look into the whole source code, just clone
and checkout the cython branch. By calling the following calls, you'll
have the full environment setup and are about to test:
stream=bass.StreamCreate(48000,2,0,cb,0) # cb is the callback function
# do something
This should do the trick. The relevant c callback function can be found
Honestly, it would be really nice if someone has some ideas to help me
out. I'm quite new to c (actually learning it while studying for over 2
or 3 months now) and therefore not good at it, so it might be that i'm
missing something really easy.
Thanks for everyone who red through my brainfreeze and tries to look
into that stuff. A feedback would be just great!
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.