From c71c4e8571f57c36c233bf41b850478c21bb9518 Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Thu, 30 Jun 2011 03:50:09 +0000 Subject: [PATCH] added gstreamer magic (not working yet) --- dart-sounds/sound.pl | 148 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 125 insertions(+), 23 deletions(-) diff --git a/dart-sounds/sound.pl b/dart-sounds/sound.pl index d0ee71a..8279a11 100755 --- a/dart-sounds/sound.pl +++ b/dart-sounds/sound.pl @@ -1,39 +1,141 @@ #!/usr/bin/perl -T -w use strict; +use Glib qw(TRUE FALSE); +use GStreamer -init; +use threads; + +my $loop; +my $l_SPipeline; +my $l_SSource; +my $l_SOggDemuxer; +my $l_SVorbisDecoder; +my $l_SAudioConverter; +my $l_SSink; +my $pipe_in; + $ENV{PATH}=''; +&init(); &main(); + exit 0; + + +sub init() +{ + $loop = Glib::MainLoop -> new(); + + # create a new pipeline to hold the elements + $l_SPipeline = GStreamer::Pipeline -> new("pipeline"); + + $l_SSource = GStreamer::ElementFactory->make(fdsrc => "fd-source"); + $l_SOggDemuxer = GStreamer::ElementFactory->make(oggdemux => "ogg-demuxer"); + $l_SVorbisDecoder = GStreamer::ElementFactory->make(vorbisdec => "vorbis-decoder"); + $l_SAudioConverter = GStreamer::ElementFactory->make(audioconvert => "converter"); + $l_SSink = GStreamer::ElementFactory->make(autoaudiosink => "sink"); + + my $pipe_out; + pipe $pipe_out, $pipe_in; + $l_SSource->set(fd => fileno($pipe_out)); + + $l_SPipeline -> add($l_SSource, $l_SOggDemuxer, $l_SVorbisDecoder, $l_SAudioConverter, $l_SSink); + + $l_SSource->link($l_SOggDemuxer) or die "Can't link source to OGG-muxer"; + $l_SVorbisDecoder->link($l_SAudioConverter, $l_SSink) or die "Can't link rest of stuff"; + $l_SOggDemuxer->link_pads("src", $l_SVorbisDecoder, "sink"); + $l_SOggDemuxer->signal_connect("pad_added", \&pgotm_addPads) or die "Can't link Pads to vorbis";; + + $l_SPipeline -> get_bus() -> add_watch(\&my_bus_callback, $loop); + + print "setting pipeline to paused\n"; + $l_SPipeline -> set_state("paused"); + print "setting pipeline to playing\n"; + $l_SPipeline -> set_state("playing"); +} + +# Add pads +sub pgotm_addPads { + my ($element, $pad, $data) = @_; + + my $l_SCaps = $pad->get_caps(); + my $l_SMime = $l_SCaps->get_structure(0) -> { name }; + + printf "Mime that is added is: %sn", $l_SMime; + printf "A new pad %s was created %sn", $pad->get_name(), $pad->get_parent()->get_name(); + + # If it's vorbis audio + if( $l_SMime eq "audio/x-vorbis" ) { + # Can it link to the audiopad? + my $l_SSinkPad = $l_SVorbisDecoder->get_pad("sink"); + + # Check if vorbis not linked.. + if( !$l_SSinkPad->is_linked() ) { + if( $pad->link( $l_SSinkPad ) ) { + die "Can't link pads!\n"; + } + } + } +} + +sub my_bus_callback { + my ($bus, $message, $loop) = @_; + + if ($message -> type & "error") { + warn $message -> error; + $loop -> quit(); + } + + elsif ($message -> type & "eos") { + print "EOS received!\n"; + $loop -> quit(); + } + + return TRUE; +} + sub play { my ($file)=@_; - print "$file\n"; - my $cmd = "/usr/bin/gst-launch-0.10 filesrc location=${file}.ogg ! oggdemux ! vorbisdec ! audioconvert ! autoaudiosink"; - print "$cmd\n"; - print `$cmd`; + print "trying to play ${file}.ogg\n"; + + my $data; + open(my $fh, '<', "${file}.ogg") or die $!; + while(my $n = read($fh, $data, 1024)) + { + print "${n} bytes read\n"; + print $pipe_in $data; + } + print "finished read"; + close($fh); } + sub main { - while (my $schuss = ) - { - print $schuss; - chomp $schuss; + Glib::Timeout->add (5000, \&play, "double"); + + $loop->run(); + + +# while (my $schuss = ) +# { +# print $schuss; +# chomp $schuss; - my ($mult,$zahl)=split /\s+/,$schuss; - if ($mult==2) - { - play("double") - } elsif ($mult==3) { - play("triple") - } - ($zahl) = $zahl =~ m/(\d+)/; - if ($zahl >0 && $zahl < 21) - { - play($zahl) - } else { - play("bull") - } - } +# my ($mult,$zahl)=split /\s+/,$schuss; +# if ($mult==2) +# { +# play("double") +# } elsif ($mult==3) { +# play("triple") +# } +# ($zahl) = $zahl =~ m/(\d+)/; +# if ($zahl >0 && $zahl < 21) +# { +# play($zahl) +# } else { +# play("bull") +# } +# } } -- 1.7.10.4