audio.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002  
00003 import usb
00004 import time
00005 import sys
00006 import errno
00007  
00008 ANDROID_VENDOR_ID_LIST = (0x0502, 0x1914, 0x1F3A, 0x1b8e, 0x16D5, 0x0E79, 0x0b05, 0x1D91, 0x04B7, 0x1219, 0x413c, 0x03fc, 0x297F, 0x2207, 0x0489, 0x04C5, 0x0F1C, 0x091E, 0x0414, 0x1E85, 0x271D, 0x18d1, 0x201E, 0x19A5, 0x109b, 0x0c2e, 0x03f0, 0x0bb4, 0x12D1, 0x2314, 0x8087, 0x067e, 0x2420, 0x24E3, 0x2116, 0x2237, 0x0482, 0x1949, 0x17EF, 0x2006, 0x1004, 0x25E3, 0x2A96, 0x22b8, 0x0DB0, 0x0e8d, 0x0409, 0x2080, 0x0955, 0x22D9, 0x2257, 0x2836, 0x10A9, 0x1D4D, 0x0471, 0x04DA, 0x1662, 0x29e4, 0x1D45, 0x05c6, 0x0408, 0x1532, 0x2207, 0x04e8, 0x04dd, 0x1F53, 0x29a9, 0x1d9c, 0x054C, 0x0FCE, 0x1BBB, 0x1d09, 0x2340, 0x0451, 0x0930, 0x1E68, 0x2A49, 0xE040, 0x0531, 0x2717, 0x2916, 0x1EBF, 0x19D2)
00009 ANDROID_AUDIO_PRODUCT_ID_LIST = (0x2D02, 0x2D03)
00010 
00011 def log(msg):
00012     sys.stderr.write(msg + '\n')
00013 
00014 def main():
00015     serial_number = None
00016     dev = None
00017 
00018     if len(sys.argv) > 1:
00019         serial_number = sys.argv[1]
00020         for _dev in usb.core.find(find_all=True, custom_match = lambda d: d.idVendor in ANDROID_VENDOR_ID_LIST):
00021             if _dev.serial_number == serial_number:
00022                 dev = _dev
00023                 break
00024     else:
00025         for _dev in usb.core.find(find_all=True, custom_match = lambda d: d.idVendor in ANDROID_VENDOR_ID_LIST):
00026             if serial_number == None:
00027                 serial_number = _dev.serial_number
00028                 dev = _dev
00029             else:
00030                 log("more than one device, please specify serial number to use")
00031                 sys.exit(1)
00032 
00033     if dev == None:
00034         log("device not found")
00035         sys.exit(1)
00036 
00037     if dev.idProduct in ANDROID_AUDIO_PRODUCT_ID_LIST:
00038         log("device is already in audio mode")
00039     else:
00040         log("set to audio mode")
00041         assert dev.ctrl_transfer( usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_OUT, 52, 0, 2, "d") == 1 #description 
00042         assert dev.ctrl_transfer( usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_OUT, 52, 0, 3, "v") == 1 #version
00043         assert dev.ctrl_transfer( usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_OUT, 52, 0, 4, "u") == 1 #url
00044         assert dev.ctrl_transfer( usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_OUT, 52, 0, 5, "s") == 1 #serialNumber   
00045         assert dev.ctrl_transfer( usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_OUT, 58, 1, 0, "") == 0  #set stereo audio
00046         assert dev.ctrl_transfer( usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_OUT, 53, 0, 0, "") == 0  #accessory_start
00047 
00048     intf = None
00049     ep_in = None
00050     start_tm = time.time()
00051 
00052     while True:
00053         try:
00054             if dev == None:
00055                 for _dev in usb.core.find(find_all=True, custom_match = lambda d: d.idVendor in ANDROID_VENDOR_ID_LIST):
00056                     if _dev.serial_number == serial_number:
00057                         dev = _dev
00058                         break
00059 
00060             if dev != None:
00061                 cfg = dev.get_active_configuration()
00062 
00063                 for intf in usb.util.find_descriptor(cfg, find_all=True, bInterfaceClass=1): #1 means audio
00064                     # log("intf: "+str(intf))
00065                     ep_in = usb.util.find_descriptor( intf, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN and (e.bmAttributes & 3) == 1) #1 means Isochronous
00066                     # log("Endpoint: "+str(ep_in))
00067                     if ep_in != None: break
00068                 if ep_in != None: break;
00069 
00070                 intf = None
00071                 dev = None
00072 
00073         except usb.core.USBError as e:
00074             pass
00075 
00076         if time.time() - start_tm >= 5:
00077             log("time out")
00078             sys.exit(1)
00079 
00080         time.sleep(0.25)
00081 
00082     intf.set_altsetting()
00083 
00084     log("OK, start read. wMaxPacketSize:"+str(ep_in.wMaxPacketSize))
00085 
00086     buf = usb.util.create_buffer(ep_in.wMaxPacketSize*16)
00087     buf_l = usb.util.create_buffer(ep_in.wMaxPacketSize*8)
00088 
00089     while True:
00090         n = 0
00091         try:
00092             n = ep_in.read(buf, 10*1000)
00093         except usb.core.USBError as e:
00094             if e.errno != errno.ETIMEDOUT:
00095                 log("read err "+ str(e))
00096                 exit(1)
00097         if n > 0:
00098             log("----read "+str(n) + " bytes----")
00099             n = n/4*4
00100             # buf.tofile(sys.stdout)
00101             i = 2
00102             j = 0
00103             while i < n:
00104                 buf_l[j] = buf[i]
00105                 buf_l[j+1] = buf[i+1]
00106                 j = j+2
00107                 i = i+4
00108             buf_l.tofile(sys.stdout)
00109  
00110     log("exiting application")
00111 
00112 try:
00113     main()
00114 except KeyboardInterrupt:
00115     sys.exit(1) # or 1, or whatever


dji_ronin
Author(s):
autogenerated on Sat Jun 8 2019 20:15:31