Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Music-Player

by Perforin (Novice)
on Aug 29, 2007 at 19:34 UTC ( [id://635918]=sourcecode: print w/replies, xml ) Need Help??
Category: Audio Related Programs
Author/Contact Info Perforin perforin@warezmail.net
Description: Little script which plays mp3,wav,wma and midi files!
This is actually version 2! When you start the script, it will show you how to use it. Itīs very simple... and oldschool :P

#!/usr/bin/perl
# ############################
#             --==Oldschool Music Player==--                #
#                                                                     
+             #
#         Coded by Perforin | dark-codez                    #
#                                                                     
+             #
#                   www.dark-codez.org                            #
#                                                                     
+             #
#             Join the dark side of coding!                     #
#############################

my $version = 2.0;

use Win32::MediaPlayer;

if (@ARGV < 1) {
print " \n +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+\n";
print " |O|l|d|s|c|h|o|o|l| |P|l|a|y|e|r|\n";
print " +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+\n\n\n";
print " +-+-+-+-+-+-+SyNtaX+-+-+-+-+-+-+\n\n";
print " \UUsage1: \E\n";
print " perl player.pl music.mp3 100\n";
print " Play one Music-File!\n";
print " \UUsage2: \E\n";
print " perl player.pl -all C:\ 100\n";
print " Play all music files in the Directory!\n";
print " \UUsage3: \E\n";
print " perl player.pl -last \n";
print " Displays the last song!\n\n";
print " +-+-+-+-+-+-+SyNtaX+-+-+-+-+-+-+\n";
exit;
}
if ($ARGV[0] =~ "-last") {
open(PLAYED,"<played.txt") || die print "Cannot find list!"."\n";
@PLAYED = <PLAYED>;
close(PLAYED);
print "\n";
print @PLAYED;
exit;
} elsif ($ARGV[0] =~ "-all") {
my $Dir = $ARGV[1];
chomp($Direction);
my $Vol = $ARGV[2];
chomp($Vol);
if ($Vol =~ /a-z/ or /A-Z/) {print "Wrong Volume!\n" and exit; }

chdir "$Dir";

print " \n +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+\n";
print " |O|l|d|s|c|h|o|o|l| |P|l|a|y|e|r|\n";
print " +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+\n\n";
print " BE OLDSCHOOL! USE OLSCHOOL PLAYER \n\n\n";
print " AND FEEL THE SOUND OF YOUR BLACK BOX!\n\n\n";

foreach $music (<*.mp3>,<*.wma>,<*.wav>,<*.midi>) {
open(PLAYED,">played.txt");
    $winmm = new Win32::MediaPlayer;
    $winmm->load("$music");        
    $winmm->play;                     
    $winmm->volume($Vol);              
    $winmm->seek('00:00');           
print " Oldschool Player plays: $music\n\n";
print PLAYED "$Direction"."\n";
my $i = 1;
   print ' Total Length : '.$winmm->length(1),$/;
 while($winmm->pos(1) != $winmm->length(1)) {
          print ' Now Position: '.$winmm->pos(1)."\r";
    $i++
    }
    $winmm->close;
print " \n\n\n Track finished! \n\n\n";
close(PLAYED);
}
} else {
my $Direction = $ARGV[0];
chomp($Direction);
my $Volume = $ARGV[1];
chomp($Volume);

if ($Volume =~ /a-z/ or /A-Z/) {print "Wrong Volume!\n" and exit; }

print " \n +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+\n";
print " |O|l|d|s|c|h|o|o|l| |P|l|a|y|e|r|\n";
print " +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+\n\n";
print " BE OLDSCHOOL! USE OLSCHOOL PLAYER \n\n\n";
print " AND FEEL THE SOUND OF YOUR BLACK BOX!\n\n\n";

open(PLAYED,">played.txt");
    $winmm = new Win32::MediaPlayer;
    $winmm->load("$Direction");        
    $winmm->play;                     
    $winmm->volume($Volume);              
    $winmm->seek('00:00');           
print " Oldschool Player plays: $Direction\n\n";
print PLAYED "$Direction"."\n";
my $i = 1;
   print ' Total Length : '.$winmm->length(1),$/;
 while($winmm->pos(1) != $winmm->length(1)) {
          print ' Now Position: '.$winmm->pos(1)."\r";
    $i++
    }
    $winmm->close;
print " \n\n\n Track finished! \n\n\n";
close(PLAYED);
}
Replies are listed 'Best First'.
Re: Music-Player
by jdporter (Paladin) on Aug 29, 2007 at 22:35 UTC

    Nice idea, horrible implementation. Mega-minus marks for style problems (too many to list) and for posting code without use strict; use warnings. User interface (e.g. error feedback) is awful, and you've got bugs aplenty. Here's a cleaned up version:

    #!/usr/bin/perl ################################# # --==Oldschool Music Player==-- # # Coded by Perforin | dark-codez # # www.dark-codez.org # # Join the dark side of coding! ################################# my $version = 2.99; use Win32::MediaPlayer; use Getopt::Long; use strict; use warnings; $|=1; my $default_played_filename = 'played.txt'; my $played_filename = $default_played_filename; my $volume = 100; my $all_in_dir; GetOptions( 'playedfile|file=s' => \$played_filename, # let user override 'last|history!' => \&show_last_played, 'volume=i' => \$volume, 'directory|folder|alldir|allindir=s' => \$all_in_dir, 'help|usage!' => \&usage, ); defined($volume) && $volume =~ /^\s*\d+\s*$/ && $volume >= 0 && $volume <= 100 or die "Invalid volume; must be a number between 0 and 100.\n"; print <<EOF; +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+ |O|l|d|s|c|h|o|o|l| |P|l|a|y|e|r| +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+ BE OLDSCHOOL! USE OLSCHOOL PLAYER AND FEEL THE SOUND OF YOUR BLACK BOX! EOF open PLAYED,'>',$played_filename or die "Can't open $played_filename for writing - $!\n"; # make sure writes to PLAYED autoflush: { my $oldfh = select STDERR; $|=1; select $oldfh; } if ( $all_in_dir ) { chdir $all_in_dir or die "chdir $all_in_dir - $!\n"; play_media_file($_) for <*.mp3>,<*.wma>,<*.wav>,<*.midi>; } else { @ARGV or usage(); play_media_file($_) for @ARGV; } END { close PLAYED; } sub usage { exit print <<EOF; +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+ |O|l|d|s|c|h|o|o|l| |P|l|a|y|e|r| +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+ +-+-+-+-+-+-+SyNtaX+-+-+-+-+-+-+ Usage: Play specific media files: $0 song1.mp3 song2.mp3 Play all media files in a directory: $0 -all C:\\songs List song(s) played last time: $0 -last Display usage statement: $0 -help Options: Set volume: -volume N N must be a number in range 1 .. 100. Default is 100. Set 'last played songs' file: -played FILE default is "$default_played_filename" +-+-+-+-+-+-+SyNtaX+-+-+-+-+-+-+ EOF } sub show_last_played { open PLAYED,'<',$played_filename or die "Can't open $played_filename for reading - $!\n"; print "\n", <PLAYED>; close PLAYED; exit; } sub play_media_file { eval{ my $media_file = shift; my $winmm = new Win32::MediaPlayer; my $loaded = $winmm->load($media_file); $loaded eq '1' or die "Error loading file '$media_file' - $loaded\n"; $winmm->play; $winmm->volume($volume); $winmm->seek('00:00'); print "PLAYED $media_file\n"; print PLAYED "$media_file\n"; print " Oldschool Player plays: $media_file\n\n"; printf " Total Length: %7s\n", $winmm->length(1); while ( $winmm->pos() < $winmm->length() ) { printf " Now Position: %7s\r", $winmm->pos(1); } $winmm->close; print " \n\n\n Track finished! \n\n\n"; }; $@ and warn $@; }
    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      I like your version a bit better than his, but I've still got to give a (--) for your initial complaint
      ...and for posting code without use strict; use warnings.
      Read a nice little rant about the "usestrictusewarnings" dogma at point #9 (line 702) of this page. And most of your corrections are cosmetic anyway.

      Anyway, a REALLY old school player would play the song by running various loops of instructions that cause the machine to hum at different frequencies. (My dad claims to have done this with punch cards in the 70s.) But I don't think that's even possible on modern hardware or in Perl. (/me waits eagerly to be proven wrong.)


      /usr/bin/perl '-nemap$.%$_||redo,2..$.++;print$.--'

        It's an interesting discussion (I don't think I'd call it a "rant") and as usual MJD makes a lot of sense. What I don't get from it is that I was somehow wrong to have suggested that the OP include the pragmas.

        Of course, it isn't really about having the use strict; use warnings in the code as posted. It's about posting code which is strict safe and warnings clean. In fact, if the OP had enabled strict, he would have caught a non-trivial bug having to do with variable naming ($Dir vs. $Direction). And warnings would have (or could have, if he paid close enough attention) helped him catch the bug in the line

        if ( $Volume =~ /a-z/ or /A-Z/ ) . . .

        A word spoken in Mind will reach its own level, in the objective world, by its own weight
        Alright, so, "back in the day" I programmed a lovely music player in BASIC, which later upgraded to QBASIC, and was eventually compiled with Quick BASIC. Because of the way the sound was produced (using the BASIC "play" command), I was able to control the music tempo, key, etc. using some fairly simple subroutines. My music program would play any one of several hundred hymns that I programmed into it, and each was user-selectable for any key or tempo within a certain range. So, if "Amazing Grace" were written in the key of C but a bass singer wanted it transposed to Eb, no problem. Want that song a little faster? Can do!

        I've never seen any "modern" music player match that. Can an mp3 player, for example, transpose the key for you?

        Like you, dbw, I question if this is even possible on the modern hardware and software. I know for a fact that Visual Basic (the logical upgrade for Quick BASIC), does NOT have music capability. That has frustrated me to no end. All it can do is a pitiful beep. But then, is Perl any better?

        Blessings,

        ~ Polyglot ~

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://635918]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found