# Bad audio quality on headphones only



## Kova (Sep 4, 2016)

Hello,

I am getting bad spectral distribution (bass and treble are too weak) on headphones. Spectral distribution on speakers is fine.

It's the same across different headphones, so the problem is at the jack. Tweaking params with sysctl has practically no effect. Neither has a (unrelated) kernel upgrade from 10.3 RELEASE to 12.0 CURRENT, so I doubt uname -a would even matter.

Now, the usual litany of information:
CPU is Intel Core i7-6500U (amd64 k8 (Skylake))

`cat /dev/sndstat` with hw.snd.verbose=2:


```
FreeBSD Audio Driver (64bit 2009061500/amd64)
Installed devices:
pcm0: <Realtek (0x0255) (Analog 2.0+HP/2.0)> on hdaa0  (1p:1v/1r:1v) default
  snddev flags=0x2e2<AUTOVCHAN,BUSY,MPSAFE,REGISTERED,VPC>
  [pcm0:play:dsp0.p0]: spd 48000, fmt 0x00200010, flags 0x00002100, 0x00000004
  interrupts 21320, underruns 0, feed 21320, ready 0 [b:4096/2048/2|bs:4096/2048/2]
  channel flags=0x2100<BUSY,HAS_VCHAN>
  {userland} -> feeder_mixer(0x00200010) -> {hardware}
  pcm0:play:dsp0.p0[pcm0:virtual:dsp0.vp0]: spd 48000, fmt 0x00201000/0x00200010, flags 0x10000000, 0x00000023
  interrupts 0, underruns 0, feed 0, ready 0 [b:0/0/0|bs:131072/4096/32]
  channel flags=0x10000000<VIRTUAL>
  {userland} -> feeder_root(0x00201000) -> feeder_format(0x00201000 -> 0x00200010) -> feeder_volume(0x00200010) -> {hardware}
  [pcm0:record:dsp0.r0]: spd 48000, fmt 0x00200010, flags 0x00002100, 0x00000005
  interrupts 0, overruns 0, feed 0, hfree 4096, sfree 4096 [b:4096/2048/2|bs:4096/2048/2]
  channel flags=0x2100<BUSY,HAS_VCHAN>
  {hardware} -> feeder_root(0x00200010) -> feeder_mixer(0x00200010) -> {userland}
  pcm0:record:dsp0.r0[pcm0:virtual:dsp0.vr0]: spd 8000, fmt 0x00100008, flags 0x10000000, 0x00000000
  interrupts 0, overruns 0, feed 0, hfree 0, sfree 0 [b:0/0/0|bs:0/0/0]
  channel flags=0x10000000<VIRTUAL>
  {hardware} -> feeder_root(0x00000000) -> {userland}
pcm1: <Intel (0x2809) (HDMI/DP 8ch)> on hdaa1  (1p:1v/0r:0v)
  snddev flags=0x2e7<SIMPLEX,AUTOVCHAN,SOFTPCMVOL,BUSY,MPSAFE,REGISTERED,VPC>
  [pcm1:play:dsp1.p0]: spd 48000, fmt 0x00200010, flags 0x00006100, 0x00000004
  interrupts 0, underruns 0, feed 0, ready 0 [b:4096/2048/2|bs:4096/2048/2]
  channel flags=0x6100<BUSY,HAS_VCHAN,VCHAN_PASSTHROUGH>
  {userland} -> feeder_mixer(0x00200010) -> {hardware}
  pcm1:play:dsp1.p0[pcm1:virtual:dsp1.vp0]: spd 8000, fmt 0x00100008, flags 0x10000000, 0x00000000
  interrupts 0, underruns 0, feed 0, ready 0 [b:0/0/0|bs:0/0/0]
  channel flags=0x10000000<VIRTUAL>
  {userland} -> feeder_root(0x00000000) -> {hardware}
No devices installed from userspace.
```

`dmesg | grep hda`:

```
hdac0: <Intel Sunrise Point-LP HDA Controller> mem 0xd1220000-0xd1223fff,0xd1200000-0xd120ffff irq 16 at device 31.3 on pci0
hdacc0: <Realtek (0x0255) HDA CODEC> at cad 0 on hdac0
hdaa0: <Realtek (0x0255) Audio Function Group> at nid 1 on hdacc0
pcm0: <Realtek (0x0255) (Analog 2.0+HP/2.0)> at nid 20,33 and 18 on hdaa0
hdacc1: <Intel (0x2809) HDA CODEC> at cad 2 on hdac0
hdaa1: <Intel (0x2809) Audio Function Group> at nid 1 on hdacc1
pcm1: <Intel (0x2809) (HDMI/DP 8ch)> at nid 3 on hdaa1
```

NB the command also reveals the following line:

