Note: The part of guide with native alsa setup is mostly for dedicated htpc users who don't use Ubuntu as an everyday desktop (like XbmcBuntu or minimal setup).
With the Pulseaudio setup guide which i add later will be suitable for ubuntu normal desktop setup.

Requirements:
- You need to have Nvidia hdmi audio integrated codec on your motherboard. Like the IONs or 9300 9400 integrated motherboard chipsets.
- You need Ubuntu Jaunty (or Karmic)
- You need the Nvidia drivers 185 series. IMPORTANT: the 190 series driver breaks lpcm hdmi output totally or partially
- At last both not least, you must have a HDMI 1.1 compatibe HDMI AV receiver (Like Onkyo TX-SR605 what i have) connected with hdmi cable
- I will try to update this guide to work with the newest Nvidia graphic cards G210, GT220
- UPDATE 19.10.2009 I got somewhere supporting these cards, just check my Post #30

Update 21.10.2009
Xbmc developers are currently working on a brand new sound rendering system called "master audio". Tha branch has not been updated for a while but it will be continued and might reach mainline in the far future. That could be a solution which utilize the good things in Alsa and also Pulseaudio. That will change the game for sure, but until than this is what we have.

Why HDMI audio over the good old well working SPDIF.

SPDIF connection, either optical or coaxial is bandwidth limited. This means you can only achieve 2 channel UNCOMPRESSED lpcm audio up to (96khz, 24bits in the best case) or with AC3 or DTS LOSSY codecs you can have 5.1 multichannel audio with 48khz, 16bits. So at the best case you can only have Lossy multichannel and only in limited bitrates.

HDMI connection can handle 7.1 multichannel UNCOMPRESSED audio up to 192khhz, 24bits or can bitstream DTS-HD, DOLBY TRUEHD LOSSLESS formats to your AV receiver, if it supports those formats.

It is true that the available HD multichannel material is limited yet. Mostly to Blu-ray movies and HD DVD-Audio or SACD music.

But there are also the codecs which can not be bitstreamed over SPDIF nor HDMI. These are Apple's multichannel AAC used by .MOV formats, or multichannel HD or SD FLAC music or video sound, WMA HD or SD multichannel material etc. The only way you can enjoy these formats is to decode them to uncompressed LPCM and send them over HDMI.

So there are already a lot of reasons why you want HDMI over SPDIF. And i haven't spoken about the fact that this way you only need one cable for all your 8 channel audio and the FULL HD quality Video signal. If you already using hdmi connection with your audio than this is a must for you as current alsa drivers only support 48khz/16bits with horrible quality CD playback.

What this guide can provide:
- 7.1 multichannel LPCM connection up to 192khz/24bits on nvidia hardware.
- DTS, AC3 bitstreaming over HDMI
- working navigation sounds
- playback of sound on it's original bitrate UNTOUCHED without any upmixing/downmixing to achieve the best possible quality with native ALSA configuration or
- high quality mixing with operation-system sounds on HD bitrates up to 192khz/24bits with pulseaudio configuration (Maybe i add this part a little later)

Just a note that if you use pulseaudio or even with alsa dmix module or default alsa device, your sound is always up/downsampled to a certain bitrate. This could cause serious quality drops even if alsa converts the sound to the same bitrate.

Here are some test results where you can see that only a decent sample rate converter can get close to the original sound. Those SRC-s use a lot of CPU time. The best is to have the native sound untouched, unmixed, unmodified.

What's not working and what are some side effects.
- DTS-HS, DOLBY-TRUEHD bitstreaming is not working due to hardware and software limitations.
- We will have to update alsa and compile a custom kernel module (this will change in the future as the patches will be applied in the newer kernels, but only at the best case in Ubuntu 10.04) what means that every time you let ubuntu update your working kernel, you will need to make a little additional work to get things up and working again, or you just don't let ubuntu update the kernel.
- In any case you can easily revert the modifications to get back your original setup.

I was in contact with an Nvidia developer We Ni and a great Alsa developer Takashi Iwai to help them testing things out. I should thank them all this great stuff, letting the drivers be ready for HD audio. All credits go to them.

The patched file what we will use (patch_nvhdmi.c) finally went into the kernel tree in the past days so with the next Alsa release and possibly next kernel releases will already include the most important parts.

We will use the famous Ubuntu Alsa upgrade script which i modified to patch the source to enable all bitrates and channel configurations.

Ths script updates your system to the latest packages. To make things safer i suggest doing this step before running the script to see if everything went ok.

Note that updating your system could install a new kernel over your existing one. In this case your custom kernel modules have to be recompiled. For example if your nvidia driver was not installed from ppa, you will have to recompile the nvidia kernel module with the driver installer, or the better use nvidia ppa for installing the dkms auto update module and you wont'have any problems with updates anymore.https://launchpad.net/~nvidia-vdpau/+archive/ppa

