Mercurial > hg > auditok
changeset 188:0914e845c21b
Implement __getitem__ in AudioRegion
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Wed, 10 Apr 2019 21:05:12 +0100 |
parents | 949678a8cf25 |
children | d665d51309f8 |
files | auditok/core.py |
diffstat | 1 files changed, 30 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/auditok/core.py Fri Apr 05 20:07:50 2019 +0100 +++ b/auditok/core.py Wed Apr 10 21:05:12 2019 +0100 @@ -399,6 +399,36 @@ return self return other.add(self) + def __getitem__(self, index): + err_message = "AudioRegion index must a slice object without a step" + if not isinstance(index, slice): + raise TypeError(err_message) + if index.step is not None: + raise ValueError(err_message) + + start_ms = index.start if index.start is not None else 0 + stop_ms = index.stop if index.stop is not None else len(self) + if not (isinstance(start_ms, int) and isinstance(stop_ms, int)): + raise TypeError("Slicing Audioregion requires integers") + + if start_ms < 0: + start_ms += len(self) + if stop_ms < 0: + stop_ms += len(self) + + samples_per_ms = self.sr / 1000 + bytes_per_ms = samples_per_ms * self.sw * self.channels + # if a fraction of a sample is covered, return the whole sample + onset = int(start_ms * bytes_per_ms) + offset = round(stop_ms * bytes_per_ms + 0.5) + # recompute start_ms based on actual onset + actual_start_s = onset / bytes_per_ms / 1000 + new_start = ( + self.start + actual_start_s + ) # TODO deal with negative indices + data = self._data[onset:offset] + return AudioRegion(data, new_start, self.sr, self.sw, self.ch) + class StreamTokenizer: """