2008-02-19

Better Audio Under Linux with TerraTec DMX6Fire (ICE1712 based)

Some background

I've been using Linux as main OS for more than two years, here is my experience of the audio on linux so far.
At the early stage of my linux usage, I spent some time to figure out how to use sound on linux, setup alsa, and use optical output (s/pdif) for better quality(the poor built-in sound card in the Desktop can produce pretty clean sound with this, but you need some device can accept optical in, I used an old minidisc player for that).
After switch to a Thinkpad t60 as my main development machine, I don't have optical output anymore, then I noticed that the sound quality was pretty poor with the default sound output, I didn't dig into that, just use my cell phone for music listening in work.
I have a windows PC at home for a long time, the biggest reason is I have a good sound card on it (TerraTec DMX6Fire, cost me quite some money years before), and I have some musical software on windows. After not doing anything with the software for a few years, and feeling more and more comfortable with Linux, I decide to reinstall it with Ubuntu too, then leads to this article.

The sound quality is ... POOL

Ubuntu install pretty smooth on this machine, find all the hardware including the sound card, reboot correctly, and sound works, with a pretty bad poor quality, and the digital output(Both optical and coaxial) on the card stop working anymore, the sound quality from the analog out is terrible, very not clear, it's impossible to enjoy any music on this pretty cool sound card with the default configuration at all!
Since this is Linux and the hardware is already been recognized, I think it's just something related to the configuration, so I started to google around and try to fix this, cost me about two days so far, here is what I've learned from this.
* http://alsa.opensrc.org/index.php/Ice1712

How to make it better

The biggest reason to cause the messy sound is the configuration of the card, since it's a sort of professional class card, it's not as simple as most consumer card, need some setup to make it working in the right way, there is a graphics tool for ICE1712 based cards. it's name is envy24control in package alsa-tools-gui , for my card, I am using "envy24control -p 8 -w 15" to start it, "-p 8" means use 8 pcm outputs, "-w 15" to make the window wider enough to show everything without a scrollbar.
The most important here is to make the pcm channels as stereo, the cards has 10 mono channels, by default players will play sound to the first two channels as a stereo one, but if you didn't adjust the digital mixer correctly, you will hear mix-down mono sound which is pretty bad. so you should mute the right part of pcm output 1 and mute the left part of pcm output 2.
If you have experience of using a hardware mixer, then this is much easier to understand, by allowing left/right control over each channel, you can adjust the pan value(left/right position) for it, but for stereo channels, you must set the mixer as stereo by pairing two mono channels.
Then I can have rather clean stereo again, but the sound quality is still not very nice, it's much worse than using foobar2000 on windows with ASIO output with the same card. and the digital output is not working correctly yet. so I still have a lots works to do.

How to get digital and make the sound quality even better

Using envy24control to direct digital output from s/pdif out (on Patchbay/Router tab), can use iec958 as alsa output, but you need to make sure the internal clock is same with the sound you're playing, otherwise it will be too fast or too slow. you can use envy24control's Hardware Settings to change it.

Note: Be careful about the combinations with Professional/Consumer and different clock rate, I found out that Professional+44100 and Consumer+48000 works best, other combinations will cause problems. (Noise on coaxial, or wrong clock display on my amplifier). My amplifier doesn't support higher clock rate, so I don't know how that works.

There is a good post about ALSA's sample rate converter in dmix, and how to improve it, I followed it, feels that the sound is a bit better, but not very sure about the actual effect


* http://www.hydrogenaudio.org/forums/index.php?showtopic=47591

Then I decide to give JACK a try, according to it's website at http://jackaudio.org/

JACK is a low-latency audio server, written for POSIX conformant operating systems such as GNU/Linux and Apple's OS X. It can connect a number of different applications to an audio device, as well as allowing them to share audio between themselves. Its clients can run in their own processes (ie. as normal applications), or can they can run within the JACK server (ie. as a "plugin").
JACK was designed from the ground up for professional audio work, and its design focuses on two key areas: synchronous execution of all clients, and low latency operation.
Sounds pretty good to me, and I found some guy saying that it's sound quality is pretty good on the web, after several hours of using it, I will say, it's a very cool and very good stuff, I will stick using it for a quite long time I think. The sound quality over JACK is noticeable better, make me very satisfied, and seems with JACK, can do low latency audio/midi/synth stuffs under Linux too (you can install some good musical software with package ubuntustudio-audio and ubuntustudio-audio-plugins), I haven't tried them yet, but some of them seems pretty nice.

