File: rew.py

package info (click to toggle)
hifiberry-dsp 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,456 kB
  • sloc: xml: 6,099; python: 4,248; sh: 70; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 5,698 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'''
Copyright (c) 2018 Modul 9/HiFiBerry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import logging
from hifiberrydsp.filtering.biquad import Biquad


class REWParser():

    def __init__(self):
        pass

    @staticmethod
    def readfilters(filename, fs=48000):
        filters = []

        with open(filename) as file:
            for line in file.readlines():
                if line.startswith("Filter"):
                    parts = line.split()
                    if len(parts) >= 12 and parts[2] == "ON" and \
                            parts[3] == "PK" and \
                            parts[4] == "Fc" and parts[6] == "Hz" and \
                            parts[7] == "Gain" and parts[9] == "dB" and \
                            parts[10] == "Q":

                        fc = float(parts[5])
                        gain = float(parts[8])
                        q = float(parts[11])
                        logging.info("Filter EQ fc=%s, q=%s, gain=%s, fs=%s",
                                     fc, q, gain, fs)
                        filters.append(
                            Biquad.peaking_eq(fc, q, gain, fs))
                    elif len(parts) >= 6 and parts[2] == "ON" and \
                            parts[3] == "LP" and \
                            parts[4] == "Fc" and parts[6] == "Hz":

                        fc = float(parts[5])
                        logging.info("Filter LP fc=%s", fc)
                        filters.append(
                            Biquad.low_pass(fc, 0.707, fs))
                    elif len(parts) >= 9 and parts[2] == "ON" and \
                            parts[3] == "LPQ" and \
                            parts[4] == "Fc" and parts[6] == "Hz" and \
                            parts[7] == "Q":
                        fc = float(parts[5])
                        q = float(parts[8])
                        logging.info("Filter LPQ fc=%s, q=%s", fc, q)
                        filters.append(
                            Biquad.low_pass(fc, q, fs))
                    elif len(parts) >= 10 and parts[2] == "ON" and \
                            parts[3] == "LS" and \
                            parts[4] == "Fc" and parts[6] == "Hz" and \
                            parts[7] == "Gain" and parts[9] == "dB":
                        fc = float(parts[5])
                        db = float(parts[8])
                        q = 0.707
                        logging.info("Filter LS fc=%s, db=%s", fc, db)
                        filters.append(
                            Biquad.low_shelf(fc, q, db, fs))
                    elif len(parts) >= 6 and parts[2] == "ON" and \
                            parts[3] == "HP" and \
                            parts[4] == "Fc" and parts[6] == "Hz":

                        fc = float(parts[5])
                        logging.info("Filter HP fc=%s", fc)
                        filters.append(
                            Biquad.high_pass(fc, 0.707, fs))
                    elif len(parts) >= 9 and parts[2] == "ON" and \
                            parts[3] == "HPQ" and \
                            parts[4] == "Fc" and parts[6] == "Hz" and \
                            parts[7] == "Q":
                        fc = float(parts[5])
                        q = float(parts[8])
                        logging.info("Filter HPQ fc=%s, q=%s", fc, q)
                        filters.append(
                            Biquad.high_pass(fc, q, fs))
                    elif len(parts) >= 10 and parts[2] == "ON" and \
                            parts[3] == "HS" and \
                            parts[4] == "Fc" and parts[6] == "Hz" and \
                            parts[7] == "Gain" and parts[9] == "dB":
                        fc = float(parts[5])
                        db = float(parts[8])
                        q = 0.707
                        logging.info("Filter HS fc=%s, db=%s", fc, db)
                        filters.append(
                            Biquad.high_shelf(fc, q, db, fs))
                    elif len(parts) >= 7 and parts[2] == "ON" and \
                            parts[3] == "NO" and \
                            parts[4] == "Fc" and parts[6] == "Hz":
                        fc = float(parts[5])
                        q = 0.707
                        logging.info("Filter NO fc=%s q=%s", fc, q)
                        filters.append(
                            Biquad.notch(fc, q, fs))

                    else:
                        if len(parts) >= 4 and parts[2] != "OFF" and parts[3] != "None":
                            print("Filter type " + parts[3] +
                                  " not yet supported")

            return filters