Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49c2df1

Browse files
Removed silly dependence on AL module.
1 parent e0d95c3 commit 49c2df1

1 file changed

Lines changed: 31 additions & 54 deletions

File tree

Lib/aifc.py

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@
134134
# writeframesraw.
135135

136136
import builtin
137-
import AL
138137
try:
139138
import CL
140139
except ImportError:
@@ -147,16 +146,6 @@
147146
_skiplist = 'COMT', 'INST', 'MIDI', 'AESD', \
148147
'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO'
149148

150-
_nchannelslist = [(1, AL.MONO), (2, AL.STEREO)]
151-
_sampwidthlist = [(8, AL.SAMPLE_8), (16, AL.SAMPLE_16), (24, AL.SAMPLE_24)]
152-
_frameratelist = [(48000, AL.RATE_48000),
153-
(44100, AL.RATE_44100),
154-
(32000, AL.RATE_32000),
155-
(22050, AL.RATE_22050),
156-
(16000, AL.RATE_16000),
157-
(11025, AL.RATE_11025),
158-
( 8000, AL.RATE_8000)]
159-
160149
def _convert1(value, list):
161150
for t in list:
162151
if value == t[0]:
@@ -420,19 +409,15 @@ def initfp(self, file):
420409
if not self._comm_chunk_read or not self._ssnd_chunk:
421410
raise Error, 'COMM chunk and/or SSND chunk missing'
422411
if self._aifc and self._decomp:
423-
params = [CL.ORIGINAL_FORMAT, 0, \
424-
CL.BITS_PER_COMPONENT, 0, \
412+
params = [CL.ORIGINAL_FORMAT, 0,
413+
CL.BITS_PER_COMPONENT, self._sampwidth * 8,
425414
CL.FRAME_RATE, self._framerate]
426-
if self._nchannels == AL.MONO:
415+
if self._nchannels == 1:
427416
params[1] = CL.MONO
428-
else:
417+
elif self._nchannels == 2:
429418
params[1] = CL.STEREO_INTERLEAVED
430-
if self._sampwidth == AL.SAMPLE_8:
431-
params[3] = 8
432-
elif self._sampwidth == AL.SAMPLE_16:
433-
params[3] = 16
434419
else:
435-
params[3] = 24
420+
raise Error, 'cannot compress more than 2 channels'
436421
self._decomp.SetParams(params)
437422

438423
def __init__(self, f):
@@ -539,13 +524,10 @@ def _ulaw2lin(self, data):
539524
return audioop.ulaw2lin(data, 2)
540525

541526
def _read_comm_chunk(self, chunk):
542-
nchannels = _read_short(chunk)
543-
self._nchannels = _convert1(nchannels, _nchannelslist)
527+
self._nchannels = _read_short(chunk)
544528
self._nframes = _read_long(chunk)
545-
sampwidth = _read_short(chunk)
546-
self._sampwidth = _convert1(sampwidth, _sampwidthlist)
547-
framerate = _read_float(chunk)
548-
self._framerate = _convert1(framerate, _frameratelist)
529+
self._sampwidth = (_read_short(chunk) + 7) / 8
530+
self._framerate = _read_float(chunk)
549531
self._framesize = self._nchannels * self._sampwidth
550532
if self._aifc:
551533
#DEBUG: SGI's soundeditor produces a bad size :-(
@@ -694,7 +676,8 @@ def aifc(self):
694676
def setnchannels(self, nchannels):
695677
if self._nframeswritten:
696678
raise Error, 'cannot change parameters after starting to write'
697-
dummy = _convert2(nchannels, _nchannelslist)
679+
if nchannels < 1:
680+
raise Error, 'bad # of channels'
698681
self._nchannels = nchannels
699682

700683
def getnchannels(self):
@@ -705,7 +688,8 @@ def getnchannels(self):
705688
def setsampwidth(self, sampwidth):
706689
if self._nframeswritten:
707690
raise Error, 'cannot change parameters after starting to write'
708-
dummy = _convert2(sampwidth, _sampwidthlist)
691+
if sampwidth < 1 or sampwidth > 4:
692+
raise Error, 'bad sample width'
709693
self._sampwidth = sampwidth
710694

711695
def getsampwidth(self):
@@ -716,7 +700,8 @@ def getsampwidth(self):
716700
def setframerate(self, framerate):
717701
if self._nframeswritten:
718702
raise Error, 'cannot change parameters after starting to write'
719-
dummy = _convert2(framerate, _frameratelist)
703+
if framerate <= 0:
704+
raise Error, 'bad frame rate'
720705
self._framerate = framerate
721706

722707
def getframerate(self):
@@ -756,15 +741,11 @@ def setparams(self, (nchannels, sampwidth, framerate, nframes, comptype, compnam
756741
raise Error, 'cannot change parameters after starting to write'
757742
if comptype not in ('NONE', 'ULAW', 'ALAW'):
758743
raise Error, 'unsupported compression type'
759-
dummy = _convert2(nchannels, _nchannelslist)
760-
dummy = _convert2(sampwidth, _sampwidthlist)
761-
dummy = _convert2(framerate, _frameratelist)
762-
self._nchannels = nchannels
763-
self._sampwidth = sampwidth
764-
self._framerate = framerate
765-
self._nframes = nframes
766-
self._comptype = comptype
767-
self._compname = compname
744+
self.setnchannels(nchannels)
745+
self.setsampwidth(sampwidth)
746+
self.setframerate(framerate)
747+
self.setnframes(nframes)
748+
self.setcomptype(comptype, compname)
768749

769750
def getparams(self):
770751
if not self._nchannels or not self._sampwidth or not self._framerate:
@@ -849,8 +830,8 @@ def _ensure_header_written(self, datasize):
849830
if not self._nframeswritten:
850831
if self._comptype in ('ULAW', 'ALAW'):
851832
if not self._sampwidth:
852-
self._sampwidth = AL.SAMPLE_16
853-
if self._sampwidth != AL.SAMPLE_16:
833+
self._sampwidth = 2
834+
if self._sampwidth != 2:
854835
raise Error, 'sample width must be 2 when compressing with ULAW or ALAW'
855836
if not self._nchannels:
856837
raise Error, '# channels not specified'
@@ -879,21 +860,17 @@ def _init_compression(self):
879860
else:
880861
raise Error, 'unsupported compression type'
881862
self._comp = cl.OpenCompressor(scheme)
882-
params = [CL.ORIGINAL_FORMAT, 0, \
883-
CL.BITS_PER_COMPONENT, 0, \
884-
CL.FRAME_RATE, self._framerate, \
885-
CL.FRAME_BUFFER_SIZE, 100, \
863+
params = [CL.ORIGINAL_FORMAT, 0,
864+
CL.BITS_PER_COMPONENT, self._sampwidth * 8,
865+
CL.FRAME_RATE, self._framerate,
866+
CL.FRAME_BUFFER_SIZE, 100,
886867
CL.COMPRESSED_BUFFER_SIZE, 100]
887-
if self._nchannels == AL.MONO:
868+
if self._nchannels == 1:
888869
params[1] = CL.MONO
889-
else:
870+
elif self._nchannels == 2:
890871
params[1] = CL.STEREO_INTERLEAVED
891-
if self._sampwidth == AL.SAMPLE_8:
892-
params[3] = 8
893-
elif self._sampwidth == AL.SAMPLE_16:
894-
params[3] = 16
895872
else:
896-
params[3] = 24
873+
raise Error, 'cannot compress more than 2 channels'
897874
self._comp.SetParams(params)
898875
# the compressor produces a header which we ignore
899876
dummy = self._comp.Compress(0, '')
@@ -923,11 +900,11 @@ def _write_header(self, initlength):
923900
self._file.write('AIFF')
924901
self._file.write('COMM')
925902
_write_long(self._file, commlength)
926-
_write_short(self._file, _convert2(self._nchannels, _nchannelslist))
903+
_write_short(self._file, self._nchannels)
927904
self._nframes_pos = self._file.tell()
928905
_write_long(self._file, self._nframes)
929-
_write_short(self._file, _convert2(self._sampwidth, _sampwidthlist))
930-
_write_float(self._file, _convert2(self._framerate, _frameratelist))
906+
_write_short(self._file, self._sampwidth * 8)
907+
_write_float(self._file, self._framerate)
931908
if self._aifc:
932909
self._file.write(self._comptype)
933910
_write_string(self._file, self._compname)

0 commit comments

Comments
 (0)