#!/usr/bin/perl -w #use lib ""; use MP3::Tag; use File::Find; use Time::HiRes qw(usleep); use Getopt::Std; use TeleServ; $prog_name = $0; $player_name = "/usr/bin/mpg123"; @mp3files = (); sub getid3 { my $filename = shift; my $mp3 = MP3::Tag->new($filename); my @out_id3 = $mp3->autoinfo(); return @out_id3; } sub formatid3 { my @param = @_; my @formats = qw(t n p a c y g); return if (scalar @param != 8); my $format_str = pop @param; for(my $i = 0; $i < 7; $i++) { $format_str =~ s/\%$formats[$i]/$param[$i]/g; } return $format_str; } sub wanted { /^.*\.[mM][pP]3\z/s && push(@mp3files, $File::Find::name); } # Return string array to scroll on LCD sub scroll_arr { my ($str, $num) = @_; my @out_arr; return $str if (length($str) <= 16); my $len = length($str) - $num; for( $i = 0; $i <= $len; $i++ ) { $out_arr[$i] = substr($str, $i, $num); } return @out_arr; } sub child_kill { my $p = shift; $SIG{CLD} = IGNORE; kill(KILL, $p) if ($p); } sub usage { print "Usage: $prog_name -d -f dir_name\n"; print "\tRead man page for more detail\n"; exit 0; } getopts("d:f:h",\%opts); $opts{d} = "/dev/ttyS0" unless($opts{d}); $opts{f} = "%t" unless($opts{f}); usage() unless($opt{h} || $ARGV[0] || -d $ARGV[0]); File::Find::find({wanted => \&wanted}, $ARGV[0]); tsOpen($opts{d}); tsSetTerminalMode; $mp3_ind = 0; while (1) { $name_file = $mp3files[$mp3_ind]; $print_str="Unsuported" unless ($print_str = formatid3(getid3($name_file), $opts{f})); @scroll = scroll_arr($print_str, 16); #Run player (fork) if( $pid = open(CHILD,"|-") ) { #Set $proc_exit when player is stopped $proc_exit = 0; $SIG{CLD} = sub { $proc_exit = 1; }; #direction of scroll on LCD $scr_course = 1; $scr_ind = 0; while (1) { #one position scroll on LCD if( $scr_course ) { if ($scr_ind >= scalar(@scroll) - 1) { $scr_course = 0; } else { $scr_ind++; } } else { if ($scr_ind <= 0) { $scr_course = 1; } else { $scr_ind--; } } tsPrint($scroll[$scr_ind]); usleep 400000; #Next or Previous file playing $key = tsKey(); if ($proc_exit || ($key eq "u")) { $mp3_ind++; $mp3_ind = 0 if( $mp3_ind >= scalar(@mp3files) ); last; } elsif ($key eq "d") { $mp3_ind--; $mp3_ind = scalar(@mp3files) - 1 if( $mp3_ind < 0 ); last; } elsif ($key eq "r") { tsPrint("Stop"); child_kill $pid; while(tsWaitKey() ne "r") {} goto EXIT; } } child_kill $pid; EXIT: close CHILD; } else { exec $player_name, $name_file; } }