From b50e3b37383f069208b542c3d962835e7f64bbf0 Mon Sep 17 00:00:00 2001 From: CJ Carey Date: Mon, 12 Dec 2016 15:36:41 -0500 Subject: [PATCH] PEP8 fixes, merging cues w/ cuelabels --- scipy/io/wavfile.py | 86 ++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/scipy/io/wavfile.py b/scipy/io/wavfile.py index a7972b916517..ea336f93c1b2 100644 --- a/scipy/io/wavfile.py +++ b/scipy/io/wavfile.py @@ -14,6 +14,7 @@ import numpy import struct import warnings +from collections import defaultdict __all__ = [ @@ -40,8 +41,6 @@ def _read_fmt_chunk(fid, is_big_endian): """ Returns ------- - size : int - size of format subchunk in bytes (minus 8 for "fmt " and itself) format_tag : int PCM, float, or compressed format channels : int @@ -60,23 +59,22 @@ def _read_fmt_chunk(fid, is_big_endian): else: fmt = '<' - size = res = struct.unpack(fmt+'I', fid.read(4))[0] + size = struct.unpack(fmt+'I', fid.read(4))[0] if size < 16: raise ValueError("Binary structure of wave file is not compliant") - res = struct.unpack(fmt+'HHIIHH', fid.read(16)) + (format_tag, channels, fs, bytes_per_second, block_align, bit_depth + ) = struct.unpack(fmt+'HHIIHH', fid.read(16)) bytes_read = 16 - format_tag, channels, fs, bytes_per_second, block_align, bit_depth = res - - if format_tag == WAVE_FORMAT_EXTENSIBLE and size >= (16+2): + if format_tag == WAVE_FORMAT_EXTENSIBLE and size >= 18: ext_chunk_size = struct.unpack(fmt+'H', fid.read(2))[0] bytes_read += 2 if ext_chunk_size >= 22: extensible_chunk_data = fid.read(22) bytes_read += 22 - raw_guid = extensible_chunk_data[2+4:2+4+16] + raw_guid = extensible_chunk_data[6:22] # GUID template {XXXXXXXX-0000-0010-8000-00AA00389B71} (RFC-2361) # MS GUID byte order: first three groups are native byte order, # rest is Big Endian @@ -96,8 +94,7 @@ def _read_fmt_chunk(fid, is_big_endian): if size > bytes_read: fid.read(size - bytes_read) - return (size, format_tag, channels, fs, bytes_per_second, block_align, - bit_depth) + return format_tag, channels, fs, bytes_per_second, block_align, bit_depth # assumes file pointer is immediately after the 'data' id @@ -131,7 +128,7 @@ def _read_data_chunk(fid, format_tag, channels, bit_depth, is_big_endian, # if odd number of bytes, move 1 byte further (data chunk is word-aligned) if size % 2 == 1: - fid.seek(1, 1) + fid.seek(1, 1) if bit_depth == 24: a = numpy.empty((len(data)/3, 4), dtype='u1') @@ -160,7 +157,7 @@ def _skip_unknown_chunk(fid, is_big_endian): # if odd number of bytes, move 1 byte further # (data chunk is word-aligned) if size % 2 == 1: - size += 1 + size += 1 fid.seek(size, 1) @@ -246,8 +243,7 @@ def read(filename, mmap=False, return_cues=False, return_pitch=False): channels = 1 bit_depth = 8 format_tag = WAVE_FORMAT_PCM - cue = [] - cuelabels = [] + cues = defaultdict(dict) pitch = 0.0 while fid.tell() < file_size: # read the next chunk @@ -261,29 +257,27 @@ def read(filename, mmap=False, return_cues=False, return_pitch=False): if chunk_id == b'fmt ': fmt_chunk_received = True fmt_chunk = _read_fmt_chunk(fid, is_big_endian) - format_tag, channels, fs = fmt_chunk[1:4] - bit_depth = fmt_chunk[6] + format_tag, channels, fs, _, __, bit_depth = fmt_chunk elif chunk_id == b'data': if not fmt_chunk_received: raise ValueError("No fmt chunk before data") data = _read_data_chunk(fid, format_tag, channels, bit_depth, is_big_endian, mmap=mmap) elif chunk_id == b'cue ': - str1 = fid.read(8) - _, numcue = struct.unpack('