Assignment 8
Get Psyched Music Mix
"People often think a good mix should rise and fall, but people are wrong. It should be all rise, baby."
-Barney Stinson
Task
Your best friend Barney has asked you to write a program that will help him organize his MP3 collection. This program should examine the contents of a directory (containing MP3s) and list the tracks, sorted from fastest to slowest.
In order to implement this program, you will need to use some new python modules:
- os - to examine the contents of directories
- sys - to process user input from the command line
- mutagen - to read metadata (ID3 tags) from MP3 files
You will also need to employ a new data type, the dictionary.
Background
Metadata for MP3 files generally takes the form of information about a track's artist, album, etc. The standard format for MP3 metadata is the ID3 tag, which is compatible with most music players. For this assignment, a particular tag of interest is the beats-per-minute (BPM), which measures the tempo of a song. The higher the BPM, the faster the pace of the song.
Details
Part 1
While writing programs, you will often require capabilities beyond the standard libraries (the os and sys modules are standard). In such cases, it is possible to create a library, but generally some research will save you from re-inventing the wheel. For this assignment, reading the ID3 tag from an MP3 file is a key feature. Fortunately, the mutagen module is able to handle this task.
How to install:
Mac OS X
- Download the mutagen module
- Unzip the file.
- Open a Terminal window (in Applications->Utilities) and type 'cd Desktop/mutagen'
- type 'sudo python setup.py install' and enter your password when prompted.
- You can now delete the mutagen files on your desktop.
In either case, test if the installation worked by starting IDLE and typing 'import mutagen'.
Part 2
Write a function that retrieves the artist, track name, track length, and BPM from an MP3 file. It should take a filename as an argument, and return a dictionary. The keys in this dictionary should be "artist", "name", "length", and "BPM" (with values set accordingly.) If BPM information is not in the MP3 metadata, then that dictionary entry should not be used.
For full credit, the definition of this function must be get_MP3_info(filename) and it must return a dictionary.
Part 3
Write a function that recursively traverses a directory and calls get_MP3_info() on each MP3 file that it finds. It should take a directory path as an argument, and return a list. Each item in the list should be a dictionary containing MP3 metadata (obtained from get_MP3_info()).
For full credit, the definition of this function must be process_MP3_library(directory) and it must return a list.
Part 4
Write a function that prints the information gathered in Parts 2 and 3. The tracks should be sorted by descending BPM (highest to lowest). If BPM information is not present, you can skip printing that file's information. For example, a line of output might look like this:
R.E.M. Drive 4:31 140.05
For full credit, the definition of this function must be print_sorted_info(list) and it should not return a value.
Part 5
This program should be executed via the command line, e.g. python mix.py music. The last token (in this case, music) should be the name of a directory, which will be passed as an argument to process_MP3_library(). You can access arguments from the command line by using the argv variable (which is a list) in the sys module.
Sample data
You will need files that contain BPM information, which is not normally present. To test your program, you can download the following music collection, which contains several MP3 files with BPM information in the ID3 tags [1]. Keep in mind that your program will be tested on another set of files.
Assignment submission details
- Place all of your code in a file called mix_<UCID>.py (where <UCID> corresponds to your UCSD e-mail address). For instance jsmith@ucsd.edu would turn in mix_jsmith.py
- At the start of the file, write your name in comments. Also write a short explanation of how your program works, highlighting any assumptions you made about the problem, experiments you ran to show that the code worked, and anything else you want to communicate about the assignment.
- Attach this file to an e-mail message addressed to cg8w1@icogsci1.ucsd.edu with the subject "<UCID> mix.py"
- Send the email no later than Friday, March 16 at 6:00 pm.
Hints/Recommendations
- Start simple! Write and test one function at a time.
| [1] | Note that these files are licensed under a Creative Commons license and can be freely distributed (for non-commercial use). |