SkinBlues has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have an IOUSBHostDevice I want to access and read data from. I use the command `ioreg` to show the properties of the device. I want to read a data stream from the USB device in Perl. The device is an audio interface plugged in by USB. The data i want to read is a sound wave. I am new to Perl and looking for some advice.
  • Comment on What can I do with an “IOUSBHostDevice”?

Replies are listed 'Best First'.
Re: What can I do with an “IOUSBHostDevice”?
by Corion (Patriarch) on Nov 21, 2019 at 08:13 UTC

    Mostly, this will depend on the USB protocol and whatever library you're using to read the data.

    If you're using USB::LibUSB to read from the device, you will have to reassemble the USB packets into the audio stream. You want to have read USB in a NutShell.

    I found many Stackoverflow questions without replies, but https://stackoverflow.com/questions/47229082|USB Control Transfer to get AudioStreaming Descriptor with libusb] has some code that suggests that it might actually work.

    Personally, I think you'll be better off using the audio capabilities of your OS to read the (decoded) audio stream, but depending on the setup, it may be desireable to do it all yourself instead of fighting Pulse Audio and/or ALSA.

    In the opposite direction, there is libmaru, which is an userspace driver to output audio to an USB device.

      It seems like USB::LibUSB requires an installation to use. Is there a way to do it without installing anything? I found the properties of an AppleUSBAudioStream. Would it be possible to directly access the port where the data is stored? --here is the beginning of the IOUSBHostDevice, found using the terminal command `ioreg -r -c IOUSBHostDevice -l`
      +-o Scarlett Solo USB@14200000 <class IOUSBHostDevice, id 0x100002be5 +, registered, matched, active, busy 0 (3820 ms), retain 28> | { | "sessionID" = 107384098679337 | "USBSpeed" = 3 | "IOServiceLegacyMatchingRegistryID" = 4294978535 | "idProduct" = 33285 | "bDeviceClass" = 239 | "IOPowerManagement" = {"PowerOverrideOn"=Yes,"CapabilityFlags"=3 +2768,"MaxPowerState"=2,"DevicePowerState"=2,"ChildrenPowerState"=2,"D +riverPowerState"=0,"CurrentPowerState"=2} | "bcdDevice" = 1052 | "USB Product Name" = "Scarlett Solo USB" | "AppleUSBAlternateServiceRegistryID" = 4294978535 | "locationID" = 337641472 | "bDeviceSubClass" = 2 | "kUSBCurrentConfiguration" = 1 | "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOU +SBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} | "bDeviceProtocol" = 1 | "USBPortType" = 0 | "USB Vendor Name" = "Focusrite" | "idVendor" = 4661 | "IOGeneralInterest" = "IOCommand is not serializable" | "kUSBVendorString" = "Focusrite" | "IOClassNameOverride" = "IOUSBDevice" | }
      -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------- IOUSBHostInterface
      +-o Scarlett Solo USB@0 <class IOUSBHostInterface, id 0x100002bf5, re +gistered, matched, active, busy 0 (3813 ms), retain 8> | { | "USBPortType" = 0 | "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOU +SBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} | "bcdDevice" = 1052 | "USBSpeed" = 3 | "idProduct" = 33285 | "bConfigurationValue" = 1 | "bInterfaceSubClass" = 1 | "locationID" = 337641472 | "IOGeneralInterest" = "IOCommand is not serializable" | "IOServiceLegacyMatchingRegistryID" = 4294978553 | "IOClassNameOverride" = "IOUSBInterface" | "AppleUSBAlternateServiceRegistryID" = 4294978553 | "idVendor" = 4661 | "bInterfaceProtocol" = 32 | "bAlternateSetting" = 0 | "bInterfaceNumber" = 0 | "bInterfaceClass" = 1 | }
      -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------- AppleUSBAudioDevice
      +-o AppleUSBAudioDevice <class AppleUSBAudioDevice, id 0x100002c0f, r +egistered, matched, active, busy 0 (1 ms), retain 14> | { | "IOClass" = "AppleUSBAudioDevice" | "CFBundleIdentifier" = "com.apple.driver.AppleUSBAudio" | "IOProviderClass" = "IOUSBHostInterface" | "IOAudioDeviceName" = "Scarlett Solo USB" | "IOPowerManagement" = {"CapabilityFlags"=32768,"CurrentPowerStat +e"=1,"MaxPowerState"=1,"DriverPowerState"=1} | "IOAudioDeviceModelID" = "Scarlett Solo USB:1235:8205" | "IOProbeScore" = 50000 | "bInterfaceSubClass" = 1 | "IOMatchCategory" = "IODefaultMatchCategory" | "IOAudioDeviceManufacturerName" = "Focusrite" | "IOAudioDeviceCanBeDefaults" = 7 | "Clock Domain" = 0 | "bInterfaceClass" = 1 | "IOAudioDeviceTransportType" = 1970496032 | }
      -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------- AppleUSBAudioEngine
      +-o AppleUSBAudioEngine <class AppleUSBAudioEngine, id 0x100002c11, r +egistered, matched, active, busy 0 (1 ms), retain 15> | { | "IOAudioStreamSampleFormatByteOrder" = "Little Endian" | "IOAudioEngineClockIsStable" = Yes | "IOAudioEngineInputSampleOffset" = 241 | "idProduct" = 33285 | "IOGeneralInterest" = "IOCommand is not serializable" | "IOAudioEngineInputSampleLatency" = 192 | "IOAudioEngineState" = 0 | "IOAudioEngineDescription" = "Scarlett Solo USB" | "IOAudioEngineNumSampleFramesPerBuffer" = 49152 | "IOAudioEngineGlobalUniqueID" = "AppleUSBAudioEngine:Focusrite:S +carlett Solo USB:14200000:2,1" | "idVendor" = 4661 | "IOAudioEngineClockDomain" = 272910464 | "IOAudioSampleRate" = {"IOAudioSampleRateWholeNumber"=192000,"IO +AudioSampleRateFraction"=0} | "IOAudioEngineFlavor" = 1 | "IOAudioEngineChannelNames" = {"OutputChannel2"="Output 2","Outp +utChannel1"="Output 1","InputChannel1"="Input 1","InputChannel2"="Inp +ut 2"} | "IOAudioEngineSampleOffset" = 193 | "IOAudioEngineOutputSampleLatency" = 192 | }
      -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------- The full representation of AppleUSBAudioStream. There are 4 AppleUSBAudioStreams available.
      +-o AppleUSBAudioStream <class AppleUSBAudioStream, id 0x100002c12, r +egistered, matched, active, busy 0 (0 ms), retain 9> | { | "IOAudioStreamNumClients" = 0 | "IOAudioStreamSampleFormatByteOrder" = "Little Endian" | "IOAudioStreamStartingChannelNumber" = 1 | "idProduct" = 33285 | "IOAudioStreamAvailable" = 1 | "IOAudioStreamFormat" = {"IOAudioStreamAlignment"=0,"IOAudioSt +reamNumericRepresentation"=1936289396,"IOAudioStreamIsMixable"=1,"IOA +udioStreamFormatFlags"=0,"IOAudioStreamByteOrder"=1,"IOAudioStreamBit +Depth"=24,"IOAudioStreamSampleFormat"=1819304813,"IOAudioStreamDriver +Tag"=131073,"IOAudioStreamNumChannels"=2,"IOAudioStreamBytesPerPacket +"=8,"IOAudioStreamBitWidth"=32,"IOAudioStreamFramesPerPacket"=1} | "IOAudioStreamDirection" = 1 | "IOAudioStreamAvailableFormats" = ({"IOAudioStreamMaximumSampl +eRate"={"IOAudioSampleRateWholeNumber"=44100,"IOAudioSampleRateFracti +on"=0},"IOAudioStreamAlignment"=0,"IOAudioStreamNumericRepresentation +"=1936289396,"IOAudioStreamIsMixable"=1,"IOAudioStreamFormatFlags"=0, +"IOAudioStreamByteOrder"=1,"IOAudioStreamBitDepth"=24,"IOAudioStreamS +ampleFormat"=1819304813,"IOAudioStreamDriverTag"=131073,"IOAudioStrea +mNumChannels"=2,"IOAudioStreamBytesPerPacket"=8,"IOAudioStreamBitWidt +h"=32,"IOAudioStreamFramesPerPacket"=1,"IOAudioStreamMinimumSampleRat +e"={"IOAudioSampleRateWholeNumber"=44100,"IOAudioSampleRateFraction"= +0}},{"IOAudioStreamMaximumSampleRate"={"IOAudioSampleRateWholeNumber" +=44100,"IOAudioSampleRateFraction"=0},"IOAudioStreamAlignment"=0,"IOA +udioStreamNumericRepresentation"=1936289396,"IOAudioStreamIsMixable"= +0,"IOAudioStreamFormatFlags"=0,"IOAudioStreamByteOrder"=1,"IOAudioStr +eamBitDepth"=24,"IOAudioStreamSampleFormat"=1819304813,"IOAudioStream +DriverTag"=131073,"IOAudioStreamNumChannels"=2,"IOAudioStreamBytesPer +Packet"=8,"IOAudioStreamBitWidth"=32,"IOAudioStreamFramesPerPacket"=1 +,"IOAudioStreamMinimumSampleRate"={"IOAudioSampleRateWholeNumber"=441 +00,"IOAudioSampleRateFraction"=0}},{"IOAudioStreamMaximumSampleRate"= +{"IOAudioSampleRateWholeNumber"=48000,"IOAudioSampleRateFraction"=0}, +"IOAudioStreamAlignment"=0,"IOAudioStreamNumericRepresentation"=19362 +89396,"IOAudioStreamIsMixable"=1,"IOAudioStreamFormatFlags"=0,"IOAudi +oStreamByteOrder"=1,"IOAudioStreamBitDepth"=24,"IOAudioStreamSampleFo +rmat"=1819304813,"IOAudioStreamDriverTag"=131073,"IOAudioStreamNumCha +nnels"=2,"IOAudioStreamBytesPerPacket"=8,"IOAudioStreamBitWidth"=32," +IOAudioStreamFramesPerPacket"=1,"IOAudioStreamMinimumSampleRate"={"IO +AudioSampleRateWholeNumber"=48000,"IOAudioSampleRateFraction"=0}},{"I +OAudioStreamMaximumSampleRate"={"IOAudioSampleRateWholeNumber"=48000, +"IOAudioSampleRateFraction"=0},"IOAudioStreamAlignment"=0,"IOAudioStr +eamNumericRepresentation"=1936289396,"IOAudioStreamIsMixable"=0,"IOAu +dioStreamFormatFlags"=0,"IOAudioStreamByteOrder"=1,"IOAudioStreamBitD +epth"=24,"IOAudioStreamSampleFormat"=1819304813,"IOAudioStreamDriverT +ag"=131073,"IOAudioStreamNumChannels"=2,"IOAudioStreamBytesPerPacket" +=8,"IOAudioStreamBitWidth"=32,"IOAudioStreamFramesPerPacket"=1,"IOAud +ioStreamMinimumSampleRate"={"IOAudioSampleRateWholeNumber"=48000,"IOA +udioSampleRateFraction"=0}},{"IOAudioStreamMaximumSampleRate"={"IOAud +ioSampleRateWholeNumber"=88200,"IOAudioSampleRateFraction"=0},"IOAudi +oStreamAlignment"=0,"IOAudioStreamNumericRepresentation"=1936289396," +IOAudioStreamIsMixable"=1,"IOAudioStreamFormatFlags"=0,"IOAudioStream +ByteOrder"=1,"IOAudioStreamBitDepth"=24,"IOAudioStreamSampleFormat"=1 +819304813,"IOAudioStreamDriverTag"=131073,"IOAudioStreamNumChannels"= +2,"IOAudioStreamBytesPerPacket"=8,"IOAudioStreamBitWidth"=32,"IOAudio +StreamFramesPerPacket"=1,"IOAudioStreamMinimumSampleRate"={"IOAudioSa +mpleRateWholeNumber"=88200,"IOAudioSampleRateFraction"=0}},{"IOAudioS +treamMaximumSampleRate"={"IOAudioSampleRateWholeNumber"=88200,"IOAudi +oSampleRateFraction"=0},"IOAudioStreamAlignment"=0,"IOAudioStreamNume +ricRepresentation"=1936289396,"IOAudioStreamIsMixable"=0,"IOAudioStre +amFormatFlags"=0,"IOAudioStreamByteOrder"=1,"IOAudioStreamBitDepth"=2 +4,"IOAudioStreamSampleFormat"=1819304813,"IOAudioStreamDriverTag"=131 +073,"IOAudioStreamNumChannels"=2,"IOAudioStreamBytesPerPacket"=8,"IOA +udioStreamBitWidth"=32,"IOAudioStreamFramesPerPacket"=1,"IOAudioStrea +mMinimumSampleRate"={"IOAudioSampleRateWholeNumber"=88200,"IOAudioSam +pleRateFraction"=0}},{"IOAudioStreamMaximumSampleRate"={"IOAudioSampl +eRateWholeNumber"=96000,"IOAudioSampleRateFraction"=0},"IOAudioStream +Alignment"=0,"IOAudioStreamNumericRepresentation"=1936289396,"IOAudio +StreamIsMixable"=1,"IOAudioStreamFormatFlags"=0,"IOAudioStreamByteOrd +er"=1,"IOAudioStreamBitDepth"=24,"IOAudioStreamSampleFormat"=18193048 +13,"IOAudioStreamDriverTag"=131073,"IOAudioStreamNumChannels"=2,"IOAu +dioStreamBytesPerPacket"=8,"IOAudioStreamBitWidth"=32,"IOAudioStreamF +ramesPerPacket"=1,"IOAudioStreamMinimumSampleRate"={"IOAudioSampleRat +eWholeNumber"=96000,"IOAudioSampleRateFraction"=0}},{"IOAudioStreamMa +ximumSampleRate"={"IOAudioSampleRateWholeNumber"=96000,"IOAudioSample +RateFraction"=0},"IOAudioStreamAlignment"=0,"IOAudioStreamNumericRepr +esentation"=1936289396,"IOAudioStreamIsMixable"=0,"IOAudioStreamForma +tFlags"=0,"IOAudioStreamByteOrder"=1,"IOAudioStreamBitDepth"=24,"IOAu +dioStreamSampleFormat"=1819304813,"IOAudioStreamDriverTag"=131073,"IO +AudioStreamNumChannels"=2,"IOAudioStreamBytesPerPacket"=8,"IOAudioStr +eamBitWidth"=32,"IOAudioStreamFramesPerPacket"=1,"IOAudioStreamMinimu +mSampleRate"={"IOAudioSampleRateWholeNumber"=96000,"IOAudioSampleRate +Fraction"=0}},{"IOAudioStreamMaximumSampleRate"={"IOAudioSampleRateWh +oleNumber"=176400,"IOAudioSampleRateFraction"=0},"IOAudioStreamAlignm +ent"=0,"IOAudioStreamNumericRepresentation"=1936289396,"IOAudioStream +IsMixable"=1,"IOAudioStreamFormatFlags"=0,"IOAudioStreamByteOrder"=1, +"IOAudioStreamBitDepth"=24,"IOAudioStreamSampleFormat"=1819304813,"IO +AudioStreamDriverTag"=131073,"IOAudioStreamNumChannels"=2,"IOAudioStr +eamBytesPerPacket"=8,"IOAudioStreamBitWidth"=32,"IOAudioStreamFramesP +erPacket"=1,"IOAudioStreamMinimumSampleRate"={"IOAudioSampleRateWhole +Number"=176400,"IOAudioSampleRateFraction"=0}},{"IOAudioStreamMaximum +SampleRate"={"IOAudioSampleRateWholeNumber"=176400,"IOAudioSampleRate +Fraction"=0},"IOAudioStreamAlignment"=0,"IOAudioStreamNumericRepresen +tation"=1936289396,"IOAudioStreamIsMixable"=0,"IOAudioStreamFormatFla +gs"=0,"IOAudioStreamByteOrder"=1,"IOAudioStreamBitDepth"=24,"IOAudioS +treamSampleFormat"=1819304813,"IOAudioStreamDriverTag"=131073,"IOAudi +oStreamNumChannels"=2,"IOAudioStreamBytesPerPacket"=8,"IOAudioStreamB +itWidth"=32,"IOAudioStreamFramesPerPacket"=1,"IOAudioStreamMinimumSam +pleRate"={"IOAudioSampleRateWholeNumber"=176400,"IOAudioSampleRateFra +ction"=0}},{"IOAudioStreamMaximumSampleRate"={"IOAudioSampleRateWhole +Number"=192000,"IOAudioSampleRateFraction"=0},"IOAudioStreamAlignment +"=0,"IOAudioStreamNumericRepresentation"=1936289396,"IOAudioStreamIsM +ixable"=1,"IOAudioStreamFormatFlags"=0,"IOAudioStreamByteOrder"=1,"IO +AudioStreamBitDepth"=24,"IOAudioStreamSampleFormat"=1819304813,"IOAud +ioStreamDriverTag"=131073,"IOAudioStreamNumChannels"=2,"IOAudioStream +BytesPerPacket"=8,"IOAudioStreamBitWidth"=32,"IOAudioStreamFramesPerP +acket"=1,"IOAudioStreamMinimumSampleRate"={"IOAudioSampleRateWholeNum +ber"=192000,"IOAudioSampleRateFraction"=0}},{"IOAudioStreamMaximumSam +pleRate"={"IOAudioSampleRateWholeNumber"=192000,"IOAudioSampleRateFra +ction"=0},"IOAudioStreamAlignment"=0,"IOAudioStreamNumericRepresentat +ion"=1936289396,"IOAudioStreamIsMixable"=0,"IOAudioStreamFormatFlags" +=0,"IOAudioStreamByteOrder"=1,"IOAudioStreamBitDepth"=24,"IOAudioStre +amSampleFormat"=1819304813,"IOAudioStreamDriverTag"=131073,"IOAudioSt +reamNumChannels"=2,"IOAudioStreamBytesPerPacket"=8,"IOAudioStreamBitW +idth"=32,"IOAudioStreamFramesPerPacket"=1,"IOAudioStreamMinimumSample +Rate"={"IOAudioSampleRateWholeNumber"=192000,"IOAudioSampleRateFracti +on"=0}}) | "IOAudioStreamID" = 0 | "IOAudioStreamStartingChannelID" = 1 | "Transfer Alignment" = 1 | "idVendor" = 4661 | "IOAudioStreamTerminalType" = 257 | "IOAudioStreamDescription" = "Scarlett Solo USB " | }