This project is a continuation of my previous RPi Zero 2 W Audio Gadget project.
The goal of this project is to use the RPi Zero 2 W as a fully functional DAC and DSP. This will be accomplished by using CamillaDSP as well as some other useful pieces of software to connect the USB capture device on the Pi to the Allo miniBOSS DAC. The DSP functionality of CamillaDSP will not really be covered, but the basic config provided with this guide can be expanded upon very easily to add filters, mixers, processing pipelines, and more. The basic config can also be adapted for use with other input/output devices.
This project also adds volume and mute controls that are adjusted from the USB host device. In this case, a Windows computer is used as the host, and the volume and mute on the RPi can be adjusted using the volume control and mute button in Windows.
There are a few assumptions that are made for this guide:
- You followed the previous guide and have a working g_audio device
- Your username is "pi": this can be changed, but you will need to change every instance of "pi" in the config files and.service files
- The port you want to use for the CamillaDSP websocket is "1234": This can also be easily changed by editing the various config files and start command for
camilladsp.service
and the websocket port inusb-control.py
- You are running kernel 5.18-rc3 or greater on the RPi: Older kernels may work, but 5.18 includes improvements to the g_audio driver and, from my testing, works much better with Windows hosts than previous versions.
First, the Allo miniBOSS must be added as an overlay:
sudo nano /boot/config.txt
Change the dtparam=audio
parameter as follows to disable the onboard audio card:
dtparam=audio=off
Then add this line to the end of the file:
dtoverlay=allo-boss-dac-pcm512x-audio
Now reboot your RPi and check that the DAC shows up:
sudo reboot
aplay -l && arecord -l
The output should be similar to this:
You may have more or less devices depending on what audio devices are currently plugged in to you pi, but BossDAC (pcm512x-hifi-0) should be in the list of playback devices.
Step 2: Reconfiguring g_audioNow the UAC2 sound card needs to be reconfigured. The miniBOSS is a 384kHz/32-bit DAC so the USB capture device will be set as such. For simplicity, only use one sample rate. At the time of this writing, changing sample rates is not handled easily by CamillaDSP.
Create a new configuration file for g_audio to be read at boot.
sudo nano /etc/modprobe.d/g_audio.conf
then add the appropriate settings to the file:
options g_audio c_srate=384000 c_ssize=4 p_chmask=0 iProduct="RPi USB DAC"
c_srate
- the capture sample rate in Hzc_ssize
- the size of a sample in bytesp_chmask
- setting the playback channel mask to 0 disables USB playbackiProduct
- a string to describe the device
See my previous guide for more information on what each of these settings does. These can be used to further customize the USB audio device to meet your needs.
If you want to test that this worked properly, reboot and check the currently loaded settings with:
grep -H '' /sys/module/g_audio/parameters/*
Step 3: Setting Up CamillaDSPFirst, install dependencies:
sudo apt install alsa-utils python3 python3-pip python3-websocket python3-aiohttp python3-jsonschema python3-numpy python3-matplotlib unzip git -y
Create the folder structure:
mkdir camilladsp
cd camilladsp
mkdir coeffs configs
NOTE: From this point forward, this guide assumes you are in the ~/camilladsp/
directory.
Now add the CamillaDSP config file for this device:
wget https://raw.githubusercontent.com/Masonrf/RPi-Z2W-USB-Soundcard/main/camilladsp.yml -P configs
Make the log file:
touch camilladsp.log
Now download the latest version of CamillaDSP. This command depends on the architecture of the device it will be run on.
For 64-bit RPi:
wget https://github.com/HEnquist/camilladsp/releases/latest/download/camilladsp-linux-aarch64.tar.gz
For 32-bit RPi:
wget https://github.com/HEnquist/camilladsp/releases/latest/download/camilladsp-linux-armv7.tar.gz
Extract the tarball:
tar -xvf camilladsp-linux-*.tar.gz
Now try to start up CamillaDSP:
./camilladsp -g-10 -p 1234 configs/camilladsp.yml
It should (hopefully) start up without any errors. You may get warnings about wait time outs if you do not have any audio playing from the host:
If you want to do some testing, but only want errors, change the start command to only show errors:
./camilladsp -g-10 -p 1234 configs/camilladsp.yml -l error
Now we can start CamillaDSP at boot as a service.
Download the service file and enable it at boot:
sudo wget https://raw.githubusercontent.com/Masonrf/RPi-Z2W-USB-Soundcard/main/camilladsp.service -P /lib/systemd/system
sudo systemctl enable camilladsp
Make sure you get the last line "Created symlink..."
Install pycamilladsp and its plotting functionality:
sudo git clone https://github.com/HEnquist/pycamilladsp
cd pycamilladsp
sudo pip3 install .
cd ..
sudo git clone https://github.com/HEnquist/pycamilladsp-plot
cd pycamilladsp-plot
sudo pip3 install .
cd ..
Install camillagui:
wget https://github.com/HEnquist/camillagui-backend/releases/latest/download/camillagui.zip
unzip camillagui.zip -d camillagui
Next, we need to reconfigure camillagui:
nano ~/camilladsp/camillagui/config/camillagui.yml
change default_config
and active_config
to the path for the camillaDSP device config:
default_config: "~/camilladsp/configs/camilladsp.yml"
active_config: "~/camilladsp/configs/camilladsp.yml"
Now add CamillaGUI as a service and enable at boot:
sudo wget https://raw.githubusercontent.com/Masonrf/RPi-Z2W-USB-Soundcard/main/camillagui.service -P /lib/systemd/system
sudo systemctl enable camillagui
Now reboot and go to http://(your pi ip):5000/
in your browser
You should see a gui interface for CamillaDSP
If not, check that the services are active:
sudo systemctl status camilladsp.service
sudo systemctl status camillagui.service
If you run into any problems, shoot me a direct message on hackster.
Step 5: Adding Host Volume Control/MuteThe final step is adding a python program to connect the USB volume and mute control signals sent from the host computer to CamillaDSP.
Install dependencies:
sudo apt install libasound2-dev
pip install pyalsaaudio
Download the python program and test it:
wget https://raw.githubusercontent.com/Masonrf/RPi-Z2W-USB-Soundcard/main/usb-control.py
python3 usb-control.py
You should see values change as you change the host computer volume and mute selection. You should also be able to hear the changes while the python program is running and you have sound playing through the USB connection.
Now add this functionality as a service at boot:
sudo wget https://raw.githubusercontent.com/Masonrf/RPi-Z2W-USB-Soundcard/main/usb-control.service -P /lib/systemd/system
sudo systemctl enable usb-control
Finally, reboot for the changes to take effect.
You should now be able to mute and change the volume from your host computer!
If you are having problems adjusting the volume of the USB gadget from Linux host computers, user Cheff has identified a solution that may work. Specifically, PulseAudio seems to have problems with hardware volume control of audio devices that don't have a "common" name. In these cases, it defaults to using software volume control.
Comments