class StaticMemorySource(StaticSource): '''Helper class for default implementation of `StaticSource`. Do not use directly.''' def __init__(self, data, audio_format): '''Construct a memory source over the given data buffer. ''' self._file = BytesIO(data) self._max_offset = len(data) self.audio_format = audio_format self._duration = len(data) / float(audio_format.bytes_per_second) def seek(self, timestamp): offset = int(timestamp * self.audio_format.bytes_per_second) # Align to sample if self.audio_format.bytes_per_sample == 2: offset &= 0xfffffffe elif self.audio_format.bytes_per_sample == 4: offset &= 0xfffffffc self._file.seek(offset) def get_audio_data(self, bytes): offset = self._file.tell() timestamp = float(offset) / self.audio_format.bytes_per_second # Align to sample size if self.audio_format.bytes_per_sample == 2: bytes &= 0xfffffffe elif self.audio_format.bytes_per_sample == 4: bytes &= 0xfffffffc data = self._file.read(bytes) if not len(data): return None duration = float(len(data)) / self.audio_format.bytes_per_second return AudioData(data, len(data), timestamp, duration, [])
def __init__(self, file): if not hasattr(file, 'seek'): file = BytesIO(file.read()) super(RIFFFile, self).__init__(file, 0)