Skip to content

Commit

Permalink
capture_audio returns a piddle and a hashref describing the audio for…
Browse files Browse the repository at this point in the history
…mat.
  • Loading branch information
zpmorgan committed Jan 23, 2012
1 parent 4cfa38d commit 7f1693e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
27 changes: 24 additions & 3 deletions lib/PDL/GStreamer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,37 @@ sub capture_audio{
my $signedness = $signed eq 'true';
#ignoring depth. I don't suppose it's relevant.
my ($width) = $caps =~ /width=\(int\)(\d+)\b/;
my ($channels) = $caps =~ /channels=\(int\)(\d)/;

$self->player->set_state('playing');
my $num_buffers = POSIX::ceil($seconds *($width/8) * $rate / $initbuf->size);
my $num_buffers = POSIX::ceil(
$channels * $seconds *($width/8) * $rate / $initbuf->size);

my @datas;
for (1..$num_buffers){
my $buf = $self->_audiosink->pull_buffer();
push @datas,$buf->data;
}

my $ptemplate; #TEMPLATE for unpack. bleh.
$ptemplate = 'n' if (($width==16) and !$littleendian);
$ptemplate = 's' if (($width==16) and $littleendian);
die "$caps unpackable?" unless $ptemplate;

my $data = join '',@datas;
warn length $data;;
return $data;
my $format = {
littleendian => $littleendian,
rate => $rate,
width => $width,
signed => $signedness,
channels => $channels,
packtemplate => $ptemplate,
};

my @data = unpack ($ptemplate.'*' , $data); #bleh.
my $piddle = pdl(@data);
$piddle->reshape($channels, $piddle->dim(0)/$channels);
return ($piddle,$format);
}


Expand Down
11 changes: 6 additions & 5 deletions screenshot_imag2d.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
$screenshot = $noir->capture_image;
imag2d($screenshot/256);

my $sound = ''; #'blah'x999;
$sound = $noir->capture_audio(8);

my ($sound,$format) = $noir->capture_audio(20);
#die $sound->dims;
#this isn't very portable.
#slice out the first channel.
my $rawsound = pack ($format->{packtemplate} .'*' , $sound->slice('0')->list);
my $pa;
open ($pa,'|pacat --format=s16le');
print $pa $sound;
open ($pa,'|pacat --format=s16le --channels=1');
print $pa $rawsound;
close($pa);

0 comments on commit 7f1693e

Please sign in to comment.