Difference between revisions of "Template:WM8960 Audio HAT Guide"

From Waveshare Wiki
Jump to: navigation, search
m (Text replacement - "http://www.waveshare.com/w/upload/" to "https://files.waveshare.com/upload/")
 
(20 intermediate revisions by one other user not shown)
Line 1: Line 1:
  
The guides can only be used with Raspberry Pi OS  
+
<font color="#FF0000">The guides can only be used with Raspberry Pi OS.</font>
  
 
==Install Driver==
 
==Install Driver==
Update system:<br />
+
Update system:<br />
 
<pre>
 
<pre>
 
sudo apt-get update
 
sudo apt-get update
 
sudo apt-get upgrade
 
sudo apt-get upgrade
 
</pre>
 
</pre>
:Clone driver
+
Clone driver:
 
<pre>
 
<pre>
 
git clone https://github.com/waveshare/WM8960-Audio-HAT
 
git clone https://github.com/waveshare/WM8960-Audio-HAT
 
</pre>
 
</pre>
Install WM8960 driver:<br />
+
Install WM8960 driver:<br />
 
<pre>
 
<pre>
 
cd WM8960-Audio-HAT
 
cd WM8960-Audio-HAT
Line 19: Line 19:
 
</pre>
 
</pre>
  
Check if the driver is installed<br />
+
Check if the driver is installed.<br />
 
<pre>
 
<pre>
 
sudo dkms status
 
sudo dkms status
Line 29: Line 29:
  
  
Note: If the WM8960 Audio HAT has a noise problem, please reinstall the driver and test it again. <br />
+
Note: If the WM8960 Audio HAT has a noise problem, please reinstall the driver and test it again.<br />
  
 
If the 3.5mm jack has no audio output, please run the following command to restart the service.
 
If the 3.5mm jack has no audio output, please run the following command to restart the service.
Line 35: Line 35:
 
sudo systemctl restart wm8960-soundcard.service
 
sudo systemctl restart wm8960-soundcard.service
 
</pre>
 
</pre>
 
  
 
==Check the Soundcard==
 
==Check the Soundcard==
Line 61: Line 60:
 
sudo arecord -f cd -Dhw:0 | aplay -Dhw:0
 
sudo arecord -f cd -Dhw:0 | aplay -Dhw:0
 
</pre>
 
</pre>
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<br />
+
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.<br />
  
 
===Record===
 
===Record===
Line 67: Line 66:
 
sudo arecord -D hw:0,0 -f S32_LE -r 16000 -c 2 test.wav
 
sudo arecord -D hw:0,0 -f S32_LE -r 16000 -c 2 test.wav
 
</pre>
 
</pre>
test.wav is the file name outputed<br />
+
test.wav is the file name outputed.<br />
  
 
===Play===
 
===Play===
Line 73: Line 72:
 
sudo aplay -Dhw:0 test.wav
 
sudo aplay -Dhw:0 test.wav
 
</pre>
 
</pre>
Play the audio<br />
+
Play the audio.<br />
  
 
===Adjust the volume===
 
===Adjust the volume===
The default volume is small, and you can install the alsamixer to adjust it<br />
+
The default volume is small, and you can install the alsamixer to adjust it.<br />
 
<pre>
 
<pre>
 
sudo alsamixer
 
sudo alsamixer
Line 82: Line 81:
 
If WM8960 is not the default sound card, you should press F6 to choose an audio device.<br />
 
If WM8960 is not the default sound card, you should press F6 to choose an audio device.<br />
 
[[file:wm8960_audio_hat_alsamixer.png|500px]]<br />
 
[[file:wm8960_audio_hat_alsamixer.png|500px]]<br />
 
+
In fact, there are many options on the right that can be adjusted.
  
 
===mpg123 palyer===
 
===mpg123 palyer===
aplay command can only support wav file, if you want to paly MP3 file, you can install mpg123 tool.:<br />
+
aplay command can only support wav files, if you want to play MP3 file, you can install the mpg123 tool:<br />
 
<pre>
 
<pre>
 
sudo apt-get install mpg123  
 
sudo apt-get install mpg123  
Line 93: Line 92:
  
 
===smplayer for GUI controlling===
 
===smplayer for GUI controlling===
Note that this software can only work for
+
Note that this software can only work for:
 
<pre>
 
<pre>
 
sudo apt-get install smplayer
 
sudo apt-get install smplayer
 
</pre>
 
</pre>
Right click and set the wm8960-soundcard as default device<br />
+
Right-click and set the wm8960-soundcard as the default device.<br />
 
[[file:wm8960_audio_hat_smplayer.png|500px]]<br />
 
[[file:wm8960_audio_hat_smplayer.png|500px]]<br />
Open the smplayer software in Menu, choose a audio fiel and play<br />
+
Open the smplayer software in Menu, choose an audio file and play.<br />
 
[[file:wm8960_audio_hat_smplayer2.png|500px]]<br />
 
[[file:wm8960_audio_hat_smplayer2.png|500px]]<br />
 
[[file:wm8960_audio_hat_smplayer3.png|500px]]<br />
 
[[file:wm8960_audio_hat_smplayer3.png|500px]]<br />
  
 
==Examples==
 
==Examples==
We prvide examples for testing<br />
+
We provide examples for testing.<br />
  
 
*Install ibraries
 
*Install ibraries
Line 115: Line 114:
 
</pre>
 
</pre>
  
*Download examples
+
*Download examples:
 
<pre>
 
<pre>
wget http://www.waveshare.com/w/upload/1/19/WM8960_Audio_HAT_Code.tar.gz
+
wget https://files.waveshare.com/upload/1/19/WM8960_Audio_HAT_Code.tar.gz
 
