Skip to content

Commit

Permalink
Got callback working but is very very shaky. Also please fix pack for…
Browse files Browse the repository at this point in the history
…mat for AUDIO_S8 signed integer
  • Loading branch information
Kartik Thakore committed Dec 30, 2009
1 parent 6a81c93 commit 9574696
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
28 changes: 19 additions & 9 deletions src/Core/objects/AudioSpec.xs
Expand Up @@ -42,34 +42,41 @@ extern PerlInterpreter *parent_perl;
#include <SDL.h>
#include <SDL_audio.h>

static SV * callbacksv;
static int perl_c;
static char* callbacksv;

void
sdl_perl_audio_callback ( void* data, Uint8 *stream, int len )
audio_callback ( void* data, Uint8 *stream, int len )
{



PERL_SET_CONTEXT(perl_c); {
PerlInterpreter *my_perl = perl_c;

dSP;
SV* sv = newSVpv("a",1);
SvCUR_set(sv,len);
SvLEN_set(sv,len);
SvPV_set(sv,(char*)stream);

ENTER;
SAVETMPS;
PUSHMARK(SP);


XPUSHs(sv_2mortal(newSViv(sizeof(int))));
XPUSHs(sv_2mortal(newSViv(len)));
XPUSHs( sv );

PUTBACK;

call_sv(callbacksv,G_VOID|G_DISCARD);
call_pv(callbacksv,G_VOID|G_DISCARD);

FREETMPS;
LEAVE;

} PERL_SET_CONTEXT(NULL);

}

MODULE = SDL::AudioSpec PACKAGE = SDL::AudioSpec PREFIX = audiospec_
Expand Down Expand Up @@ -146,10 +153,13 @@ audiospec_samples ( audiospec, ... )
void
audiospec_callback( audiospec, cb )
SDL_AudioSpec *audiospec
SV* cb
char* cb
CODE:
dTHX;
perl_c = my_perl;
callbacksv = cb;
audiospec->callback = sdl_perl_audio_callback;
audiospec->callback = audio_callback;



void
Expand Down
33 changes: 16 additions & 17 deletions t/core_audiospec.t
Expand Up @@ -4,7 +4,7 @@ use SDL;
use SDL::Audio;
use SDL::AudioSpec;
use Test::More;
use Devel::Peek;


use lib 't/lib';
use SDL::TestTool;
Expand All @@ -20,34 +20,33 @@ my $desired = SDL::AudioSpec->new;
$desired->format(SDL::Constants::AUDIO_S8);
$desired->channels(1);
$desired->samples(4096);
$desired->callback(
$desired->callback('main::callback');
my $pass = 0;

sub{
sub callback
{
my ($int_size, $len, $stream) = @_;

warn "got $int_size $len $stream";
foreach(0..$len)
{
my $new = rand()*120;
substr($stream, $_*$int_size, $int_size, pack('i', $new));

}
$pass = 1 if $pass == 0;
#foreach(0..$len)
#{
# my $new = rand()*120;
# substr($stream, $_*$int_size, $int_size, pack('i', $new));

}


# }
#pack needs to be fixed

);
}

die 'AudioMixer, Unable to open audio: '. SDL::get_error() if( SDL::Audio::open($desired, $obtained) <0 );


SDL::Audio::pause(0);

# sleep(4);
sleep(1);

# SDL::Audio::close();
SDL::Audio::close();

pass '[callback] tested';
is $pass, 1, '[callback] tested';


33 changes: 17 additions & 16 deletions test/script.pl
Expand Up @@ -38,29 +38,31 @@

SDL::Mouse::show_cursor(0);

sub setup_audio
{
my $desired = SDL::AudioSpec->new;
my $obtained = SDL::AudioSpec->new;
$desired->freq ( 44100 );
$desired->format ( AUDIO_S8 );
$desired->samples ( 4096 );

#$desired->userdata ( NULL );
$desired->callback(

sub{
sub callback{
my ($int_size, $len, $stream) = @_;

warn "got $int_size $len $stream";
warn "call back is running \n";
warn "Unpacked is: ", join ', ', unpack('i*', $stream), "\n";
foreach(0..$len)
{
my $new = rand()*120;
substr($stream, $_*$int_size, $int_size, pack('i', $new));


}

}

});
sub setup_audio
{
my $desired = SDL::AudioSpec->new;
my $obtained = SDL::AudioSpec->new;
$desired->freq ( 44100 );
$desired->format ( AUDIO_S8 );
$desired->samples ( 4096 );

#$desired->userdata ( NULL );
$desired->callback( 'main::callback'); #canno
$desired->channels ( 1 );


Expand All @@ -72,8 +74,7 @@ sub setup_audio

}
setup_audio();
sleep (10);
die();

our $time = $app->ticks;
our $sevent = SDL::Event->new();
our $timer = Event->timer
Expand Down

0 comments on commit 9574696

Please sign in to comment.