I will leave the JACK related stuffs in another post, includes some experiments about JACK, how to use ALSA over JACK.

2007-07-05

Web park prototype release (See more than 1 pages in one tab)

Current I am using a relative big screen(22' widescreen LCD) which is at 1680X1050. It's pretty good, but it's hard to utilize it when surfing web, most web pages will only take a small part of the whole screen, for example a maximized firefox browsing google will look like this:
I really don't like the blank area, so I develop a prototype for using the big screen more efficiently, currently it looks like this:

As you can see, two different pages are shown together(it's configurable, if you have a really big screen, you can show more than 2 pages). and click on google's search result will load the page on it's right, so to me, means a lot of tab operations become needless.

This project is currently in a very early stage, only have some basic functions, if you want to try it out, just visit http://yjpark.org/webpark, click the left-top button to install an extension for firefox (It's needed to add hook to user's click)

The second button is for login which is not implemented yet, the third can config how many pages you want to see in the window, the fourth can add a new page. on right side, each opened page will have a thumbnail(currently all same, will customize it in the future), you can click on it to switch(sub page will have no thumbnail, a sub page is the one which supposed to be on a new window, but with the extension, I can catch it and display it on right of the original page). the small buttons on top of each page can be used to maximize and close a page.


2007-01-01

Turbo dojo widgets

Background

In our project, we chose dojo as the AJAX toolkits, we use dojo widget programmatically, it's easy to use and easy to extend, but dojo's widget has big performance problem in our project.

In one page, we can have thousands of widgets, creating a widget in dojo is really slow, showing such a page needs like 20 minutes 100% CPU usage, it's far from acceptable, so I have to figure out some method to boost dojo dramatically.

dojo's widget is very powerful, you can define attach points and connect events in the templates, then dojo will parse the templates and pick up all the custom tags and process on them, after reading some dojo source code, I found out that the template parsing is the most time consuming part in widget creations, it takes like 80-90% of the whole time, so I decided to try to optimize this part first.

Every time creating a widget with dojo.widget.createWidget(), the template of that widget will be parsed, that is actually not needed, every kind of widget will have same template, so the result of the first parse can be cached, then it's not needed to parse the same template anymore.

I've seen some one in dojo's maillist talking about cloning a widget, I think it's the same idea I have here, but I don't have enough time to actually patch dojo to have a faster widget creation, so I first do some work to help our project running fast with dojo.

As I said before, our project create all the widgets in javascript, my turbo way only work in this scenario, if you want to use the widgets in html, this post can not help you.

My trick

common/turbo.js

dojo.provide("common.turbo");

dojo.require("dojo.widget.*");
dojo.require("dojo.widget.Manager");

_cachedWidgets = {}

common.turbo.createWidget = function(name, props, refNode, position){
var prototype = _cachedWidgets[name];
if (prototype == undefined){
prototype = dojo.widget.createWidget(name, props, refNode, position);
if (prototype["turboInit"] != undefined){
prototype.widgetType = name;
prototype.index = 0;
_cachedWidgets[name] = common.turbo.cloneWidget(prototype, {}, false);
prototype.turboInit();
}else{
//alert(name);
}
return prototype;
}
var result = common.turbo.cloneWidget(prototype, props, true);
if (refNode != null) refNode.parentNode.replaceChild(result.domNode, refNode);
return result;
}

common.turbo.cloneWidget = function(prototype, props, init){
var result = {};
for (k in prototype) result[k] = prototype[k];
for (k in props) result[k] = props[k];
var widgetId = props["id"];
result.index = ++prototype.index;
if (widgetId != undefined){
result.widgetId = widgetId;
}else{
result.widgetId = prototype.widgetType + "_" + result.index;
}
result.domNode = prototype.domNode.cloneNode(true);
if (init) result.turboInit();
dojo.widget.manager.add(result);
return result;
}

The logic here is simple, in our javascript code, we call common.turbo.createWidget() instead of dojo.widget.createWidget() to create a widget.

In common.turbo.createWidget(), it will call dojo.widget.createWidget() to create the first widget, so I don't need to write code to parse template and can keep using most of dojo's power(will talk about the limitation later), then after got the widget created, we check whether it has a method named as turboInit(), if it does, then means this widget is one of our turbo widgets, then we can use common.turbo.cloneWidget() to get a clone and cache it, then if the same type of widget is needed in the future, the cached prototype can be used to make another clone, then we make sure dojo's slow widget creation will only happen once per type, that is acceptable.

Here is an example of turboed widget

dojo.provide("common.widget.Button2");
dojo.widget.manager.registerWidgetPackage("common.widget");

dojo.require("dojo.widget.*");
dojo.require("dojo.widget.Button");

dojo.widget.defineWidget(
"common.widget.Button2",
dojo.widget.html.Button,
{
templateString:
'<div class="dojoButton" style="position:relative;">'
+ '
<div class="dojoButtonContents" align=center '
+ 'dojoAttachPoint="containerNode" '
+ 'style="position:absolute;'
+ 'z-index:2;"
></div>'
+ '
<img dojoAttachPoint="leftImage" '
+ 'style="position:absolute;left:0px;"
>'
+ '
<img dojoAttachPoint="centerImage" '
+ 'style="position:absolute;z-index:1;"
>'
+ '
<img dojoAttachPoint="rightImage" '
+ 'style="position:absolute;top:0px;right:0px;"
>'
+'</div
>',

turboInit: function(){
this.containerNode = this.domNode.childNodes[0];
this.leftImage = this.domNode.childNodes[1];
this.centerImage = this.domNode.childNodes[2];
this.rightImage = this.domNode.childNodes[3];

this.containerNode.innerHTML = this.caption;
this.sizeMyself();

dojo.event.connect(this.domNode, "onmouseup", this, this.onMouseUp);
dojo.event.connect(this.domNode, "onmousedown", this, this.onMouseDown);
dojo.event.connect(this.domNode, "onmouseover", this, this.onMouseOver);
dojo.event.connect(this.domNode, "onmouseout", this, this.onMouseOut);
dojo.event.connect(this.domNode, "onclick", this, this.buttonClick);
}

}
);

This is a subclass of dojo.widget.html.Button, in turboInit(), it rebuild the dojoAttachPoint from domNode, and attach events.

The usage of Button2 is same with other widget, just use common.turbo.createWidget() to create it.

By using this simple technology, our ajax page is pretty fast now, the same page now only takes like 20-30 seconds in widget creation part. Then with some lazy loading tech, our project is fast enough now.

The limitation of this approach

The dojoAttachPoint and dojoAttachEvent only works for the first widget, all cloned widgets needs to set them up in turboInit(), now for my own widget, I just don't use them in templates, in the case of changing a current widget like the example of Button2.js, I leave them in template and setup them up again in the turboInit() now.

And for better perforamnce, I didn't call all the functions dojo calls when create a new widget, e.g. postMixInProperties() and postCreate(), it's easy to call them in common.widget.cloneWidget(), while in our project it's not needed.

The simple clone in common.widget.cloneWidget() is not a deep one, I think this is better, you can choose deep copy some property of reinitialize it in turboInit().

Not all properties needs to be copied, actually some of them may have wrong value after the copy.

Future work

As I said, if I add the similiar hack to dojo's framework, then it's possible to just make all template parsing cached for all type of widgets, and don't need the turboInit() trick. This may or may not be easy, if I got time, will try to figure it out.

2006-09-07

Setup ssh to use public key authentication (without input password)

Under Linux, we use ssh and scp a lot, if you don't want to enter password everytime, here is how to set it to use public/private key for authorization.

First you need to generate a public/private key pair to identify yourself, use 'ssh-keygen -t rsa'
Then you need to add the generated public key to the remote host, here is how I do it

At Local Machine
scp id_rsa.pub yjpark@exobox:
ssh-add
At Remote Machine:
mkdir .ssh
chmod 600 .ssh
cd .ssh
cat ../id_rsa.pub >> authorized_keys
chmod 600 authorized_keys
That's All

make sure that .ssh and authorized_keys is not accessed by anyone except the owner, otherwise it will not work

How to setup window's background color under Gnome

In current version of gnome, there is no tools for setting up background color of window, it's background setting only change desktop's background.

I really don't like white background, I want the background gray, so I setup some applications's background manually, but some application do not have background color setting(e.g. some versions of Eclipse), and it's not a good way to do that, so I spent little time on it.

First I tried some themes, some of them has gray background, while I don't like there colors setting, so what I need to do is to modify some themes to fit my requirement.

In gnome's theme setting -> Theme Details, you can choose different setting of Controls, window Border or Icons, window's background belongs to Controls, I picked up !LighthouseBlue as a base setting.

The themes file are under /usr/share/themes, I only modified gtk-2.0/gtkrc
bg[NORMAL] = "#D0D0D0" //background color of window
base[NORMAL] = "#CCCCC0" //background color or controls

That make me quite happy. I didn't find good tool for editing theme, there is a definition of the values: http://developer.gnome.org/doc/API/gtk/gtk-resource-files.html