Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Album Information Database

by bitsmart (Initiate)
on Jan 04, 2007 at 16:23 UTC ( [id://592977]=sourcecode: print w/replies, xml ) Need Help??
Category: Audio Related Programs
Author/Contact Info Andrew Prentice - bitsmart0@gmail.com
Description: These two scripts are designed to store and retrieve tracks with BPM (beats per minute). Intended for DJs and EJs to find tracks with similar BPMs for the purpose of making mixes. The code is commented in a very elementary way because these scripts were designed for teaching, myself and others. I wrote these in 2001. "add.pl" - Adds whole albums/singles/12" records to a simple text database. "bpm.pl" - Queries the simple text database and returns tracks with same BPM.
add.pl
#!/usr/bin/perl
########################################
#             A. I. D. v1.0            #
#       add database entry script      #
#           coded by bitsmart          #
#       (c) 2007 Andrew Prentice       #
#          bitsmart0@gmail.com         #
########################################
print "\nA. I. D. v1.0 (2007) Andrew Prentice\n";
print "\nA lbum\nI nformation\nD atabase\n";
print "\nArtist/Album: ";
chomp($info = <STDIN>); #get the name of the Artist and Album
print "creating new file or appending to existing file..."; #make it l
+ook like we're doing something
$dbentry = "BPM-$info.txt"; #add DB prefix
open(DBENTRY, ">>$dbentry") || die "cannot open file $dbentry: $!"; #o
+pen w/ error handling
print DBENTRY "$info\n "; #append Album info
close(DBENTRY); #make sure we're all neat
print "done.\n"; #done creating or appending the file
print "How many tracks do you want to add? ";
$numberoftracks = <STDIN>; #how many tracks?
chomp($numberoftracks); #remove trailing newline
$counter = 1; #need counter to count up to no. of tracks
while ($counter <= $numberoftracks) { #cycle thru each of the tracks a
+nd get info
    print "\nTrack name $counter: ";
    chomp($trackname = <STDIN>); #name of track
    print "Track length $counter: ";
    chomp($tracklength = <STDIN>); #length of track
    print "Track BPM $counter: ";
    chomp($trackbpm = <STDIN>); #beats per minute of track
    print "adding track..."; #make it look like we're doing something
    $track = "$counter - $trackname - $tracklength - BPM=$trackbpm"; #
+string it together
    open(TRACK, ">>$dbentry") || die "cannot open file $dbentry"; #ope
+n w/ error handling
    print TRACK "\n$track"; #add each song to the file
    close(TRACK); #make sure we're all neat
    $counter++; #increment our counter
    print "done."; #done adding track
}
open(LAST, ">>$dbentry") || die "cannot open file $dbentry"; #one more
+ time w/ error handling
print LAST "\n"; #add one last newline for good measure. =)
close(LAST); #make sure we're all neat
print "\nAll done! Added $info to the database.\n"; #self explanatory
bpm.pl
#!/usr/bin/perl
########################################
#             A. I. D. v1.0            #
#        database search script        #
#           coded by bitsmart          #
#       (c) 2007 Andrew Prentice       #
#          bitsmart0@gmail.com         #
########################################
print "\nThis program searches for songs with the same BPM.\n";
print "\nWhat BPM do you want to search for? ";
chomp($inpt = <STDIN>); #get the BPM to search for
print "searching..."; #make it look like we're doing something
$bpm = "BPM=$inpt"; #add our little tag
opendir(DIREC,".") || die "cannot open directory"; #open current direc
+tory w/ error handling
foreach $name(sort(readdir(DIREC))) { #for each filename beginning w/ 
+"BPM-", add to array
    if ($name =~ /^BPM-/) { #if the name contains "BPM-"...
        push @files, $name; #add it!
    }
    print "."; #add another "doing something" mark
}
closedir(DIREC); #close the dir
foreach my $filename ( @files ) { #for each filename in the array
    open(PHILE, '<', $filename) || die "cannot open $filename: $!"; #o
+pen file w/ error handling
    my @lines = scalar <PHILE>; #get the Artist/Album info
    while ( <PHILE> ) {
        push @lines, $_ if /$bpm/;
    }
    if ( @lines > 1 ) {
        print @lines;
    }
    else {
        print "sorry, no matching BPMs found."; #deliver the bad news
    }
    close PHILE; #finish up
}
Replies are listed 'Best First'.
Re: Album Information Database
by jwkrahn (Abbot) on Jan 05, 2007 at 13:42 UTC

    add.pl

    $dbentry = "BPM-$info.txt"; #add DB prefix if (-e $dbentry) { #check if file already exists (it should't) open(DBENTRY, ">>$dbentry") || die "cannot open file $dbentry\n"; +#open w/ error handling print DBENTRY "$info\n "; #append Album info (shouldn't have to ap +pend) close(DBENTRY); #make sure we're all neat } else { #we should be creating a new file open(DBENTRY, ">$dbentry") || die "cannot create file $dbentry\n"; + #open w/ error handling print DBENTRY "$info\n "; #add the Album info close(DBENTRY); #make sure we're all neat }
    You do realize that append mode will create the file if it does not already exist? So that should simply be:
    $dbentry = "BPM-$info.txt"; #add DB prefix open(DBENTRY, ">>$dbentry") || die "cannot open file $dbentry: $!"; #o +pen w/ error handling print DBENTRY "$info\n "; #append Album info close(DBENTRY); #make sure we're all neat

    bpm.pl

    @files = (@files, $name); #add it!
    Using push would be more efficient (and idiomatic.)
    push @files, $name; #add it!

    You should at least test your code with warnings enabled!

    $ perl -Wc bpm.pl Found = in conditional, should be == at bpm.pl line 33.
    That line should be:
    if ($flag eq "yes") { #if the flag is on

    But you could simplify that a lot and there is no need to store all of the files lines in an array, just store the ones that match /$bpm/:

    foreach my $filename ( @files ) { #for each filename in the array open(PHILE, '<', $filename) || die "cannot open $filename: $!"; #o +pen file w/ error handling my @lines = scalar <PHILE>; #get the Artist/Album info while ( <PHILE> ) { push @lines, $_ if /$bpm/; } if ( @lines > 1 ) { print @lines; } else { print "sorry, no matching BPMs found."; #deliver the bad news } close PHILE; #finish up }

Re: Album Information Database
by andyford (Curate) on Mar 03, 2007 at 17:44 UTC

    I store BPM values, album names, and well, everything! in the id3v2 or ogg tags. I recommend it because it keeps everything together and bonus points because the tags can be recognized by other programs.

    non-Perl: Andy Ford

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found