BUY THIS BOOK
Add to Cart

Print Book $29.95


Add to Cart

PDF $23.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £20.95

What is this?

Looking to Reprint or License this content?


Linux Multimedia Hacks
Linux Multimedia Hacks Tips & Tools for Taming Images, Audio, and Video

By Kyle Rankin
Book Price: $29.95 USD
£20.95 GBP
PDF Price: $23.99

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Images
Ever since the GIMP emerged as a killer app for image editing, Linux has held a unique place when it comes to image tweaking. On one hand, a number of great tools allow you to perform all sorts of manipulation on an image both in a GUI and in an automated command-line form. What's more, all these utilities are free. On the other hand, Linux is not popular with professionals, because some notable commercial image-editing applications aren't natively available on Linux. However, even without commercial support, the free tools available on a Linux machine provide a great deal of image-editing power, whether you are a pro or a novice.
The hacks in this chapter provide you with the tools and know-how to perform standard changes to your images, from cropping to resizing to creating thumbnails to adding watermarks. I have also included a whole series of hacks that delve into the power of the ImageMagick suite of tools. This command-line suite lets you perform all sorts of functions on your images and the command-line interface makes it simple to write scripts that automatically modify your images. I've even included some sample scripts to get you started.
If you take a lot of digital photos, you will find a number of hacks to help you through the full process of using a digital camera with Linux. Some of the hacks include tips to access your camera under Linux, automatically sync it with your computer, tweak your photos, and even remove red eye. I also cover some tools that help you create slideshows and manage even large directories full of years of photographs.
Use command-line or GUI tools to take screenshots in a variety of formats.
If you spend any time changing the look of your desktop with fancy themes and icons, what fun is it if you don't post your new cool desktop for everyone to see? The final proof of any successful desktop tweaking is a screenshot you can show off to your friends. You can use a number of methods to take screenshots, and this hack introduces you to some of the most common ones.
One of the simplest and most universal ways to take a screenshot is with the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 1–12: Introduction
Ever since the GIMP emerged as a killer app for image editing, Linux has held a unique place when it comes to image tweaking. On one hand, a number of great tools allow you to perform all sorts of manipulation on an image both in a GUI and in an automated command-line form. What's more, all these utilities are free. On the other hand, Linux is not popular with professionals, because some notable commercial image-editing applications aren't natively available on Linux. However, even without commercial support, the free tools available on a Linux machine provide a great deal of image-editing power, whether you are a pro or a novice.
The hacks in this chapter provide you with the tools and know-how to perform standard changes to your images, from cropping to resizing to creating thumbnails to adding watermarks. I have also included a whole series of hacks that delve into the power of the ImageMagick suite of tools. This command-line suite lets you perform all sorts of functions on your images and the command-line interface makes it simple to write scripts that automatically modify your images. I've even included some sample scripts to get you started.
If you take a lot of digital photos, you will find a number of hacks to help you through the full process of using a digital camera with Linux. Some of the hacks include tips to access your camera under Linux, automatically sync it with your computer, tweak your photos, and even remove red eye. I also cover some tools that help you create slideshows and manage even large directories full of years of photographs.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Take a Screenshot
Use command-line or GUI tools to take screenshots in a variety of formats.
If you spend any time changing the look of your desktop with fancy themes and icons, what fun is it if you don't post your new cool desktop for everyone to see? The final proof of any successful desktop tweaking is a screenshot you can show off to your friends. You can use a number of methods to take screenshots, and this hack introduces you to some of the most common ones.
One of the simplest and most universal ways to take a screenshot is with the import command from the ImageMagick suite of utilities. This is a very common tool, and chances are that ImageMagick is already installed by your distribution; if not, packages should be readily available. import has two major screenshot modes. To take a picture of the entire screen, type:
	$ import -window root screenshot.png 
The -window root argument tells import to take a picture of the entire screen. If you type:
	$ import screenshot.png 
Your cursor turns into crosshairs, allowing you to drag across the section of screen you want to copy. Once you release the mouse button, the part of the screen that is selected becomes the part import will put into your image. import supports all common image formats used for screenshots, including JPEG, PNG, and BMP (useful for lossless screenshots). All you have to do is name your output file with the file extension you want—import will figure out the rest.
If you want to set a time delay so you can arrange your windows or possibly hide the terminal containing the import command, just preface your import command with sleep. To allow five seconds before a screenshot is taken, type:
	$ sleep 5; import -window root screenshot.png
I like to timestamp my screenshots because it makes it easy to tell when they were taken, and prevents me from overwriting other screenshots. To name a screenshot after today's date, encapsulate the date command you want to use inside backticks. The following command will create a screenshot named YYYY-MM-DD.png with YYYY, MM, and DD filled in with the year, month, and day respectively:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Convert from One Image Format to Another
Use the ImageMagick convert tool to change between image formats.
When you deal with images on a regular basis, it's often useful to convert them to different image formats. You can use graphical tools such as the GIMP to open files and save them into different formats, but if you deal with a lot of images you might find that process a bit cumbersome. The ImageMagick tool convert solves this problem by providing a command-line interface to image conversion. With convert you can change any ImageMagick-supported image format to any other ImageMagick-supported image format. The full list of supported formats is rather large—you can view the full list in the ImageMagick man page (the man page is ImageMagick, not imagemagick)—but among the supported formats are BMP, CMYK, GIF, JPEG, PBM, PNG, RGB, SVG, TIFF, and XPM.
The ImageMagick tools are commonly used by a number of other frontends, so you are likely to find the convert tool already packaged for your distribution with the rest of the ImageMagick tools. The standard usage of convert is simple: provide an input file and an output file as arguments, and convert will figure out the format based on the file extension. So, to convert a BMP to a PNG, type:
	$ convert image.bmp image.png