```
hdac0: Command timeout on address 2
```
Could that be related? Just included for the sake of completeness.

`grep hda /boot/device.hints`:

```
hint.hdac.0.config="64bit"
hint.hdaa.0.nid20.config="as=1 seq=0 misc=0"
hint.hdaa.0.nid33.config="as=1 seq=15 device=Headphones ctype=Analog"
```
nid20 is the internal speaker.

`sysctl dev.hdaa.0.nid33` nid33 is the jack for the headphones:

```
dev.hdaa.0.nid33: pin: Headphones (Black Jack)
  Widget cap: 0x0040058d PWR UNSOL STEREO
  Association: 0 (0x8000)
  Pin cap: 0x0001001c PDC HP OUT EAPD
  Pin config: 0x0227101f as=1 seq=15 device=Headphones conn=Jack ctype=Analog loc=Front color=Black misc=0
  Pin control: 0x000000c0 HP OUT
  EAPD: 0x00000002 EAPD
  Output amp: 0x80000000 mute=1 step=0 size=0 offset=0 (0/0dB)
  Connections: 2
  + <- nid=12 [audio mixer] (selected)
  + [DISABLED] <- nid=13 [audio mixer] [DISABLED]
```

I'm getting the feeling this could be a problem with the Realtek codec (though if it were, I suppose the speakers would be equally affected, which isn't the case)...


----------



## Juha Nurmela (Sep 5, 2016)

Have you gone thru the GPIO settings, in case one of them affects the HP amp ? Not b*y likely, sigh, but ...

```
set -x
for i in 0 1 2 3 4 5 6 7
do
sysctl dev.hdaa.0.gpio_config=$i=set
sleep 1
sysctl dev.hdaa.0.gpio_config=$i=clear
sleep 1
done
```

I found the schematics for my PC from an pirate archive. hint: tanya

Juha


----------



## Kova (Sep 5, 2016)

Juha Nurmela said:


> Have you gone thru the GPIO settings, in case one of them affects the HP amp ? Not b*y likely, sigh, but ...
> 
> ```
> sysctl dev.hdaa.0.gpio_config=$i=set
> ...



I tried to, but when I looked it up in the man page on sterm, the entry was underlined, so I didn't see the underscore in the name. I kept getting 'unknown oid'.
Thanks a lot =) I'll try it out and get back to you.


----------



## Juha Nurmela (Sep 5, 2016)

Also scan the gpo_*config* bits while you are there.
Do you happen to have to turn all the mixer knobs to the max, or everything just barely open ?

Juha


----------



## Kova (Sep 5, 2016)

Hey Juha,

I finally got out of classes at university. I decided to try various programs to see if the problem is with them (and not the driver or codec).
I tried the old trick of 'cp $filename /dev/dsp' - and, lo and behold, the outputs are indistinguishable in quality!
I tried VLC Media Player - and the sound is distorted as with Firefox.
I'll admit I had only listened to music videos with Firefox and started troubleshooting from the lowest level.

As for dev.hdaa.0, the .gpo_config field seems to be empty...
`sysctl dev.hdaa.0.gpo_config`

```
dev.hdaa.0.gpo_config:
```

Thanks for the ideas nevertheless =)


----------



## Juha Nurmela (Sep 5, 2016)

Really depressing to peek at what Linux does with this ALC codec. 5000+ lines of setup for different Realtek chips. *Unified* audio architecture.

If you have steam left, try the _ovref50_ or _80_ settings too.

Juha


----------



## Kova (Sep 5, 2016)

Juha Nurmela said:


> If you have steam left, try the _ovref50_ or _80_ settings too.



Set, with no effect...

Could this be due to some library that VLC and Firefox both depend on? I don't remember the exact dependency list.


----------



## abishai (Sep 5, 2016)

Heh, looks like we have similar issues https://forums.freebsd.org/threads/57542/#post-328385


----------



## Juha Nurmela (Sep 5, 2016)

Kova said:


> Output amp: 0x80000000 mute=1 step=0 size=0 offset=0 (0/0dB)



Ugh, what's that mute=1 ?


Ugh^2, too many cooks, it just tells that widget contains a mute gate.


----------



## Juha Nurmela (Sep 5, 2016)

Or was that when the phones were not connected? Just a while ago, someone (shepper probably) said to use `sysctl hw.snd.verbose=4` and unplug/replug. It should blurt out (disconnected) and (connected) on the console.


----------



## Kova (Sep 5, 2016)

abishai said:


> Heh, looks like we have similar issues https://forums.freebsd.org/threads/57542/#post-328385


Wow, you have practically the exact same config as me...


----------



## Kova (Sep 5, 2016)

Juha Nurmela said:


> Ugh, what's that mute=1 ?


That thing's on even when the headphones are connected... I wondered about that too.


----------



## edenist (Aug 28, 2017)

Sorry for the necro, but this is pretty much the only thread I could find on this matter [plus another in Hardware made at nearly the same time].

I've also been experiencing issues with headphone audio quality with intel HDA audio, though in my case it is using the Cirrus driver.

Here is the output from my /dev/sndstat


```
FreeBSD Audio Driver (64bit 2009061500/amd64)
Installed devices:
pcm0: <Cirrus Logic CS4206 (Analog)> on hdaa0  (1p:2v/1r:1v)
   snddev flags=0x2e2<AUTOVCHAN,BUSY,MPSAFE,REGISTERED,VPC>
   [pcm0:play:dsp0.p0]: spd 48000, fmt 0x00200010, flags 0x00002100, 0x00000004
   interrupts 7011, underruns 0, feed 7011, ready 0 [b:4096/2048/2|bs:4096/2048/2]
   channel flags=0x2100<BUSY,HAS_VCHAN>
   {userland} -> feeder_mixer(0x00200010) -> {hardware}
   pcm0:play:dsp0.p0[pcm0:virtual:dsp0.vp0]: spd 44100/48000, fmt 0x00200010, flags 0x10000000, 0x00000029
   interrupts 0, underruns 0, feed 0, ready 0 [b:0/0/0|bs:65536/2048/32]
   channel flags=0x10000000<VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> feeder_rate(0x00200010 q:1 44100 -> 48000) -> {hardware}
   pcm0:play:dsp0.p0[pcm0:virtual:dsp0.vp1]: spd 44100/48000, fmt 0x00200010, flags 0x10000000, 0x00000029
   interrupts 0, underruns 0, feed 0, ready 0 [b:0/0/0|bs:65536/2048/32]
   channel flags=0x10000000<VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> feeder_rate(0x00200010 q:1 44100 -> 48000) -> {hardware}
   [pcm0:record:dsp0.r0]: spd 48000, fmt 0x00200010, flags 0x00002100, 0x00000005
   interrupts 0, overruns 0, feed 0, hfree 4096, sfree 4096 [b:4096/2048/2|bs:4096/2048/2]
   channel flags=0x2100<BUSY,HAS_VCHAN>
   {hardware} -> feeder_root(0x00200010) -> feeder_mixer(0x00200010) -> {userland}
   pcm0:record:dsp0.r0[pcm0:virtual:dsp0.vr0]: spd 8000, fmt 0x00100008, flags 0x10000000, 0x00000000
   interrupts 0, overruns 0, feed 0, hfree 0, sfree 0 [b:0/0/0|bs:0/0/0]
   channel flags=0x10000000<VIRTUAL>
   {hardware} -> feeder_root(0x00000000) -> {userland}
