Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello, I'm trying to complete my Audio::BASSXS module by wrapping more of the functions supported by the BASS library.

One of the things I want to do is add support for callbacks. This turns out to be not so simple. Here's what I'm trying to do:

In bass.h there's the following function:
HSTREAM BASSDEF(BASS_StreamCreate)(DWORD freq, DWORD chans, DWORD flag +s, void *proc, DWORD user);
This proc parameter takes a STREAMPROC callback, which is defined like this:
typedef DWORD (CALLBACK STREAMPROC)(HSTREAM handle, void *buffer, DWOR +D length, DWORD user);
Obviously I want to achieve that the BASS_StreamCreate can be called from perl, taking a coderef as a parameter.
There's an example how to call this in C supplied with the BASS package, which looks like this (I stripped it down to the most important piece):
DWORD CALLBACK stream(HSTREAM handle, char *buffer, int length, DWORD +user) { int c; // check how much recorded data is buffered c=BASS_ChannelGetData(rchan,0,BASS_DATA_AVAILABLE); c-=length; if (c>2*chunk+1764) { // buffer has gotten pretty large so remove +some c-=chunk; // leave a single 'chunk' BASS_ChannelGetData(rchan,0,c); // remove it } // fetch recorded data into stream c=BASS_ChannelGetData(rchan,buffer,length); if (c<length) memset(buffer+c,0,length-c); // short of data return length; } // create a stream to play the recording data, and start it chan=BASS_StreamCreate(44100,2,0,(STREAMPROC*)stream,0); BASS_StreamPlay(chan,0,BASS_SAMPLE_LOOP);


My XS code (also stripped down to show relevant parts) looks like this:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include <bass.h> #include "callback.c" #include "const-c.inc" MODULE = Audio::BASSXS PACKAGE = Audio::BASSXS INCLUDE: const-xs.inc PROTOTYPES: DISABLE HSTREAM BASS_StreamCreate(freq, chans, flags, proc, user) DWORD freq DWORD chans DWORD flags SV * proc DWORD user CODE: if (streamproccallback == (SV*)NULL) /* First time, so create a new SV */ streamproccallback = newSVsv(proc) ; else /* Been here before, so overwrite */ SvSetSV(streamproccallback, proc) ; RETVAL = BASS_StreamCreate(freq,chans,flags,(STREAMPROC*)MyStreamP +roc, user); OUTPUT: RETVAL
And the callback.c looks like this (not stripped down...this is all there is):
static SV * streamproccallback = (SV*)NULL; DWORD CALLBACK MyStreamProc(handle,buffer,length,user) HSTREAM handle; char *buffer; int length; DWORD user; { if (streamproccallback == (SV*)NULL) return 0; dSP ; int ret; SV * shandle; SV * sbuffer; SV * slength; SV * suser; ENTER; SAVETMPS; shandle = sv_2mortal(newSViv(handle)); sbuffer = sv_2mortal(newSVpv(buffer,length)); slength = sv_2mortal(newSViv(length)); suser = sv_2mortal(newSViv(user)); PUSHMARK(SP); XPUSHs(shandle); XPUSHs(sbuffer); XPUSHs(slength); XPUSHs(suser); PUTBACK; ret = call_sv(streamproccallback, G_SCALAR); buffer = SvPV(sbuffer,length); FREETMPS; LEAVE; return ret; }
This all compiles without a problem. However, when I run the following script:
#!/usr/bin/perl use strict; use warnings; use Audio::BASSXS; our $audio = 'test.mp3'; open(FH, "<", $audio) || die "Can't open $audio: $!"; binmode(FH); sub testsub { # We get 4 parameters in the @_: # ($handle,$buffer,$length,$user) = @_; my $read = sysread(FH, $_[1], $_[2]); unless ($read) { $read = $read|BASS_STREAMPROC_END; } warn "$read\n"; return $read; } BASS_Init(1,44100,0,0,0); my $stream = BASS_StreamCreate(44100,2,0,\&testsub, 0); warn BASS_ErrorString(BASS_ErrorGetCode) unless $stream; BASS_StreamPlay($stream,0,BASS_SAMPLE_LOOP); sleep(10); close(FH);
This however prints '35280' (so my callback is called once at least), and then ends with 'Unhandled exception in perl.exe (BASSXS.dll): 0xC0000005: Access violation' Then the MSVC debugger starts and shows me it has ended at the line     if (streamproccallback == (SV*)NULL) return 0; in callback.c. I've tried many, many different things (that if statement where it ends now wasn't there yesterday, and then it crashed on the dSP statement), but can't find anything that works. Does anyone have an idea?


Jouke Visser
Speaking at the 2004 O'Reilly Open Source Convention about pVoice

In reply to callbacks in XS code by Jouke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found