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:
This proc parameter takes a STREAMPROC callback, which is defined like this:
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?