pcm1: <Cirrus Logic CS4206 (Digital)> on hdaa0  (1p:1v/1r:1v)
   snddev flags=0x2e6<AUTOVCHAN,SOFTPCMVOL,BUSY,MPSAFE,REGISTERED,VPC>
   [pcm1:play:dsp1.p0]: spd 48000, fmt 0x00200010, flags 0x00006100, 0x00000004
   interrupts 2720, underruns 0, feed 2720, ready 0 [b:4096/2048/2|bs:4096/2048/2]
   channel flags=0x6100<BUSY,HAS_VCHAN,VCHAN_PASSTHROUGH>
   {userland} -> feeder_mixer(0x00200010) -> {hardware}
   pcm1:play:dsp1.p0[pcm1:virtual:dsp1.vp0]: spd 44100/48000, fmt 0x00200010, flags 0x10000000, 0x00000029
   interrupts 0, underruns 0, feed 0, ready 0 [b:0/0/0|bs:65536/2048/32]
   channel flags=0x10000000<VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> feeder_rate(0x00200010 q:1 44100 -> 48000) -> {hardware}
   [pcm1:record:dsp1.r0]: spd 48000, fmt 0x00200010, flags 0x00006100, 0x00000005
   interrupts 0, overruns 0, feed 0, hfree 4096, sfree 4096 [b:4096/2048/2|bs:4096/2048/2]
   channel flags=0x6100<BUSY,HAS_VCHAN,VCHAN_PASSTHROUGH>
   {hardware} -> feeder_root(0x00200010) -> feeder_mixer(0x00200010) -> {userland}
   pcm1:record:dsp1.r0[pcm1:virtual:dsp1.vr0]: spd 8000, fmt 0x00100008, flags 0x10000000, 0x00000000
   interrupts 0, overruns 0, feed 0, hfree 0, sfree 0 [b:0/0/0|bs:0/0/0]
   channel flags=0x10000000<VIRTUAL>
   {hardware} -> feeder_root(0x00000000) -> {userland}
