4

I know that there are already some questions which are similar to mine but they are old and doesn't helped me. I'm very new with libusb but I already come to the point where I get the libusb_device_handle. The Problem is that I don't know what the next step is. One part of my program is to capture audio from my FM radio stick. This stick sends audio over usb and has also an interface to change the frequency and so on.

With windows the stick is recognized as audio input device and I'm able to listen to that. My program should run on android 4.2 and above but my smartphone on which I want test my app does not support audio input devices over usb therefore I want to get it working with libusb.

The libusb -v command on Ubuntu looks like this:

Bus 002 Device 007: ID 10c4:818a Cygnal Integrated Products, Inc. Silicon Labs FM Radio Reference Design
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x10c4 Cygnal Integrated Products, Inc.
  idProduct          0x818a Silicon Labs FM Radio Reference Design
  bcdDevice            1.00
  iManufacturer           1 SILICON LABORATORIES INC.
  iProduct                2 FM Radio
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength          145
    bNumInterfaces          3
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass         1 Audio
      bInterfaceSubClass      1 Control Device
      bInterfaceProtocol      0 
      iInterface              0 
      AudioControl Interface Descriptor:
        bLength                 9
        bDescriptorType        36
        bDescriptorSubtype      1 (HEADER)
        bcdADC               1.00
        wTotalLength           43
        bInCollection           1
        baInterfaceNr( 0)       1
      AudioControl Interface Descriptor:
        bLength                12
        bDescriptorType        36
        bDescriptorSubtype      2 (INPUT_TERMINAL)
        bTerminalID             1
        wTerminalType      0x0710 Radio Receiver
        bAssocTerminal          0
        bNrChannels             2
        wChannelConfig     0x0003
          Left Front (L)
          Right Front (R)
        iChannelNames           0 
        iTerminal               0 
      AudioControl Interface Descriptor:
        bLength                13
        bDescriptorType        36
        bDescriptorSubtype      6 (FEATURE_UNIT)
        bUnitID                 2
        bSourceID               1
        bControlSize            2
        bmaControls( 0)      0x01
        bmaControls( 0)      0x00
          Mute Control
        bmaControls( 1)      0x00
        bmaControls( 1)      0x00
        bmaControls( 2)      0x00
        bmaControls( 2)      0x00
        iFeature                0 
      AudioControl Interface Descriptor:
        bLength                 9
        bDescriptorType        36
        bDescriptorSubtype      3 (OUTPUT_TERMINAL)
        bTerminalID             3
        wTerminalType      0x0101 USB Streaming
        bAssocTerminal          0
        bSourceID               2
        iTerminal               0 
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass         1 Audio
      bInterfaceSubClass      2 Streaming
      bInterfaceProtocol      0 
      iInterface              0 
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       1
      bNumEndpoints           1
      bInterfaceClass         1 Audio
      bInterfaceSubClass      2 Streaming
      bInterfaceProtocol      0 
      iInterface              0 
      AudioStreaming Interface Descriptor:
        bLength                 7
        bDescriptorType        36
        bDescriptorSubtype      1 (AS_GENERAL)
        bTerminalLink           3
        bDelay                  0 frames
        wFormatTag              1 PCM
      AudioStreaming Interface Descriptor:
        bLength                11
        bDescriptorType        36
        bDescriptorSubtype      2 (FORMAT_TYPE)
        bFormatType             1 (FORMAT_TYPE_I)
        bNrChannels             2
        bSubframeSize           2
        bBitResolution         16
        bSamFreqType            1 Discrete
        tSamFreq[ 0]        96000
      Endpoint Descriptor:
        bLength                 9
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            5
          Transfer Type            Isochronous
          Synch Type               Asynchronous
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               1
        bRefresh                0
        bSynchAddress           0
        AudioControl Endpoint Descriptor:
          bLength                 7
          bDescriptorType        37
          bDescriptorSubtype      1 (EP_GENERAL)
          bmAttributes         0x00
          bLockDelayUnits         2 Decoded PCM samples
          wLockDelay              0 Decoded PCM samples
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        2
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 No Subclass
      bInterfaceProtocol      0 None
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength     203
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval              10
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
Device Status:     0x0000
  (Bus Powered)

If it helps the source code of that radio stick is open source and can be downloaded.

Please can somebody tell me what I have to do after libusb_open2 to capture the audio and write it to an file

Edited 28.08.2014:
So, I'm now able to get data from my audio stick but I think there is something wrong. I'm allocation 10 libusb_transfer objects with 10 iso packets for each transfer. The callback function will be called and I checked the actual_length from the libusb_iso_packet_descriptor object. The packet size is 512 but the actual_length is between 380 and 382. To check if the data is correct I'm also stopping the time between the first and the last callback. If I stop the transfer the sum of libusb_iso_packet_descriptor->length is too big and the sum of libusb_iso_packet_descriptor->actual_length is to low.

Thanks Thomas

truthz03
  • 41
  • 3
  • Since this is a USBAudio device I believe you should be able to use the audio APIs to open the audio stream - from there you can pack your samples in to a file, I haven't worked with Android audio, but have on Windows and this is how I did it. – Preston Aug 23 '14 at 14:12
  • Thanks for your answer. The problem is that android doesn't offer an audio API. The included APIs can only access the default speaker and microphone. – truthz03 Aug 24 '14 at 16:50
  • Oooh - didn't realize that, I assumed it was supported. At this point you should just reference the [USB Audio Spec](http://www.usb.org/developers/devclass_docs/audio10.pdf). Good luck. – Preston Aug 25 '14 at 02:08
  • I think it's okay if the data actual_length is lower than you requested. It may be because the packet size differs from the iso packet size: compare [libusb_get_max_packet_size](http://libusb.sourceforge.net/api-1.0/group__dev.html#gac81968047e262409e78f3fe24321b604) to [libusb_get_max_iso_packet_size](http://libusb.sourceforge.net/api-1.0/group__dev.html#gaec10b71c7209760db55ee0f8768bb4f0) in the libusb-1.0 API doc. Please try just pulling the number of bytes given by `actual_length` from the data buffer. – JellicleCat Sep 29 '16 at 14:43

0 Answers0