Changeset 388 for python/vendor/current/Lib/aifc.py
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Lib/aifc.py
r2 r388 124 124 When all frames have been written, either call writeframes('') or 125 125 close() to patch up the sizes in the header. 126 Marks can be added anytime. If there are any marks, y pu must call126 Marks can be added anytime. If there are any marks, you must call 127 127 close() after all frames have been written. 128 128 The close() method is called automatically when the class instance … … 160 160 try: 161 161 return struct.unpack('>h', file.read(2))[0] 162 except struct.error: 163 raise EOFError 164 165 def _read_ushort(file): 166 try: 167 return struct.unpack('>H', file.read(2))[0] 162 168 except struct.error: 163 169 raise EOFError … … 195 201 f.write(struct.pack('>h', x)) 196 202 203 def _write_ushort(f, x): 204 f.write(struct.pack('>H', x)) 205 197 206 def _write_long(f, x): 207 f.write(struct.pack('>l', x)) 208 209 def _write_ulong(f, x): 198 210 f.write(struct.pack('>L', x)) 199 211 … … 201 213 if len(s) > 255: 202 214 raise ValueError("string exceeds maximum pstring length") 203 f.write( chr(len(s)))215 f.write(struct.pack('B', len(s))) 204 216 f.write(s) 205 217 if len(s) & 1 == 0: … … 219 231 else: 220 232 fmant, expon = math.frexp(x) 221 if expon > 16384 or fmant >= 1 :# Infinity or NaN233 if expon > 16384 or fmant >= 1 or fmant != fmant: # Infinity or NaN 222 234 expon = sign|0x7FFF 223 235 himant = 0 … … 235 247 fsmant = math.floor(fmant) 236 248 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) 240 252 241 253 from chunk import Chunk … … 410 422 if self._convert and data: 411 423 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) 413 425 return data 414 426 … … 421 433 dummy = self._decomp.SetParam(cl.FRAME_BUFFER_SIZE, 422 434 len(data) * 2) 423 return self._decomp.Decompress(len(data) / self._nchannels,435 return self._decomp.Decompress(len(data) // self._nchannels, 424 436 data) 425 437 … … 440 452 self._nchannels = _read_short(chunk) 441 453 self._nframes = _read_long(chunk) 442 self._sampwidth = (_read_short(chunk) + 7) / 8454 self._sampwidth = (_read_short(chunk) + 7) // 8 443 455 self._framerate = int(_read_float(chunk)) 444 456 self._framesize = self._nchannels * self._sampwidth … … 469 481 else: 470 482 self._convert = self._adpcm2lin 471 self._ framesize = self._framesize / 4483 self._sampwidth = 2 472 484 return 473 485 # for ULAW and ALAW try Compression Library … … 475 487 import cl 476 488 except ImportError: 477 if self._comptype == 'ULAW':489 if self._comptype in ('ULAW', 'ulaw'): 478 490 try: 479 491 import audioop 480 492 self._convert = self._ulaw2lin 481 self._ framesize = self._framesize /2493 self._sampwidth = 2 482 494 return 483 495 except ImportError: 484 496 pass 485 497 raise Error, 'cannot read compressed AIFF-C files' 486 if self._comptype == 'ULAW':498 if self._comptype in ('ULAW', 'ulaw'): 487 499 scheme = cl.G711_ULAW 488 self._framesize = self._framesize / 2 489 elif self._comptype == 'ALAW': 500 elif self._comptype in ('ALAW', 'alaw'): 490 501 scheme = cl.G711_ALAW 491 self._framesize = self._framesize / 2492 502 else: 493 503 raise Error, 'unsupported compression type' 494 504 self._decomp = cl.OpenDecompressor(scheme) 495 505 self._convert = self._decomp_data 506 self._sampwidth = 2 496 507 else: 497 508 self._comptype = 'NONE' … … 644 655 if self._nframeswritten: 645 656 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'): 647 658 raise Error, 'unsupported compression type' 648 659 self._comptype = comptype … … 664 675 if self._nframeswritten: 665 676 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'): 667 678 raise Error, 'unsupported compression type' 668 679 self.setnchannels(nchannels) … … 707 718 def writeframesraw(self, data): 708 719 self._ensure_header_written(len(data)) 709 nframes = len(data) / (self._sampwidth * self._nchannels)720 nframes = len(data) // (self._sampwidth * self._nchannels) 710 721 if self._convert: 711 722 data = self._convert(data) … … 721 732 722 733 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() 737 756 738 757 # … … 785 804 import cl 786 805 except ImportError: 787 if self._comptype == 'ULAW':806 if self._comptype in ('ULAW', 'ulaw'): 788 807 try: 789 808 import audioop … … 793 812 pass 794 813 raise Error, 'cannot write compressed AIFF-C files' 795 if self._comptype == 'ULAW':814 if self._comptype in ('ULAW', 'ulaw'): 796 815 scheme = cl.G711_ULAW 797 elif self._comptype == 'ALAW':816 elif self._comptype in ('ALAW', 'alaw'): 798 817 scheme = cl.G711_ALAW 799 818 else: … … 821 840 self._file.write('FORM') 822 841 if not self._nframes: 823 self._nframes = initlength / (self._nchannels * self._sampwidth)842 self._nframes = initlength // (self._nchannels * self._sampwidth) 824 843 self._datalength = self._nframes * self._nchannels * self._sampwidth 825 844 if self._datalength & 1: … … 827 846 if self._aifc: 828 847 if self._comptype in ('ULAW', 'ALAW'): 829 self._datalength = self._datalength / 2848 self._datalength = self._datalength // 2 830 849 if self._datalength & 1: 831 850 self._datalength = self._datalength + 1 832 851 elif self._comptype == 'G722': 833 self._datalength = (self._datalength + 3) / 4852 self._datalength = (self._datalength + 3) // 4 834 853 if self._datalength & 1: 835 854 self._datalength = self._datalength + 1 … … 839 858 self._file.write('AIFC') 840 859 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) 843 862 else: 844 863 self._file.write('AIFF') 845 864 self._file.write('COMM') 846 _write_ long(self._file, commlength)865 _write_ulong(self._file, commlength) 847 866 _write_short(self._file, self._nchannels) 848 867 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) 851 873 _write_float(self._file, self._framerate) 852 874 if self._aifc: … … 855 877 self._file.write('SSND') 856 878 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) 860 882 861 883 def _write_form_length(self, datalength): … … 868 890 commlength = 18 869 891 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) 872 894 return commlength 873 895 … … 887 909 dummy = self._write_form_length(datalength) 888 910 self._file.seek(self._nframes_pos, 0) 889 _write_ long(self._file, self._nframeswritten)911 _write_ulong(self._file, self._nframeswritten) 890 912 self._file.seek(self._ssnd_length_pos, 0) 891 _write_ long(self._file, datalength + 8)913 _write_ulong(self._file, datalength + 8) 892 914 self._file.seek(curpos, 0) 893 915 self._nframes = self._nframeswritten … … 904 926 if len(name) & 1 == 0: 905 927 length = length + 1 906 _write_ long(self._file, length)928 _write_ulong(self._file, length) 907 929 self._marklength = length + 8 908 930 _write_short(self._file, len(self._markers)) … … 910 932 id, pos, name = marker 911 933 _write_short(self._file, id) 912 _write_ long(self._file, pos)934 _write_ulong(self._file, pos) 913 935 _write_string(self._file, name) 914 936 … … 934 956 fn = sys.argv[1] 935 957 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: 954 981 f.close() 955 print "Done."
Note:
See TracChangeset
for help on using the changeset viewer.