Start by making sure you've installed I2C support, see the previous page (Blinka Setup) on how to do it!

Install Voicecard software

Make sure you've got the BrainCraft HAT or Voice Bonnet installed, and I2C support installed as well!

When you run sudo i2cdetect -y 1

you should see an entry under 1A, indicating the hardware sees the audio card. The number may also appear as UU if you already installed software

Raspberry Pi OS Desktop Setup

If you have the desktop version of Raspberry Pi OS desktop, follow these instructions.

cd ~
sudo apt-get install -y git
git clone https://github.com/ubopod/ubo-sdk.git
cd /home/pi/ubo-sdk/system/setup/
sudo bash install_wm8960.sh

Reboot with

sudo reboot

Raspberry Pi OS Lite Setup

Do not install this on the Desktop Version, as the desktop will stop loading after installation!

At the command line run:

cd ~
sudo apt-get install -y git
git clone https://github.com/HinTak/seeed-voicecard
cd seeed-voicecard
sudo ./install.sh

At the end you should see something like this:

Run the following script to fix issues with the installer:

curl -sS https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/main/voice_bonnet.sh | bash

Reboot with

sudo reboot

Checking for the Card

On reboot run

sudo aplay -l

To list all sound cards, you should see it at the bottom

On a Raspberry Pi 4 and 5, your card number may be Card 2 instead of 1 because of the second HDMI port. You'll need to make some changes to some of the commands to reflect this further down.

If your card number differs from the above image, take note of your number.

You can use alsamixer to adjust the volume, dont forget to select the card with F6

A gain of about 60% is plenty loud!

Headphone/Speaker Test

Make sure the Audio On/Off switch is set to ON!

With either headphones plugged into the headphone jack or a speaker attached to the speaker port, run

speaker-test -c2

you will hear white noise coming out of the speakers/headphones!

If you don't hear anything, make sure you have the audio on/off switch set!

Microphone Test

There are two microphones, and now we can test that they work. This test is best done with headphones, not using the speaker port, because it can cause a painful feedback effect if the speakers are next to the mics!

Run:

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

If your sound card ID is not #2, then replace the number in both of the -Dhw: parameters with your actual number.

Then either gently rub each microphone, or speak to hear yourself echoed!

Control-C to quit when done

Your audio subsystem is now completely tested!

Python Libraries

The Microphone and Voice Card are installed as Linux level devices, so using them in is done as you would with any system level audio device. If you would like to make use of audio in Python, you can use the PyAudio library. To install pyaudio and its dependencies, run the following code:

sudo apt-get install libportaudio2 portaudio19-dev
sudo pip3 install pyaudio

Python Usage

Here is a basic test script to enumerate the devices and record for 10 seconds. When prompted, choose the device called seeed-2mic-voicecard.

Copy and paste the following code into a file called audiotest.py.

import pyaudio
import wave

FORMAT = pyaudio.paInt16
CHANNELS = 1           # Number of channels
BITRATE = 44100        # Audio Bitrate
CHUNK_SIZE = 512       # Chunk size to 
RECORDING_LENGTH = 10  # Recording Length in seconds
WAVE_OUTPUT_FILENAME = "myrecording.wav"
audio = pyaudio.PyAudio()

info = audio.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
    if (audio.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
        print("Input Device id ", i, " - ", audio.get_device_info_by_host_api_device_index(0, i).get('name'))

print("Which Input Device would you like to use?")
device_id = int(input()) # Choose a device
print("Recording using Input Device ID "+str(device_id))

stream = audio.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=BITRATE,
    input=True,
    input_device_index = device_id,
    frames_per_buffer=CHUNK_SIZE
)

recording_frames = []

for i in range(int(BITRATE / CHUNK_SIZE * RECORDING_LENGTH)):
    data = stream.read(CHUNK_SIZE)
    recording_frames.append(data)

stream.stop_stream()
stream.close()
audio.terminate()

waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(BITRATE)
waveFile.writeframes(b''.join(recording_frames))
waveFile.close()

Run the code with the following command:

sudo python3 audiotest.py

When finished, you can test playing back the file with the following command:

aplay recordedFile.wav

This guide was first published on Oct 06, 2020. It was last updated on Mar 13, 2024.

This page (Audio Setup) was last updated on Mar 08, 2024.

Text editor powered by tinymce.