Skip to content

Commit e025d53

Browse files
committed
wave: rename encoding API to format
'format' is the term used in the wave audio specification
1 parent 9263225 commit e025d53

3 files changed

Lines changed: 54 additions & 34 deletions

File tree

Lib/test/audiotests.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def tearDown(self):
2727
unlink(TESTFN)
2828

2929
def check_params(self, f, nchannels, sampwidth, framerate, nframes,
30-
comptype, compname, encoding):
30+
comptype, compname, format):
3131
self.assertEqual(f.getnchannels(), nchannels)
3232
self.assertEqual(f.getsampwidth(), sampwidth)
3333
self.assertEqual(f.getframerate(), framerate)
3434
self.assertEqual(f.getnframes(), nframes)
3535
self.assertEqual(f.getcomptype(), comptype)
3636
self.assertEqual(f.getcompname(), compname)
37-
self.assertEqual(f.getencoding(), encoding)
37+
self.assertEqual(f.getformat(), format)
3838

3939
params = f.getparams()
4040
self.assertEqual(params,
@@ -62,7 +62,7 @@ def create_file(self, testfile):
6262
f.setsampwidth(self.sampwidth)
6363
f.setframerate(self.framerate)
6464
f.setcomptype(self.comptype, self.compname)
65-
f.setencoding(self.encoding)
65+
f.setformat(self.format)
6666
return f
6767

6868
def check_file(self, testfile, nframes, frames):
@@ -72,14 +72,14 @@ def check_file(self, testfile, nframes, frames):
7272
self.assertEqual(f.getframerate(), self.framerate)
7373
self.assertEqual(f.getnframes(), nframes)
7474
self.assertEqual(f.readframes(nframes), frames)
75-
self.assertEqual(f.getencoding(), self.encoding)
75+
self.assertEqual(f.getformat(), self.format)
7676

7777
def test_write_params(self):
7878
f = self.create_file(TESTFN)
7979
f.setnframes(self.nframes)
8080
f.writeframes(self.frames)
8181
self.check_params(f, self.nchannels, self.sampwidth, self.framerate,
82-
self.nframes, self.comptype, self.compname, self.encoding)
82+
self.nframes, self.comptype, self.compname, self.format)
8383
f.close()
8484

8585
def test_write_context_manager_calls_close(self):
@@ -263,7 +263,7 @@ def test_read_params(self):
263263
f = self.f = self.module.open(self.sndfilepath)
264264
#self.assertEqual(f.getfp().name, self.sndfilepath)
265265
self.check_params(f, self.nchannels, self.sampwidth, self.framerate,
266-
self.sndfilenframes, self.comptype, self.compname, self.encoding)
266+
self.sndfilenframes, self.comptype, self.compname, self.format)
267267

268268
def test_close(self):
269269
with open(self.sndfilepath, 'rb') as testfile:

