Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5)

Delta Between Two Patch Sets: Lib/sunau.py

Issue 18878: Add support of the 'with' statement to sunau.open.
Left Patch Set: Created 6 years, 7 months ago
Right Patch Set: Created 6 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/whatsnew/3.4.rst ('k') | Lib/test/test_sunau.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Stuff to parse Sun and NeXT audio files. 1 """Stuff to parse Sun and NeXT audio files.
2 2
3 An audio file consists of a header followed by the data. The structure 3 An audio file consists of a header followed by the data. The structure
4 of the header is as follows. 4 of the header is as follows.
5 5
6 +---------------+ 6 +---------------+
7 | magic word | 7 | magic word |
8 +---------------+ 8 +---------------+
9 | header size | 9 | header size |
10 +---------------+ 10 +---------------+
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 This returns an instance of a class with the following public methods: 45 This returns an instance of a class with the following public methods:
46 getnchannels() -- returns number of audio channels (1 for 46 getnchannels() -- returns number of audio channels (1 for
47 mono, 2 for stereo) 47 mono, 2 for stereo)
48 getsampwidth() -- returns sample width in bytes 48 getsampwidth() -- returns sample width in bytes
49 getframerate() -- returns sampling frequency 49 getframerate() -- returns sampling frequency
50 getnframes() -- returns number of audio frames 50 getnframes() -- returns number of audio frames
51 getcomptype() -- returns compression type ('NONE' or 'ULAW') 51 getcomptype() -- returns compression type ('NONE' or 'ULAW')
52 getcompname() -- returns human-readable version of 52 getcompname() -- returns human-readable version of
53 compression type ('not compressed' matches 'NONE') 53 compression type ('not compressed' matches 'NONE')
54 getparams() -- returns a tuple consisting of all of the 54 getparams() -- returns a namedtuple consisting of all of the
55 above in the above order 55 above in the above order
56 getmarkers() -- returns None (for compatibility with the 56 getmarkers() -- returns None (for compatibility with the
57 aifc module) 57 aifc module)
58 getmark(id) -- raises an error since the mark does not 58 getmark(id) -- raises an error since the mark does not
59 exist (for compatibility with the aifc module) 59 exist (for compatibility with the aifc module)
60 readframes(n) -- returns at most n frames of audio 60 readframes(n) -- returns at most n frames of audio
61 rewind() -- rewind to the beginning of the audio stream 61 rewind() -- rewind to the beginning of the audio stream
62 setpos(pos) -- seek to the specified position 62 setpos(pos) -- seek to the specified position
63 tell() -- return the current position 63 tell() -- return the current position
64 close() -- close the instance (make it unusable) 64 close() -- close the instance (make it unusable)
(...skipping 30 matching lines...) Expand all
95 writeframes. The total number of frames does not need to be set, 95 writeframes. The total number of frames does not need to be set,
96 but when it is set to the correct value, the header does not have to 96 but when it is set to the correct value, the header does not have to
97 be patched up. 97 be patched up.
98 It is best to first set all parameters, perhaps possibly the 98 It is best to first set all parameters, perhaps possibly the
99 compression type, and then write audio frames using writeframesraw. 99 compression type, and then write audio frames using writeframesraw.
100 When all frames have been written, either call writeframes('') or 100 When all frames have been written, either call writeframes('') or
101 close() to patch up the sizes in the header. 101 close() to patch up the sizes in the header.
102 The close() method is called automatically when the class instance 102 The close() method is called automatically when the class instance
103 is destroyed. 103 is destroyed.
104 """ 104 """
105
106 from collections import namedtuple
107
108 _sunau_params = namedtuple('_sunau_params',
109 'nchannels sampwidth framerate nframes comptype compn ame')
105 110
106 # from <multimedia/audio_filehdr.h> 111 # from <multimedia/audio_filehdr.h>
107 AUDIO_FILE_MAGIC = 0x2e736e64 112 AUDIO_FILE_MAGIC = 0x2e736e64
108 AUDIO_FILE_ENCODING_MULAW_8 = 1 113 AUDIO_FILE_ENCODING_MULAW_8 = 1
109 AUDIO_FILE_ENCODING_LINEAR_8 = 2 114 AUDIO_FILE_ENCODING_LINEAR_8 = 2
110 AUDIO_FILE_ENCODING_LINEAR_16 = 3 115 AUDIO_FILE_ENCODING_LINEAR_16 = 3
111 AUDIO_FILE_ENCODING_LINEAR_24 = 4 116 AUDIO_FILE_ENCODING_LINEAR_24 = 4
112 AUDIO_FILE_ENCODING_LINEAR_32 = 5 117 AUDIO_FILE_ENCODING_LINEAR_32 = 5
113 AUDIO_FILE_ENCODING_FLOAT = 6 118 AUDIO_FILE_ENCODING_FLOAT = 6
114 AUDIO_FILE_ENCODING_DOUBLE = 7 119 AUDIO_FILE_ENCODING_DOUBLE = 7
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 246
242 def getcompname(self): 247 def getcompname(self):
243 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8: 248 if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
244 return 'CCITT G.711 u-law' 249 return 'CCITT G.711 u-law'
245 elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8: 250 elif self._encoding == AUDIO_FILE_ENCODING_ALAW_8:
246 return 'CCITT G.711 A-law' 251 return 'CCITT G.711 A-law'
247 else: 252 else:
248 return 'not compressed' 253 return 'not compressed'
249 254
250 def getparams(self): 255 def getparams(self):
251 return self.getnchannels(), self.getsampwidth(), \ 256 return _sunau_params(self.getnchannels(), self.getsampwidth(),
252 self.getframerate(), self.getnframes(), \ 257 self.getframerate(), self.getnframes(),
253 self.getcomptype(), self.getcompname() 258 self.getcomptype(), self.getcompname())
254 259
255 def getmarkers(self): 260 def getmarkers(self):
256 return None 261 return None
257 262
258 def getmark(self, id): 263 def getmark(self, id):
259 raise Error('no marks') 264 raise Error('no marks')
260 265
261 def readframes(self, nframes): 266 def readframes(self, nframes):
262 if self._encoding in _simple_encodings: 267 if self._encoding in _simple_encodings:
263 if nframes == AUDIO_UNKNOWN_SIZE: 268 if nframes == AUDIO_UNKNOWN_SIZE:
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 391
387 def setparams(self, params): 392 def setparams(self, params):
388 nchannels, sampwidth, framerate, nframes, comptype, compname = params 393 nchannels, sampwidth, framerate, nframes, comptype, compname = params
389 self.setnchannels(nchannels) 394 self.setnchannels(nchannels)
390 self.setsampwidth(sampwidth) 395 self.setsampwidth(sampwidth)
391 self.setframerate(framerate) 396 self.setframerate(framerate)
392 self.setnframes(nframes) 397 self.setnframes(nframes)
393 self.setcomptype(comptype, compname) 398 self.setcomptype(comptype, compname)
394 399
395 def getparams(self): 400 def getparams(self):
396 return self.getnchannels(), self.getsampwidth(), \ 401 return _sunau_getparams(self.getnchannels(), self.getsampwidth(),
397 self.getframerate(), self.getnframes(), \ 402 self.getframerate(), self.getnframes(),
398 self.getcomptype(), self.getcompname() 403 self.getcomptype(), self.getcompname())
399 404
400 def tell(self): 405 def tell(self):
401 return self._nframeswritten 406 return self._nframeswritten
402 407
403 def writeframesraw(self, data): 408 def writeframesraw(self, data):
404 self._ensure_header_written() 409 self._ensure_header_written()
405 nframes = len(data) / self._framesize 410 nframes = len(data) / self._framesize
406 if self._comptype == 'ULAW': 411 if self._comptype == 'ULAW':
407 import audioop 412 import audioop
408 data = audioop.lin2ulaw(data, self._sampwidth) 413 data = audioop.lin2ulaw(data, self._sampwidth)
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 else: 496 else:
492 mode = 'rb' 497 mode = 'rb'
493 if mode in ('r', 'rb'): 498 if mode in ('r', 'rb'):
494 return Au_read(f) 499 return Au_read(f)
495 elif mode in ('w', 'wb'): 500 elif mode in ('w', 'wb'):
496 return Au_write(f) 501 return Au_write(f)
497 else: 502 else:
498 raise Error("mode must be 'r', 'rb', 'w', or 'wb'") 503 raise Error("mode must be 'r', 'rb', 'w', or 'wb'")
499 504
500 openfp = open 505 openfp = open
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+