def wave_header_footer(self): """returns (header, footer) tuple of strings containing all data before and after the PCM stream may raise ValueError if there's a problem with the header or footer data may raise IOError if there's a problem reading header or footer data from the file""" from audiotools import decoders decoder = decoders.SHNDecoder(open(self.filename, "rb")) (head, tail) = decoder.pcm_split() decoder.close() if ((head[0:4] == b"RIFF") and (head[8:12] == b"WAVE")): return (head, tail) else: raise ValueError("invalid wave header")
def has_foreign_aiff_chunks(self): """returns True if the audio file contains non-audio AIFF chunks during transcoding, if the source audio file has foreign AIFF chunks and the target audio format supports foreign AIFF chunks, conversion should be routed through .aiff conversion to avoid losing those chunks""" from audiotools import decoders from audiotools import bitstream from io import BytesIO try: (head, tail) = decoders.SHNDecoder(open(self.filename, "rb")).pcm_split() header = bitstream.BitstreamReader(BytesIO(head), 0) (FORM, SIZE, AIFF) = header.parse("4b 32u 4b") if ((FORM != 'FORM') or (AIFF != 'AIFF')): return False # if the tail has room for chunks, there must be some foreign ones if (len(tail) >= 8): return True # otherwise, check the header for foreign chunks total_size = len(head) - bitstream.format_byte_size("4b 32u 4b") while (total_size >= 8): (chunk_id, chunk_size) = header.parse("4b 32u") total_size -= bitstream.format_byte_size("4b 32u") if (chunk_id not in ('COMM', 'SSND')): return True else: if (chunk_size % 2): header.skip_bytes(chunk_size + 1) total_size -= chunk_size + 1 else: header.skip_bytes(chunk_size) total_size -= chunk_size else: # no foreign chunks found return False except IOError: return False
def aiff_header_footer(self): """returns (header, footer) tuple of strings containing all data before and after the PCM stream if self.has_foreign_aiff_chunks() is False, may raise ValueError if the file has no header and footer for any reason""" from audiotools import decoders from audiotools import bitstream from io import BytesIO decoder = decoders.SHNDecoder(open(self.filename, "rb")) (head, tail) = decoder.pcm_split() decoder.close() if ((head[0:4] == b"FORM") and (head[8:12] == b"AIFF")): return (head, tail) else: raise ValueError("invalid AIFF header")
def aiff_header_footer(self): """returns (header, footer) tuple of strings containing all data before and after the PCM stream if self.has_foreign_aiff_chunks() is False, may raise ValueError if the file has no header and footer for any reason""" from audiotools import decoders from audiotools import bitstream from io import BytesIO (head, tail) = decoders.SHNDecoder(open(self.filename, "rb")).pcm_split() header = bitstream.BitstreamReader(BytesIO(head), 0) (FORM, SIZE, AIFF) = header.parse("4b 32u 4b") if ((FORM != 'FORM') or (AIFF != 'AIFF')): raise ValueError("invalid AIFF header") else: return (head, tail)
def wave_header_footer(self): """returns (header, footer) tuple of strings containing all data before and after the PCM stream may raise ValueError if there's a problem with the header or footer data may raise IOError if there's a problem reading header or footer data from the file""" from audiotools import decoders from audiotools import bitstream from io import BytesIO (head, tail) = decoders.SHNDecoder(open(self.filename, "rb")).pcm_split() header = bitstream.BitstreamReader(BytesIO(head), 1) (RIFF, SIZE, WAVE) = header.parse("4b 32u 4b") if ((RIFF != 'RIFF') or (WAVE != 'WAVE')): raise ValueError("invalid wave header") else: return (head, tail)