Using libsamplerate to improve audio quality
#1
This information is not strictly XBMC specific, but it is probably very useful to many XBMC users. I and many others have been using dmix in our asound.conf file to output stereo audio through spdif.

This worked for me, but I have always had noise when listening to music that sounded somewhat like a scratch on a record. It is a scratchy noise that can be heard at regular intervals. After a good bit of searching and reading, I found that dmix is not very good at resampling.

First, you need a couple of packages:
Code:
sudo apt-get install libsamplerate0 libasound2-plugins

You'll need to change your asound.conf file to include the following:
Code:
pcm.48k_best {
        type rate
        slave {
                pcm "hw:0,1"
                format S32_LE
                rate 48000
        }
        converter "samplerate_best"
}

pcm.!xbmc_default {
        type plug
        slave {
                pcm 48k_best
        }
}

Some of this (hw:0,1 especially) may have to change for your system. Also, samplerate_best is very CPU intensive (about 20% on my E4500), so if you want something less taxing, try samplerate_medium, etc.

Finally, set XBMC to use xbmc_default instead of default for its audio device.

I'd really be interested to know if this is useful for others.
Reply
#2
XBMC will output stereo to SPDIF if you change the audio output to "analog". Does this change offer better sound quality than that?
Reply
#3
Hmmm... I didn't realize that setting XBMC to Analog would output to spdif. That's pretty weird.

I'm not an audio expert, but I have done enough software audio processing to understand this thread.

Anyway, technically, samplerate_best should improve pretty much any setup. In Ubunutu, everything gets resampled to 48000 Hz so that it can be mixed in ALSA. By default, this resampling uses a simple algorithm that is low quality, but also not CPU intensive.

Practically, however, it depends on your ears, speakers, receiver, etc. whether you will be able to hear the difference. It definitely helped in my case, since it got rid of the scratchy noises, but I could also hear a difference between samplerate_linear and samplerate_best.
Reply
#4
XBMC uses SSRC to resample to 48kHz at least on the win32 versions. Whether or not this is done on the ALSA player is another story.
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#5
When I tried using iec958 as my default audio device and playing music, it was played slightly faster than it should be. Most likely, this was 44.1kHz samples being played at 48kHz, indicating that it was not getting resampled anywhere along the way.
Reply
#6
to start with, you shouldn't be having a asound.conf with downmix stuff in it. (well atleast not overriding the default device). xbmc should setup alsa correctly for that anyway. we have our own "asound.conf" inside system dir.
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#7
Yeah, I'm probably not doing things the ideal way, but out of the box, neither 2 channels audio or passthrough works (even outside XBMC). The "default" device sends audio to the analog speaker port, and I don't even have an iec958 device in my "aplay -L" list. Maybe I'm wrong, but I believe that forces me to have a asound.conf file.

For iec958, I use:
Code:
pcm.!iec958 {
        type plug
        slave {
                pcm "hw:0,1"
                format S32_LE
        }
}

Trying that for default give me fast audio (no hw resampling), so I had to resample to 48kHz. When I was first trying to get it to work, I found information on using dmix not to mix channels, but to resample.

This at least gave me audio out the spdif port, both from Linux in general, and XBMC specifically.

Recent changes to how XBMC handles passthrough broke my configuration, so I created a setting identical to the iec958 above to prevent XBMC from doing AES stuff.
Reply
#8
What is XBMC's resampling logic currently? Does it resample everything to 48khz internally like xbmc on xbox, or does it pass it to the OS to handle resampling? I'd like the option to output whatever sample rate the source is if the sound card can handle it, ie 44.1khz, 48khz, 96khz, etc. Maybe just have a check box in the settings asking what sample rates your receiver supports? Then just resample to the highest supported samplerate via SRC. Thanks.

Dustin
Reply
#9
well paplayer resamples everything to 48k i think. dvdplayer does nothing of the kind asfar as i know. it will just rely on hardware for that.

i suppose we could have some settings for that, but i'd be surprised if alsa didn't handle that internally if you start it with something different than the hardware supports. could be wrong thou.
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#10
There are a lot of sound cards, onboard and discrete, that support other sample rates besides 48khz. I know the xbox needed to have everything at 48khz, but with the linux port, it is no longer entirely necessary, nor desirable.

Personally, I would prefer to see no resampling unless it is absolutely necessary. Getting Bit Perfect playback is actually very important to audiophiles. If you are playing an mp3 that is 44.1khz and you have a sound card and receiver that both support 44.1khz, then xbmc should output 44.1khz. It gets a little complicated because I think some sound cards support 44.1khz over spdif, but require 48khz if using the sound card's internal DACs.

Another issue is if you want to mix gui sounds with media currently playing. I think the desirable approach to this would be to resample the gui sounds to the currently playing media's sample rate rather than the other way around. This would also require an internal mixer, which I don't believe xbmc has, correct?

What are your thoughts on this? Thanks.

