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

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