File: audump.py

package info (click to toggle)
rust-speakersafetyd 1.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: python: 460; makefile: 45; sh: 12
file content (303 lines) | stat: -rw-r--r-- 10,018 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import struct, sys, os.path, plistlib, pprint

LABELS = {
    "spp3": {
        0: {
            0: "thermal protection enabled",
            1: "displacement protection enabled",
            2: "thermal/power control gain attack time (s)",
            3: "thermal/power control gain release time (s)",
            4: "ambient temperature",
            5: "SafeTlim",
            6: "SafeTlimTimeMin",
            7: "SafeTlimOffset",
            8: "LookaheadDelay_ms",
            9: "peak attack time (s)",
            10: "peak decay time (s)",
            11: "feedback integration time",
            12: "thermal gain (dB)",
            13: "displacement gain (dB)",
            14: "spk pwr averaging window time (s)",
            15: "modeled speaker power",
            16: "measured speaker power",
            17: "power control gain",
            18: "CPMS power control enabled",
            19: "CPMS power control closed loop",
        },
        4: {
            0: "temperature limit",
            1: "hard temp limit headroom",
            2: "T_sett_vc",
            3: "T_sett_mg",
            4: "tau_Tvc",
            5: "tau_Tmg",
            6: "ThermalFFSpeedupFactor",
            7: "temperature",
            8: "OL temperature",
            9: "Reb_ref",
            10: "Rshunt",
            11: "Rampout",
            12: "mt",
            13: "ct",
            14: "kt",
            15: "ag",
            16: "g_bw",
            17: "Q_d",
            18: "phi",
            19: "x_lim",
            20: "ThermalMeasurementMethod",
            21: "pilot tone enabled",
            22: "CL thermal feedback enabled",
            23: "TlimErrDecayTime",
            24: "TempSenseWindowTime",
            25: "TempSenseSmoothingTau",
            26: "a_t_inv",
            27: "PilotAmplHi_dB",
            28: "PilotAmplLo_dB",
            29: "PilotUpperThres",
            30: "PilotLowerThres",
            31: "PilotDecayTime",
            32: "PilotFreq",
            33: "LPMLSPreGain",
            34: "LPMLSPostGain",
            35: "LPMLSLowerCorner",
            36: "LPMLS pre clip level",
            37: "mu_Re",
            38: "mu_Le",
            39: "mu mechanical (PU)",
            40: "Max relative displacement",
            41: "abs(Min relative displacement)",
            42: "DisplacementProtectionType",
            64: "thermal gain",
            65: "displacement gain",
            66: "power control gain",
            67: "PilotDecayTimeStage2",
            68: "PilotEnableThres",
        },
    },
    "atsp": {
        0: {
            0: "Bypass",
            40: "Gain link all audio channels",
            1: "speakerType A: Amplifier sensitivity [V/Fs]",
            2: "speakerType A: VoiceCoil: DC resistance [Ohms]",
            3: "speakerType A: VoiceCoil: thermal resistance [C/Watt]",
            4: "speakerType A: Voice Coil: thermal time constant [s]",
            5: "speakerType A: Magnet: thermal resistance  [C/Watt]",
            6: "speakerType A: Magnet: thermal time constant [s]",
            7: "speakerType A: Ambient temperature, [C]",
            # The target temperature of the speakers
            8: "speakerType A: Temperature limit [C]",
            9: "speakerType A: Attack time (ms)",
            10: "speakerType A: Release time (ms)",
            11: "speakerType A: Temperature hard limit headroom [C]",
            12: "speakerType A: Gain link",
            13: "speakerType A: Audio channel assignment",
            14: "speakerType B: Amplifier sensitivity [V/Fs",
            15: "speakerType B: VoiceCoil: DC resistance [Ohms]",
            16: "speakerType B: VoiceCoil: thermal resistance [C/Watt]",
            17: "speakerType B: Voice Coil: thermal time constant [s]",
            18: "speakerType B: Magnet: thermal resistance  [C/Watt]",
            19: "speakerType B: Magnet: thermal time constant [s]",
            20: "speakerType B: Ambient temperature, [C]",
            21: "speakerType B: Temperature limit [C]",
            22: "speakerType B: Attack time (ms)",
            23: "speakerType B: Release time (ms)",
            24: "speakerType B: Temperature hard limit headroom [C]",
            25: "speakerType B: Gain link",
            26: "speakerType B: Audio channel assignment",
            27: "speakerType C: Amplifier sensitivity [V/Fs]",
            28: "speakerType C: VoiceCoil: DC resistance [Ohms]",
            29: "speakerType C: VoiceCoil: thermal resistance [C/Watt]",
            30: "speakerType C: Voice Coil: thermal time constant [s]",
            31: "speakerType C: Magnet: thermal resistance  [C/Watt]",
            32: "speakerType C: Magnet: thermal time constant [s]",
            33: "speakerType C: Ambient temperature, [C]",
            34: "speakerType C: Temperature limit [C]",
            35: "speakerType C: Attack time (ms)",
            36: "speakerType C: Release time (ms)",
            37: "speakerType C: Temperature hard limit headroom [C]",
            38: "speakerType C: Gain link",
            39: "speakerType C: Audio channel assignment",
        }
    }
}
        