One of the advantages to a command line image conversion tool is that it lends itself really well to scripting. For instance, the following command converts an entire directory of BMP files to JPEG—the bit of sed in the command preserves the filenames but changes the extension to .jpg, and the results are fed to convert, which knows from the extension what format to make the new files:
	$ for i in *.bmp; do j=`echo $i | sed -e 's/\.bmp/\.jpg/'`; \ 
	convert $i $j; done;
The backslash at the end of the first line denotes a line break in this book—you can enter everything as one entry.
convert also supports a wide range of image processing functions it can perform as it is converting an image. Even if you don't want to convert from one image format to another, you can still use
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Make Image Thumbnails
Use a simple script to create thumbnails from any number of images.
[Hack #2] introduced the convert utility as a means to convert between different image formats and perform basic image processing from the command line. convert also has resizing options that make it perfect for creating image thumbnails.
The primary argument used to create an image thumbnail is -thumbnail. This argument tells convert to create a thumbnail of the input image with the specified geometry. Thumbnails are scaled-down versions of the original, which means the height-to-width ratio is automatically preserved, so you need to specify only one dimension for the conversion. For instance, to create a thumbnail that has a width of 160 pixels, type:
	$ convert -thumbnail 160 image.jpg thumbnail.jpg
To create a thumbnail with a height of 160 pixels, precede the geometry with x:
	$ convert -thumbnail x160 image.jpg thumbnail.jpg 
This command comes in particularly handy if you plan to upload a lot of digital photos to a web gallery, because you can script the entire operation. To create a thumbnail with a width of 160 pixels from every JPEG image in a directory, type
	$ for i in *.jpg; do convert -thumbnail 160 $i thumb-$i; done; 
Each image in the directory will have a thumbnail with thumb- at the beginning of the filename. This command also works well to create a series of thumbnails of different sizes—just run two different convert commands in the script. So, to create a mid-sized thumbnail with a width of 400 pixels and a small thumbnail with a width of 160 pixels, type:
	$ for i in *.jpg; do convert -thumbnail 400 $i mid-$i; \
	convert -thumbnail 160 $i thumb-$i; done;
You can even get fancy and combine other convert arguments with your commands. For instance, to add a 10-pixel beveled grey frame around the mid-size image, and a 2-pixel grey frame around the small thumbnail, type:
	$ for i in *.jpg; do convert -thumbnail 400 -frame 10x10+2x2 \ 
	-mattecolor "#999999" $i mid-$i; convert -thumbnail 160 -frame 2x2 \ 
	-mattecolor "#999999" $i thumb-$i; done;
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Animate Images
Pass arguments to the convert tool to turn a directory of images into an animated GIF—all without opening a GUI tool.
The convert utility has plenty of tricks up its sleeve. One of the tricks is the simple conversion of a series of images into a single animated GIF file. Since convert can handle a large variety of image formats, you don't even need to start out with a directory of GIF-only images.
When it comes to image processing, convert does a lot of the heavy lifting on its own. In fact, the only option you need to concern yourself with is the -delay option, which specifies, in milliseconds, how long to wait between each image change. You can use the animate tool from the ImageMagick suite to test your animation. To see what your animation would look like with a one-second delay on a directory full of GIF images, type:
	$ animate -delay 100 *.gif
This command displays each image in alphabetical order, one after the other. If you want to display them in a different order, specify the images in that order on the command line. Once you are satisfied with your delay rate you can use the same number with the convert command. To convert a directory of GIF files into a single animated GIF with one second between frames, type:
	$ convert -delay 100 *.gif animated.gif 
Because I used a file glob here, I assumed the images were in alphabetical sequence. If they aren't in order, either specify each file on the command line (with the animated GIF specified last) or rename the files so that they are sequential. To see your newly created animated GIF, type:
	$ display animated.gif
This tool can be particularly useful if you take periodic images with a webcam, even if they are JPEGs. Simply put all the webcam images you want to animate into a directory and type:
	$ convert -delay 100 *.jpg animated.gif
Replace *.jpg with a file glob that matches your image format.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Leave a Watermark
Use the composite tool from ImageMagick to add custom watermarks to your images.
Copy protection, particularly digital copy protection, is a topic that has been discussed more and more over recent years. In the case of protecting graphical content, often what you want the most is simply to prevent people from taking your images and using them as their own. To protect against redistribution, many online image galleries have taken to adding watermarks—logos or text that identify the owners of the content—to their images (see Figure 1-1). The watermark labels the owner of the content kind of like your mom writing your name on your underwear before you went to camp. If someone decides to take your image and host it on their site, they must cut out your watermark and leave an easily identifiable hole in the image. This hack will describe how to use the ImageMagick tool composite to add your own custom watermark to your image gallery.
Before you watermark your images, you must first create the image you will use as a watermark. How the watermark is designed is mostly a matter of taste, but there are a few conventions you can use to make a good watermark:
  • Your watermark should have relatively small dimensions as compared to the image you will watermark. This might seem to go without saying, but if a watermark is too big, not only will it prohibit people from copying your image, it will also prohibiting people from seeing your image. Try your watermark on a couple of representative images to see just how much of the image it obscures.
  • A watermark should have a transparent background. This convention follows the thinking of the previous one. The goal is to display the watermark but not obscure the image, so a transparent background ensures your logo will be seen but the large borders around the watermark won't.
  • Consider a monochromatic watermark. It might be tempting to create a colorful and fancy watermark for your images, but the watermark will be combined with images of various colors, so a colorful watermark may clash with some of your images.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Pull Images from a Digital Camera
Get the images from your camera with either standard USB storage device support or gphoto.
So you have a digital camera full of pictures, and you have a Linux machine with the GIMP raring to go, but you need to get the pictures from the camera to the computer under Linux. As this hack illustrates, using your digital camera under Linux is not much of a headache, particularly with newer distributions and newer cameras.
Although there are many ways to categorize digital cameras, when it comes to using them under Linux there are only two categories: cameras that act as USB storage devices and cameras that don't. Generally, newer cameras act like USB storage devices (such as a USB thumb drive) when plugged into a computer, but many older cameras use proprietary communication standards even though they use standard USBs or serial ports. The easiest way to tell which type of camera you have is to plug it into a non-Linux system. If it shows up as a regular hard drive, under Linux you can treat it like a USB storage device. Otherwise you may have to go the gtkam route, which I describe later, in the section, "Non-USB Storage Devices."
Most modern desktop Linux distributions offer automatic management of USB storage devices—just plug in a USB key or other device, and KDE or Gnome will automatically create a new icon on your desktop that you can click to access the device. If you have such a distribution, then using your digital camera is easy: plug in the camera, click on the icon that appears on the desktop, and browse it like you would any other hard drive. You can copy and paste images to your desktop or another folder, and when you are done you can close the window, right-click on the icon, unmount the camera, and unplug it.
If you don't have such a desktop environment, it still isn't too difficult—you just have to go through an extra step or two. In order to mount your camera, you need to determine which SCSI drive Linux has assigned it. To do this, run tail on your /var/log/syslog or /var/log/messages log file as root, and then plug in the device. You should see output something like this:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Manage Photos with f-spot
Install f-spot and get an iPhoto-like photo organization tool under Linux.
Digital cameras have become so cheap and ubiquitous that every moment is a picture opportunity because there is always a camera—or camera phone—within reach. And with the ever-declining cost of flash storage media, the expense of taking just one more picture is so close to free that it hardly counts. These days, even casual photographers have thousands of images they need to keep track of and, because the photos are digital, they need to keep track of them on their computer. f-spot is a tool that aims to bring ease of use and power to photo management in Linux. It has a tag-based system of classifying groups of photos, as well as an integrated timeline feature that lets the user manage large collections by the date the picture was taken. f-spot also features good "touch up" tools that allow you to resize, crop, adjust color, and rotate groups of photos quickly without having to resort to larger tools such as the GIMP. f-spot is currently maintained by Larry Ewing, who, coincidentally, designed Tux the Penguin.
f-spot is built on the Mono platform and, as such, requires the runtime to be installed. This is usually handled by your Linux distributor, but might require the installation of Mono and GTK# by the end user. You can find Mono and GTK# at http://www.go-mono.com. The latest source code to f-spot can be found at its homepage at http://www.gnome.org/projects/f-spot. f-spot is still relatively young, so updates are common. But even at its young age, f-spot already has some powerful features.
When you first launch f-spot, you are met by an import dialog box. From here you can choose a folder to import your pictures from. (You can also search recursively inside folders and assign tags to the pictures. I'll go into tagging later.) After selecting the folder and clicking OK, f-spot begins importing your photos. The graphical thumbnails in the progress screen let you monitor your photos as they're being imported. When importing is complete, your photos will be thumbnailed in the main
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Edit Images
Use the GIMP to crop, scale, and otherwise edit your photos.
[Hack #6] discusses different ways under Linux to access the photos from a digital camera, but what tools do you use once the photos are on your computer? Sometimes you might be fine with a picture, but other times you may want to crop out something, resize the photo, or rotate it so it has the right orientation. While there are a number of programs that can do all these things—even some that run from the command line—the GIMP is the de facto standard when you want to edit images graphically. In this hack, I cover how to perform some of the common changes you might want to make to a photo or other image. The GIMP can do a number of pretty advanced things, but this hack sticks to the basics.
The first step is to install the GIMP. Installation is made easy by the fact that this is a very popular program that has been around for a long time. Any major Linux distribution should offer the GIMP in its packages, and some even install it by default, so check your application menu. If it isn't installed, just use your standard packaging tool to install it.
To start the GIMP, select it from your desktop's menu or, alternatively, type gimp in a terminal. The first time the GIMP runs, it goes through a basic configuration wizard; the defaults should be suitable for most users. The default interface might seem daunting at first if you aren't experienced with image editing tools, but for basic editing you only need to be concerned with a few of the widgets you see. After the GIMP has loaded, click File → Open and select the image you want to edit.
The GIMP will open the image into its own window and depending on the size of the image, the GIMP might scale down the image to fit on the screen. To zoom in, hit the + key on the keyboard or click the magnifying glass button on the toolbar and then click on the image. To zoom out, hit the–key or click the magnifying glass button, and then hold Ctrl when you click on the image.
One common image-editing function is image cropping. Cropping lets you select the part of the image that you want to keep and removes everything else. This can be useful if a picture is a bit off-center, or if you want to focus on a particular subject in a photo. To crop an image, first click the crop tool on the toolbar (it's the one with the knife icon), right-click on your image and select Tools → Transform Tools → Crop & Resize, or press Shift-C. Next move your mouse to the top corner of the area you want to crop; then click and drag the cursor down to the opposite corner. As you move the cursor, use the location of the cursor shown at the bottom left-hand corner of the window to gauge the cursor location. Use the Crop & Resize window that appears to fine-tune the coordinates (Figure 1-3). Then click the Crop button in the Crop & Resize window to complete the crop.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Remove Red Eye from Photos
Follow a few basic steps with the GIMP to remove red eye from photos.
Red eye happens to the best of us. Nowadays many cameras have red-eye reduction features, but whether you are dealing with older scanned-in photos, or you just forgot to turn on that feature, sometimes you get digital photos that have red eye in them. Since red eye seemingly turns even the sweetest child into demon spawn, you generally want to remove it from your photos before you send them out to relatives. Although it might seem as if you need extensive experience and expensive tools to edit your photos, with the GIMP you can take out red eye in a few simple steps.
The basic steps in this procedure are to select the red area of the eye and then open the channel mixer and change the red, green, and blue values until the red eye looks like a normal pupil. The specifics of these steps are a bit more involved, but as you'll see, they mostly require a bit of time and tinkering.
First you need to select the red eye. Zoom in your image (hit the + key) until the part of the picture with the red eye is mostly filling the window. There are a number of ways to select the red part of the eye, but the fuzzy selection tool is the easiest to use.
The simplest way to select the red part of the eye is to go into the different color channels and disable all but either the green or the blue color channel. Click File → Dialogues → Channels, and click both the red and blue channels so that only the green channel is selected (alternatively, you can select only the blue channel). Now click the eye icon next to the red channel so that you see only the blue and green channels in the image.
Change to the fuzzy selection tool (hit the Z key or select the tool from the toolbar) and click on a red portion of the eye. The first time you do this, you probably won't select all the red, so cancel the selection with Ctrl-Shift-A. Then go to the fuzzy selection tool settings window, make sure feather edges is turned on, increase the threshold, and try again. This part of the process probably requires the most tinkering, so just try different threshold values until you select all of the red. Once you have it right, hold down the shift key and click on a red section of the other eye to select it, too. Now you can go back to your channels and turn on visibility for the red channel and make sure that all three color channels are on. With both eyes selected, you can move on to taking out the red.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Create a Slideshow
Use Kuickshow or gThumb to turn a directory of photos into a slideshow.
For just about every vacation, there is a vacation slide presentation. After all, most people don't just take vacation pictures for themselves, they take them for their friends and family to see as well. In the past, to set up a slide presentation you had to take your film to get developed into slides, and then arrange them into your slide projector. Today with a digital camera, you can quickly, easily, and inexpensively set up a slideshow with open source tools.
There are a number of open source slideshow tools, but for the purposes of this hack I discuss two: KuickShow and gThumb. These slideshow programs are parts of the GNOME and KDE desktop environments respectively, but you can run them on either environment. In addition, one of the programs most likely will be prepackaged by most major Linux distributions.
KuickShow is a simple slideshow program for KDE. You can launch it from the Graphics programs menu for your desktop environment, or you can type kuickshow from the command line. The KuickShow interface is relatively simple. The toolbar along the top provides you with file system navigation tools and buttons to launch the slideshow and configure KuickShow itself (see Figure 1-4). The main section of the window along the bottom is a basic file system browser that lets you navigate to the section of the file system containing your images. Navigate to your images directory and then click the Start Slideshow icon on the toolbar, click File → Start Slideshow, or hit F2 to start your slideshow. KuickShow will display the images one by one, with a delay between images. Hit Esc to exit the slideshow.
KuickShow is pretty configurable. Click Settings → Configure KuickShow to open the configuration window. Here you can configure the background color that displays behind images, apply brightness, contrast, and gamma adjustments, toggle whether to switch to full screen, configure the delay between slides, and change the default key bindings.
gThumb is a GTK application that is more photo management program than slideshow tool. Select gThumb from the Graphics programs menu for your desktop environment or type
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Automatically Synchronize Your Camera and Computer
With a custom hotplug script, you can automatically synchronize your computer and USB storage device camera just by plugging in the camera.
On a particular vacation where I brought both my laptop and my digital camera, I noticed I was offloading the day's pictures onto my laptop each night. The manual process was to plug in the camera, mount it as a USB storage device, create a new directory for the images, copy the images to the new directory, and, finally, unmount the camera. With a few tweaks to hotplug and a little detective work, I was able to make the entire process hands free: I now plug in the camera, and it automatically synchronizes everything for me. In this hack I describe the tweaks to hotplug, along with tips for creating your own custom synchronization script.
Hotplug is a program under Linux that manages hot-pluggable devices. For the most part it is a program you don't have to think about—just plug in your USB mouse, for instance, and hotplug will make sure the applicable drivers are loaded. Hotplug is very powerful, however, in that it allows you to customize what to do when a certain device is plugged in. The first step to configuring hotplug is to plug in a camera and scan the system logs to make sure the camera is recognized. In the case of my camera, hotplug recognized that it was a USB storage device and made sure my usb-storage module was already loaded:
	Jan 19 15:46:27 clover kernel: hub.c: new USB device 00:02.0-1, assigned 
	address 4 
	Jan 19 15:46:27 clover kernel: WARNING: USB Mass Storage data integrity not 
	assured 
	Jan 19 15:46:27 clover kernel: USB Mass Storage device found at 4 
	Jan 19 15:46:31 clover usb.agent[10819]: kernel driver usb-storage already 
	loaded
USB drives (and in this case cameras that function as USB drives) work as regular SCSI hard drives in this situation and require the sd_mod module to be loaded. You could just mount the device that is created (/dev/sda1 typically, unless multiple drives are plugged in simultaneously), copy files, and be finished with the camera. But if you want to automatically synchronize this camera to your computer, you must perform special commands just for this drive, and not for any other USB storage device. To set up automatic synchronization, you first need some information from
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Make a Screen-Capture Movie
Create animated screen-capture movies as the ultimate aid in a presentation or tutorial.
If a picture is worth a thousand words, then how about a thousand pictures—or a graphical animation? Getting your message across depends a lot on the tools you have at hand, the environment you'll be presenting to, and the audience. It's one thing to stand in front of a classroom with a blackboard, a projector, and a room full of PCs, but it's quite another to describe a set of ideas to multiple people standing in front of a kiosk, in a hall filled with competing noises and distractions. Wouldn't it be great to create an animated presentation complete with screenshots and textual notes without first taking a course in multimedia and purchasing expensive development applications? Of course it would!
Let's set the stage: the objective is to create a screen-shot movie that demonstrates how to use a feature in an application. In addition, you should be able to provide commentary within the movie to explain what's going on.
Ideally, you can make a movie with tools that don't take long to learn and use. The technique demonstrated here shows how to capture screenshots in rapid succession. These screenshots are then converted into a single file that can be read by nothing more complicated than a browser.
For this hack you'll need the following tools:
  • The ImageMagick command-line utilities to create, edit, and convert images from one format to another
  • xwininfo, the window information utility for X
  • A small bash script to record multiple screen captures
Launch the application you want to record. Use xwininfo to obtain the target window's hexadecimal ID number. When you run the command, your cursor changes to a crosshair—just click on the window whose information you want to capture. In this example, the ID number, 0x140001d, comes from the fifth line in the output below:
	$ xwininfo 

	xwininfo: Please select the window about which you
              would like information by clicking the
			  mouse in that window.
    xwininfo: Window id: 0x140001d "making movies.sxw - OpenOffice.org 1.1.0 " 
      Absolute upper-left X: 4 
	  Absolute upper-left Y: 18 
	  Relative upper-left X: 0 
	  Relative upper-left Y: 0 
	  Width: 920
	  Height: 630
	  Depth: 16
	  Visual Class: TrueColor
	  Border width: 0 
	  Class: InputOutput 
	  Colormap: 0x1400001 (installed) 
	  Bit Gravity State: ForgetGravity 
	  Window Gravity State: StaticGravity 
	  Backing Store State: NotUseful 
	  Save Under State: no 
	  Map State: IsViewable 
	  Override Redirect State: no 
	  Corners: +4+18 -100+18 -100-120 +4-120 
	  -geometry 920x630+4+18
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Audio
Many people in the Linux community believe that choice is good. Perhaps second only to text editors, audio applications under Linux truly embody this philosophy. Even if you limit yourself to strictly audio players, you don't have enough fingers and toes to count all the programs out there. XMMS, Rhythmbox, amaroK, juK, and others are on the GUI end; hybrid programs can play both audio and video files; and command-line tools such as mpg123 and mpg321, may also have GUI frontends.
Everyone listens to music differently. Some people listen to a single CD or album, every track in sequence. Others put their entire music collection on shuffle. Some people like minimalist audio players while others want every option and effect under the sun. With all the audio choices under Linux, no matter how you like to listen to your music, Linux has an application that fits. Beyond simply listening to music, Linux also has a number of tools to help you rip, mix, manage, and burn your audio—even "scratching" your digital audio is possible.
In this chapter, I highlight some of the best Linux audio applications. There are also hacks in this chapter that take you through the full audio extraction process from ripping CDs to encoding them to tagging the resulting files. Linux has a number of amazing audio tools to automate common tasks, and I've tried to include some of the most useful. At the end of the chapter, I also cover how to access a number of different portable media players under Linux so your music can follow you wherever you go. So put on some music, crank the volume up to 11, and let Linux handle the rest.
Use the mixer to understand and tweak the different volume levels your sound card provides.
Almost every audio device you encounter has some way to control the volume, whether through a volume knob on a radio, the remote control on a television, or the series of controls on a sophisticated sound board. To control the sound on a Linux system, you use a mixer program. Even though the purpose of the mixer—to change the volume—is simple, often the sheer number of options the mixer provides can prove daunting to new users: it's not as simple as adjusting a single master volume control. Also, Linux has two different systems for sound, OSS and ALSA, so there is additional complexity that is not immediately obvious. In this hack, I cover a few mixer programs and describe the differences among Master, PCM, and the other major volume controls.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 13–46: Introduction
Many people in the Linux community believe that choice is good. Perhaps second only to text editors, audio applications under Linux truly embody this philosophy. Even if you limit yourself to strictly audio players, you don't have enough fingers and toes to count all the programs out there. XMMS, Rhythmbox, amaroK, juK, and others are on the GUI end; hybrid programs can play both audio and video files; and command-line tools such as mpg123 and mpg321, may also have GUI frontends.
Everyone listens to music differently. Some people listen to a single CD or album, every track in sequence. Others put their entire music collection on shuffle. Some people like minimalist audio players while others want every option and effect under the sun. With all the audio choices under Linux, no matter how you like to listen to your music, Linux has an application that fits. Beyond simply listening to music, Linux also has a number of tools to help you rip, mix, manage, and burn your audio—even "scratching" your digital audio is possible.
In this chapter, I highlight some of the best Linux audio applications. There are also hacks in this chapter that take you through the full audio extraction process from ripping CDs to encoding them to tagging the resulting files. Linux has a number of amazing audio tools to automate common tasks, and I've tried to include some of the most useful. At the end of the chapter, I also cover how to access a number of different portable media players under Linux so your music can follow you wherever you go. So put on some music, crank the volume up to 11, and let Linux handle the rest.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mix Your Audio for Perfect Sound
Use the mixer to understand and tweak the different volume levels your sound card provides.
Almost every audio device you encounter has some way to control the volume, whether through a volume knob on a radio, the remote control on a television, or the series of controls on a sophisticated sound board. To control the sound on a Linux system, you use a mixer program. Even though the purpose of the mixer—to change the volume—is simple, often the sheer number of options the mixer provides can prove daunting to new users: it's not as simple as adjusting a single master volume control. Also, Linux has two different systems for sound, OSS and ALSA, so there is additional complexity that is not immediately obvious. In this hack, I cover a few mixer programs and describe the differences among Master, PCM, and the other major volume controls.
You can launch a mixer program a few different ways. If you use the GNOME or KDE desktop environments, the quickest way to access the mixer is to click the speaker icon on the desktop panel. Windows users will find this applet similar to the speaker on the Windows taskbar. A single click on the icon lets you change the master volume (see Figure 2-1); a right-click shows you various options for the mixer applet. For common volume control, you may only need to access the master volume. To access all of the advanced volume controls, double-click on the applet to launch a complete mixer.
Figure 2-1: Panel Mixer applet
Whether you use OSS or ALSA sound drivers, you will find a base set of volume controls in a mixer application that are based on the default volume controls on the OSS mixer (/dev/mixer). OSS is the older of the two sound systems. As such, it is often more stable than ALSA, although a number of newer sound cards are only partially supported in their OSS drivers, if at all. OSS is still present in the latest Linux kernels, but ALSA is the preferred sound system, as it often provides more complete support for newer sound cards—something you will notice in the different options you have in an ALSA-ready mixer. Whichever sound system you use, when you launch the mixer applet, you will see at least these basic volume controls:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Surround Yourself with Sound
Get theater-quality sound out of your multi-speaker setup.
When you've invested a lot of money in your audio and speaker setup, you want to make sure you get the most out of it. Don't settle for basic stereo sound—make your bass go boom boom, your center channel speak, and your rear speakers provide ambience. Under Windows, configuring this is often quite easy, as the soundcard manufacturer usually provides a program to enable surround sound, but no such software exists for Linux, so you need to go a more manual route. This hack will show you how to enable surround sound, test it, and use it with MPlayer [Hack #48] .
For this hack to work, you need a soundcard that supports more than just stereo output and, of course, multiple speakers. The ideal setup, and the one I am targeting in this hack, is what is known as "5.1 surround sound." This means you have a pair of front speakers, a pair of rear speakers, a center channel speaker, and a subwoofer. As you can see, this is 5 speakers plus a .1 for the subwoofer. (Just to confuse things, some settings will call this a 6 channel configuration.) Here is what you can expect out of each speaker:
Front right and left
These two speakers provide basic stereo output. Most music CDs are designed to output stereo sound, which means these two speakers are all that is required to have an enjoyable music listening experience. Though adequate for listening to a DVD, you'll miss out on many positional effects (the placement of sounds so that it seems as if they are coming from specific places, both on and off screen). When your system is configured for 5.1 surround sound, these speakers will provide the bulk of your nonspeaking sound.
Subwoofer
The job of the subwoofer is to output low frequency sound—just how low depends upon the quality of your subwoofer. With a subwoofer added to your setup, your other speakers are relieved of the duty of producing low bass sounds. This is good, because those speakers aren't really designed for that type of output, and they'll sound better because they will only be playing the audio they can handle well. Bass output is nondirectional, so it doesn't matter where you place the subwoofer: you should still be able to hear and feel its effects. When the subwoofer is combined with just the front right and left speakers, it is known as 2.1 surround sound.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Play Multiple Sounds at the Same Time
Play audio from multiple sources—even if your sound card doesn't support it.
Most Linux users don't think too much about exactly how sound is mixed. On many sound cards, you can send only a single sound at a time. So, for instance, if you are playing an MP3, you won't be able to hear any desktop sounds you have configured, and your instant messenger won't audibly alert you to incoming messages. Although some sound cards can support mixing from multiple sources, many less-expensive cards and those integrated on motherboards don't, so as a result you need to use a sound server. A sound server, in effect, sits between your applications and the sound card, mixes the different sound sources, and presents a single source for output. The default sound server under KDE is aRts, and the default under GNOME is EsounD. This hack covers how to use and configure aRts and EsounD.
aRts (Analog Real-Time Synthesizer) is a system of modules used to synthesize audio. These modules can perform various audio tasks, such as adding audio effects, mixing, filtering, and a number of other things. The artsd sound server uses this system to mix audio from multiple sources in real time. Because it sits between the sound card and applications, an application must have aRts support to directly take advantage of the artsd server (there's a workaround if it doesn't, which I will talk about in just a bit). aRts is a core component of KDE (at least for the time being), so it is fully supported there, and a number of other media applications can use aRts as well, through the use of plug-ins.
EsounD (or Enlightened Sound Daemon) is a sound server like artsd. It sits between the sound card and programs accessing the sound card so that it can mix sounds and pass them along as output. Because of this, an application needs EsounD support to work with esd properly. Since GNOME defaults to using EsounD as its sound server, GNOME applications generally have EsounD support. EsounD has been around for some time, so there's a good chance that recent media applications (again, particularly those for GNOME) support EsounD either directly or through a plug-in.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Get MP3 Libraries for Red Hat–Based Distributions
Enable MP3 support on Red Hat–based distributions with a simple addition to yum.
Software licenses, particularly licenses for multimedia libraries, have always been a touchy point for Linux. There are a number of proprietary media formats out there competing for our eye and ear. Many of these media formats have restrictions in their licensing that require either the user or, more often, the developer of multimedia software to pay a licensing fee to use their library. One such media format is MP3.
Many people consider MP3 to be a "free" format simply because it doesn't have DRM functionality built-in but, in fact, the company Fraunhofer Gesellschaft owns a number of patents on parts of the technology that creates MP3s and requires that MP3 encoders and possibly MP3 decoders pay licensing fees. Most Linux distributions manage the MP3 encoder licensing simply by not including MP3 encoding libraries directly into their distribution. Instead you must download the encoder from a third-party source to limit the distribution's liability ( [Hack #24] will provide more information about MP3 encoders under Linux). For the most part, Linux distributions have historically ignored the liability for shipping MP3 decoding libraries—that is, until Red Hat 8. Starting with this version, Red Hat dstopped shipping MP3 libraries with the OS (http://www.redhat.com/advice/speaks_80mm.html). As a result, if you use Red Hat or a derivative such as Fedora or CentOS, you will need to go through an additional step to install MP3 support.
To install MP3 support on Red Hat–based systems, you will first need to add a third-party package repository to your package manager. Although there are several repositories that provide the package you need, for this example, I use the Dag software repository (http://dag.wieers.com/home-made/apt) for everything.
The first step is to add the Dag GPG key to RPM's list of keys. To do this, open a console, become root, and type the following:
	# rpm --import http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Configure Network Sound
Set up local sound to play across the network.
In "Mix Your Audio for Perfect Sound" [Hack #13] I discuss how to use EsounD as a sound server to mix sounds from multiple programs for a sound card that supports only one input. One extra feature of EsounD is the ability to send and receive sound over the network. This can be particularly useful if you use programs like VNC over a local network, because you can use audio applications remotely and hear them on your local machine. Plus, in general, it's just pretty cool to be able to send sound around over the network.
To do this esd must be installed on the server (the machine on which you hear the sound). You don't have to have esd installed on the client—your client doesn't even need a sound card—but make sure your programs are EsounD-compatible. Most distributions should have EsounD packaged, so install it from your package management tool. If you use GNOME, EsounD is probably already installed. Once esd is installed on the server, you need to start it with special options so that it knows to listen to the network. Kill any open esd processes, and then start esd from the command line:
	greenfly@napoleon:~$ esd -promiscuous -tcp -public -port 16001
You should hear a series of beeps on your computer to tell you that esd has started. Now on your client machine open a terminal and type:
	greenfly@moses:~$ esdplay --server="ip_of_server:16001" sample.wav
Replace ip_of_server with the IP address of the esd server. The audio file will play back on the remote server, so be sure to adjust the mixer on that machine so that you can hear it. This works fine on an application-by-application basis, but you can also set an environment variable so that any EsounD-compatible applications you start will output to the remote server. Type the following in a terminal:
	greenfly@moses:~$ export ESPEAKER="ip_of_server:16001"
Now you can start XMMS, mplayer, xine, or any of the EsounD-capable audio players from this terminal and the sound will be sent over the network. Be sure to add this setting to your
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Manage Your Audio with XMMS
XMMS is a classic Linux audio player you can use to easily play a number of different audio file types.
A number of audio players are available under Linux, but XMMS is a classic that you are likely to find available no matter which Linux distribution you use. XMMShas an interface similar to Winamp under Windows and can play most popular audio formats, including MP3, WAV, Ogg Vorbis, and audio CDs, along with many other formats, if you install the proper input plug-in.
The default XMMS interface has controls to play, pause, stop, and skip forward and backward in your playlist—believe me, they're all there, just very tiny. To open the playlist editor, click the button labeled PL on the interface, right-click on XMMS, and select Playlist Editor, or type Ctrl-E. Within the playlist editor, you can add, delete, arrange, and sort tracks. To add tracks, select the add button and browse to the directory, or simply drag and drop files from your file manager onto the playlist. You can also save playlists you have created so that you can refer to them later. All playlist options that are available via buttons on the bottom of the interface can also be accessed if you right-click on the playlist. The audio settings are adjusted with the equalizer. To display the equalizer, click the EQ button next to the PL button on the interface, right-click, and select Graphical EQ, or type the keyboard shortcut Ctrl-G.
XMMS supports streaming audio. To add a streaming audio source to the playlist, click Add → URL and type in the URL.
Once the playlist has been set up, click the play button or type x. The buttons to control playback including previous track, play, pause, stop, and next track can be accessed with z, x, c, v, and b, respectively. Notice how all of the keys on the keyboard line up in the same order as on the interface. As a track is playing, you can control the volume and balance directly from the main window. To skip ahead in a track, just click and drag the long bar that slowly progresses along the window. The main window also has checkboxes so that you can toggle shuffle and repeat modes for the playlist.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Shuffle Your Music the Smart Way
Use IMMS to weight your music collection based on your listening habits.
Think of all the ways you interact with a computer each day. Any action you take, or even don't take, conveys some meaning. For example, when listening to your music collection, you might sometimes skip songs. What does that mean? There are a number of possibilities. Maybe you do not like the song that was playing, or it does not suit your current mood, or, possibly, you've listened to this song too much and would rather it be played less often.
Is it possible to build a system that uses this information to learn which music you prefer and play it more often? Yes! Intelligent Multimedia Management System (IMMS) is an attempt to create such a system—an adaptive playlist framework that tracks your listening patterns and dynamically adapts to suit your personal music tastes. Plug-ins are currently available for two popular Linux media players: XMMS (http://www.xmms.org/) and Beep Media Player (http://beepmp.sourceforge.net/).
The main feature of IMMS is its complete transparency to the user. It is incredibly unobtrusive—you never have to interact with IMMS directly. Just continue using your player as usual and, over time, IMMS will influence the song selection to cater to your preferences. When your music player chooses the next "random" track in shuffle mode, IMMS weights its choice, based on which songs you've played and which songs you've skipped previously.
IMMS also offers a number of features beyond basic rating. For example, it keeps track of when a song was played last and makes sure the same song does not repeat too often—a common complaint with traditional shuffle-based systems. Moreover, it is able to recognize different versions of the same songs (for example remixes) and treat them as the same song.
IMMS also learns which songs should be played together and which should not. It does so by both watching and learning from the user, and performing acoustic analysis on the actual songs to determine their tempo and spectral "color," and then attempts to group more similar songs together.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Try Rhythmbox
Use Rhythmbox to manage your audio collection much like the iTunes audio player.
There are many different approaches to audio players under Linux. Some players focus on minimalism, others focus on features, and some aim to provide similar functionality to a media player on another platform. Rhythmbox falls into the latter category, as it was created to provide an iTunes-like interface for audio under Linux.
Rhythmbox is a popular package, and most major distributions package it. If your distribution doesn't, you can download the source from the official project page at http://www.rhythmbox.org and compile and install it yourself.
To start Rhythmbox, launch it from your application menu or type rhythmbox in a console. The first time you run it, Rhythmbox will take you through an initial configuration wizard so it can find your music files. This step is optional, but I recommend you go through the full wizard so that Rhythmbox can find your files.
After you are finished with the wizard, Rhythmbox will begin scanning your entire MP3 collection (or other media files) for its internal database. Instead of organizing your collection by directory path, Rhythmbox organizes your collection based on the ID3 tags it finds in your MP3s or Oggs, so you can quickly build collections or find songs based on the artist or album. The initial scanning process can be a bit time consuming (ten to thirty minutes, depending on your machine and the size of your collection), and you can watch the collection grow in the main window as Rhythmbox finds new music.
Unlike music players such as XMMS, Rhythmbox has a large main window but displays only a basic set of controls. Most of the window is taken up with playlist and collection information (see Figure 2-5). The left panel displays music options, such as your complete library of music, playlists you created, streaming radio, and so forth. The right side of the main window is split into a browser and a track list. The browser provides filters that apply to the track list to make it easy to filter and find tracks based on attributes such as artist and album (or genre, in the case of streaming audio).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Let amaroK Rock Your Music Collection
Use amaroK to integrate great music features such as album art, Audioscrobbler, Musicbrainz, song lyrics, and great playlist management into one application.
As more people have started listening to their music collections on their computers, a number of Internet services have appeared to help manage collections and to utilize all this great metadata that exists on MP3s. The services have ranged from CDDB and FreeDB, which provide album and track information based on CD signatures, to services that provide album art and lyrics. A number of the music players under Linux have utilized one or two of these types of services either natively or through the use of plugins, but the amaroK music player seems to be one of the best at integrating them all by default into a useful and powerful interface. In this hack, I discuss many of the different features of the amaroK player and how to get the most out of them.
amaroK