Friday, 25 December 2015

Is mpd bit-perfect? Seems like it's not

So, recently I've been listening to some music via Audinst USB HUD mini audiocard and Sennheiser HD25 headphones. I've noticed, that all 16-bit flac files I've been listening, have unusual noise. It's very low amplitude, however is noticeable.

First thing to blame was my sound card — cheap (~100$) device from unknown Korean vendor... What would you expect? But just of curiosity, I converted those flac files to 24 bit and noise gone. That's interesting! Then I tried converting to mp3. No noise. Ogg? — Nope. Wav? — Nope.

I started experimenting. It appeared, that parasite noise was only present for 16-bit flac files, and only if there was some signal (flac file with all samples == 0 sounded silent as it should). Noise was a bit different depending on the content of the file, and I assumed that it depends on the signal being played.

I checked other players, specifically moc, and couldn't reproduce that noise.

Obviously, mpd was one to blame. I filed a bug to mpd bug-tracker, which naturally was closed due to lack of evidence (and you can't blame mpd developers for that. I too wouldn't believe in such non-sense from a stranger).

So I started thinking about getting an evidence to what I've been hearing.

Luckily, mpd provides fifo output. To my understanding, decoded data (raw pcm) is streamed to that fifo for other apps to read. Perfect!

So I got my sample flac file (about 1 sec long), its decoded wav version, and played each of them dumping fifo output to a file like this
$ dd if=/tmp/mpd.fifo of=$output_file

Results are more confusing then I expected.

At first: lengths of these samples appeared to be different.
DataLength, samples
Reference wav and flac files44453
Dump of the flac file via mpd fifo44416
Dump of the wav file via mpd fifo40960

Second, obviously: sample data was different too.

I asked for some comments from developers about that on mpd bug tracker issue (also, sample files can be downloaded there), and still waiting for developers response.

For years I've been believing, that ALSA and mpd can deliver bit-perfect playback, but apparently that's not the case. I do hope, that everything will be sorted out and (if needed) fixed.

Wednesday, 17 September 2014

randrctl — profile-based screen manager for X

While having some free time, decided to automate one of my daily routines — setting up external display.

Idea appeared long before I was told about famous (or not?) autorandr project.
So if you are familiar with autorandr, you are already familiar with randrctl idea.


How often have you to plug external display to your laptop, how many different setups do you have? I have 3: no external display, office setup, where laptop stands below old 19" external display and home setup, where laptop stands beside of my shiny 23" fullhd display. To set them all up, I created 3 scripts in my home directory:,,

That couldn't be good. So I decided to develop an utility to manage my screen setup in the same way networks are managed in archlinux. So it would be possible to do
$ randrctl switch-to office
instead of calling custom shell script.


And here it is: randrctl on github (tah-dam!). Project has a descriptive readme (I believe), but here is a brief list of what is done already
  • support for display mode (resolution), position, rotation and panning (the latter is not supported in autorandr afaik)
  • switching between profiles
  • executing custom commands before and after the switch or in the case randrctl fails
  • profile creation from current screen setup

Under the hood it performs calls to xrandr utility to get info about current setup and to apply new settings.


Installation instructions are on github. Basic idea is that you clone a git repo and allow python to install the module. And then you copy exemplary configs to specific locations and copy completion functions for bash and zsh if you need them.

For archers there is a package on AUR.


