home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / sndhdr.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  5.6 KB  |  193 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. __all__ = [
  5.     'what',
  6.     'whathdr']
  7.  
  8. def what(filename):
  9.     res = whathdr(filename)
  10.     return res
  11.  
  12.  
  13. def whathdr(filename):
  14.     f = open(filename, 'rb')
  15.     h = f.read(512)
  16.     for tf in tests:
  17.         res = tf(h, f)
  18.         if res:
  19.             return res
  20.     
  21.  
  22. tests = []
  23.  
  24. def test_aifc(h, f):
  25.     import aifc
  26.     if h[:4] != 'FORM':
  27.         return None
  28.     if None[8:12] == 'AIFC':
  29.         fmt = 'aifc'
  30.     elif h[8:12] == 'AIFF':
  31.         fmt = 'aiff'
  32.     else:
  33.         return None
  34.     None.seek(0)
  35.     
  36.     try:
  37.         a = aifc.openfp(f, 'r')
  38.     except (EOFError, aifc.Error):
  39.         return None
  40.  
  41.     return (fmt, a.getframerate(), a.getnchannels(), a.getnframes(), 8 * a.getsampwidth())
  42.  
  43. tests.append(test_aifc)
  44.  
  45. def test_au(h, f):
  46.     if h[:4] == '.snd':
  47.         f = get_long_be
  48.     elif h[:4] in ('\x00ds.', 'dns.'):
  49.         f = get_long_le
  50.     else:
  51.         return None
  52.     type = None
  53.     hdr_size = f(h[4:8])
  54.     data_size = f(h[8:12])
  55.     encoding = f(h[12:16])
  56.     rate = f(h[16:20])
  57.     nchannels = f(h[20:24])
  58.     sample_size = 1
  59.     if encoding == 1:
  60.         sample_bits = 'U'
  61.     elif encoding == 2:
  62.         sample_bits = 8
  63.     elif encoding == 3:
  64.         sample_bits = 16
  65.         sample_size = 2
  66.     else:
  67.         sample_bits = '?'
  68.     frame_size = sample_size * nchannels
  69.     return (type, rate, nchannels, data_size // frame_size, sample_bits)
  70.  
  71. tests.append(test_au)
  72.  
  73. def test_hcom(h, f):
  74.     if h[65:69] != 'FSSD' or h[128:132] != 'HCOM':
  75.         return None
  76.     divisor = None(h[144:148])
  77.     return ('hcom', 22050 // divisor, 1, -1, 8)
  78.  
  79. tests.append(test_hcom)
  80.  
  81. def test_voc(h, f):
  82.     if h[:20] != 'Creative Voice File\x1a':
  83.         return None
  84.     sbseek = None(h[20:22])
  85.     rate = 0
  86.     if sbseek <= sbseek:
  87.         pass
  88.     elif sbseek < 500 and h[sbseek] == '\x01':
  89.         ratecode = ord(h[sbseek + 4])
  90.         rate = int(1e+06 / (256 - ratecode))
  91.     return ('voc', rate, 1, -1, 8)
  92.  
  93. tests.append(test_voc)
  94.  
  95. def test_wav(h, f):
  96.     if h[:4] != 'RIFF' and h[8:12] != 'WAVE' or h[12:16] != 'fmt ':
  97.         return None
  98.     style = None(h[20:22])
  99.     nchannels = get_short_le(h[22:24])
  100.     rate = get_long_le(h[24:28])
  101.     sample_bits = get_short_le(h[34:36])
  102.     return ('wav', rate, nchannels, -1, sample_bits)
  103.  
  104. tests.append(test_wav)
  105.  
  106. def test_8svx(h, f):
  107.     if h[:4] != 'FORM' or h[8:12] != '8SVX':
  108.         return None
  109.  
  110. tests.append(test_8svx)
  111.  
  112. def test_sndt(h, f):
  113.     if h[:5] == 'SOUND':
  114.         nsamples = get_long_le(h[8:12])
  115.         rate = get_short_le(h[20:22])
  116.         return ('sndt', rate, 1, nsamples, 8)
  117.  
  118. tests.append(test_sndt)
  119.  
  120. def test_sndr(h, f):
  121.     if h[:2] == '\x00\x00':
  122.         rate = get_short_le(h[2:4])
  123.         if rate <= rate:
  124.             pass
  125.         elif rate <= 25000:
  126.             return ('sndr', rate, 1, -1, 8)
  127.  
  128. tests.append(test_sndr)
  129.  
  130. def get_long_be(s):
  131.     return ord(s[0]) << 24 | ord(s[1]) << 16 | ord(s[2]) << 8 | ord(s[3])
  132.  
  133.  
  134. def get_long_le(s):
  135.     return ord(s[3]) << 24 | ord(s[2]) << 16 | ord(s[1]) << 8 | ord(s[0])
  136.  
  137.  
  138. def get_short_be(s):
  139.     return ord(s[0]) << 8 | ord(s[1])
  140.  
  141.  
  142. def get_short_le(s):
  143.     return ord(s[1]) << 8 | ord(s[0])
  144.  
  145.  
  146. def test():
  147.     import sys
  148.     recursive = 0
  149.     if sys.argv[1:] and sys.argv[1] == '-r':
  150.         del sys.argv[1:2]
  151.         recursive = 1
  152.     
  153.     try:
  154.         if sys.argv[1:]:
  155.             testall(sys.argv[1:], recursive, 1)
  156.         else:
  157.             testall([
  158.                 '.'], recursive, 1)
  159.     except KeyboardInterrupt:
  160.         sys.stderr.write('\n[Interrupted]\n')
  161.         sys.exit(1)
  162.  
  163.  
  164.  
  165. def testall(list, recursive, toplevel):
  166.     import sys
  167.     import os
  168.     for filename in list:
  169.         if os.path.isdir(filename):
  170.             print filename + '/:',
  171.             if recursive or toplevel:
  172.                 print 'recursing down:'
  173.                 import glob
  174.                 names = glob.glob(os.path.join(filename, '*'))
  175.                 testall(names, recursive, 0)
  176.             else:
  177.                 print '*** directory (use -r) ***'
  178.         print filename + ':',
  179.         sys.stdout.flush()
  180.         
  181.         try:
  182.             print what(filename)
  183.         continue
  184.         except IOError:
  185.             print '*** not found ***'
  186.             continue
  187.         
  188.  
  189.     
  190.  
  191. if __name__ == '__main__':
  192.     test()
  193.