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...)

Saturday, May 16, 2009

Wolfram Alpha is Cool

I recently encountered a new technology called Wolfram Alpha, and I think it is very cool.

So what is Wolfram Alpha? It is an answer engine. Unlike a search engine like Google, Wolfram Alpha attempts to provide straight forward and simple answer, rather than a list of web pages that may provide the answer.

For example, if I type in "hong kong population" to Wolfram Alpha, it will tell me it is 6.94 million people. If I type in "Citizen Kane", it will tell me it is a movie that is directed by Orson Welles, released in 1941. If I type in "35 + 27", it will tell me the answer is 62. If I type in "convert US $35 to hong kong dollar", it will give me $272.46. That is freaking amazing.

Obviously, Wolfram Alpha does not have all the answers in the world, but for whatever it knows, it seems to be able to find it quickly and provide a straight forward answer.

Another interesting fact is Wolfram Alpha is written using Mathematica, a symbolic computation application. This is quite different from a typical online database which is probably written using some variants of standard programming languages such as Java or php, and accessing a database behind it.

This choice of implementation is probably significant. Whatever the underlying technology is, Wolfram Alpha's logic is probably operating at a more abstract level than something that is written in a standard programming language.

Saturday, May 9, 2009

金庸小說的山中老人

金庸小說《倚天屠龍記》中有一段關於聖火令武功的來源:

謝遜道:「明教傳自波斯,這首波斯曲子跟明教有些淵源,卻不是明教的歌兒。這曲子是兩百多年前波斯一位著名的詩人峨默做的,據說波斯人個個會唱。當日我聽韓夫人唱了這歌頗受感觸,問起來歷,她曾詳細說給我聽。

「其時波斯大哲野芒設帳授徒,門下有三個傑出的弟子:峨默長於文學,尼若牟擅於政事,霍山武功精強。三人意氣相投,相互誓約,他年禍福與共,富貴不忘。後 來尼若牟青雲得意,做到教王的首相。他兩個舊友前來投奔,尼若牟請於教王,授了霍山的官職。峨默不願居官,只求一筆年金,以便靜居研習天文曆數,飲酒吟 詩。一一依從,相待甚厚。

「不料雄心勃勃,不甘久居人下,陰謀叛變。事敗後結黨據山,成為威震天下的一個宗派首領。該派專以殺人為務,名為依斯美良派,當十字軍之時,西域提起 『山中老人』霍山之名,無不心驚色變。其時西域各國君王喪生於『山中老人』手下者不計其數。韓夫人言道,極西海外有一大國,叫做英格蘭,該國國王愛德華得 罪了山中老人,被他遣人行刺,國王身中毒刃,幸得王后捨身救夫,吸去傷口中毒液,國王方得不死。霍山不顧舊日恩義,更遣人刺殺波斯首相尼若牟。首相臨死時 口吟峨默詩句,便是這兩句『來如流水兮逝如風,不知何處來兮何所終』了。韓夫人又道,後來『山中老人』一派武功為波斯明教中人習得。波斯三使武功詭異古 怪,料想便出於這山中老人。」

究竟這故事來源何處?峨默,尼若牟,霍山,又是何許人呢?

用google一查,找到了這網頁:
http://www.gamez.com.tw/archive/tid-116977.html

此網頁解釋了山中老人和尼若牟的來源。山中老人有可能是哈桑·伊本·薩巴,而尼若牟即是尼扎木.穆勒克。歷史上都有其人。

開始有趣了,想進一步調查。上述網頁中的人物只有中文譯名,開始有點難查。於是決定先找找尼扎木.穆勒克的英文名。不難查,是Nizam al—Mulk。

再用wikipedia查Nizam al—Mulk,找到以下文章:
http://en.wikipedia.org/wiki/Nizam_al-Mulk

其中一段很有趣:
"This account is particularly interesting in light of a possibly apocryphal story recounted by Jorge Luis Borges. In this story a pact is formed between a young Nizam ul-Mulk (at that time known as Abdul Khassem) and his two friends, Omar Khayyam and Hassan-i-Sabah. Their agreement stated that if one should rise to prominence, that they would help the other two to do likewise. Nizam ul-Mulk was the first to do this when he was appointed vizier to the sultan Alp Arslan. To fulfill the pact he offered both friends positions of rank within the court. Omar refused the offer, asking instead to be given the means to continue his studies indefinitely. This Nizam did, as well as building him an observatory. Although Hassan, unlike Omar, decided to accept the appointment offered to him, he was forced to flee after plotting to dispose Nizam as vizier. Subsequently, Hassan came upon and conquered the fortress of Alamut, from where he established the Assassins."

這不就是金庸的山中老人故事嗎?

峨默(Omar Khayyam),尼若牟(Nizam ul-Mulk),霍山(Hassan-i-Sabah)出來了。三個都是真有其人。至於三人師兄弟關係,Jorge Luis Borges是根據The Three Schoolmates的傳說來寫的。可能這傳說太吸引,金庸和Borges都使用了。

註: 山中老人霍山的Assassins組織,正是英語中殺手(assassin), 行刺(assassination)的來源。

Wednesday, March 25, 2009

[Analysis] ES analysis for March 26, 2009

We are dealing with an awkward situation here. ES was appearing to break the uptrend, dropping to below 790 in afternoon trading. Yet within 30 minutes of closing, it recoup all the losses and went back to 810 level.

Let's analysis the price movement from an intraday standpoint first. When I look at a 5-minute chart, it tells me the following:
  • The market is still sensitive to bad news. The poor treasury sales is enough to trigger a drop to wipe out the gains created by two days of positive news.
  • The last minutes rally is clearly a short covering rally. The short sellers are not committed. At below 790, they rather take the profit and run.
To me, the strong drop in early afternoon indicates that the buyers do not consider stocks cheap when ES is above 800. It is hard to tell what price the market considers stocks are good value, but longing above 800 sounds not like a good strategy unless more economic data suggests otherwise.

The bears' reaction is easier to understand. GDP and Jobless claim numbers are to be announced on Thursday at 8:30 in the morning. There is no need to fight these data.

This makes Thursday's economic data and price action are extremely critical in determining which direction ES is heading.

The market has been receiving a series of good news lately. The current ES price level seems to indicate that it is expecting more good news to come. So unless the economic data is exceedingly good, ES cannot justify at current price level and will eventually drop. Remember, today's price action tells us that the buyers do not consider ES above 800 cheap.

Trading plan:
  • Prepare to short after 8:30 data. Interpret the data first.
  • Spikes that push ES close to 820 are good shorting opportunity. Either use long put or prepare to cut loss should ES breaks 830. Ideally, short at around 9:30-10:00.
  • If economic data is so-so or bad, after 8:30, ES will likely drops. In which case, look for intraday rebound to short.
Position: None

[Position Update] Short Call Covered

Today I covered my ES 820 short call at 21 - 12 points profit.

I was unable to trade according to my own plan because my ideal long price (795) was not reached until late into the trading session. I was expecting an early morning pullback, but instead I had to trade with a morning rise and sharp afternoon drop.

I've only scalped with YM today.

Position: None