pcm2: <Cirrus Logic CS4206 (Analog Headphones)> on hdaa0  (1p:4v/0r:0v) default
   snddev flags=0x200002e3<SIMPLEX,AUTOVCHAN,BUSY,MPSAFE,REGISTERED,VPC,PRIO_WR>
   [pcm2:play:dsp2.p0]: spd 48000, fmt 0x00200010, flags 0x00002100, 0x00000004
   interrupts 13436, underruns 0, feed 13436, ready 0 [b:4096/2048/2|bs:4096/2048/2]
   channel flags=0x2100<BUSY,HAS_VCHAN>
   {userland} -> feeder_mixer(0x00200010) -> {hardware}
   pcm2:play:dsp2.p0[pcm2:virtual:dsp2.vp0]: spd 44100/48000, fmt 0x00200010, flags 0x10001154, 0x00000029, pid 23724 (firefox)
   interrupts 0, underruns 0, feed 15043, ready 0 [b:0/0/0|bs:16384/8192/2]
   channel flags=0x10001154<RUNNING,NOTRIGGER,NBIO,BUSY,HAS_SIZE,VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> feeder_rate(0x00200010 q:1 44100 -> 48000) -> {hardware}
   pcm2:play:dsp2.p0[pcm2:virtual:dsp2.vp1]: spd 48000, fmt 0x00200010, flags 0x10001154, 0x00000021, pid 25047 (firefox)
   interrupts 0, underruns 0, feed 3945, ready 0 [b:0/0/0|bs:16384/8192/2]
   channel flags=0x10001154<RUNNING,NOTRIGGER,NBIO,BUSY,HAS_SIZE,VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> {hardware}
   pcm2:play:dsp2.p0[pcm2:virtual:dsp2.vp2]: spd 44100/48000, fmt 0x00200010, flags 0x10001154, 0x00000029, pid 67314 (firefox)
   interrupts 0, underruns 0, feed 498, ready 0 [b:0/0/0|bs:16384/8192/2]
   channel flags=0x10001154<RUNNING,NOTRIGGER,NBIO,BUSY,HAS_SIZE,VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> feeder_rate(0x00200010 q:1 44100 -> 48000) -> {hardware}
   pcm2:play:dsp2.p0[pcm2:virtual:dsp2.vp3]: spd 48000, fmt 0x00200010, flags 0x10000000, 0x00000021
   interrupts 0, underruns 0, feed 0, ready 0 [b:0/0/0|bs:65536/2048/32]
   channel flags=0x10000000<VIRTUAL>
   {userland} -> feeder_root(0x00200010) -> feeder_volume(0x00200010) -> {hardware}
No devices installed from userspace.
```

One of the things which caught my attention in my output [which is missing from OP] is the "feeder_rate" parameter.
Specifically, feeder_rate(0x00200010 q:1 44100 -> 48000).

I'm not an expert on sound systems, but what this says to me is that my output is set to 48kHz, yet the media I am listening to [mp3 files] was 44.1kHz.
I have checked the freeBSD manual page for details on these parameters [https://www.freebsd.org/cgi/man.cgi?query=sound&manpath=FreeBSD+8-current]

While there is parameter for _hw.snd.feeder_*_*_rate_*_*_min _and_ hw.snd.feeder_*_*_rate_*_*_max_, setting these had zero affect.

However, the _hw.snd.feeder_*_*_rate_quality_ parameter did spark my interest. This is the quality of the conversion between different sample rates. By default it is set to 1 [q:1 in the output above].

I executed the following command:
`sysctl hw.snd.feeder_rate_quality=4`

And the sound quality is now perfect [well, 97% perfect according to the documentation.... heh ]

Hopefully this information helps some people out.


----------



## rigoletto@ (Aug 28, 2017)

> my output is set to 48kHz, yet the media I am listening to [mp3 files] was 44.1kHz.



It is sound(4) remixing (dsp processing) to the default frequency (48kHz). The only way to complete get rid of remixing is setting it to play bitperfect:

`dev.pcm.%d.bitperfect=1`

However, if you play something in a given frequency your sound card does not support you will get no sound.


----------



## felselva (Dec 29, 2018)

Sorry the necro-bump, but I had this problem (distorted audio in headphones), and after googling a lot it seems this problem is a bit common with Realtek, but I found a solution that worked for me. _Maybe_ it will work for someone else, reason of my post.

After the usual configuration (the one from the handbook) I activate the headphones (Realtek ALC255):
- sysctl hw.snd.default_unit=4

Then I tweaked with the PCM levels with the mixer:
- mixer pcm 50:100

Seems like the reason of the problem is that the hardware or driver requires the PCM from the left and right to be balanced. For me, 50:100 did the trick.


----------

