Gigi Sayfan specializes in cross-platform object-oriented programming in C/C++/ C#/Python/Java with emphasis on large-scale distributed systems. He is currently trying to build intelligent machines inspired by the brain at Numenta (www.numenta.com).
My wife recently completed certification as a spinning instructor -- you know, riding real hard on a stationary bike -- and began teaching classes. It turns out that music is very important for spinning, and instructors have to prepare CDs with proper tracks for warm-up, main work-out, and cool down. Each part is measured and you have to find the right tracks with the proper combined length. Of course, you could do this manually by looking at the duration of each track and try to add them together, but this approach is tedious and error-prone. A more serious problem is that the duration of MP3 files is recorded as an ID3 tag in the file header. There is no guarantee that this number is correct (and often it is not). This situation is simply screaming for a software solution.
The solution I present in this article -- the "MP3 Duration Calculator" -- lets you select MP3 files from a directory and calculate the total duration of the selected files on-the-fly. The complete source code and related files are available here. To calculate the duration of each track, the software actually opens the file in a media player and gets an accurate duration number. Figure 1 shows the folder selection dialog where you choose the folder that contains the MP3 files.
Figure 2 shows the main window of the MP3 Duration Converter.
The program is a WPF application implemented in C# over the .NET 3.5 framework. It demonstrates a few interesting aspects such as:
- Dynamically loading MP3 files
- Working with the MediaElement component
- Asynchronous execution of long operations while keeping the UI responsive
- On-the-fly resizing of GridView columns (more obscure then it should be)
- Data binding and custom conversion
- Application state persistence
An Introduction to WPF
WPF (Windows Presentation Foundation) is the user interface/display system of Windows. It is built on top of DirectX and can take advantage of hardware acceleration. It is more advanced than previous UI APIs (including the .NET Windows Forms) all based on GDI/GDI+ and the User32 API. WPF offers a declarative UI (XAML), animation, multi-media support, rich text model, web-like layout model, browser-like page-based navigation, rich drawing model (including 3D) based on primitives and not pixels, totally customizable look-and-feel for controls (via templates and styling), and more.
Silverlight, Microsoft's rich web client platform, is based on the same design and is almost compatible, which really helps if you want to multi-target your application to the desktop and the browser. At this writing, WPF is not supported by Mono (and probably won't in the near future), so if you need to target multiple platforms your best bet is still the Windows Forms API.
XAML, short for "eXtensible Application Markup Language," is an XML dialect for instantiating .NET objects and hooking them up. It can be used for many purposes, but currently its primary use is to define WPF user interfaces. It provides the following capabilities:
- Full namespace support
- Resource definition
- Animation definition
- Control template definition
- Hooking up event handlers
- Data binding
- Design time integration with Visual Studio 2008
This is a rich feature set for a UI description markup language. It should be noted that XAML is not required and you can code any WPF construct programmatically. However, many things are easier with XAML, especially if you use Visual Studio as your IDE.
MP3 Duration Calculator Design
The design is straightforward. There are four classes:
- App (the application class)
- TrackDurations (engine)
- DurationConverter (a helper class)
- MainWindow (GUI).
The App and MainWindow classes are a code-behind classes for the corresponding XAML files: App.xaml and MainWindow.xaml.