Saturday, June 20, 2009

Getting AC3 Pass Through Working in Linux

This guide is about sending AC3 stream (Dolby Digital or DTS) to a receiver when playing a video file in Linux, and have the receiver doing the decoding rather than Linux.

Issues

Linux movie players require a sound backend to play. There are two main flavors of sound backend, gstreamer and xine. Unfortunately the AC3 pass through capability is broken in gstreamer. So we need to use xine. Thus the choice of the correct movie player becomes critical if you want AC3 pass through. Adding in the complexity of ALSA, setting up AC3 pass through in Linux is non trivial.

Approach

I am going to use totem-xine for movie playback, and set totem-xine to use ALSA with AC3 pass through.

My Setup

My PC has two sound cards, the on board sound card and a M-Audio Revolution 5.1. The connection to the receiver is on the M-Audio.

Linux distro I am using is Ubuntu 9.04. Aside from getting totem-xine, the instructions should apply to all distros.

Step 0: Preparation

You should have the following files for testing purpose:
  • a video file that has a Dolby Digital or DTS track
  • a .wav file
Your receiver should already be connected to your sound card using coax or toslink cable.

You should know how to open a text terminal and edit text files using application such as gedit.

Step 1: Obtaining totem-xine

I am going to use totem-xine in this guide. Totem-xine is identical to the default movie player except totem-xine uses xine as the sound backend.

To obtain totem-xine, open a text terminal, type:
sudo apt-get install totem-xine
After you install totem-xine, if you right click on a video file, and choose Open With, you should see Movie Player (xine) as a valid choice. Movie Player (xine) is totem-xine.

If you use totem-xine to play a movie, chances are you can hear the sound from the default sound card, but not in Dolby Digital or DTS.

Step 2: Locating the Correct Sound Device

In this step, we will locate the sound device where sound is being sent from your PC to your receiver.

Note that we are using ALSA here. ALSA is the default API for sound drivers in Linux.

In a text terminal, type:
aplay -L
This command will list all the audio devices that are controlled by ALSA. In my computer, the command produces:
front:CARD=NVidia,DEV=0
HDA NVidia, ALC662 Analog
Front speakers
surround40:CARD=NVidia,DEV=0
HDA NVidia, ALC662 Analog
4.0 Surround output to Front and Rear speakers
....
The output of the command is the name of each device, followed by a few lines of its description. You should look for an S/PDIF output device that belongs to the sound card of your choice, i.e. the connection from your sound card to the receiver.

One of the device listed in my setup is:
iec958:CARD=Revolution51,DEV=0
M Audio Revolution-5.1, ICE1724
IEC958 (S/PDIF) Digital Audio Output
This is the audio device I am looking for: iec958:CARD=Revolution51,DEV=0.

Use the .wav file to test the device. In the terminal, type:
aplay --device=your_device sound.wav
Using my setup as example, it is:
aplay --device=iec958:CARD=Revolution51,DEV=0 sound.wav
You should hear some sound from your receiver. If not, you have selected the wrong device and should look for another device.

Step 3: Configuring totem-xine

In this step, we are gonna configure totem-xine to do AC3 pass through on the correct sound output device. Unfortunately, totem-xine's user interface is not flexible enough to do what we want. We will need to edit its config file.

Here is where totem-xine's config file is located:
your home directory/.config/totem/xine-config
You should make a backup copy of xine-config in case you made a mistake.

Use your favorite text editor to edit xine-config, look for the line:
#audio.driver:auto
Change it to:
audio.drive:alsa
Look for the line:
audio.output.speaker_arrangement:Pass Through
Make sure the speaker arrangement is set to Pass Through.

Save the config file. Open a video file using totem-xine and exit right away. This step is crucial because it produces extra lines in the config file for further editing.

Open the totem-xine config file again, look for the line:
audio.device.alsa_front_device:default
Change it to:
audio.device.alsa_front_device:iec958:CARD=Revolution51,DEV=0
Note: iec958:CARD=Revolution51,DEV=0 is the sound device in my setup. You should change it to your sound device found in the previous step.

Look for the line:
#audio.device.alsa_passthrough_device:iec958:AES0=0x6,AES1=0x82,AES2=0x0,AES3=0x2
Change it to:
audio.device.alsa_passthrough_device:iec958:CARD=Revolution51,DEV=0
Save the config file. You are now all set. When you play a video file in totem-xine, AC3 pass through should work.

Monday, June 1, 2009

It's Not Linux's Fault, But... (Part 2 - Sound)

In this second part of the "It's Not Linux's Fault, But..." series, I am going to talk about my experience with Ubuntu 9.04 when it comes to sound and music.

First of all, let me briefly describe my PC. It's a fair typical PC with on board sound connected to computer speakers, and I have an M-Audio sound card that is connected to my stereo receiver. So the ideal setup for me is to play music through the M-Audio sound card, while the normal PC sound effect goes through the on board. This is basically how I set up sound in Gnome (Gnome is Ubuntu's desktop environment, and I prefer it over the alternative KDE).

I use Foobar as my music player in Windows. So the first thing I did was to look for a music player. After some research, I found that Amarok seemed to be the best music player on Ubuntu. I gave it a try and I liked it. Unfortunately, Amarok is a KDE application, and it ignored the sound device preference I have set in Gnome and played music to my PC speakers. I could not find any sound device preference in Amarok to make Amarok played music to my receiver.

