BelaPlatform/bela-image-builder

UAC1/UAC2

Opened this issue · 4 comments

tried the usual configfs stuff in /opt/Bela/bela_gadget.sh:

mkdir -p functions/uac2.usb0
....
ln -s functions/uac2.usb0 configs/c.1/

so that the default values are loaded:

root@bela:/sys/kernel/config/usb_gadget/g# for a in functions/uac2.usb0/*; do echo $a:; cat $a; done
functions/uac2.usb0/c_chmask:
3
functions/uac2.usb0/c_srate:
64000
functions/uac2.usb0/c_ssize:
2
functions/uac2.usb0/p_chmask:
3
functions/uac2.usb0/p_srate:
48000
functions/uac2.usb0/p_ssize:
2

see here for some "documentation".

as a result, aplay -L gives:

default:CARD=UAC2Gadget
    UAC2_Gadget, UAC2 PCM
    Default Audio Device
sysdefault:CARD=UAC2Gadget
    UAC2_Gadget, UAC2 PCM
    Default Audio Device
dmix:CARD=UAC2Gadget,DEV=0
    UAC2_Gadget, UAC2 PCM
    Direct sample mixing device
dsnoop:CARD=UAC2Gadget,DEV=0
    UAC2_Gadget, UAC2 PCM
    Direct sample snooping device
hw:CARD=UAC2Gadget,DEV=0
    UAC2_Gadget, UAC2 PCM
    Direct hardware device without any conversions
plughw:CARD=UAC2Gadget,DEV=0
    UAC2_Gadget, UAC2 PCM
    Hardware device with all software conversions

but if I try to play a sound I get:

$ aplay -D hw:CARD=UAC2Gadget,DEV=0 /root/Bela/examples/terminal-only/samples/sample.wav
Playing WAVE '/root/Bela/examples/terminal-only/samples/sample.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono
aplay: set_params:1305: Channels count non available

Also, it does NOT show up on the host (tried Mac and Linux) as an audio device.

If I use uac1 instead, then the default parameters are:

root@bela:/sys/kernel/config/usb_gadget/g# for a in functions/uac1.usb0/*; do echo $a:; cat $a; done
functions/uac1.usb0/audio_buf_size:
48000
functions/uac1.usb0/fn_cap:
/dev/snd/pcmC0D0c
functions/uac1.usb0/fn_cntl:
/dev/snd/controlC0
functions/uac1.usb0/fn_play:
/dev/snd/pcmC0D0p
functions/uac1.usb0/req_buf_size:
200
functions/uac1.usb0/req_count:
256

then if I mknod the device files appropriately (see here):

mknod /dev/snd/pcmC0D0c c 116 24
mknod /dev/snd/pcmC0D0p c 116 16
mknod /dev/snd/controlC0 c 116 0

then I get this when running;

root@bela:/sys/kernel/config/usb_gadget/g# ls /sys/class/udc/ > UDC
ls: write error: No such device

which in turn dmesges this:

[  214.519683] usb0: HOST MAC b6:e8:3f:ac:d9:86
[  214.519819] usb0: MAC 16:09:d3:c0:73:89
[  214.527247] configfs-gadget gadget: unable to open sound control device file: /dev/snd/controlC0
[  214.536150] configfs-gadget gadget: we need at least one control device
[  214.542844] configfs-gadget gadget: unbind function 'rndis'/dc7c4cc0
[  214.549258] configfs-gadget gadget: unbind function 'Mass Storage Function'/dafb4880
[  214.557072] configfs-gadget gadget: unbind function 'acm'/da867c00
[  214.563307] configfs-gadget gadget: unbind function 'gmidi function'/daada400
[  214.632260] configfs-gadget musb-hdrc.0.auto: failed to start g: -19

which is funny, because /dev/snd/controlC0 is actually there.
The above causes the unload of all the gadgets :(

Hi Guilio, did you ever figure this out? I'm trying to configure g_audio on a raspberry pi (aghast!). I have it playing stereo audio into my mac fine, but don't seem to be able to configure it for 4 channels. I can't quite get my head around the configfs stuff.

sorry didn't see this message before. Just found the email notification in the spam. No I never figured it out (I didn't spend any more time on it); I pointed a couple of people at this to give it a try and never heard back. So maybe it's not that straightforward? Do you have a link of your working config for the Pi?

probably a kernel version issue, plus I should have used plughw: instead of hw:, or at least specified the --channel count.