All configuration is currently stored under /etc/randrctl/ (which may be not such a good idea, because screen setup usually do not require root privileges, I know. I'm working on it).

/etc/randrctl/config.ini allows to declare custom commands on profile switch or failure. The whole property value gets executed in a shell, so all these &&s, ||s, pipes and redirections are supported. While composing your custom commands, you can get profile name from $randr_profile environment variable. Error, if it happens, is stored to $randr_error.

Profiles are stored under /etc/randrctl/profiles/ as plain text files in json format. Profile structure and property names are self-descriptive, I believe:
    "outputs": {
        "LVDS1": {
            "mode": "1366x768",
            "panning": "1366x1080"
        "DP1": {
            "mode": "1920x1080",
            "pos": "1366x0".
            "rotate": "inverted"
    "primary": "DP1"


Running randrctl without parameters (or with canonical -h or --help) will tell you all you can do with it. And you can:
  • check your current screen setup
    $ randrctl show
  • dump it to file (note that root is required here)
    # randrctl dump my_setup
  • check stored profiles
    $ randrctl show my_setup
  • list your profiles
    $ randrctl list
  • and finally, apply profile settings
    $ randrctl switch-to my_setup

Planned features

Right now I'm thinking about 2 things:
  1. autodetecting when display is plugged via udev and applying profile, that matches display EDID (I come with this idea before knowing about autorandr, I swear ;))
  2. support for user profiles and configurations. This will allow to run randrctl without root
Of course there is always a room for code and experience improvements, so these will take place also.

Tuesday, 18 February 2014

Secondary display not turning on after resume in linux

Occasionally my external display resisted to turn on after being re-plugged into laptop after suspend-resume cycle. The issue appeared again today, after I suspended laptop in the office and resumed it at home.

Tuning Display and Monitor in KDE Control Centre had no effect. Any attempts to turn on external display with xrandr failed silently or with error cannot find crtc for output.

Eventually, after playing around, noticed suspicious xrandr output

% xrandr
Screen 0: minimum 320 x 200, current 1440 x 1848, maximum 32767 x 32767
LVDS1 connected primary 1366x768+0+1080 (normal left inverted right x axis y axis) 277mm x 156mm
   1366x768       60.0*+
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
VGA1 disconnected 1440x900+0+0 (normal left inverted right x axis y axis) # suspicious line

It appeared, that VGA1 output still remains kind of active, and laptop can only handle one external display at time.
Solution was as simple as this
% xrandr --output VGA1 --off

Saturday, 9 November 2013

Listen tracks from youtube in mpd

I don't have my collection of music in the office. no longer offers its great radio.

Luckily, there are tons of tracks on youtube.
Luckily, I use linux and incredibly powerful mpd player, so I don't have to keep youtube open

Suppose, you want to listen to some vocal trance on youtube.

Use youtube-dl to determine what formats are available
$ youtube-dl -f
[youtube] Setting language
[youtube] HS5fHWmzqIg: Downloading video webpage
[youtube] HS5fHWmzqIg: Downloading video info webpage
[youtube] HS5fHWmzqIg: Extracting video information
Available formats:
22      :       mp4     [720x1280]
18      :       mp4     [360x640]
43      :       webm    [360x640]
5       :       flv     [240x400]
36      :       3gp     [240x320]
17      :       3gp     [144x176]
136     :       mp4     [720p] (DASH Video)
135     :       mp4     [480p] (DASH Video)
134     :       mp4     [360p] (DASH Video)
133     :       mp4     [240p] (DASH Video)
160     :       mp4     [192p] (DASH Video)
141     :       m4a     [256k] (DASH Audio)
172     :       webm    [256k] (DASH Audio)
140     :       m4a     [128k] (DASH Audio)
171     :       webm    [128k] (DASH Audio)
139     :       m4a     [48k] (DASH Audio)

There are 5 audio formats for this video. 141th has highest bitrate. So all we have to do, is to get real URL for this format and feed it to mpd
$ mpc add `youtube-dl -f141 -g`

That's all, track from youtube is already in your mpd playlist. Enjoy!

Thursday, 7 November 2013

Saturday, 23 March 2013

Eliminate LED screen flicker with Intel i915

Step-by-step solution

Actions described here may cause damage to your hardware. Do at your own risk.
  1. Install Intel GPU tools
  2. Read reference clock value with command
    # intel_reg_read 0xC6204
    and put the value in the box below
  3. Type desired PWM frequency in the box below
  4. Click
  5. In your terminal execute
    # intel_reg_write 0xC8254 0x7a107a1
Finally, to make things permanent, run this command on system startup. I use systemd so here is what I have
Description=LED PWM frequency 

ExecStart=/usr/bin/intel_reg_write 0xc8254 %I

Enable service on startup
# systemctl enable pwmfrequency@0x7a107a1


PWM modulation frequency of LED backlight in laptops with Intel i915 GPU may be controlled by GPU itself (sometimes it is not the case though). So adjustment of PWM modulation frequency becomes possible by write to special HW register.

In i915 (at least, in Sandy Bridge) PWM modulation frequency is controlled by value of the 4 upper bytes of the register 0xC8254. According to the Intel HD Graphics programmer's reference manual the value of these 4 bytes
represents the period of the PWM stream in PCH display raw clocks multiplied by 128.
PCH display raw clock frequency is held in another register 0xC6204 and by default it equals to 125 MHz (0x7D).
Resulting PWM modulation frequency can be calculated by formula
fPWM = regC6204 / 128 / regC8254{7:4}
For example, for reference clock frequency equal to 125 MHz and value of the regC8254 equal to 0x12281228 (default on my Thinkpad X220) PWM modulation frequency will be equal to approximately 200 Hz.

See also