I am building a music player system using a Raspberry Pi with Raspbian and a NuForce uDAC-3 USB-DAC.
I got mpd use the DAC instead of Pi's sound system using these lines in /etc/mpd.conf. As far as I know, the essential thing here is selecting hw device 1 instead of default 0.
audio_output {
type "alsa"
name "My ALSA Device"
device "hw:1,0" # optional
format "44100:16:2" # optional
mixer_device "default" # optional
mixer_control "PCM" # optional
mixer_index "0" # optional
}
The driver used for my DAC (snd_usb_audio) doesn't, however, support hardware volume control. There is no volume control available for it in alsamixer, for example. As far as I know, that's a known "feature" for that driver or it's support for that DAC. I got mpd control the volume by uncommenting this in /etc/mpd.conf:
mixer_type "software"
The primary problem now is that there is some lag in volume control that wasn't there with the integrated sound system. I mean, when I slide the volume control in my client program (QMPDClient), there is a short but notable delay before the change in volume can be heard. It's bearable but makes me wonder if everything really works as it should.
The second problem, somewhat related to the first one, is that I am wondering if there is a way to make the sound more perfect as far as any configuration files are concerned.
I have tried various examples of /etc/asound.conf I have found on the internet but either I don't understand what they are supposed to do or they simply are not working. What I thought I would get is either a Master volume control for the DAC recognized by mpd or a virtual sound card that would have a Master volume control and that would feed the sound to the DAC. Initially, /etc/asound.conf was empty, and it still is, now that nothing there seems to affect anything.
Just for the case it has any relevance, the music is in .flac files ripped from CDs.
The snd-usb-audio driver does support hardware volume control in the external dac. The question is whether your dac supports it. I'm using the Micromega MYDAC set to USB 2.0 with the little switch on the back. After plugging it in, dmesg gives:
$ dmesg
[ 489.232193] usb 2-2: new high-speed USB device number 4 using ehci-pci
[ 489.365330] usb 2-2: New USB device found, idVendor=26f2, idProduct=0200
[ 489.365340] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 489.365348] usb 2-2: Product: MICROMEGA MYDAC
[ 489.365355] usb 2-2: Manufacturer: MICROMEGA
[ 489.365361] usb 2-2: SerialNumber: 0000
[ 489.855449] usbcore: registered new interface driver snd-usb-audio
Using amixer I can see the volume control interface:
$ amixer -c MYDAC scontents
Simple mixer control 'MICROMEGA Clock Selector',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 127
Mono:
Front Left: Playback 127 [100%] [0.00dB] [off]
Front Right: Playback 127 [100%] [0.00dB] [off]
Simple mixer control 'MICROMEGA Clock Selector',1
Capabilities: pvolume pvolume-joined pswitch pswitch-joined
Playback channels: Mono
Limits: Playback 0 - 127
Mono: Playback 121 [95%] [-6.00dB] [off]
The audio_output section of my mpd.conf contains:
audio_output {
type "alsa"
name "MICROMEGA MYDAC"
device "hw:MYDAC"
mixer_type "hardware"
mixer_device "hw:MYDAC"
mixer_control "MICROMEGA Clock Selector"
replay_gain_handler "mixer"
auto_resample "no"
auto_channels "no"
auto_format "no"
}
With mpc commands or any other mpd client the volume can now be set to any percentage:
$ mpc volume 100
Oscar Peterson - On A Clear Day You Can See Forever
[playing] #169/213 0:30/4:25 (11%)
volume:100% repeat: on random: on single: off consume: off
$ mpc volume 90
Oscar Peterson - On A Clear Day You Can See Forever
[playing] #169/213 0:33/4:25 (12%)
volume: 90% repeat: on random: on single: off consume: off
However this is where the bad news starts. Looking at the interface with amixer we see what the external DAC actually did when we set its volume to 90%. Since its limits for volume are 0..127, it set the volume to 90% of 127 which is 114. Now 114 is 127-13 so it simply reduced the volume by 13 dB!
$ amixer -c MYDAC scontents
Simple mixer control 'MICROMEGA Clock Selector',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 127
Mono:
Front Left: Playback 114 [90%] [-13.00dB] [on]
Front Right: Playback 114 [90%] [-13.00dB] [on]
So every step down from 127 reduces the volume by 1 dB. This is not at all what 90% of the volume should be. The dB scale should work as follows:
0 dB = 100%
-0.9 dB = 90%
-1.9 dB = 80%
-6 dB = 50%
-20 dB = 10%
So the DAC should have reduced the volume by 0.9 dB, not by 13 dB.
This becomes even more disastrous when you want to use replaygain to automatically control the volume. mpd uses the dB scale as I gave above. I ripped all my CDs to flac and added replaygain tags. These work fine on two other systems not using an external DAC (Poweramp on Android on a Samsung tablet and Deadbeaf on the openPandora device). All volumes come out nicely even.
When I use mpd with the MYDAC the following happens, for example. mpd plays a song with track replaygain of -4.3 dB. So mpd instructs the interface to go to 60% since 20log 0.60 = -4.3 dB. However, the interface doesn't go to 60% of the volume. Instead it sets its parameter 0..127 to its 60% value, which is 0.60 x 127=76. Since the max parameter value 127 corresponds to 0 dB and 76 is 127-51, the DAC simply reduces to -51 dB instead of the intended -4.3 dB. The result is that the music can't be heard at all anymore!
$ metaflac --list 01.Dancers_in_Love.flac
....
METADATA block #2
type: 4 (VORBIS_COMMENT)
comments: 11
comment[0]: ARTIST=Duke Ellington
comment[1]: ALBUM=The Small Groups
comment[2]: TITLE=Dancers in Love
comment[3]: GENRE=Big Band
comment[4]: TRACKNUMBER=01
comment[5]: CDDB=7d10d619
comment[6]: REPLAYGAIN_REFERENCE_LOUDNESS=89.0 dB
comment[7]: REPLAYGAIN_TRACK_GAIN=-4.34 dB
comment[8]: REPLAYGAIN_TRACK_PEAK=0.81216431
comment[9]: REPLAYGAIN_ALBUM_GAIN=-3.61 dB
comment[10]: REPLAYGAIN_ALBUM_PEAK=0.81216431
$ mpc
Duke Ellington - Dancers in Love
[playing] #90/213 0:04/1:55 (3%)
volume: 60% repeat: on random: on single: off consume: off
$ amixer -c MYDAC scontents
Simple mixer control 'MICROMEGA Clock Selector',0
Capabilities: pvolume pswitch
Playback channels: Front Left - Front Right
Limits: Playback 0 - 127
Mono:
Front Left: Playback 76 [60%] [-51.00dB] [on]
Front Right: Playback 76 [60%] [-51.00dB] [on]
It seems that the interpretation of volume in percentage and volume in dB by this external DAC is totally useless. Unfortunately I have a further external DAC that gives similar outputs of the "amixer scontents" command, i.e., it reduces by whole 1dB steps and it maps percentage volume control commands simply to a percentage of the volume parameter of the DAC. I can't say who is at fault here. I would argue the DAC manufacturers. The net result is that volume control in an external DAC is effectively not possible.
I have not found any report of an external DAC that would do hardware volume control correctly according to the dB scale. So I guess software mixer volume control is the only option, even though you loose quality that way. I would love to stand corrected though.