tar zxvf WM8960_Audio_HAT_Code.tar.gz
 
tar zxvf WM8960_Audio_HAT_Code.tar.gz
 
sudo chmod 777 -R WM8960_Audio_HAT_Code
 
sudo chmod 777 -R WM8960_Audio_HAT_Code
 
</pre>
 
</pre>
  
*Play
+
*Play:
 
<pre>
 
<pre>
 
sudo python playwav.py music.wav
 
sudo python playwav.py music.wav
 
</pre>
 
</pre>
  
*Record
+
*Record:
 
<pre>
 
<pre>
 
sudo python recordwav.py out.wav
 
sudo python recordwav.py out.wav
 
</pre>
 
</pre>
 +
==WM8960==
 +
Try printing to the position shown in the picture in U-boot, and press any key to switch to the device tree.<br/>
 +
[[File: VisionFive013.jpg]]<br/>
 +
 +
Replace the command at startup with the following:<br/>
 +
<pre>
 +
ext4load mmc 1:3 a0000000 /boot/uEnv.txt
 +
env import a0000000 17c
 +
setenv fdtfile starfive/jh7110-visionfive-v2-wm8960.dtb
 +
sysboot mmc 1:3 ext2 b0000000 /boot/extlinux/extlinux.conf
 +
</pre>
 +
==202302 System, Driver Loading==
 +
Boot and enter the system, and input the commands:
 +
nano /boot/uEnv.txt
 +
Go to the system boot configuration file, comment out section 1, and add section 2:
 +
fdtfile=starfive/jh7110-visionfive-v2-wm8960.dtb
 +
[[File:202302 System Loading01.png]]<br/>
 +
Reboot.
 +
 +
==Start to Use==
 +
Install alsa-utils<br/>
 +
<pre>
 +
apt install alsa-utils
 +
</pre>
 +
If the module is not connected at the beginning, you need to restart VisionFive2 to detect the audio device.<br/>
 +
Type aplay -l to list sound cards and digital audio devices.<br/>
 +
Pay attention to the position marked on the picture, which indicates the device number of the sound card and digital audio equipment, where card0 and device0 are the headphone jacks on the VisionFive2 board, card0 and device2 are the headphone jacks on the WM8960 module.<br/>
 +
[[File:VisionFive290.jpg]]<br/>
 +
Type arecord -l to list sound cards and recording devices.<br/>
 +
Pay attention to the position marked on the picture, which indicates the device number of the sound card and recording device, card0 and device1 are the two patch microphones on the WM8960 module.<br/>
 +
[[File:VisionFive291.jpg]]<br/>
 +
Input the command:<br/>
 +
arecord -Dhw:0,1 -f S32_LE -r16000 -c2 -d 10 -t wav sound_32b_16k.wav
 +
Start recording 10 seconds of 32-bit, 16kHz audio, and save it in the current directory, the file name is sound_32b_16k.wav.<br/>
 +
input the command:<br/>
 +
aplay -Dhw:0,2 -f S32_LE -r16000 -t wav sound_32b_16k.wav
 +
Use the WM8960 module onboard headphone interface to play audio.<br/>
 +
<font color="#FF0000">Note that the headphone jack on the VisionFive2 board only supports audio output in 16-bit audio format. </font><br/>

Latest revision as of 06:50, 4 September 2023

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
pi@raspberrypi:~ $ 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
pi@raspberrypi:~ $ 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
pi@raspberrypi:~ $ 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
In fact, there are many options on the right that can be adjusted.

mpg123 palyer

aplay command can only support wav files, if you want to play MP3 file, you can install the 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 the default device.
Wm8960 audio hat smplayer.png
Open the smplayer software in Menu, choose an audio file and play.
Wm8960 audio hat smplayer2.png
Wm8960 audio hat smplayer3.png

Examples

We provide 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 https://files.waveshare.com/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

WM8960

Try printing to the position shown in the picture in U-boot, and press any key to switch to the device tree.
VisionFive013.jpg

Replace the command at startup with the following:

ext4load mmc 1:3 a0000000 /boot/uEnv.txt 
env import a0000000 17c
setenv fdtfile starfive/jh7110-visionfive-v2-wm8960.dtb 
sysboot mmc 1:3 ext2 b0000000 /boot/extlinux/extlinux.conf

202302 System, Driver Loading

Boot and enter the system, and input the commands:

nano /boot/uEnv.txt

Go to the system boot configuration file, comment out section 1, and add section 2:

fdtfile=starfive/jh7110-visionfive-v2-wm8960.dtb

202302 System Loading01.png
Reboot.

Start to Use

Install alsa-utils

apt install alsa-utils

If the module is not connected at the beginning, you need to restart VisionFive2 to detect the audio device.
Type aplay -l to list sound cards and digital audio devices.
Pay attention to the position marked on the picture, which indicates the device number of the sound card and digital audio equipment, where card0 and device0 are the headphone jacks on the VisionFive2 board, card0 and device2 are the headphone jacks on the WM8960 module.
VisionFive290.jpg
Type arecord -l to list sound cards and recording devices.
Pay attention to the position marked on the picture, which indicates the device number of the sound card and recording device, card0 and device1 are the two patch microphones on the WM8960 module.
VisionFive291.jpg
Input the command:

arecord -Dhw:0,1 -f S32_LE -r16000 -c2 -d 10 -t wav sound_32b_16k.wav

Start recording 10 seconds of 32-bit, 16kHz audio, and save it in the current directory, the file name is sound_32b_16k.wav.
input the command:

aplay -Dhw:0,2 -f S32_LE -r16000 -t wav sound_32b_16k.wav

Use the WM8960 module onboard headphone interface to play audio.
Note that the headphone jack on the VisionFive2 board only supports audio output in 16-bit audio format.