Dustin
Reply
#11
herg Wrote:You'll need to change your asound.conf file to include the following:
Code:
pcm.48k_best {
        type rate
        slave {
                pcm "hw:0,1"
                format S32_LE
                rate 48000
        }
        converter "samplerate_best"
}

pcm.!xbmc_default {
        type plug
        slave {
                pcm 48k_best
        }
}

Some of this (hw:0,1 especially) may have to change for your system. Also, samplerate_best is very CPU intensive (about 20% on my E4500), so if you want something less taxing, try samplerate_medium, etc.

Finally, set XBMC to use xbmc_default instead of default for its audio device.

I'd really be interested to know if this is useful for others.

Another way to do it is to just add the following line to your asound.conf:
Code:
defaults.pcm.rate_converter "samplerate_best"

That way samplerate_best will be used for all samplerate conversions whenever and if such conversions occur.
Reply
#12
After reading this thread I was worried I wasn't getting bit-perfect 44.1 audio out of XBMC, or anything on Linux. I did some research today:

First, my receiver says it is getting 44.1kHz PCM. This is via optical SPDIF from the ALC883 chip on my mobo.

Second, I've configured XBMC to send audio to "iec958" (not "default". That should allow XBMC (and Linux) to output the 44.1 signal without upsampling, according to this page:

http://www.acaciaclose.co.uk/31307/42432...on*id*val*

Quote:For Die hard Audiophiles this resampling is a real source of irritation as CD audio is made using PCM at 44.1Khz. The upsampling in ALSA is unfortunately very low quality, and if you have a half decent amplifier and speakers, you will notice that CD playback has a congested compressed sound to it. This is very easy to overcome, by sending all your audio directly to your soundcards SPDIF or IEC958 Connector. The device name for this connector is typically "cards.pcm.iec958". So just configure this device as the audio device in MythTV.
XBMC machine: ABIT AN-M2HD with nVidia 7050 onboard (Ubuntu Hardy + latest nVidia driver), AMD Athlon BE-2350, Microsoft MCE IR Remote, Logitech PS3 USB keyboard, 33w idle, 53w max, 720p plasma over HDMI
Reply
#13
I just noticed that adding defaults.pcm.rate_converter "samplerate_best" to my asound.conf causes XBMC to crash/exit when trying to play a video with an audio samplerate other than 48k. (I.e. when video audio needs samplerate conversion.)

Anyway, XBMC crashes/exits and gives the following error message:

Quote:ALSA lib pcm_rate.c:1381Sadsnd_pcm_rate_open) Cannot find rate converter

There is nothing relevant in the log, it looks like XBMC exits without writing an error message (to the log).

Playing music works fine, which got me thinking that perhaps paplayer and dvdplayer are compiled with different versions of the alsa lib or something. But that's just my not very informed guess. There's gotta be some difference though since "samplerate_best" works in one player but not the other.

Any ideas?
Reply
#14
Smile 
Sup People! I wanted to contribute back to the community. I have a soundrc file I wish to share with you all incase it may be of help.

In Short: After I stopped with the svn xbmc and went to the PPA xbmc distribution for linux, I suddenly found none of my vids with ac3 encoded audio would playback any audio. But all other audio would. So I installed the digital out daughter board o my sound card (SPDIF). Great! Now I have ac3 playback but now have 2 channels on my reciever for xbmc. 1) Analog, 2) Digital. So I changed the default on Ubuntu in the MediaSelector to have iec958 (Digital/SPDIF) as the default output. I then changed my .asoundrc to "pcm.!default spdif". This worked except I noticed I had no mp3 playback!!! So I edited my .asoundrc to be like my original with upmixing/downmixing but to have spdif as default device (I shall attach this config as well as "OLD asoundrc"). Also this old config I think is like analog even though my amp detects digital.

Ok now I had all playback except the xbmc skin UI sounds! For F$%#!!! So today I started a new asoundrc config from other configs I downloaded ages ago! And finally SUCCESS!!!! I have playback of every sound event! I have the skin UI sounds (mono wav files I noticed). So we got mono playback on digital out. I got MP3 playback. So got Stereo playkback on digital out. I still have AC3 (Dolby PL II) plackback and the amp detects DD PL II. And I have DTS playback. Amp detects "Digital".

So I am so happy I want to share with you all. Also my config outputs 48K sample audio, but I think it could be easily changed to 44.1K if you needed to. Not sure there. And with this config the most one would need to do is change the device number on card 0 to what ever your system is for digital out. Use the aplay -l command to find your playback dev # on your soundcard dev #. For me its card 0, Dev 2. For some maybe Card 0, dev 1, or whatever.

Cheers!

Paul.

P.S The good current config is "TUXBMC Digital asoundrc"
Tho old config will have old in the file name.

Opps! Thought I could attached files to posts. Ok here are the links

http://www.4shared.com/file/64200355/214...undrc.html

http://www.4shared.com/file/64200359/28f...LDbak.html
Reply

Logout Mark Read Team Forum Stats Members Help
Using libsamplerate to improve audio quality0