def create_filter(cls, definition, fs): ''' creates a filter from a textual representation ''' definition = definition.lower().strip() if definition.startswith("lp:"): try: (_lp, f, q) = definition.split(":") q = float(q) except: (_lp, f) = definition.split(":") q = 0.707 f = parse_frequency(f) return Biquad.low_pass(f, q, fs) elif definition.startswith("hp:"): try: (_hp, f, q) = definition.split(":") q = float(q) except: (_hp, f) = definition.split(":") q = 0.707 f = parse_frequency(f) return Biquad.high_pass(f, q, fs) elif definition.startswith("eq:"): try: (_eq, f, q, dbgain) = definition.split(":") q = float(q) f = parse_frequency(f) dbgain = parse_decibel(dbgain) return Biquad.peaking_eq(f, q, dbgain, fs) except: return None elif definition.startswith("vol:"): try: (_vol, db) = definition.split(":") db = parse_decibel(db) return Biquad.volume(db) except: return None elif definition.startswith("pass"): return Biquad.pass_filter() elif definition == "mute" or definition == "null": return Biquad.mute() else: print("unknown", definition) return None
def cmd_tonecontrol(self): if len(self.args.parameters) > 2: filtertype = self.args.parameters[0].lower() frequency = parse_frequency(self.args.parameters[1]) dbgain = parse_decibel(self.args.parameters[2]) else: print( "parameter missing, need type frequency db, e.g. ls 200Hz 3db") sys.exit(1) if frequency <= 0: print("frequency {} invalid".format(frequency)) sys.exit(1) if filtertype not in ["ls", "hs"]: print("filter type {} unsupported".format(type)) sys.exit(1) filterdef = "{}:{}:{}".format(filtertype, frequency, dbgain) tonefilter = Biquad.create_filter(filterdef, self.dsptk.get_samplerate()) if tonefilter is None: print("can't handle filter {}".format(filterdef)) sys.exit(1) if filtertype[0] == "l": low = tonefilter high = None else: low = None high = tonefilter try: self.dsptk.set_tonecontrol_filters(low, high) except Exception as e: print(e) sys.exit(1)
def create_filter(cls, definition, fs): ''' creates a filter from a textual representation ''' definition = definition.lower().strip() if definition.startswith("lp:"): try: (_lp, f, q) = definition.split(":") q = float(q) except: (_lp, f) = definition.split(":") q = 0.707 f = parse_frequency(f) return Biquad.low_pass(f, q, fs) elif definition.startswith("hp:"): try: (_hp, f, q) = definition.split(":") q = float(q) except: (_hp, f) = definition.split(":") q = 0.707 f = parse_frequency(f) return Biquad.high_pass(f, q, fs) elif definition.startswith("eq:"): try: (_eq, f, q, dbgain) = definition.split(":") q = float(q) f = parse_frequency(f) dbgain = parse_decibel(dbgain) return Biquad.peaking_eq(f, q, dbgain, fs) except: logging.error("can't parse ea filter") return None elif definition.startswith("vol:"): try: (_vol, db) = definition.split(":") db = parse_decibel(db) return Biquad.volume(db) except: logging.error("can't parse vol filter") return None elif definition.startswith("coeff:"): try: coeffs = definition.split(":") coeffs = coeffs[1:] numc = [] for c in coeffs: numc.append(float(c)) if len(numc) == 5: return Biquad(1, numc[0], numc[1], numc[2], numc[3], numc[4], "biquad from coefficients") elif len(numc) == 6: return Biquad(numc[0], numc[1], numc[2], numc[3], numc[4], numc[5], "biquad from coefficients") else: logging.error("5 or 6 biquad coefficients expected") except Exception as e: logging.error("can't parse biquad filter (%s)", e) return None elif definition.startswith("pass"): return Biquad.pass_filter() elif definition == "mute" or definition == "null": return Biquad.mute() else: logging.error("can't parse %s filter", definition) return None