# HG changeset patch # User Amine Sehili # Date 1546977352 -3600 # Node ID a3da97fad36e85a223d02cdd6449f95545064489 # Parent cee5037f17adf145e0842341ec17c14ffbb770dd Add _normalize_use_channel diff -r cee5037f17ad -r a3da97fad36e auditok/io.py --- 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.