Template: WM8960 Audio HAT Guide

From Waveshare Wiki
Jump to: navigation, search

The guides can only be used with Raspberry Pi OS

Install Driver

Update system:

sudo apt-get update
sudo apt-get upgrade

:Clone driver

git clone https://github.com/waveshare/WM8960-Audio-HAT

Install WM8960 driver:

cd WM8960-Audio-HAT
sudo ./install.sh 
sudo reboot

Check if the driver is installed

sudo dkms status
[email protected]:~ $ sudo dkms status 
wm8960-soundcard, 1.0, 4.19.58-v7l+, armv7l: installed


Note: If the WM8960 Audio HAT has a noise problem, please reinstall the driver and test it again. 。

If the 3.5mm jack has no audio output, please run the following command to restart the service.

sudo systemctl restart wm8960-soundcard.service


Check the Soundcard

  • Test playing:aplay -l
[email protected]:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: wm8960soundcard [wm8960-soundcard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
  • Test recording:arecord -l
[email protected]:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: wm8960soundcard [wm8960-soundcard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Test record/play

record and play

sudo arecord -f cd -Dhw:0 | aplay -Dhw:0

After running the command, you can hear the sound recorded by mic from earphones or speakers. Note that the speaker should be away from the mic to void from noise

Record

sudo arecord -D hw:0,0 -f S32_LE -r 16000 -c 2 test.wav

test.wav is the file name outputed

Play

sudo aplay -Dhw:0 test.wav

Play the audio

Adjust the volume

The default volume is small, and you can install the alsamixer to adjust it

sudo alsamixer

If WM8960 is not the default sound card, you should press F6 to choose an audio device.
Wm8960 audio hat alsamixer.png


mpg123 palyer

aplay command can only support wav file, if you want to paly MP3 file, you can install mpg123 tool.:

sudo apt-get install mpg123 
sudo mpg123 music.mp3

You can replace the music.mp3 with the actual file name.

smplayer for GUI controlling

Note that this software can only work for :

sudo apt-get install smplayer

Right click and set the wm8960-soundcard as default device
Wm8960 audio hat smplayer.png
Open the smplayer software in Menu, choose a audio fiel and play
Wm8960 audio hat smplayer2.png
Wm8960 audio hat smplayer3.png

Examples

We prvide examples for testing

  • Install ibraries
sudo apt-get install libasound2-dev
git clone https://github.com/larsimmisch/pyalsaaudio
cd pyalsaaudio
sudo python3 setup.py build
sudo python3 setup.py install
  • Download examples
wget http://www.waveshare.com/w/upload/1/19/WM8960_Audio_HAT_Code.tar.gz
tar zxvf WM8960_Audio_HAT_Code.tar.gz
sudo chmod 777 -R WM8960_Audio_HAT_Code
  • Play
sudo python playwav.py music.wav
  • Record
sudo python recordwav.py out.wav