Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Lib/aifc.py

    r2 r388  
    124124When all frames have been written, either call writeframes('') or
    125125close() to patch up the sizes in the header.
    126 Marks can be added anytime.  If there are any marks, ypu must call
     126Marks can be added anytime.  If there are any marks, you must call
    127127close() after all frames have been written.
    128128The close() method is called automatically when the class instance
     
    160160    try:
    161161        return struct.unpack('>h', file.read(2))[0]
     162    except struct.error:
     163        raise EOFError
     164
     165def _read_ushort(file):
     166    try:
     167        return struct.unpack('>H', file.read(2))[0]
    162168    except struct.error:
    163169        raise EOFError
     
    195201    f.write(struct.pack('>h', x))
    196202
     203def _write_ushort(f, x):
     204    f.write(struct.pack('>H', x))
     205
    197206def _write_long(f, x):
     207    f.write(struct.pack('>l', x))
     208
     209def _write_ulong(f, x):
    198210    f.write(struct.pack('>L', x))
    199211
     
    201213    if len(s) > 255:
    202214        raise ValueError("string exceeds maximum pstring length")
    203     f.write(chr(len(s)))
     215    f.write(struct.pack('B', len(s)))
    204216    f.write(s)
    205217    if len(s) & 1 == 0:
     
    219231    else:
    220232        fmant, expon = math.frexp(x)
    221         if expon > 16384 or fmant >= 1:    # Infinity or NaN
     233        if expon > 16384 or fmant >= 1 or fmant != fmant: # Infinity or NaN
    222234            expon = sign|0x7FFF
    223235            himant = 0
     
    235247            fsmant = math.floor(fmant)
    236248            lomant = long(fsmant)
    237     _write_short(f, expon)
    238     _write_long(f, himant)
    239     _write_long(f, lomant)
     249    _write_ushort(f, expon)
     250    _write_ulong(f, himant)
     251    _write_ulong(f, lomant)
    240252
    241253from chunk import Chunk
     
    410422        if self._convert and data:
    411423            data = self._convert(data)
    412         self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth)
     424        self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
    413425        return data
    414426
     
    421433        dummy = self._decomp.SetParam(cl.FRAME_BUFFER_SIZE,
    422434                          len(data) * 2)
    423         return self._decomp.Decompress(len(data) / self._nchannels,
     435        return self._decomp.Decompress(len(data) // self._nchannels,
    424436                           data)
    425437
     
    440452        self._nchannels = _read_short(chunk)
    441453        self._nframes = _read_long(chunk)
    442         self._sampwidth = (_read_short(chunk) + 7) / 8
     454        self._sampwidth = (_read_short(chunk) + 7) // 8
    443455        self._framerate = int(_read_float(chunk))
    444456        self._framesize = self._nchannels * self._sampwidth
     
    469481                    else:
    470482                        self._convert = self._adpcm2lin
    471                         self._framesize = self._framesize / 4
     483                        self._sampwidth = 2
    472484                        return
    473485                # for ULAW and ALAW try Compression Library
     
    475487                    import cl
    476488                except ImportError:
    477                     if self._comptype == 'ULAW':
     489                    if self._comptype in ('ULAW', 'ulaw'):
    478490                        try:
    479491                            import audioop
    480492                            self._convert = self._ulaw2lin
    481                             self._framesize = self._framesize / 2
     493                            self._sampwidth = 2
    482494                            return
    483495                        except ImportError:
    484496                            pass
    485497                    raise Error, 'cannot read compressed AIFF-C files'
    486                 if self._comptype == 'ULAW':
     498                if self._comptype in ('ULAW', 'ulaw'):
    487499                    scheme = cl.G711_ULAW
    488                     self._framesize = self._framesize / 2
    489                 elif self._comptype == 'ALAW':
     500                elif self._comptype in ('ALAW', 'alaw'):
    490501                    scheme = cl.G711_ALAW
    491                     self._framesize = self._framesize / 2
    492502                else:
    493503                    raise Error, 'unsupported compression type'
    494504                self._decomp = cl.OpenDecompressor(scheme)
    495505                self._convert = self._decomp_data
     506                self._sampwidth = 2
    496507        else:
    497508            self._comptype = 'NONE'
     
    644655        if self._nframeswritten:
    645656            raise Error, 'cannot change parameters after starting to write'
    646         if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
     657        if comptype not in ('NONE', 'ULAW', 'ulaw', 'ALAW', 'alaw', 'G722'):
    647658            raise Error, 'unsupported compression type'
    648659        self._comptype = comptype
     
    664675        if self._nframeswritten:
    665676            raise Error, 'cannot change parameters after starting to write'
    666         if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'):
     677        if comptype not in ('NONE', 'ULAW', 'ulaw', 'ALAW', 'alaw', 'G722'):
    667678            raise Error, 'unsupported compression type'
    668679        self.setnchannels(nchannels)
     
    707718    def writeframesraw(self, data):
    708719        self._ensure_header_written(len(data))
    709         nframes = len(data) / (self._sampwidth * self._nchannels)
     720        nframes = len(data) // (self._sampwidth * self._nchannels)
    710721        if self._convert:
    711722            data = self._convert(data)
     
    721732
    722733    def close(self):
    723         self._ensure_header_written(0)
    724         if self._datawritten & 1:
    725             # quick pad to even size
    726             self._file.write(chr(0))
    727             self._datawritten = self._datawritten + 1
    728         self._writemarkers()
    729         if self._nframeswritten != self._nframes or \
    730               self._datalength != self._datawritten or \
    731               self._marklength:
    732             self._patchheader()
    733         if self._comp:
    734             self._comp.CloseCompressor()
    735             self._comp = None
    736         self._file.close()
     734        if self._file is None:
     735            return
     736        try:
     737            self._ensure_header_written(0)
     738            if self._datawritten & 1:
     739                # quick pad to even size
     740                self._file.write(chr(0))
     741                self._datawritten = self._datawritten + 1
     742            self._writemarkers()
     743            if self._nframeswritten != self._nframes or \
     744                  self._datalength != self._datawritten or \
     745                  self._marklength:
     746                self._patchheader()
     747            if self._comp:
     748                self._comp.CloseCompressor()
     749                self._comp = None
     750        finally:
     751            # Prevent ref cycles
     752            self._convert = None
     753            f = self._file
     754            self._file = None
     755            f.close()
    737756
    738757    #
     
    785804            import cl
    786805        except ImportError:
    787             if self._comptype == 'ULAW':
     806            if self._comptype in ('ULAW', 'ulaw'):
    788807                try:
    789808                    import audioop
     
    793812                    pass
    794813            raise Error, 'cannot write compressed AIFF-C files'
    795         if self._comptype == 'ULAW':
     814        if self._comptype in ('ULAW', 'ulaw'):
    796815            scheme = cl.G711_ULAW
    797         elif self._comptype == 'ALAW':
     816        elif self._comptype in ('ALAW', 'alaw'):
    798817            scheme = cl.G711_ALAW
    799818        else:
     
    821840        self._file.write('FORM')
    822841        if not self._nframes:
    823             self._nframes = initlength / (self._nchannels * self._sampwidth)
     842            self._nframes = initlength // (self._nchannels * self._sampwidth)
    824843        self._datalength = self._nframes * self._nchannels * self._sampwidth
    825844        if self._datalength & 1:
     
    827846        if self._aifc:
    828847            if self._comptype in ('ULAW', 'ALAW'):
    829                 self._datalength = self._datalength / 2
     848                self._datalength = self._datalength // 2
    830849                if self._datalength & 1:
    831850                    self._datalength = self._datalength + 1
    832851            elif self._comptype == 'G722':
    833                 self._datalength = (self._datalength + 3) / 4
     852                self._datalength = (self._datalength + 3) // 4
    834853                if self._datalength & 1:
    835854                    self._datalength = self._datalength + 1
     
    839858            self._file.write('AIFC')
    840859            self._file.write('FVER')
    841             _write_long(self._file, 4)
    842             _write_long(self._file, self._version)
     860            _write_ulong(self._file, 4)
     861            _write_ulong(self._file, self._version)
    843862        else:
    844863            self._file.write('AIFF')
    845864        self._file.write('COMM')
    846         _write_long(self._file, commlength)
     865        _write_ulong(self._file, commlength)
    847866        _write_short(self._file, self._nchannels)
    848867        self._nframes_pos = self._file.tell()
    849         _write_long(self._file, self._nframes)
    850         _write_short(self._file, self._sampwidth * 8)
     868        _write_ulong(self._file, self._nframes)
     869        if self._comptype in ('ULAW', 'ulaw', 'ALAW', 'alaw', 'G722'):
     870            _write_short(self._file, 8)
     871        else:
     872            _write_short(self._file, self._sampwidth * 8)
    851873        _write_float(self._file, self._framerate)
    852874        if self._aifc:
     
    855877        self._file.write('SSND')
    856878        self._ssnd_length_pos = self._file.tell()
    857         _write_long(self._file, self._datalength + 8)
    858         _write_long(self._file, 0)
    859         _write_long(self._file, 0)
     879        _write_ulong(self._file, self._datalength + 8)
     880        _write_ulong(self._file, 0)
     881        _write_ulong(self._file, 0)
    860882
    861883    def _write_form_length(self, datalength):
     
    868890            commlength = 18
    869891            verslength = 0
    870         _write_long(self._file, 4 + verslength + self._marklength + \
    871                     8 + commlength + 16 + datalength)
     892        _write_ulong(self._file, 4 + verslength + self._marklength + \
     893                     8 + commlength + 16 + datalength)
    872894        return commlength
    873895
     
    887909        dummy = self._write_form_length(datalength)
    888910        self._file.seek(self._nframes_pos, 0)
    889         _write_long(self._file, self._nframeswritten)
     911        _write_ulong(self._file, self._nframeswritten)
    890912        self._file.seek(self._ssnd_length_pos, 0)
    891         _write_long(self._file, datalength + 8)
     913        _write_ulong(self._file, datalength + 8)
    892914        self._file.seek(curpos, 0)
    893915        self._nframes = self._nframeswritten
     
    904926            if len(name) & 1 == 0:
    905927                length = length + 1
    906         _write_long(self._file, length)
     928        _write_ulong(self._file, length)
    907929        self._marklength = length + 8
    908930        _write_short(self._file, len(self._markers))
     
    910932            id, pos, name = marker
    911933            _write_short(self._file, id)
    912             _write_long(self._file, pos)
     934            _write_ulong(self._file, pos)
    913935            _write_string(self._file, name)
    914936
     
    934956    fn = sys.argv[1]
    935957    f = open(fn, 'r')
    936     print "Reading", fn
    937     print "nchannels =", f.getnchannels()
    938     print "nframes   =", f.getnframes()
    939     print "sampwidth =", f.getsampwidth()
    940     print "framerate =", f.getframerate()
    941     print "comptype  =", f.getcomptype()
    942     print "compname  =", f.getcompname()
    943     if sys.argv[2:]:
    944         gn = sys.argv[2]
    945         print "Writing", gn
    946         g = open(gn, 'w')
    947         g.setparams(f.getparams())
    948         while 1:
    949             data = f.readframes(1024)
    950             if not data:
    951                 break
    952             g.writeframes(data)
    953         g.close()
     958    try:
     959        print "Reading", fn
     960        print "nchannels =", f.getnchannels()
     961        print "nframes   =", f.getnframes()
     962        print "sampwidth =", f.getsampwidth()
     963        print "framerate =", f.getframerate()
     964        print "comptype  =", f.getcomptype()
     965        print "compname  =", f.getcompname()
     966        if sys.argv[2:]:
     967            gn = sys.argv[2]
     968            print "Writing", gn
     969            g = open(gn, 'w')
     970            try:
     971                g.setparams(f.getparams())
     972                while 1:
     973                    data = f.readframes(1024)
     974                    if not data:
     975                        break
     976                    g.writeframes(data)
     977            finally:
     978                g.close()
     979            print "Done."
     980    finally:
    954981        f.close()
    955         print "Done."
Note: See TracChangeset for help on using the changeset viewer.