Skip to content

Instantly share code, notes, and snippets.

@philipperoubert
Last active November 26, 2023 01:06
Show Gist options
  • Save philipperoubert/a4dd97bd94c78a588147c044000a87da to your computer and use it in GitHub Desktop.
Save philipperoubert/a4dd97bd94c78a588147c044000a87da to your computer and use it in GitHub Desktop.
Huawei Matebook 14S Audio Fix
value=`cat selected_audio_output.txt`
if [ -z "$value" ]
then
value=0
fi
if [ $value -eq 0 ]
then
sudo hda-verb /dev/snd/hwC0D0 0x16 0x701 0x0001 // move output to speaker dac
sudo hda-verb /dev/snd/hwC0D0 0x17 0x70C 0x0002 // enable speaker
sudo hda-verb /dev/snd/hwC0D0 0x1 0x715 0x2 // disable headphones
value=1
else
sudo hda-verb /dev/snd/hwC0D0 0x16 0x701 0x0000 // move output to headphones dac
sudo hda-verb /dev/snd/hwC0D0 0x17 0x70C 0x0000 // disable speaker
sudo hda-verb /dev/snd/hwC0D0 0x1 0x717 0x2 // pin output mode
sudo hda-verb /dev/snd/hwC0D0 0x1 0x716 0x2 // pin enable
sudo hda-verb /dev/snd/hwC0D0 0x1 0x715 0x0 // clear pin value
value=0
fi
echo $value > selected_audio_output.txt
@philipperoubert
Copy link
Author

philipperoubert commented Nov 3, 2022

A little script working as a workaround for audio issues on the Huawei Matebook 14S. I bound the script to my F10 key, that way I don't have to run it in my terminal every time I switch to headphones and vice-versa. Tested on Ubuntu 22.10. Below is a little guide on how I did it.

First make sure to have hda-verb installed with apt-get install alsa-tools.

  • Next, save the above file somewhere, I saved it in my home directory.

  • I suggest you run it to ensure that it works so far, sudo sh toggle_audio.sh, try running a couple time to ensure it toggles between speakers and headphones nicely.

An issue with hda-verb is that it requires sudo, so running it from a shortcut won't work.

  • A workaround is to run sudo nano /etc/sudoers, and add the line ALL ALL=NOPASSWD: /usr/bin/hda-verb at the very end. Now running hda-verb shouldn't require sudo anymore. If this doesn't work, check where hda-verb is installed by running which hda-verb and change the path of the line you added in sudoers.

  • Finally, make a custom keyboard shortcut. In Ubuntu it's under Settings -> Keyboard -> View and Customise Shortcuts -> Custom Shortcuts

  • Give your shortcut a name, I named mine "Audio Toggle", input the command /bin/sh /home/YOURUSERNAMEHERE/toggle_audio.sh and the shortcut key you want to use. I used F10 for mine.

Alternatively, if you don't have the option under your distro, you can likely use xbindkeys https://askubuntu.com/a/948011

And voila! That should work, now every time you want to switch between headphones or speakers, you just have to press that shortcut.

For reference: thesofproject/linux#3350
In Linux for some odd reason the speakers and headphone outputs are inverted (if you plug in your headphones, sound will come out from the speakers). Until now, the solution was to unmute "headphones" in alsamixer, but that's not a practical solution. Furthermore, sound from headphones never worked. But now another solution was found involving changing the output of the ALSA hwdep device. Both speakers and headphones now work.

Kudos to @anticodeninja for having found the solution: thesofproject/linux#3350 (comment)

@ogunbaysal
Copy link

Hey, thanks for this solution.
I am using Debian 11 KDE 5.20.5 on Matebook 14s. I have a problem. I did exactly what you share. When i turn on headphone mode it works but after a little time, sound comes from both speaker and headphone even if headphone mode is turned on. After I change mode to Speaker and again Headphone, it works again. Do you have any idea why this happens?

@philipperoubert
Copy link
Author

Hey, thanks for this solution. I am using Debian 11 KDE 5.20.5 on Matebook 14s. I have a problem. I did exactly what you share. When i turn on headphone mode it works but after a little time, sound comes from both speaker and headphone even if headphone mode is turned on. After I change mode to Speaker and again Headphone, it works again. Do you have any idea why this happens?

Sorry for the belated response. Sorry to hear that this doesn't work on your PC.

I fear the issue may be coming from the distros, KDE Plasma vs Ubuntu, or maybe a difference in Kernel version. What kernel is your distro using? In my case I was using 5.19. Hope this can help.

@Smoren
Copy link

Smoren commented Aug 7, 2023

Thank for this code! I've created a daemon for listening headphones connected/disconnected and switching audio output: https://github.com/Smoren/huawei-ubuntu-sound-fix

@eniskastrati
Copy link

Hey everyone,
I just got a matebook 14 with AMD Ryzen 7 and 16gb RAM and on linux distros (already tested Mint, Ubuntu 23 and 22) is not working.
Only with bluetoth headset is working, beside that no speeker is working even if I plug in headphones in the jack they are not recognized and don't work and no sound at all.
Even the microphone input is also not even detected.
Any clue what the issue there might be and how I can fix it, cause I just don't want to change to windows after beeing for more than 4 year a linux user.
In windows works everything perfectly!
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment