diyAudio

diyAudio (https://www.diyaudio.com/forums/index.php)
-   PC Based (https://www.diyaudio.com/forums/pc-based/)
-   -   using a Raspberry Pi 4 as a USB DSP-DAC (https://www.diyaudio.com/forums/pc-based/341590-using-raspberry-pi-4-usb-dsp-dac.html)

CharlieLaub 17th August 2019 07:00 PM

using a Raspberry Pi 4 as a USB DSP-DAC
 
I have been using the Raspberry Pi platform for several years to implement DSP (via LADSPA plugins) and as streaming clients in a whole-house audio system based on Gstreamer. In this scenario, there is a "source" computer that is playing some audio stream, the stream is directed over my LAN to another Linux computer (e.g. the Pi), and then the audio is split up in to multiple channels, DSP is performed on each channel, and then it is rendered via a DAC.

I recently rediscovered the "USB Gadget" module that has been around for a few years. This uses a USB OTG host to act as one of a number of different kinds of peripherals. The way it works (as far as I understand it) is that you configure the USB OTG host to signal that it is a device of the kind of your choosing. When you connect the gadget device to another USB device, it appears as that kind of peripheral. Adafruit has a very nice tutorial about this here:
Overview | Turning your Raspberry PI Zero into a USB Gadget | Adafruit Learning System
The Adafruit tutorial is written for a Pi Zero. The Pi Zero is not really up to the task of doing DSP unless it is very basic stuff for one or two channels, however, it does show the necessary steps. Note that one of the possibilities is to create an "ethernet gadget".

It turns out that the new Raspberry Pi 4B also has an OTG capabilities via the USB-C port (typically used to provide power to the Pi). We can use this OTG host to convert the USB-C port into a USB gadget connection as long as we power the Pi via other means (e.g. via the GPIO pins).

As luck would have it, I have been using an 8-channel HDMI audio extractor HAT that can power the Pi via the GPIO pins and can provide 8 analog outputs at up to 24/192. When combined with the Pi 4 running as a USB gadget, the Pi can receive audio, perform DSP on it, and then output up to 8 independent channels. This would comprise a very nice USB DSP+DAC unit!

So, how do we set this up?

The way I plan to do it is to put the Pi into "ethernet gadget" mode. Then, when the Pi is connected to another computer via its USB-C connector, it will appear as an ethernet connection (see the Adafruit tutorial at the link above). It is then a matter of redirecting the Windows audio output over this USB ethernet connection, receiving it on the Pi, and so on.


UPDATE: SEPT 3 2019:

The script files and HOW-TO for setting up and streaming audio in this way are attached to POST #77 of this thread. This is version 1.0 for all of these documents.



last Edit: 3 SEPT 2019.

wealas 17th August 2019 08:14 PM

Sounds to me that you are better off emulating a USB UAC2 audio device rather than Ethernet, then the Rpi will show up as an audio card in Windows and you won't need any additional software or drivers on the source computer. And it might be simpler on the RPi side as well.

DPH 17th August 2019 08:40 PM

Maybe it was RPi 3 discussion I looked up some months ago but it made it sound like getting the RPi side of the spectrum to be a USB slave nicely makes the UAC2 device pathway a gigantic pain over other more straightforward, if hardware intensive, ways.

* YMMV and take everything I say with a few truckloads of salt. :)

CharlieLaub 17th August 2019 08:45 PM

Quote:

Originally Posted by wealas (https://www.diyaudio.com/forums/pc-based/341590-using-raspberry-pi-4-usb-dsp-dac-post5887404.html#post5887404)
Sounds to me that you are better off emulating a USB UAC2 audio device rather than Ethernet, then the Rpi will show up as an audio card in Windows and you won't need any additional software or drivers on the source computer. And it might be simpler on the RPi side as well.

On the surface that sounds simpler. I am, after all, trying to implement an audio sink. But let's assume I create a USB audio gadget. Then what? I have no idea how to receive the audio on the Pi in that mode. On the other hand, I have been RXing audio over ethernet for a long time and know that well.

If anyone knows more about the Pi side when it is an audio gadget, please post about it.

phofman 17th August 2019 08:48 PM

The USB audio class 2 gadget in linux kernel was discussed here recently. It appears as capturing alsa device.

wealas 17th August 2019 09:20 PM

Quote:

Originally Posted by CharlieLaub (https://www.diyaudio.com/forums/pc-based/341590-using-raspberry-pi-4-usb-dsp-dac-post5887422.html#post5887422)
On the surface that sounds simpler. I am, after all, trying to implement an audio sink. But let's assume I create a USB audio gadget. Then what? I have no idea how to receive the audio on the Pi in that mode. On the other hand, I have been RXing audio over ethernet for a long time and know that well.

If anyone knows more about the Pi side when it is an audio gadget, please post about it.



If using ethernet, why not just use ethernet then? What does the USB connection bring that an ethernet cable does not?

CharlieLaub 17th August 2019 09:21 PM

Quote:

Originally Posted by phofman (https://www.diyaudio.com/forums/pc-based/341590-using-raspberry-pi-4-usb-dsp-dac-post5887424.html#post5887424)
The USB audio class 2 gadget in linux kernel was discussed here recently. It appears as capturing alsa device.

So, on the Pi, there is a new ALSA input device? You would see it appear via arecord -l?

That would be very simple to access!

CharlieLaub 18th August 2019 12:29 AM

I did some testing with the Pi 4 configured as a USB audio gadget. Thanks for the tip, phofman.

I started by following the Adafruit guide, adding text to config.txt and cmdline.txt. Indeed, a new recording device showed up in ALSA:
Code:

pi@Pi4gadget:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: UAC2Gadget [UAC2_Gadget], device 0: UAC2 PCM [UAC2 PCM]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

The problem was in the parameters it presented via aplay --dump-hw-parameters:
format: S16_LE
sample rate: 64000
These are the defaults for the audio device. Who chose the very non-standard 64kHz as the default recording rate???

I found some source code for the g_audio gadget here:
drivers/usb/gadget/legacy/audio.c - linux-fpga-chameleon - Git at Google
The code suggested I could set parameters for the sample rate and bit depth. The channel count and map could also be set, but the default of stereo is just fine for my needs. I could not seem to set the sample rate and bit depth by providing them as parameters directly in the cmdline.txt call. After a lot of searching of the web, I found some other gadget users were only able to set parameters by moving the initialization out of config.txt and cmdline.txt and into /etc/modules plus a file in /etc/modprobe.d to set the parameter values. So, after backing out my changes to the files in /boot, I did the following:

I added two lines to /etc/modules:
Code:

dwc2
g_audio

I created the file /etc/modprobe.d/usb_g_audio.conf containing the following:
Code:

#load the USB audio gadget module with the following options
options g_audio c_srate=96000 c_ssize=4

NOTE: c_srate sets the sample rate in Hertz and c_ssize sets the number of bytes per sample (4 bytes = 32 bits)

After rebooting, when I run arecord -D hw:0,0 --dump_hw_params the result is:
Code:

pi@Pi4gadget:~ $ arecord -D hw:0,0 --dump-hw-params
Recording WAVE 'stdin' : Unsigned 8 bit, Rate 8000 Hz, Mono
HW Params of device "hw:0,0":
--------------------
ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S32_LE
SUBFORMAT:  STD
SAMPLE_BITS: 32
FRAME_BITS: 64
CHANNELS: 2
RATE: 96000
PERIOD_TIME: [500 5334)
PERIOD_SIZE: [48 512]
PERIOD_BYTES: [384 4096]
PERIODS: [4 16]
BUFFER_TIME: [2000 85334)
BUFFER_SIZE: [192 8192]
BUFFER_BYTES: [1536 65536]
TICK_TIME: ALL
--------------------
arecord: set_params:1339: Sample format non available
Available formats:
- S32_LE

So, I have been able to set up a 32-bit 96kHz USB audio device. Nice! Now I need to test it from the PC end and see how that looks.

It seems that there is one shortcoming to this approach: you can only set up one sample rate and bit depth at a time. It's possible to reconfigure it on the Pi by unloading the g_audio module and loading it again with a different set of parameters using modprobe. It's doable but you would need to be able to log into the Pi in order to do that.

On the other hand, if I set up the USB gadget as g_ether (ethernet over USB) I get the same simple USB connectivity, but I can easily ssh into the Pi to run commands, etc. The sample rate could be changed much more easily without requiring changes to the kernel modules. The only downside is that an app is needed on the PC to send windows audio over the USB-ethernet link in addition to the one running one the Pi that does the DSP, etc.

Either way, you get some nice DSP functionality plus hot-swappable USB connectivity.

CharlieLaub 18th August 2019 03:27 AM

Well, after the initial success, failure. When I connected the Pi to the host Windows PC via a USB-C cable, the USB audio device was installed on the Windows machine but there was a problem and the driver could not be started. I tried to find some information about this error, but everything I could find was just generic tips to reinstall drivers, try rebooting, etc. Garbage.

If that could be overcome, I think we could have something useful in the USB Audio Gadget. Otherwise it's a bit of a dead end.

CharlieLaub 18th August 2019 03:47 AM

So, on to plan B: the USB Ethernet gadget.

I followed the Adafruit guide. I made some good progress and then started to hit some serious snags when I tried to set up a fixed IP address for the USB Ethernet gadget connection. I need to do this so that I can ssh into the Pi from another computer. If you do not set up a fixed IP address at boot time, the Pi will assign a random address in the 192.254.X.X range. This address will change after each boot, so you would not be able to know it in advance. The IP address is needed to ssh into the Pi (I can't use Bonjour) and unless, like I was, you were also logged into the machine with keyboard mouse and screen all connected this might create an impasse.

The other problem was that setting up a fixed IP address for the USB Ethernet gadget connection by following the Adafruit example caused my wired ethernet connection to also default to the 192.254.X.X range, which is useless. If I unplugged the wired ethernet and then plugged it in again, it would get assigned an IP on my LAN but then the Ethernet gadget connection would go down and could only be brought up again by a reboot.

It seems that my Pi is not yet configured to handle two different networks with very different address ranges, etc., one connected via the ethernet and my LAN, and the other via the USB gadget. I'm not sure how to do that, but it might not be necessary (keep reading to find out why):

In the end I did get it working when I booted up the Pi without the wired or wireless network connected and then I connected the USB gadget to the host. By this time the Windows RNDIS connection was configured with the correct IP address info. At this point I could successfully SSH into the Pi from the host.

So, after a little pain, this type of USB gadget connection indeed works. The current limitation, barring a fix, is that the Pi cannot have any other internet connection if you want the USB gadget to function. Since I want to use it as a headless USB dongle and only access it via SSH from a host, that is no problem since I would not need any internet connections. It is also possible to share the host's internet connection over the USB gadget, but I have not tried that yet. You might want to access the internet to, for example, download new software or update the system from time to time. But mostly you would not need that capability in the role of a DSP-DAC unit.

I will keep working on this after some sleep.


All times are GMT. The time now is 04:30 AM.


Search Engine Optimisation provided by DragonByte SEO (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 17.65%
vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
Copyright ©1999-2020 diyAudio

Wiki