changeset 101:a3da97fad36e

Add _normalize_use_channel
author Amine Sehili <amine.sehili@gmail.com>
date Tue, 08 Jan 2019 20:55:52 +0100
parents cee5037f17ad
children 3a72db5f8798
files auditok/io.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/io.py	Tue Jan 08 20:14:18 2019 +0100
+++ b/auditok/io.py	Tue Jan 08 20:55:52 2019 +0100
@@ -59,6 +59,25 @@
     return fmt.lower()
 
 
+def _normalize_use_channel(use_channel):
+    """
+    Returns a value of `use_channel` as expected by audio read/write fuctions.
+    If `use_channel` is `None`, returns 0. If it's an integer, or the special
+    str 'mix' returns it as is. If it's `left` or `right` returns 0 or 1
+    respectively.
+    """
+    if use_channel is None:
+        return 0
+    if use_channel == "mix" or isinstance(use_channel, int):
+        return use_channel
+    try:
+        return ["left", "right"].index(use_channel)
+    except ValueError:
+        err_message = "'use_channel' parameter must be an integer "
+        "or one of ('left', 'right', 'mix'), found: '{}'".format(use_channel)
+        raise AudioParameterError(err_message)
+
+
 class AudioSource():
     """ 
     Base class for audio source objects.