On a 4.14 kernel, this works :
In /opt/Bela/bela_gadget.sh (https://github.com/BelaPlatform/bela-image-builder/blob/master/misc/rootfs/opt/Bela/bela_gadget.sh):

cd /sys/kernel/config/usb_gadget/
mkdir -p g && cd g
mkdir -p functions/uac2.usb0            # Audio
echo 44100 > functions/uac2.usb0/c_srate
echo 44100 > functions/uac2.usb0/p_srate
...
ln -s functions/uac2.usb0 configs/c.1/

[You can also set the number of channels by setting c_chmask and p_chmask. This is a mask, so you should set it to ((2^(nCh)) - 1) (e.g.: 3 means 2 channels, 63 means 6 channels)]

then reboot and ...

playback:

aplay -D plughw:CARD=UAC2Gadget,DEV=0 /root/Bela/examples/terminal-only/samples/sample.wav --channels 2

(it would probably work without plug if the audio file was stereo)

record:

arecord -D hw:CARD=UAC2Gadget,DEV=0  --rate 48000 --format S16_LE

loopback:

BUFFER=128; PERIOD=128
arecord -D hw:CARD=UAC2Gadget,DEV=0 --disable-resample --disable-format --disable-softvol --format S16_LE --rate 44100 --channels 2  --buffer-size $BUFFER --period-size $PERIOD | aplay -D hw:CARD=UAC2Gadget,DEV=0 --disable-resample --disable-format --disable-softvol --format S16_LE --rate 44100 --channels 2 --buffer-size $BUFFER --period-size $PERIOD

These are the relevant endpoints created by this operation:

        Interface #10 - Audio/Streaming ..............................................   "Playback Inactive"
            Alternate Setting   0
            Number of Endpoints   0
            Interface Class:   1   (Audio)
            Interface Subclass;   2   (Streaming)
            Interface Protocol:   32
        Interface #10 - Audio/Streaming (#1) ..............................................   "Playback Active"
            Alternate Setting   1
            Number of Endpoints   1
            Interface Class:   1   (Audio)
            Interface Subclass;   2   (Streaming)
            Interface Protocol:   32
            Audio Control Class Specific Header   
                Audio Stream General   
                    Endpoint Terminal ID:   1
                    Format Type:   1
                    Formats   PCM 
                    Number of Channels   2
                    Channel Configuration   3
                    Dump Contents (hex):   10 24 01 01 00 01 01 00 00 00 02 03 00 00 00 00 
            Audio Class Specific Audio Data Format   
                Audio Stream Format Type Desc.   
                    Format Type:   1
                    Slot Size:   2
                    Bit Resolution:   16
                    Dump Contents (hex):   06 24 02 01 02 10 
            Endpoint 0x06 - Isochronous Output   
                Address:   0x06  (OUT)
                Attributes:   0x05  (Isochronous asynchronous data endpoint)
                Max Packet Size:   0x00b4  (180 x 1  transactions opportunities per microframe)
                Polling Interval:   4 (8 microframes (1 msecs) )
            Class-Specific AS Audio EndPoint   
                Attributes:   0x00  
                bmControls:   0x00
                bLockDelayUnits:   0x00  (UNDEFINED)
                wLockDelay:   0 
        Interface #11 - Audio/Streaming ..............................................   "Capture Inactive"
            Alternate Setting   0
            Number of Endpoints   0
            Interface Class:   1   (Audio)
            Interface Subclass;   2   (Streaming)
            Interface Protocol:   32
        Interface #11 - Audio/Streaming (#1) ..............................................   "Capture Active"
            Alternate Setting   1
            Number of Endpoints   1
            Interface Class:   1   (Audio)
            Interface Subclass;   2   (Streaming)
            Interface Protocol:   32
            Audio Control Class Specific Header   
                Audio Stream General   
                    Endpoint Terminal ID:   4
                    Format Type:   1
                    Formats   PCM 
                    Number of Channels   2
                    Channel Configuration   3
                    Dump Contents (hex):   10 24 01 04 00 01 01 00 00 00 02 03 00 00 00 00 
            Audio Class Specific Audio Data Format   
                Audio Stream Format Type Desc.   
                    Format Type:   1
                    Slot Size:   2
                    Bit Resolution:   16
                    Dump Contents (hex):   06 24 02 01 02 10 
            Endpoint 0x89 - Isochronous Input   
                Address:   0x89  (IN)
                Attributes:   0x05  (Isochronous asynchronous data endpoint)
                Max Packet Size:   0x00b4  (180 x 1  transactions opportunities per microframe)
                Polling Interval:   4 (8 microframes (1 msecs) )
            Class-Specific AS Audio EndPoint   
                Attributes:   0x00  
                bmControls:   0x00
                bLockDelayUnits:   0x00  (UNDEFINED)
                wLockDelay:   0 
    Device Qualifier Descriptor   
        Descriptor Version Number:   0x0200
        Device Class   0   (Composite)
        Device Subclass   0
        Device Protocol   0
        Device MaxPacketSize:   64
        Number of Configurations:   1
        bReserved:   0