$canvas->move('text', 0, -45);
####
$tempstring .= $buf;
if($tempstring =~ /\n/){
my @temps = split(/\n/,$tempstring);
write_lyric("$temps[0] $temps[1]\n");
$tempstring = '';
}
####
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::JPEG;
use IPC::Open3;
use Imager;
use MIME::Base64;
local *IN;
local *OUT;
local *ERR;
$|=1;
my $imagenam = shift || '1Zen16.jpg' or die "need a jpg $!\n";
my $defaultkar = "sloop-john-b.kar";
my $start = 0; # clip off midi header info
my $pid = open3(\*IN,\*OUT,\*ERR,'/bin/sh');
my $timpid; #used to stop timidity with closing filehandles to sh
my $mw = new MainWindow;
my ($width,$height) = ( $mw->screenwidth(), $mw->screenheight() );
####################################################
my $imagein = Imager->new();
my $imageout;
$imagein->open(file=>$imagenam, type=>'jpeg') or die $imagein->errstr();
my $newimg = $imagein->scale(xpixels=>$width, ypixels=>$height, type=>'min');
$newimg->write(type => 'jpeg', data => \$imageout );
my $imageenc = encode_base64($imageout) or die $!;
my $image = $mw->Photo(-data => $imageenc);
####################################################
#in case you want to use Image Magick
# use Image::Magick;
# my $im = Image::Magick->new;
# $im->Read($imagenam);
# $im->Scale( geometry => $width.'x'.$height);
# my $imageout = $im->ImageToBlob();
# my $imageenc = encode_base64($imageout) or die $!;
# my $image = $mw->Photo(-data => $imageenc);
########################################################
$mw->fontCreate('big',
-family=>'courier',
-weight=>'bold',
-size=>int(-32*32/18));
my $topframe = $mw->Frame(-bg => 'steelblue')->pack(-fill =>'x');
my $lab = $topframe->Label(
-text=> 'Enter Kar file to play: ')->pack(-side=>'left');
my $entry=$topframe->Entry(-width => 80)->pack(-side=>'left');
$entry->bind('', \&send_to_shell );
$topframe->Button(-text => 'Ok',
-command => \&send_to_shell)->pack(-side=>'left');
$topframe->Button(-text => 'Exit',
-command => sub{exit})->pack(-side=>'right',-padx=>10);
my $stopbut = $topframe->Button(-text => 'Stop',
-state => 'disabled',
-command => \&send_stop)->pack(-side=>'left',-padx=>10);
my $canvas = $mw->Canvas(
-width => $width,
-height => $height,
-bg=>'black',
)->pack(-expand => 1, -fill=>'both');
my $tile = $canvas->createImage(0,0,
-image=> $image,
-anchor => 'nw',
);
$mw->fileevent(\*OUT,'readable',\&get_from);
MainLoop;
sub send_to_shell {
$canvas->delete('text');
my $cmd=$entry->get() || $defaultkar;
$cmd = "timidity $cmd";
print IN "$cmd\n";
$stopbut->configure(-state => 'normal');
$entry->configure(-state => 'disabled');
}
sub get_from {
my $buf='';
sysread OUT,$buf,1024;
if($buf =~ /Title/){
$start = 1;
#hack to get first word of actual song lyrics
my @words=split(/\s+/,$buf);
$buf = pop @words;
}
if( $start == 0){ return}
$buf =~ tr/\n/ /;
write_lyric("$buf\n");
}
sub write_lyric {
my $str = shift;
$canvas->createText($width/2, $height-50,
-fill => 'hotpink',
-font => 'big',
-text => $str,
-tags => ['text'],
);
$canvas->move('text', 0, -45);
}
sub send_stop{
my $timgrep = `ps -a | grep timidity`;
($timpid) = $timgrep =~ /(\d+)/;
system("kill $timpid");
$stopbut->configure(-state =>'disabled');
$entry->configure(-state => 'normal');
$start = 0;
}
__END__