File: sigmaparams.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 (264 lines) | stat: -rw-r--r-- 9,365 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'''
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 sys
import os.path

from hifiberrydsp.parser.xmlprofile import ATTRIBUTE_BALANCE, \
    ATTRIBUTE_FIR_FILTER_LEFT, ATTRIBUTE_FIR_FILTER_RIGHT, \
    ATTRIBUTE_CUSTOM_FILTER_LEFT, ATTRIBUTE_CUSTOM_FILTER_RIGHT, \
    ATTRIBUTE_TONECONTROL_FILTER_LEFT, ATTRIBUTE_TONECONTROL_FILTER_RIGHT, \
    ATTRIBUTE_VOL_CTL, ATTRIBUTE_VOL_LIMIT, \
    ATTRIBUTE_VOL_LIMIT_PI, ATTRIBUTE_VOL_LIMIT_SPDIF, \
    ATTRIBUTE_VOL_LIMIT_AUX, \
    ATTRIBUTE_MUTE_PI, ATTRIBUTE_MUTE_SPDIF, \
    ATTRIBUTE_MUTE_AUX, ATTRIBUTE_SPDIF_ENABLE, \
    ATTRIBUTE_MUTE_REG, \
    ATTRIBUTE_CHANNEL_SELECT, ATTRIBUTE_INVERT_MUTE, \
    ATTRIBUTE_SPDIF_SOURCE, ATTRIBUTE_AUTOMUTE, ATTRIBUTE_UNMUTE_DELAY, \
    ATTRIBUTE_AUTOMUTE_LEVEL, ATTRIBUTE_INPUT_SELECT,\
    ATTRIBUTE_LOUDNESS, ATTRIBUTE_LOUDNESS_LEVELS, \
    XmlProfile

PARAMETER_MAPPING = {
    "balance": ATTRIBUTE_BALANCE,
    "balancevalue": ATTRIBUTE_BALANCE,
    "loudness.target": ATTRIBUTE_LOUDNESS,
    "loudness.level_low": ATTRIBUTE_LOUDNESS_LEVELS,
    "volume.target": ATTRIBUTE_VOL_CTL,
    "volumelimit.target": ATTRIBUTE_VOL_LIMIT,
    "mastervol.target": ATTRIBUTE_VOL_CTL,
    "masterlimit.target": ATTRIBUTE_VOL_LIMIT,
    "channelselect": ATTRIBUTE_CHANNEL_SELECT,
    "mute": ATTRIBUTE_MUTE_REG,
    "invertmute": ATTRIBUTE_INVERT_MUTE,
    "spdifsource": ATTRIBUTE_SPDIF_SOURCE,
    "fir_l": ATTRIBUTE_FIR_FILTER_LEFT,
    "fir_r": ATTRIBUTE_FIR_FILTER_RIGHT,
    "iir_l": ATTRIBUTE_CUSTOM_FILTER_LEFT,
    "iir_r": ATTRIBUTE_CUSTOM_FILTER_RIGHT,
    "tonecontrol_l": ATTRIBUTE_TONECONTROL_FILTER_LEFT,
    "tonecontrol_r": ATTRIBUTE_TONECONTROL_FILTER_RIGHT,
    "automute": ATTRIBUTE_AUTOMUTE,
    "automutelevel": ATTRIBUTE_AUTOMUTE_LEVEL,
    "unmutedelay": ATTRIBUTE_UNMUTE_DELAY,
    "volumelimitpi.target": ATTRIBUTE_VOL_LIMIT_PI,
    "volumelimitspdif.target": ATTRIBUTE_VOL_LIMIT_SPDIF,
    "volumelimitaux.target": ATTRIBUTE_VOL_LIMIT_AUX,
    "mutepi": ATTRIBUTE_MUTE_PI,
    "mutespdif": ATTRIBUTE_MUTE_SPDIF,
    "muteaux": ATTRIBUTE_MUTE_AUX,
    "enablespdif": ATTRIBUTE_SPDIF_ENABLE,
    "toslinkenable": ATTRIBUTE_SPDIF_ENABLE,
    "inputselect": ATTRIBUTE_INPUT_SELECT,
}


for lr in ["L", "R"]:
    for channel in range(1, 5):
        name = "iir_{}{}".format(lr.lower(), channel)
        attribute = "IIR_%LR%%CHANNEL%".replace(
            "%LR%", lr).replace("%CHANNEL%", str(channel))
        PARAMETER_MAPPING[name] = attribute
        
        
for ch in ["A","B","C","D", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]:
        name = "eq_{}".format(ch.lower())
        attribute = "IIR_%CHANNEL%".replace("%CHANNEL%", ch)
        PARAMETER_MAPPING[name] = attribute
        
        name = "iir_{}".format(ch.lower())
        attribute = "IIR_%CHANNEL%".replace("%CHANNEL%", ch)
        PARAMETER_MAPPING[name] = attribute
        
        name = "delay_{}".format(ch.lower())
        attribute = "delay%CHANNEL%Register".replace("%CHANNEL%", ch)
        PARAMETER_MAPPING[name] = attribute

        name = "vol_{}".format(ch.lower())
        attribute = "levels%CHANNEL%Register".replace("%CHANNEL%", ch)
        PARAMETER_MAPPING[name] = attribute


class SigmastudioParamsFile():
    """
    This class handles metadata in DSP profile files.
    It can parse .params files exported by SigmaStudio and automatically create metadata records
    in a XML DSP profile based on standard naming.
    """

    def __init__(self, filename):
        """
        Opens a .params file and parse it
        """
        self.parameter_start_address = {}
        self.parameter_end_address = {}

        cellname = None
        address = None
        plen = 0
        pdata = False

        with open(filename) as params:
            for line in params:
                try:
                    name, value = line.split("=")
                    name = name.strip().lower()
                    value = value.strip().lower()

                    if name == "cell name":
                        cellname = value

                    if name == "parameter name":
                        paramname = value

                    if name == "parameter address":
                        address = int(value)

                except ValueError:

                    if line.lower().startswith("parameter data :"):
                        pdata = True

                    if line.strip() == "" and cellname is not None:
                        self.process_cell(cellname, paramname, address, plen)
                        cellname = None
                        paramname = None
                        address = None
                        pdata = False
                        plen = 0

                    else:
                        if pdata:
                            if line.startswith("0x"):
                                plen += 1

    def process_cell(self, cellname, paramname, address, length):
        """
        Parses a single cell in the .params file. This usually maps to a 
        DSP building block in SigmaStudio.
        
        Based on the PARAMETER_MAPPING, it will create the required 
        metadata records
        """

        name = cellname.split(".")[-1]
        
        for cell in PARAMETER_MAPPING:
            if "." in cell:
                cell_key, param_key = cell.split(".")
            else:
                cell_key = cell
                param_key = None

            if cell_key == name:
                if param_key is None or paramname.endswith(param_key):

                    attrib = PARAMETER_MAPPING[cell]

                    if attrib in self.parameter_start_address:
                        self.parameter_end_address[attrib] = address
                    else:
                        self.parameter_start_address[attrib] = address

                    if length > 1:
                        self.parameter_end_address[attrib] = address + \
                            length - 1

    def param_list(self):
        """
        Get a list of all parameters that have been loadef from the .params file.
        """
        result = {}
        for param in self.parameter_start_address:
            address = self.parameter_start_address[param]
            if param in self.parameter_end_address:
                end_address = self.parameter_end_address[param]
                length = end_address - address + 1
                address = "{}/{}".format(address, length)
            else:
                address = str(address)

            result[param] = address

        return result

    def merge_params_into_xml(self, xmlfile):
        """
        Add the metadata into a XML DSP project file.
        """
        xml = XmlProfile(xmlfile)
        param_list = self.param_list()
        xml.update_metadata(param_list)
        xml.write_xml(xmlfile)
        return param_list


def basefilename(filename):
    base = os.path.basename(filename)
    return os.path.splitext(base)[0]


def extension(filename):
    base = os.path.basename(filename)
    return os.path.splitext(base)[1]


def merge_params_main(xmlfile=None, paramsfile=None):

    if paramsfile == None:
        # called from command line
        try:
            xmlfile = sys.argv[1]
            paramsfile = sys.argv[2]
        except:
            print("call with {} xmlprofile paramsfile".format(sys.argv[0]))
            sys.exit(1)

    if extension(xmlfile) != ".xml":
        print("DSP profile file does not have the extension xml, aborting")
        sys.exit(1)

    if extension(paramsfile) != ".params":
        print("Parameters file does not have the extension params, aborting")
        sys.exit(1)

    if basefilename(xmlfile) != basefilename(paramsfile):
        print('''Warning: the two files do not share the same base name. If you're merging an incorrect
parameter file into an XML profile, this might damage your system!
        ''')

    try:
        pf = SigmastudioParamsFile(paramsfile)
    except IOError as e:
        print("can't read {} ({})".format(paramsfile, e))
        sys.exit(1)

    try:
        params = pf.merge_params_into_xml(xmlfile)
    except IOError as e:
        print("can't read or write {} ({})".format(xmlfile, e))
        sys.exit(1)

    print("added parameters to XML profile:")
    for param in sorted(params):
        print(" ", param)