Lib/test/test_wave.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from test import audiotests
33
from test import support
4-
from test.support.os_helper import FakePath
4+
from test.support.os_helper import FakePath, unlink
55
import io
66
import os
77
import struct
@@ -22,7 +22,7 @@ class WavePCM8Test(WaveTest, unittest.TestCase):
2222
sampwidth = 1
2323
framerate = 11025
2424
nframes = 48
25-
encoding = wave.WAVE_FORMAT_PCM
25+
format = wave.WAVE_FORMAT_PCM
2626
comptype = 'NONE'
2727
compname = 'not compressed'
2828
frames = bytes.fromhex("""\
@@ -40,7 +40,7 @@ class WavePCM16Test(WaveTest, unittest.TestCase):
4040
sampwidth = 2
4141
framerate = 11025
4242
nframes = 48
43-
encoding = wave.WAVE_FORMAT_PCM
43+
format = wave.WAVE_FORMAT_PCM
4444
comptype = 'NONE'
4545
compname = 'not compressed'
4646
frames = bytes.fromhex("""\
@@ -62,7 +62,7 @@ class WavePCM24Test(WaveTest, unittest.TestCase):
6262
sampwidth = 3
6363
framerate = 11025
6464
nframes = 48
65-
encoding = wave.WAVE_FORMAT_PCM
65+
format = wave.WAVE_FORMAT_PCM
6666
comptype = 'NONE'
6767
compname = 'not compressed'
6868
frames = bytes.fromhex("""\
@@ -90,7 +90,7 @@ class WavePCM24ExtTest(WaveTest, unittest.TestCase):
9090
sampwidth = 3
9191
framerate = 11025
9292
nframes = 48
93-
encoding = wave.WAVE_FORMAT_EXTENSIBLE
93+
format = wave.WAVE_FORMAT_EXTENSIBLE
9494
readonly = True # Writing EXTENSIBLE wave format is not supported.
9595
comptype = 'NONE'
9696
compname = 'not compressed'
@@ -119,7 +119,7 @@ class WavePCM32Test(WaveTest, unittest.TestCase):
119119
sampwidth = 4
120120
framerate = 11025
121121
nframes = 48
122-
encoding = wave.WAVE_FORMAT_PCM
122+
format = wave.WAVE_FORMAT_PCM
123123
comptype = 'NONE'
124124
compname = 'not compressed'
125125
frames = bytes.fromhex("""\
@@ -147,7 +147,7 @@ class WaveIeeeFloatingPointTest(WaveTest, unittest.TestCase):
147147
sampwidth = 4
148148
framerate = 11025
149149
nframes = 48
150-
encoding = wave.WAVE_FORMAT_IEEE_FLOAT
150+
format = wave.WAVE_FORMAT_IEEE_FLOAT
151151
comptype = 'NONE'
152152
compname = 'not compressed'
153153
frames = bytes.fromhex("""\
@@ -173,6 +173,26 @@ def test__all__(self):
173173

174174
class WaveLowLevelTest(unittest.TestCase):
175175

176+
def test_getformat_setformat(self):
177+
with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
178+
filename = fp.name
179+
self.addCleanup(unlink, filename)
180+
181+
with wave.open(filename, 'wb') as w:
182+
w.setnchannels(1)
183+
w.setsampwidth(2)
184+
w.setframerate(22050)
185+
self.assertEqual(w.getformat(), wave.WAVE_FORMAT_PCM)
186+
w.setformat(wave.WAVE_FORMAT_IEEE_FLOAT)
187+
self.assertEqual(w.getformat(), wave.WAVE_FORMAT_IEEE_FLOAT)
188+
189+
def test_read_getformat(self):
190+
b = b'RIFF' + struct.pack('<L', 36) + b'WAVE'
191+
b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 1, 11025, 11025, 1, 8)
192+
b += b'data' + struct.pack('<L', 0)
193+
with wave.open(io.BytesIO(b), 'rb') as r:
194+
self.assertEqual(r.getformat(), wave.WAVE_FORMAT_PCM)
195+
176196
def test_read_no_chunks(self):
177197
b = b'SPAM'
178198
with self.assertRaises(EOFError):

Lib/wave.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
getsampwidth() -- returns sample width in bytes
1616
getframerate() -- returns sampling frequency
1717
getnframes() -- returns number of audio frames
18-
getencoding() -- returns frame encoding (WAVE_FORMAT_PCM, WAVE_FORMAT_IEEE_FLOAT
18+
getformat() -- returns frame encoding (WAVE_FORMAT_PCM, WAVE_FORMAT_IEEE_FLOAT
1919
or WAVE_FORMAT_EXTENSIBLE)
2020
getcomptype() -- returns compression type ('NONE' for linear samples)
2121
getcompname() -- returns human-readable version of
@@ -44,8 +44,8 @@
4444
setsampwidth(n) -- set the sample width
4545
setframerate(n) -- set the frame rate
4646
setnframes(n) -- set the number of frames
47-
setencoding(encoding)
48-
-- set the frame encoding. Only WAVE_FORMAT_PCM,
47+
setformat(format)
48+
-- set the frame format. Only WAVE_FORMAT_PCM and
4949
WAVE_FORMAT_IEEE_FLOAT are supported.
5050
setcomptype(type, name)
5151
-- set the compression type and the
@@ -232,10 +232,10 @@ class Wave_read:
232232
available through the getsampwidth() method
233233
_framerate -- the sampling frequency
234234
available through the getframerate() method
235-
_encoding -- frame encoding
235+
_format -- frame format
236236
One of WAVE_FORMAT_PCM, WAVE_FORMAT_IEEE_FLOAT
237237
or WAVE_FORMAT_EXTENSIBLE available through
238-
getencoding() method
238+
getformat() method
239239
_comptype -- the AIFF-C compression type ('NONE' if AIFF)
240240
available through the getcomptype() method
241241
_compname -- the human-readable AIFF-C compression type
@@ -337,6 +337,9 @@ def getsampwidth(self):
337337
def getframerate(self):
338338
return self._framerate
339339

340+
def getformat(self):
341+
return self._format
342+
340343
def getcomptype(self):
341344
return self._comptype
342345

@@ -348,9 +351,6 @@ def getparams(self):
348351
self.getframerate(), self.getnframes(),
349352
self.getcomptype(), self.getcompname())
350353

351-
def getencoding(self):
352-
return self._encoding
353-
354354
def setpos(self, pos):
355355
if pos < 0 or pos > self._nframes:
356356
raise Error('position not in range')
@@ -380,16 +380,16 @@ def readframes(self, nframes):
380380

381381
def _read_fmt_chunk(self, chunk):
382382
try:
383-
self._encoding, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
383+
self._format, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
384384
except struct.error:
385385
raise EOFError from None
386-
if self._encoding not in (WAVE_FORMAT_PCM, WAVE_FORMAT_IEEE_FLOAT, WAVE_FORMAT_EXTENSIBLE):
387-
raise Error('unknown format: %r' % (self._encoding,))
386+
if self._format not in (WAVE_FORMAT_PCM, WAVE_FORMAT_IEEE_FLOAT, WAVE_FORMAT_EXTENSIBLE):
387+
raise Error('unknown format: %r' % (self._format,))
388388
try:
389389
sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
390390
except struct.error:
391391
raise EOFError from None
392-
if self._encoding == WAVE_FORMAT_EXTENSIBLE:
392+
if self._format == WAVE_FORMAT_EXTENSIBLE:
393393
try:
394394
cbSize, wValidBitsPerSample, dwChannelMask = struct.unpack_from('<HHL', chunk.read(8))
395395
# Read the entire UUID from the chunk
@@ -432,8 +432,8 @@ class Wave_write:
432432
set through the setsampwidth() or setparams() method
433433
_framerate -- the sampling frequency
434434
set through the setframerate() or setparams() method
435-
_encoding -- frame encoding
436-
set through setencoding() method
435+
_format -- frame format
436+
set through setformat() method
437437
_nframes -- the number of audio frames written to the header
438438
set through the setnframes() or setparams() method
439439
@@ -461,7 +461,7 @@ def initfp(self, file):
461461
self._file = file
462462
self._convert = None
463463
self._nchannels = 0
464-
self._encoding = WAVE_FORMAT_PCM
464+
self._format = WAVE_FORMAT_PCM
465465
self._sampwidth = 0
466466
self._framerate = 0
467467
self._nframes = 0
@@ -534,15 +534,15 @@ def setcomptype(self, comptype, compname):
534534
self._comptype = comptype
535535
self._compname = compname
536536

537-
def setencoding(self, encoding):
537+
def setformat(self, format):
538538
if self._datawritten:
539539
raise Error('cannot change parameters after starting to write')
540-
if encoding not in (WAVE_FORMAT_IEEE_FLOAT, WAVE_FORMAT_PCM):
540+
if format not in (WAVE_FORMAT_IEEE_FLOAT, WAVE_FORMAT_PCM):
541541
raise Error('unsupported wave format')
542-
self._encoding = encoding
542+
self._format = format
543543

544-
def getencoding(self):
545-
return self._encoding
544+
def getformat(self):
545+
return self._format
546546

547547
def getcomptype(self):
548548
return self._comptype
@@ -627,7 +627,7 @@ def _write_header(self, initlength):
627627
self._form_length_pos = None
628628
self._file.write(struct.pack('<L4s4sLHHLLHH4s',
629629
36 + self._datalength, b'WAVE', b'fmt ', 16,
630-
self._encoding, self._nchannels, self._framerate,
630+
self._format, self._nchannels, self._framerate,
631631
self._nchannels * self._framerate * self._sampwidth,
632632
self._nchannels * self._sampwidth,
633633
self._sampwidth * 8, b'data'))

0 commit comments

Comments
 (0)