changeset 247:56ff493c2b97

Implement __truediv__ in AudioRegion
author Amine Sehili <amine.sehili@gmail.com>
date Thu, 22 Aug 2019 20:22:28 +0200
parents 936511b60745
children c422eeff8013
files auditok/core.py
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/core.py	Tue Aug 20 20:26:04 2019 +0100
+++ b/auditok/core.py	Thu Aug 22 20:22:28 2019 +0200
@@ -631,6 +631,18 @@
     def __rmul__(self, n):
         return self * n
 
+    def __truediv__(self, n):
+        if not isinstance(n, int) or n <= 0:
+            raise TypeError(
+                "AudioRegion can only be divided by a positive int"
+            )
+        samples_per_sub_region = round(len(self) / n)
+        sub_regions = []
+        for onset in range(0, len(self), samples_per_sub_region):
+            sub_region = self[onset : onset + samples_per_sub_region]
+            sub_regions.append(sub_region)
+        return sub_regions
+
     def __eq__(self, other):
         if other is self:
             return True