in reply to Tk-Karoake Player-w-timidity

Hi Zentara! (its me renegadex) just wanted to share to you a Gtk2 version of your karaoke player. its not yet finish but atleast i was able to make it do the thing that you do (display lyrics on the go). this is the code i used (borrowed from some examples etc).
#!/usr/bin/perl # modules use strict; use Gtk2 '-init'; use Gtk2::Helper; use Image::Magick; use Goo::Canvas; use Data::Dumper; use IPC::Open2; use FileHandle; my $wfh = FileHandle->new(); my $rfh = FileHandle->new(); my $text; my $timpid; my $tag; $|=1; # i dont know what this is???? open2($rfh,$wfh, "/bin/sh"); my $tag = Gtk2::Helper->add_watch($rfh->fileno(), 'in',\&get_from); my $window = Gtk2::Window->new(); $window->fullscreen(); my $canvas = Goo::Canvas->new(); config_canvas(); $window->add($canvas); $window->show_all(); Gtk2->main(); sub config_canvas { my $res_line = `xdpyinfo | grep 'dimensions:'`; my ($dw,$dh) = $res_line =~ m/(\d*)x(\d*) pixel/; my $uw = $dw - 50; my $uh = $dh - 50; $canvas->set_bounds(-$uw/2,-$uh/2,$uw/2,$uh/2); $canvas->set('background-color'=>"black"); $canvas->set('anchor','center'); $canvas->set('integer-layout'=>1); my $root = $canvas->get_root_item; my $bkg = Goo::Canvas::Group->new($root); my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale('karaok +e/b1.jpg',$uw,$uh,1); my $img = Goo::Canvas::Image->new($bkg,$pixbuf,-$uw/2,-$uh/2); $img->signal_connect('button_press_event'=>\&mouse_press); my $video = Goo::Canvas::Group->new($root); my $lyrics = Goo::Canvas::Group->new($root); $text = Goo::Canvas::Text->new($lyrics,"testing",0,0,0,'center +'); } sub get_from { # we safely can read a chunk into $buffer my $buffer; if ( not sysread($rfh, $buffer, 1024) ) { # obviously the connected pipe was closed print "exit\n"; Gtk2::Helper->remove_watch ($tag) or die "couldn't remove watc +her"; close($rfh); return 1; } # do something with $buffer ... print $buffer . "\n"; # *always* return true return 1; } sub mouse_press { my ($widget, $target, $event) = @_; if($event->button == 1){ print "start\n"; send_to_shell(undef,'Just Beat It','/home/server/karaoke/sloop +-john-b.kar'); }else{ print "stop\n"; send_stop(); } } sub send_to_shell { my ($widget,$title,$file) = @_; print "Playing.. $title\n"; my $cmd = "timidity '$file'"; print $wfh "$cmd\n"; } sub send_stop{ my $timgrep = `ps -a | grep timidity`; ($timpid) = $timgrep =~ /(\d+)/; system("kill $timpid"); }
some of the modules here are not yet being used. anyway... im also here to ask you if there is a way to extract lyrics from a .kar file? i saw kmid2 and it was able to display the whole lyrics, and i want to know how that is made. tnx!

Replies are listed 'Best First'.
Re^2: Tk-Karoake Player-w-timidity
by zentara (Cardinal) on Aug 02, 2010 at 18:54 UTC
            my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale('karaoke/b1.jpg',$uw,$uh,1);

    Hi, since this is a pixbuf that is integral to the program, you might want to include it in your script as a bas64encoded file. Like in Re: PerlMagick Gtk2::Gdk::Pixbuf


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      just wanna contribute something in your karaoke project.. this code requires perl-MIDI-Perl to run. I got it from cpan, its not available on the standard fedora13 repo. anyway here is a code to be able to extract lyrics of a midi file...
      sub send_to_shell { my ($widget,$file) = @_; my $string; my $opus = MIDI::Opus->new({ 'from_file' => $file, 'exclusive_event_callback' => sub{ my $temp = $_[2]; chomp $temp; if($_[0] eq 'text_event'){ if($_[1] == 0){ }else{ $string = $string . $_[2]; push @arr_lyr, $_[2]; } }elsif($_[0] eq 'track_name'){ $marker = $_[2]; } }, 'include' => \@MIDI::Event::All_events, 'exclude' => \@MIDI::Event::MIDI_events, }); @line_lyrics = split(/\/|\\/,$string); shift @line_lyrics; print "Marker: $marker\n"; print "_____START_____\n"; $start = 0; # start song my $cmd = "timidity '$file'"; print $wfh "$cmd\n"; }
      enjoy! you can use this to replace the same sub routine on my previous post.
      Mabuhay Civil Engineers! :D
      just a question regarding the base64encoded. what is the difference if i just simply use this:
      my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale('karaoke/b1.jpg +',$uw,$uh,1);
      instead of this:
      my $pixbuf = do { my $loader = Gtk2::Gdk::PixbufLoader->new(); $loader->write( $bunny ); $loader->close(); $loader->get_pixbuf(); };
        The do construct was something shown to me awhile back by the venerable aristotle. The difference is this, if my mind serves me correctly:-):

        With the "_from_file" method, you need to be reading from a disk file.

        With the loader method, you can load image data which is in a scalar string, or slurped in from a disk file, often referred to as blobs in ImageMagick terminolgy. ( See Convert Gnome2::Canvas::Pixbuf to Image::Magick Array ). This is often quite handy, like when you pull an image in from the web, and want to display it, without first writing it to a disk file. Like:

        my $graph_image = get($http_request_string); $graph_image at this point is a string, with a PNG-looking header. my $loader = Gtk2::Gdk::PixbufLoader->new; $loader->write($png_data); $loader->close; my $pixbuf = $loader->get_pixbuf;

        Even though there is Gtk2::Gdk::Pixbuf->new_from_data, it does not automatically handle autodetection of file type as well as the Gtk2::Gdk::PixbufLoader. In other words, the Pixbufloader will autodetect from it's list of recognized types, making it the best method to use if you want an easy load.

        Here is a simple way to list the autodetected file types which your Gtk2 build recognizes.

        #!/usr/bin/perl use strict; use warnings; use Gtk2; my @formats = Gtk2::Gdk::Pixbuf->get_formats(); my @exts; foreach my $format ( @formats ) { foreach my $key ( keys( %$format ) ) { next unless $key eq 'extensions'; foreach my $elem ( @{ $format->{ $key } } ) { push @exts, $elem; } } } print "@exts\n";
        And finally, [the original message from [aristotle]: <p><quote aristotle> OT: I like to partition things like into smaller scopes: my $pixbuf = do { my $loader = Gtk2::Gdk::PixbufLoader->new(); $loader->write( $image_data ); $loader->close(); $loader->get_pixbuf(); }; So the next guy who looks at it doesn't have to wonder if $loader is used anywhere later, and can see that the entire point of this mumbo jumbo is to load something into $pixbuf. Regards, #Aristotle </quote aristotle>
        So in summary, if you want to learn one good method for foolproof loading of images, use the pixbufloader construct, it will seldom let you down.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku