Smoren / huawei-ubuntu-sound-fix

Fix for sound card behavior on Huawei Matebook s14 / s16 on Ubuntu 22.04 / Fedora / Arch
140 stars 24 forks source link

Should now work correctly upon reboot #6

Closed Wartybix closed 8 months ago

Wartybix commented 8 months ago

Thank you for writing this daemon :smiley:. I have added some changes to it I thought you might be interested in. Mostly I have improved it to work immediately on login - rather than having to firstly plug/unplug something in/from the headphone jack before it can function as desired.

  1. With every loop, it now checks amixer -c 0 cget numid=14 | grep -q "values=off" instead of amixer -c 0 get Headphone | grep -q "off". This checks whether or not something is plugged into the headphone jack. With the way it was before, if the headphones were left in the jack before shutdown, but removed while the PC was off, it would still report the headphone mixer control as on. The 'Headphone Jack' (i.e. numid=14) card value is more responsive, as it doesn't have this behaviour (it correctly determines whether something is plugged in or not at system startup, instead of just loading a saved value).
  2. Added pacmd set-sink-port alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp__sink "[Out] Headphones" to the switch_to_headphones function. Before, even when the speaker was disabled, the output was moved to the headphones, and the pin was enabled and cleared and whatnot, it still didn't automatically switch to the 'Headphones' port of the audio controller on boot up, as seen in the GNOME sound settings. This fixes this.
  3. The switch_to_headphones or switch_to_speaker function is called with every loop. I imagine you put these in the [ ${status} -ne ${old_status} ] conditional statement for efficiency reasons, but with the way it was before, it wouldn't work correctly after doing things like disconnecting from another audio device (e.g. a Bluetooth device) or waking up from sleep, at least until the headphone jack was plugged/unplugged again. Now these side effects don't happen when the switch_to_XXXX functions are called every loop. I don't know if calling these functions every loop makes a significant impact on the device's power usage or not. I did increase the sleep time for every loop from .3 seconds to 1 second as I thought it would improve the daemon's power usage while having a negligible impact on user experience (slightly less tiny delay when plugging/unplugging the headphone jack before audio comes on).
  4. Added an uninstaller.
  5. Made the comments in huawei-soundcard-headphones-monitor.sh go on separate lines so the lines of code are short and don't wrap around the window.

I tested this on my Matebook 14s and it works quite nicely. Hopefully it works on other machines as well.

Wartybix commented 8 months ago

Nvm, it's unbelievably power inefficient :woozy_face:

Wartybix commented 8 months ago

The CPU consumption is back to the level it was before, with the same fixes I added :smiley:. I realise now '.3' wasn't just an arbitrary value you set in the line sleep .3 and that the output has to be set every fraction of a second, otherwise some other audio process messes with the enable/disable pins after a while. So this has been reverted back. Also the switch_to_XXXX functions are only called when the jack status changes, as before, to save CPU usage.

However, there is now a 2 second delay before the program's while true loop runs in order for other audio processes on the device to do their thing before this program starts enabling/disabling pins etc.. It seems these other audio processes otherwise conflict and there is no sound from either the speaker or headphones (until the headphones are unplugged) when the computer boots with headphones plugged in :unamused:. As the switch_to_XXXX function only has one opportunity to run now (per headphone jack plug/unplug), this sleep command allows the function to run without its changes being immediately overwritten by another audio process.

Tested on my Matebook 14s again and working nicely.

Smoren commented 8 months ago

@Wartybix Thank you for your PR! I merged it.