So let's update

Code:

sudo apt-get update && sudo apt-get upgrade -y
sudo reboot

Ater reboot and everything back to normal let's get a needed package which we use (dos2unix) so that we will be able to use bash scripts downloaded from pastebin.com

Code:

sudo apt-get install tofrodos

We will add the current user to the audio group. That is sometimes needed.

I have put two more run options to the script
-p patches the downloaded source
-dpi downloads, applies patch, compiles, installs everything in one step

Now we are going to do everything in one step, but alternatively you can run the sript with -d than -p than -i options to make it in 3 steps.

Code:

sudo ./Alsa_upgrade-1.0.21_hd.sh -dpi

Press "1" when prompted !
Take a break now as this can last for 10-15 minutes. You can follow the process as it is described by the script's text output.
We can clean the unnecessary files now, except from the alsa-driver source. We will need that later if we want to make a kernel upgrade. After it is finished just reboot:

Now it is time for you to decide if you go the native ALSA way or you go in the PULSEAUDIO way.

ALSA pros:
- Native untouched crystal clear sound whatever format the sound has it goes directly to receiver
- no time delay in playback
ALSA cons:
- No software mixing: this is bad if you use Ubntu as a desktop with other apps running, or if you want xbmc music played with crossfade support

PULSEAUDIO pros:
- Software mixing possible with other apps, system sounds
- xbmc crossfade support
- The sounds are played all in a certain bitrate no matter what soundformat you play. This means you don't hear "clicks" from your receiver changing formats all the time.
PULSEAUDIO cons:
- The sounds are played all in a certain bitrate no matter what soundformat you play, this means it alway down/upmixes sounds which depending on the resampler we use, could result a bit of quality drop.
- There is a slight delay in navigation sounds

Note once again that you loose software mixing if you choose this way.
When coming back from reboot, we create a custom virtual alsa device because in nvidia driver, speaker channels are wrongly set up, so we need to change the speaker channel configuration. For this i created a "remap-surround71" virtual device in this file:

To tell the system to actually use this as a default digital output leaving the predefined hdmi device alsa definition. I created a custom HDA-Intel conf file for alsa. With this file we achieve that all ubuntu playback applications (including xbmc-tested, vlc-tested) will use default,hdmi,surround51,surround50 etc. vitrual devices as remapped channel configurations directed to your hdmi output without mixing.

Now we can test if everything went ok. First let's check out the Alsa Driver version and the available playback devices:

Code:

cat /proc/asound/version
aplay -l
alsamixer

The alsa driver version should be 1.0.21
aplay -l should show a HDMI device on card 0 device 3
alsamixer should show three iec devices, all has to be unmuted.

For gnome-desktop users who want to have this feature, knowing that they loose software mixing of system sounds set up sound configuration like this:

We'd better have a reboot now and after we can start testing sound. For speaker test to work it is needed to have the xserver started. So you need to type these commands into an xterminal (or gnome terminal) or you need to start the xserver (maybe with xbmc) and ssh over another machine to run these commands. First er test a basic 2 channel 48/16 configuration, than we jump to the most advanced 192/32 6 channel configuration. (You can stop testing with crtl+c)

With these command you can easily test your speak configuration if all channels sound in the right speaker.

XBMC SETUP ALSA WAY

If everything is ok now we just have to set up xbmc to use our tuned hdmi device. It will be very easy with one little trick. On sound settings just set it like this (note that looks like in the latest SVN and probably will be in 9.10)
Note that in older xbmc revisions just type "hdmi" for both devices.

As we lost software mixing capabilities (just like we wanted) we need to disable crossfading in xbmc's music settings like this:

There is one trick which has to be done to keep navigation sound all the time. There is a bug (xbmc or alsa) that when you play ac3, dts track and press spacebar to pause 2-3 secs and start playback again, you loose navsounds until restarting xbmc. See ticket: http://trac.xbmc.org/ticket/7338

As a help here are a lot of test files (audio and video)
With for ex. the dts file you can test pass through with hdmi.
With the aac file you can test the multichannel LPCM capability of your setup etc.

It was a bit rough isn't it, but it is worth it. It will be more and more simpler as we go in time. I will try to update this guide to keep up to date as kernels, xbmc, alsa changes. Any suggestions, contributions wellcome. I hope it will work for everyone.

REVERT CHANGES FROM ALSA WAY

If you would like to revert back what we've don just do it as follows:

Now we can test if everything went ok. First let's check out the Alsa Driver version and the available playback devices:

Code:

cat /proc/asound/version
aplay -l
alsamixer

