home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2841 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  12.1 KB  |  426 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import __builtin__
  5. __all__ = [
  6.     'open',
  7.     'openfp',
  8.     'Error']
  9.  
  10. class Error(Exception):
  11.     pass
  12.  
  13. WAVE_FORMAT_PCM = 1
  14. _array_fmts = (None, 'b', 'h', None, 'l')
  15. import struct
  16. if struct.pack('h', 1) == '\x00\x01':
  17.     big_endian = 1
  18. else:
  19.     big_endian = 0
  20. from chunk import Chunk
  21.  
  22. class Wave_read:
  23.     
  24.     def initfp(self, file):
  25.         self._convert = None
  26.         self._soundpos = 0
  27.         self._file = Chunk(file, bigendian = 0)
  28.         if self._file.getname() != 'RIFF':
  29.             raise Error, 'file does not start with RIFF id'
  30.         self._file.getname() != 'RIFF'
  31.         if self._file.read(4) != 'WAVE':
  32.             raise Error, 'not a WAVE file'
  33.         self._file.read(4) != 'WAVE'
  34.         self._fmt_chunk_read = 0
  35.         self._data_chunk = None
  36.         while None:
  37.             self._data_seek_needed = 1
  38.             
  39.             try:
  40.                 chunk = Chunk(self._file, bigendian = 0)
  41.             except EOFError:
  42.                 break
  43.  
  44.             chunkname = chunk.getname()
  45.             if chunkname == 'fmt ':
  46.                 self._read_fmt_chunk(chunk)
  47.                 self._fmt_chunk_read = 1
  48.             elif chunkname == 'data':
  49.                 if not self._fmt_chunk_read:
  50.                     raise Error, 'data chunk before fmt chunk'
  51.                 self._fmt_chunk_read
  52.                 self._data_chunk = chunk
  53.                 self._nframes = chunk.chunksize // self._framesize
  54.                 self._data_seek_needed = 0
  55.                 break
  56.             
  57.             continue
  58.             if not (self._fmt_chunk_read) or not (self._data_chunk):
  59.                 raise Error, 'fmt chunk and/or data chunk missing'
  60.             not (self._data_chunk)
  61.             return None
  62.  
  63.     
  64.     def __init__(self, f):
  65.         self._i_opened_the_file = None
  66.         if isinstance(f, basestring):
  67.             f = __builtin__.open(f, 'rb')
  68.             self._i_opened_the_file = f
  69.         
  70.         
  71.         try:
  72.             self.initfp(f)
  73.         except:
  74.             if self._i_opened_the_file:
  75.                 f.close()
  76.             
  77.             raise 
  78.  
  79.  
  80.     
  81.     def __del__(self):
  82.         self.close()
  83.  
  84.     
  85.     def getfp(self):
  86.         return self._file
  87.  
  88.     
  89.     def rewind(self):
  90.         self._data_seek_needed = 1
  91.         self._soundpos = 0
  92.  
  93.     
  94.     def close(self):
  95.         if self._i_opened_the_file:
  96.             self._i_opened_the_file.close()
  97.             self._i_opened_the_file = None
  98.         
  99.         self._file = None
  100.  
  101.     
  102.     def tell(self):
  103.         return self._soundpos
  104.  
  105.     
  106.     def getnchannels(self):
  107.         return self._nchannels
  108.  
  109.     
  110.     def getnframes(self):
  111.         return self._nframes
  112.  
  113.     
  114.     def getsampwidth(self):
  115.         return self._sampwidth
  116.  
  117.     
  118.     def getframerate(self):
  119.         return self._framerate
  120.  
  121.     
  122.     def getcomptype(self):
  123.         return self._comptype
  124.  
  125.     
  126.     def getcompname(self):
  127.         return self._compname
  128.  
  129.     
  130.     def getparams(self):
  131.         return (self.getnchannels(), self.getsampwidth(), self.getframerate(), self.getnframes(), self.getcomptype(), self.getcompname())
  132.  
  133.     
  134.     def getmarkers(self):
  135.         pass
  136.  
  137.     
  138.     def getmark(self, id):
  139.         raise Error, 'no marks'
  140.  
  141.     
  142.     def setpos(self, pos):
  143.         if pos < 0 or pos > self._nframes:
  144.             raise Error, 'position not in range'
  145.         pos > self._nframes
  146.         self._soundpos = pos
  147.         self._data_seek_needed = 1
  148.  
  149.     
  150.     def readframes(self, nframes):
  151.         if self._data_seek_needed:
  152.             self._data_chunk.seek(0, 0)
  153.             pos = self._soundpos * self._framesize
  154.             if pos:
  155.                 self._data_chunk.seek(pos, 0)
  156.             
  157.             self._data_seek_needed = 0
  158.         
  159.         if nframes == 0:
  160.             return ''
  161.         if self._convert and data:
  162.             data = self._convert(data)
  163.         
  164.         self._soundpos = self._soundpos + len(data) // self._nchannels * self._sampwidth
  165.         return data
  166.  
  167.     
  168.     def _read_fmt_chunk(self, chunk):
  169.         (wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign) = struct.unpack('<hhllh', chunk.read(14))
  170.         if wFormatTag == WAVE_FORMAT_PCM:
  171.             sampwidth = struct.unpack('<h', chunk.read(2))[0]
  172.             self._sampwidth = (sampwidth + 7) // 8
  173.         else:
  174.             raise Error, 'unknown format: %r' % (wFormatTag,)
  175.         self._framesize = (wFormatTag == WAVE_FORMAT_PCM)._nchannels * self._sampwidth
  176.         self._comptype = 'NONE'
  177.         self._compname = 'not compressed'
  178.  
  179.  
  180.  
  181. class Wave_write:
  182.     
  183.     def __init__(self, f):
  184.         self._i_opened_the_file = None
  185.         if isinstance(f, basestring):
  186.             f = __builtin__.open(f, 'wb')
  187.             self._i_opened_the_file = f
  188.         
  189.         
  190.         try:
  191.             self.initfp(f)
  192.         except:
  193.             if self._i_opened_the_file:
  194.                 f.close()
  195.             
  196.             raise 
  197.  
  198.  
  199.     
  200.     def initfp(self, file):
  201.         self._file = file
  202.         self._convert = None
  203.         self._nchannels = 0
  204.         self._sampwidth = 0
  205.         self._framerate = 0
  206.         self._nframes = 0
  207.         self._nframeswritten = 0
  208.         self._datawritten = 0
  209.         self._datalength = 0
  210.  
  211.     
  212.     def __del__(self):
  213.         self.close()
  214.  
  215.     
  216.     def setnchannels(self, nchannels):
  217.         if self._datawritten:
  218.             raise Error, 'cannot change parameters after starting to write'
  219.         self._datawritten
  220.         if nchannels < 1:
  221.             raise Error, 'bad # of channels'
  222.         nchannels < 1
  223.         self._nchannels = nchannels
  224.  
  225.     
  226.     def getnchannels(self):
  227.         if not self._nchannels:
  228.             raise Error, 'number of channels not set'
  229.         self._nchannels
  230.         return self._nchannels
  231.  
  232.     
  233.     def setsampwidth(self, sampwidth):
  234.         if self._datawritten:
  235.             raise Error, 'cannot change parameters after starting to write'
  236.         self._datawritten
  237.         if sampwidth < 1 or sampwidth > 4:
  238.             raise Error, 'bad sample width'
  239.         sampwidth > 4
  240.         self._sampwidth = sampwidth
  241.  
  242.     
  243.     def getsampwidth(self):
  244.         if not self._sampwidth:
  245.             raise Error, 'sample width not set'
  246.         self._sampwidth
  247.         return self._sampwidth
  248.  
  249.     
  250.     def setframerate(self, framerate):
  251.         if self._datawritten:
  252.             raise Error, 'cannot change parameters after starting to write'
  253.         self._datawritten
  254.         if framerate <= 0:
  255.             raise Error, 'bad frame rate'
  256.         framerate <= 0
  257.         self._framerate = framerate
  258.  
  259.     
  260.     def getframerate(self):
  261.         if not self._framerate:
  262.             raise Error, 'frame rate not set'
  263.         self._framerate
  264.         return self._framerate
  265.  
  266.     
  267.     def setnframes(self, nframes):
  268.         if self._datawritten:
  269.             raise Error, 'cannot change parameters after starting to write'
  270.         self._datawritten
  271.         self._nframes = nframes
  272.  
  273.     
  274.     def getnframes(self):
  275.         return self._nframeswritten
  276.  
  277.     
  278.     def setcomptype(self, comptype, compname):
  279.         if self._datawritten:
  280.             raise Error, 'cannot change parameters after starting to write'
  281.         self._datawritten
  282.         if comptype not in ('NONE',):
  283.             raise Error, 'unsupported compression type'
  284.         comptype not in ('NONE',)
  285.         self._comptype = comptype
  286.         self._compname = compname
  287.  
  288.     
  289.     def getcomptype(self):
  290.         return self._comptype
  291.  
  292.     
  293.     def getcompname(self):
  294.         return self._compname
  295.  
  296.     
  297.     def setparams(self, .1):
  298.         (nchannels, sampwidth, framerate, nframes, comptype, compname) = .1
  299.         if self._datawritten:
  300.             raise Error, 'cannot change parameters after starting to write'
  301.         self._datawritten
  302.         self.setnchannels(nchannels)
  303.         self.setsampwidth(sampwidth)
  304.         self.setframerate(framerate)
  305.         self.setnframes(nframes)
  306.         self.setcomptype(comptype, compname)
  307.  
  308.     
  309.     def getparams(self):
  310.         if not (self._nchannels) and not (self._sampwidth) or not (self._framerate):
  311.             raise Error, 'not all parameters set'
  312.         not (self._framerate)
  313.         return (self._nchannels, self._sampwidth, self._framerate, self._nframes, self._comptype, self._compname)
  314.  
  315.     
  316.     def setmark(self, id, pos, name):
  317.         raise Error, 'setmark() not supported'
  318.  
  319.     
  320.     def getmark(self, id):
  321.         raise Error, 'no marks'
  322.  
  323.     
  324.     def getmarkers(self):
  325.         pass
  326.  
  327.     
  328.     def tell(self):
  329.         return self._nframeswritten
  330.  
  331.     
  332.     def writeframesraw(self, data):
  333.         self._ensure_header_written(len(data))
  334.         nframes = len(data) // self._sampwidth * self._nchannels
  335.         if self._convert:
  336.             data = self._convert(data)
  337.         
  338.         if self._sampwidth > 1 and big_endian:
  339.             import array
  340.             data = array.array(_array_fmts[self._sampwidth], data)
  341.             data.byteswap()
  342.             data.tofile(self._file)
  343.             self._datawritten = self._datawritten + len(data) * self._sampwidth
  344.         else:
  345.             self._file.write(data)
  346.             self._datawritten = self._datawritten + len(data)
  347.         self._nframeswritten = self._nframeswritten + nframes
  348.  
  349.     
  350.     def writeframes(self, data):
  351.         self.writeframesraw(data)
  352.         if self._datalength != self._datawritten:
  353.             self._patchheader()
  354.         
  355.  
  356.     
  357.     def close(self):
  358.         if self._file:
  359.             self._ensure_header_written(0)
  360.             if self._datalength != self._datawritten:
  361.                 self._patchheader()
  362.             
  363.             self._file.flush()
  364.             self._file = None
  365.         
  366.         if self._i_opened_the_file:
  367.             self._i_opened_the_file.close()
  368.             self._i_opened_the_file = None
  369.         
  370.  
  371.     
  372.     def _ensure_header_written(self, datasize):
  373.         if not self._datawritten:
  374.             if not self._nchannels:
  375.                 raise Error, '# channels not specified'
  376.             self._nchannels
  377.             if not self._sampwidth:
  378.                 raise Error, 'sample width not specified'
  379.             self._sampwidth
  380.             if not self._framerate:
  381.                 raise Error, 'sampling rate not specified'
  382.             self._framerate
  383.             self._write_header(datasize)
  384.         
  385.  
  386.     
  387.     def _write_header(self, initlength):
  388.         self._file.write('RIFF')
  389.         if not self._nframes:
  390.             self._nframes = initlength / (self._nchannels * self._sampwidth)
  391.         
  392.         self._datalength = self._nframes * self._nchannels * self._sampwidth
  393.         self._form_length_pos = self._file.tell()
  394.         self._file.write(struct.pack('<l4s4slhhllhh4s', 36 + self._datalength, 'WAVE', 'fmt ', 16, WAVE_FORMAT_PCM, self._nchannels, self._framerate, self._nchannels * self._framerate * self._sampwidth, self._nchannels * self._sampwidth, self._sampwidth * 8, 'data'))
  395.         self._data_length_pos = self._file.tell()
  396.         self._file.write(struct.pack('<l', self._datalength))
  397.  
  398.     
  399.     def _patchheader(self):
  400.         if self._datawritten == self._datalength:
  401.             return None
  402.         curpos = self._file.tell()
  403.         self._file.seek(self._form_length_pos, 0)
  404.         self._file.write(struct.pack('<l', 36 + self._datawritten))
  405.         self._file.seek(self._data_length_pos, 0)
  406.         self._file.write(struct.pack('<l', self._datawritten))
  407.         self._file.seek(curpos, 0)
  408.         self._datalength = self._datawritten
  409.  
  410.  
  411.  
  412. def open(f, mode = None):
  413.     if mode is None:
  414.         if hasattr(f, 'mode'):
  415.             mode = f.mode
  416.         else:
  417.             mode = 'rb'
  418.     
  419.     if mode in ('r', 'rb'):
  420.         return Wave_read(f)
  421.     if mode in ('w', 'wb'):
  422.         return Wave_write(f)
  423.     raise Error, "mode must be 'r', 'rb', 'w', or 'wb'"
  424.  
  425. openfp = open
  426.