Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions pysoundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,17 @@ def __init__(self, file, mode='r', sample_rate=None, channels=None,
raise ValueError("Invalid mode: %s" % repr(mode))

original_format = format
if format is None:
filename = getattr(file, 'name', file)
format = str(filename).rsplit('.', 1)[-1].upper()
if self.mode == 'w' and format not in _formats:
raise TypeError(
"No format specified and unable to get format from "
"file extension: %s" % repr(filename))
filename = getattr(file, 'name', file)
file_extension = str(filename).rsplit('.', 1)[-1].upper()
if format is None and ('w' in self.mode or
file_extension == 'RAW'):
if file_extension not in _formats:
if self.mode == 'w':
raise TypeError(
"No format specified and unable to get format from "
"file extension: %s" % repr(filename))
else:
format = file_extension

self._info = _ffi.new("SF_INFO*")
if self.mode == 'w' or str(format).upper() == 'RAW':
Expand All @@ -349,7 +353,16 @@ def __init__(self, file, mode='r', sample_rate=None, channels=None,
if channels is None:
raise TypeError("channels must be specified")
self._info.channels = channels
if str(format).upper() == 'RAW' and subtype is None:
raise TypeError("RAW files must specify a subtype")
self._info.format = _format_int(format, subtype, endian)
elif self.mode == 'rw':
if sample_rate is not None:
self._info.samplerate = sample_rate
if channels is not None:
self._info.channels = channels
if format is not None:
self._info.format = _format_int(format, subtype, endian)
else:
if [sample_rate, channels, original_format, subtype, endian] != \
[None] * 5:
Expand Down
Loading