The alsa driver version should be 1.0.21
aplay -l should show a HDMI device on card 0 device 3
alsamixer should show three iec devices, all has to be unmuted.

Now we create the config file for non pulseaudio compatible applications to be played through pulseaudio. Strangely xbmc's navigations sounds come out to native alsa while music and video sound use pulseaudio server natively.

Code:

sudo cp /usr/share/alsa/pulse-alsa.conf /etc/asound.conf

Now we load some custom pulseaudio config files to define hw:0,3 (your hdmi device) as the default hw sink. Here we also define the channel numbers and channel order. We can do it with the default.pa file.

With the last line uncommented you can set a higher quality default resampler. After that pulseaudio will start using a little bit more cpu for software mixing.
The available sampler options are (by man pulse-daemon.conf)

Setting up xbmc for pulseaudio is pretty easy. We turn off crossfading for now, because there is a slight stuttering in sound while crossfading. If anyone can help what is the cause we could reenable this feature.

Note that with pulseaudio we loose passthrough as it is not supported yet by pulseaudio. But as we have multichannel lpcm sound we can just decode the streams and pass it in lpcm format to the receiver.

We can do a little testing if xbmc really uses pulseaudio nativly or not. Install pulse audio volume control utility.

Code:

sudo apt-get install pavucontrol

Run the program from main menu and open xbmc minimalized (press alt+q after start)
Note that while it is playing GUI sounds it is using the Alsa-plugin module.
Notice that i have also run a totem session which was also playing music. Software mixing works great.

If you start playing a music tracks with xbmc it should appear like this.
This means xbmc nativly using pulseaudio not through an alsa plugin rerouted to pulse.

This utility is great because you can also tune channel volume levels with it:

That's it we are done. I have to tell that sometimes specially when a new stream starts i can hear a very slight stuttering in sound. This settles as thestream continues. I'd very much appriciate if someone could help me to solve this little glitch in it.

REVERT CHANGES FROM PULSEAUDIO WAY
If you would like to revert back what we've don just do it as follows.

Unfortunately the new G210/220 models "only" have 7.1 HD LPCM equipped. But even if the hardware supports DTS-HD, DDTrueHD bitstreaming (like the new ATI 58xx and Intel's yet unreleased motherboard chipsets) it will be a lot of time until the protected audio path can be utilized by open software (maybe never, or when it will be hacked). Even on windows it is only with Arcsoft mediaplayer and PowerDVD closed source mediaplayers have the license.

Alsa-utils wouldn't compile because of some xmlto related error (i did install it before compiling). But since i already had 1.0.21 libs and utilities installed from the Arch repository i skipped those and installed only the driver part.

I did things the "alsa way", and now speaker-test works with 44100, 48000 and 192000, earlier it would work only with 48000, so far so good..

These are the two things i'm after, untouched audio and that first second or so of every file wouldn't be missing. But it still is, and neither do the navigational sounds work.
I don't mind that there's a small gap missing when watching movies and skipping/forwarding/rewinding but it's nervewracking to have it happen with music :confused2:

alanwww1 Wrote:Thanks for the feedback. So the guide helped you to set up things ?

Unfortunately the new G210/220 models "only" have 7.1 HD LPCM equipped. But even if the hardware supports DTS-HD, DDTrueHD bitstreaming (like the new ATI 58xx and Intel's yet unreleased motherboard chipsets) it will be a lot of time until the protected audio path can be utilized by open software (maybe never, or when it will be hacked). Even on windows it is only with Arcsoft mediaplayer and PowerDVD closed source mediaplayers have the license.

Sorry, I havent got any free ION pc now.
And Im not that linux expert at all (log ago used ubuntu sometime).
But i just wondering, couse I would like to get the perfect device for all of my media.

I installed several XBMC but only on win.
At least I have a lot of media and a decent receiver and now using Xtreamer media player, which is quite uncomplete.

toiva Wrote:These are the two things i'm after, untouched audio and that first second or so of every file wouldn't be missing. But it still is, and neither do the navigational sounds work.
I don't mind that there's a small gap missing when watching movies and skipping/forwarding/rewinding but it's nervewracking to have it happen with music :confused2:

For Navsounds check if this gives you sound:
speaker-test -Ddefault -c2

Navsound ALWAYS use alsa device "default" no matter what device you set in gui for music.

Have you done the part of my guide with custom HDA-Intel.conf. Maybe in Arch-Linux it has a different location. Check that. Try to find it with:

sudo find / -name HDA-Intel.conf

With missing one second it is absolutely normal. It is also with my AV receiver. Note that it is because your av receiver does not "know" what format you are going to be playing, so you receiver has to switch between pcm formats when you start playing. But this is only with the album's first track's first second.