"""
ATSP protection behavior:

Max gain reduction is 20dB.
"Temperature limit" is the target temperature
If temperature exceeds limit + "Temperature hard limit headroom",
protection goes into panic mode and triggers 20dB reduction.

For settings:
    amp = 12 r = 4
    rVc = 50 aVc = 2 rMg = 1 aMg = 1
    Ta = 50 Tlim = 150 Theadroom = 5

We see this limiter behavior:
In      Out
0       -9.7
-8      -9.7
-9      -9.6
-9.5    -9.7
-9.8    -9.9
-10     -10

In other words, it behaves like a hard limit / compressor with infinite ratio.

Theadroom has no influence on the gain reduction, it just affects stability
(temperature does exceed Tlim transiently, if the transient is > Theadroom
 it panics). Too low a Theadroom leads to unstable behavior.
"""


def dump_audata(labels, data):
    top = {}
    while data:
        hdr = data[:0xc]
        data = data[0xc:]
        typ, grp, cnt = struct.unpack(">III", hdr)
        d = {}
        for i in range(cnt):
            blk = data[:0x8]
            data = data[0x8:]
            key, val = struct.unpack(">If", blk)
            if typ in labels:
                if key in labels[typ]:
                    key = labels[typ][key]
            d[key] = val
        top[(typ, grp)] = d
    pprint.pprint(top, stream=sys.stderr)
    return top

def process_spp3(e):
    # Grab the plist file, which is mostly redundant but contains
    # some details not in the au preset
    for i in prop["Boxes"]:
        if i["Name"] == e["displayname"]:
            for p in i["Properties"]:
                if p["Number"] == 64003:
                    path = os.path.join(base, "DSP", p["Path"].split("/DSP/")[1])

    pl = plistlib.load(open(path, "rb"))

    d = dump_audata(LABELS["spp3"], e["aupreset"]["data"])
    spkrs = ""
    channels = 0
    gbl = d[(0, 0)]
    for (typ, ch), p in sorted(d.items()):
        if typ != 4:
            continue
        chp = pl["ChannelSpecificParams"][f"Channel{ch}"]
        channels += 2
        spkrs += f"""

[Speaker/{chp["SpeakerName"]}]
group = {chp["SpeakerGroup"]}
tr_coil = {p["T_sett_vc"]:.2f}
tr_magnet = {p["T_sett_mg"]:.2f}
tau_coil = {p["tau_Tvc"]:.2f}
tau_magnet = {p["tau_Tmg"]:.2f}
t_limit = {p["temperature limit"]:.1f}
t_headroom = {p["hard temp limit headroom"]:.1f}
z_nominal = {p["Reb_ref"]:.2f}
z_shunt = {p["Rshunt"]:.2f}
a_t_20c = {chp["CL"]["a_t_20C"]:.8f}
a_t_35c = {chp["CL"]["a_t_35C"]:.8f}
is_scale = 3.75
vs_scale = 14
is_chan = {2 * ch}
vs_chan = {2 * ch + 1}"""

    print(f"""\
[Globals]
visense_pcm = 2
t_ambient = {gbl["ambient temperature"]}
t_hysteresis = 5.0
t_window = 20.0
channels = {channels}
period = 4096
link_gains = True
uclamp_max = 64

[Controls]
vsense = VSENSE Switch
isense = ISENSE Switch
amp_gain = Amp Gain Volume
volume = Speaker Volume{spkrs}""")

def process_atsp(e):
    # print(e)
    d = dump_audata(LABELS["atsp"], e["aupreset"]["data"])[(0,0)]
    t_ambient = None
    
    spkrs = ""
    channels = 0
    for gid, gn in enumerate("ABC"):
        p = f"speakerType {gn}: "
        ch = int(d[p + "Audio channel assignment"])
        if not ch:
            continue
        if ch == 0xffff:
            ch = 1
        
        ambient = d[p + "Ambient temperature, [C]"]
        assert t_ambient is None or t_ambient == ambient
        t_ambient = ambient

        for i in range(16):
            if ch & (1 << i):
                channels += 2
                spkrs += f"""

[Speaker/{gn}_ch{i}]
group = {gid}
tr_coil = {d[p + "VoiceCoil: thermal resistance [C/Watt]"]:.2f}
tr_magnet = {d[p + "Magnet: thermal resistance  [C/Watt]"]:.2f}
tau_coil = {d[p + "Voice Coil: thermal time constant [s]"]:.2f}
tau_magnet = {d[p + "Magnet: thermal time constant [s]"]:.2f}
t_limit = {d[p + "Temperature limit [C]"]:.1f}
t_headroom = {d[p + "Temperature hard limit headroom [C]"]:.1f}
z_nominal = {d[p + "VoiceCoil: DC resistance [Ohms]"]:.2f}
a_t_20c = 0.0037
a_t_35c = 0.0037
is_scale = 3.75
vs_scale = 14
is_chan = {2 * i}
vs_chan = {2 * i + 1}"""

    print(f"""\
[Globals]
visense_pcm = 2
t_ambient = {t_ambient}
t_hysteresis = 5.0
t_window = 20.0
channels = {channels}
period = 4096
link_gains = {bool(d["Gain link all audio channels"])}
uclamp_max = 64

[Controls]
vsense = VSENSE Switch
isense = ISENSE Switch
amp_gain = Amp Gain Volume
volume = Speaker Volume{spkrs}""")

if __name__ == "__main__":
    base = sys.argv[1]

    au = plistlib.load(open(os.path.join(base, "DSP/Strips/builtin_speaker_out_general.austrip"), "rb"))
    try:
        prop = plistlib.load(open(os.path.join(base, "DSP/Strips/builtin_speaker_out_general.propstrip"), "rb"))
    except:
        prop = None

    for s in au["strips"]:
        for e in s["effects"]:
            if e["unit"]["subtype"].to_bytes(4) == b"spp3":
                process_spp3(e)
            if e["unit"]["subtype"].to_bytes(4) == b"atsp":
                process_atsp(e)