mirror of
https://github.com/iluvcapra/wavinfo.git
synced 2026-01-01 17:30:41 +00:00
58 lines
1.1 KiB
Python
58 lines
1.1 KiB
Python
|
|
import struct
|
|
|
|
from collections import namedtuple
|
|
|
|
ListChunkDescriptor = namedtuple('ListChunkDescriptor' , 'signature children')
|
|
|
|
class ChunkDescriptor(namedtuple('ChunkDescriptor', 'ident start length') ):
|
|
def read_data(self, from_stream):
|
|
from_stream.seek(self.start)
|
|
return from_stream.read(self.length)
|
|
|
|
|
|
def parse_list_chunk(stream, length):
|
|
children = []
|
|
|
|
start = stream.tell()
|
|
|
|
signature = stream.read(4)
|
|
|
|
while (stream.tell() - start) < length:
|
|
children.append(parse_chunk(stream))
|
|
|
|
return ListChunkDescriptor(signature=signature, children=children)
|
|
|
|
|
|
def parse_chunk(stream):
|
|
ident = stream.read(4)
|
|
if len(ident) != 4:
|
|
return
|
|
|
|
sizeb = stream.read(4)
|
|
size = struct.unpack('<I',sizeb)[0]
|
|
|
|
displacement = size
|
|
if displacement % 2 is not 0:
|
|
displacement = displacement + 1
|
|
|
|
if ident in [b'RIFF',b'LIST']:
|
|
return parse_list_chunk(stream=stream, length=size)
|
|
else:
|
|
start = stream.tell()
|
|
stream.seek(displacement,1)
|
|
return ChunkDescriptor(ident=ident, start=start, length=size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|