Upon some investigation, I found that Amarok use a backend call Phonon for music playback, and there is no simple way to configure Phonon in Gnome. After some googling, I noticed that the Amarok 2.1 beta has sound device configuration, so I downloaded it (along with zillion KDE related things). The sound device configuration was there in the beta, I can play test sound to my M-Audio sound card, but alas, Amarok still ignores the setting. At this point I gave up on Amarok.

Right now I am resorting to using Rythmbox (Gnome's music player). Rythmbox sounds kind of flat to me, inferior to what I used to from Foobar. Like anything audio, this problem could very well be psychological, but I have nothing to compare Rythmbox's sound to.

My next task is to find a tool to rip CD. I need something more sophisticated than the built-in CD ripper. Specially, I need a program that can read CD multiple times to reduce error, which the built-in CD ripper won't do. Instead of hunting down another application, I found that my Windows CD ripping program Exact Audio Copy runs fine in Wine (Linux's Windows API implementation) so I just used that. Yippe, it worked.

Sound track tagging is another matter. Again I have a specific need. I need the tagging program to be able to import tags from a text file, and applies the tags to the music files. I tried using EasyTag (Linux tagging program), but I am unable to find a way to read text files in EasyTag.

Back to using Windows program again. Mp3Tag (another Windows program) can read tags from text file, it runs in Wine, except the Unicode characters are broken. So for any music track that have non-roman characters in its tag, I have to resort to entering it manually using EasyTag.

Skype works, but again, not right out of the box. In fact, I had to spend a couple of hours to figure out how it works.

I had two issues that prevented me from using Skype. The first issue is by default, Ubuntu assigns my mic input to the jack at the back of my computer. Like most people, I plug my headset into the front jack. The solution is to use alsamixer (runs in terminal) to assign the input source to front mic. Then I had to use the Gnome Sound Recorder application to test the mic.

The second issue is by default, Skype's default sound device setting does not work for me. In Skype's menu, there are three sound device assignments (Sound In, Sound Out and Ringing). Each can be assigned to one of the available 14 sound devices.

The solution was to first assign Sound Out and Ringing to pulse (Gnome's sound engine), then use trial and error to find out which sound device works for Sound in.

By the way, Skype also tend to mess up the mixer setting, causing the mic input level to be too low. There is a setting to turn this off when you assign sound devices in Skype.

I need to make calls using Skype. If Skype does not work, I simple am not going to use Ubuntu on my desktop.

(to be continue...)

It's Not Linux's Fault, But... (Part 1 - Installation)

Ubuntu 9.04 is very cool. With Compiz Fusion installed, the desktop experience is down right amazing.

Yet, to get there, I have to spend a lot of time installing things and tweaking my system and at the end of the day, a lot of what I consider essential applications are still not working.

To be fair, a lot of my problems are not Linux's fault. Usually, it is an application or a piece of hardware that does not have native Linux support by their vendor. There is very little the Linux developers can do about it.

However, whether the problem is a geniune Linux problem or a vendor problem is beside the point. For most users, they are not going to distinguish the differences. What counts is the total experience, and if my experience with desktop Linux is typical, I am afraid that desktop Linux is never going to be mainstream.

This is the first part of a 3 part series documenting my installation of Ubuntu 9.04 as my desktop OS.

I am quite familiar with Linux, using it mostly for small server setups. Recently I decided to give Ubuntu 9.04 a try as an desktop OS because I want to look at some available desktop OS options before upgrading to Windows 7 (my current desktop is XP).

My first trouble was Grub, the boot manager of choice of Ubuntu. Because I wanted a dual boot system, so I created a partition in my hard drive and installed Ubuntu 9.04 on it, keeping my Windows installation in another partition. After the installation, I restarted my computer. To my surprise, I was still booting into Windows. After some googling, I found that I had installed Grub in the wrong hard drive. Once I installed Grub on the correct drive, I could boot into Ubuntu, but then Grub failed to boot into Windows.

I suspect most casual users who want to give desktop Linux a try would simply give up at this point.

Apparently, the automatically generated Grub script was pointing into the wrong partition where Windows was located. I had to modify the Grub script to correct that. By the time Grub worked, 30 minutes of my life was gone.

The next challenge was my wireless network card. I had a Trendnet card. I am familiar with this card enough that I know there is no native Linux driver. So I used ndiswrapper (driver wrapper that lets Linux uses Windows networking driver) as a substitute. In the process, I have to look up the wireless chipset, find the right driver and install the driver with ndiswrapper with command lines. This time I had it easy, I am comfortable with Unix command lines, but I can imagine someone who is not will be quite turn off.

The next challenge setting up Chinese input methods. Setting up SCIM for multi-language input itself is easy enough, but the availability and quality of input methods are rather poor compared to Windows. I have no formal training in Chinese input method and relies on an rather obscure method I learned on Windows for inputting Chinese. Unfortunately, the equivalent method on Linux sucks badly. If you are not familiar with inputting with a non-Roman alphabet language, you probably have no idea what I am talking about, but take my words for it, it is so bad to a point I am in the process of modifying a Chinese input method to suit my need, and it is not a one day job.

(to be continue...)