Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PERL TUTORIALS > CULTURED PERL: FUN WITH MP3 AND PERL, PART 2


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Cultured Perl: Fun with MP3 and Perl, Part 2
By Teodor Zlatanov - 2004-04-08 Page:  1 2 3 4 5 6

More manipulating and guessing MP3 tags with Perl

Ted continues his look at manipulating and guessing MP3 tags with Perl, FreeDB, and various CPAN models via his autotag.pl application.

This article is the second of a two-part series. Before reading this article, please take a look at Part 1, which will introduce you to the autotag.pl application and the rationale for the various modules used in it. We pick up directly where we left off last time.

Preparing for the main loop

The main loop of autotag.pl will identify and tag music. In order to do that, some preparations are in order. First, I create the FreeDB search object using the WebService::FreeDB module.

The search object inherits the DEBUG setting from the autotag.pl configuration, so that the user does not have to remember a FREEDB_DEBUG setting for autotag.pl. The host is provided by the autotag.pl configuration as well.

Listing 1. Creating the WebService::FreeDB object


my $cddb = WebService::FreeDB->new(DEBUG => $config->DEBUG(),
                                   HOST => $config->FREEDB_HOST);

die "Could not initialize the FreeDB service"
 unless defined $cddb;

Next, I create some hashes: %discs, %olddiscinfo, and %disc_counts. Also, the @common list is created. All these variables will be useful in the course of the main loop. Note that every search result in FreeDB is identified by a unique ID, and that's all I'm storing until much later in the program.

I step through all the user-provided command-line searching switches, such as -artist and -album (using the keys of the %freedb_searches hash instead of listing the switches manually). The get() method of AppConfig can be used to get the value of an individual parameter; because the parameters of interest are always array references, I automatically de-reference them. If no searching switches are provided, I enter the interactive mode where the user can provide search criteria interactively.

Listing 2. Are the searching switches on?

my $search_count = 0;
foreach my $search (keys %freedb_searches)
{
 $search_count += scalar @{$config->get($search)};
}

print "Search count is $search_count\n"
 if $config->DEBUG();

It may seem like a little thing, but using the %freedb_searches hash to get the list of searching switches makes the code shorter and more maintainable. You should always look for such ways to eliminate repetition of constants and string literals in your programs.

Armed with my knowledge of the search count, I may need to enter the interactive query mode (the user is asked if he wants to do so, and if not, the program quits gracefully). In the interactive mode, the user starts with some primitive guesses of the artist and track name using the appropriately named guess_artist_and_track() function. These guesses are made across all the files given to autotag.pl, and guesses accumulate in the %guessed hash, using sub-hashes so that repeated finds of an artist's name in all the MP3 files will only generate one guess. The user is then asked whether those guesses are good for each search. For instance, when I ask for artist searches, the artist name guesses are offered to the user first.

Thus, in due order I come to the interactive query for searches. For each search in the %freedb_searches hash, the user adds more searches. If he just presses Enter, the read_line() function will return an empty string, and such input is taken as an indication that the user wants to go on.

Listing 3. Interactive query of searches

while (my $data =
 read_line("Add a search by $search or ENTER to go on: ", ''))
{
 last unless defined $data && length $data;
 push @{$config->get($search)}, $data;
}

Again, I use the get() method of AppConfig to get an array reference to the configuration list and push the user-provided data into that array.



View Cultured Perl: Fun with MP3 and Perl, Part 2 Discussion

Page:  1 2 3 4 5 6 Next Page: Initial searches

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.