led-controller-version-3-effect-creation 3.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- led_controller_version_3_effect_creation-3.0.2.dist-info/METADATA +144 -0
- led_controller_version_3_effect_creation-3.0.2.dist-info/RECORD +25 -0
- led_controller_version_3_effect_creation-3.0.2.dist-info/WHEEL +4 -0
- led_controller_version_3_effect_creation-3.0.2.dist-info/entry_points.txt +3 -0
- lefx/effect_creation/__init__.py +54 -0
- lefx/effect_creation/build.py +217 -0
- lefx/effect_creation/cli.py +156 -0
- lefx/effect_creation/imports.py +143 -0
- lefx/effect_creation/scaffold.py +330 -0
- lefx/effect_creation/source.py +258 -0
- lefx/effect_creation/studio/__init__.py +95 -0
- lefx/effect_creation/studio/app.py +169 -0
- lefx/effect_creation/studio/authoring.py +196 -0
- lefx/effect_creation/studio/blueprint.py +759 -0
- lefx/effect_creation/studio/calibrate.py +185 -0
- lefx/effect_creation/studio/calibration_page.py +420 -0
- lefx/effect_creation/studio/catalogue.py +219 -0
- lefx/effect_creation/studio/parameters.py +730 -0
- lefx/effect_creation/studio/preset_dialog.py +188 -0
- lefx/effect_creation/studio/project.py +302 -0
- lefx/effect_creation/studio/ring.py +172 -0
- lefx/effect_creation/studio/session.py +307 -0
- lefx/effect_creation/studio/source_editor.py +902 -0
- lefx/effect_creation/studio/window.py +656 -0
- lefx/effect_creation/validate.py +263 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""Working out how a microphone array is rotated against its LED ring.
|
|
2
|
+
|
|
3
|
+
The procedure is the one a person would do by hand, made repeatable: light a
|
|
4
|
+
known bearing, speak from it, write down what the device measured, and do that
|
|
5
|
+
often enough that the noise averages out. What comes back is the difference
|
|
6
|
+
between the two zeros — a property of the board, which is why the answer belongs
|
|
7
|
+
to the device and not to any effect.
|
|
8
|
+
|
|
9
|
+
Angles do not average the way numbers do. The mean of 350° and 10° is 0°, not
|
|
10
|
+
180°, and a calibration computed with an arithmetic mean would be quietly wrong
|
|
11
|
+
for exactly the bearings that cross the seam. So everything here goes through
|
|
12
|
+
unit vectors.
|
|
13
|
+
|
|
14
|
+
No Qt: this is the part with the arithmetic in it.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import math
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
|
|
22
|
+
from lefx.sdk import DoaCalibration
|
|
23
|
+
|
|
24
|
+
MIN_SAMPLES = 3
|
|
25
|
+
"""Below this a "calibration" is one reading and a rounding error."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def circular_mean(angles: list[float]) -> float:
|
|
29
|
+
"""The average direction, on a circle where 359° and 1° are two degrees apart."""
|
|
30
|
+
if not angles:
|
|
31
|
+
raise ValueError("no angles to average")
|
|
32
|
+
x = sum(math.cos(math.radians(angle)) for angle in angles)
|
|
33
|
+
y = sum(math.sin(math.radians(angle)) for angle in angles)
|
|
34
|
+
if abs(x) < 1e-12 and abs(y) < 1e-12:
|
|
35
|
+
# Readings spread evenly around the circle have no mean direction. Any
|
|
36
|
+
# answer here would be arbitrary, and arbitrary is what a calibration
|
|
37
|
+
# must never be.
|
|
38
|
+
raise ValueError("these angles have no mean direction")
|
|
39
|
+
degrees = math.degrees(math.atan2(y, x)) % 360.0
|
|
40
|
+
# A mean of 350° and 10° is zero, and floating point reaches it from below:
|
|
41
|
+
# a hair under 360 wrapped to just under 360 rather than to 0. Say zero.
|
|
42
|
+
return 0.0 if degrees >= 360.0 - 1e-9 else degrees
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def circular_spread(angles: list[float]) -> float:
|
|
46
|
+
"""How far apart the readings are, in degrees, as one number.
|
|
47
|
+
|
|
48
|
+
Zero when they all agree, rising towards 90° as they scatter. This is what
|
|
49
|
+
says whether a calibration is worth keeping: a tight cluster means the
|
|
50
|
+
device heard the same direction every time, and a wide one means it did not
|
|
51
|
+
and the mean of it is not a measurement.
|
|
52
|
+
"""
|
|
53
|
+
if not angles:
|
|
54
|
+
return 0.0
|
|
55
|
+
x = sum(math.cos(math.radians(angle)) for angle in angles) / len(angles)
|
|
56
|
+
y = sum(math.sin(math.radians(angle)) for angle in angles) / len(angles)
|
|
57
|
+
strength = math.hypot(x, y)
|
|
58
|
+
if strength >= 1.0:
|
|
59
|
+
return 0.0
|
|
60
|
+
# The circular standard deviation, which is the honest version of "spread"
|
|
61
|
+
# for directions. It grows without bound as readings scatter, so it is
|
|
62
|
+
# capped at 180°: beyond that the number stops meaning anything a person
|
|
63
|
+
# could act on, and "as disagreeing as two directions can be" is the answer.
|
|
64
|
+
deviation = math.degrees(math.sqrt(-2.0 * math.log(max(strength, 1e-12))))
|
|
65
|
+
return min(180.0, deviation)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def difference(expected_deg: float, measured_deg: float) -> float:
|
|
69
|
+
"""How far the ring's bearing is from the array's, as a rotation."""
|
|
70
|
+
return (expected_deg - measured_deg) % 360.0
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass(slots=True, frozen=True)
|
|
74
|
+
class Sample:
|
|
75
|
+
"""One reading: where the light was, and what the device heard."""
|
|
76
|
+
|
|
77
|
+
expected_deg: float
|
|
78
|
+
"""The bearing the probe was lighting — known, because we chose it."""
|
|
79
|
+
|
|
80
|
+
measured_deg: float
|
|
81
|
+
"""What the device reported, with no calibration applied."""
|
|
82
|
+
|
|
83
|
+
def residual(self, calibration: DoaCalibration) -> float:
|
|
84
|
+
"""How wrong this reading still is once a calibration is applied.
|
|
85
|
+
|
|
86
|
+
Signed and folded into ±180, so residuals average to zero when the fit
|
|
87
|
+
is right rather than to 180 when it is exactly opposite.
|
|
88
|
+
"""
|
|
89
|
+
corrected = calibration.apply(self.measured_deg)
|
|
90
|
+
return (corrected - self.expected_deg + 180.0) % 360.0 - 180.0
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(slots=True, frozen=True)
|
|
94
|
+
class Fit:
|
|
95
|
+
"""What a set of samples says the calibration should be."""
|
|
96
|
+
|
|
97
|
+
calibration: DoaCalibration
|
|
98
|
+
spread_deg: float
|
|
99
|
+
"""The scatter of the underlying rotations. Small means the device agreed
|
|
100
|
+
with itself; large means the samples are not describing one rotation."""
|
|
101
|
+
|
|
102
|
+
residual_deg: float
|
|
103
|
+
"""The worst remaining error once the calibration is applied."""
|
|
104
|
+
|
|
105
|
+
samples: tuple[Sample, ...] = field(default_factory=tuple)
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def trustworthy(self) -> bool:
|
|
109
|
+
"""Whether this is a measurement or a shrug.
|
|
110
|
+
|
|
111
|
+
Half an LED on a twelve-LED ring is 15°, which is also the resolution
|
|
112
|
+
the ring can express. A fit scattered wider than that is not saying
|
|
113
|
+
anything the ring could show.
|
|
114
|
+
"""
|
|
115
|
+
return len(self.samples) >= MIN_SAMPLES and self.spread_deg <= 15.0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def fit_calibration(samples: list[Sample], *, allow_reverse: bool = True) -> Fit:
|
|
119
|
+
"""The rotation that best explains the readings, and how well it does.
|
|
120
|
+
|
|
121
|
+
Both senses are tried when ``allow_reverse``. Which way an array counts is
|
|
122
|
+
not something to be assumed from a datasheet, and the samples answer it for
|
|
123
|
+
free: with the wrong sense, no single rotation lines the readings up, and
|
|
124
|
+
the scatter says so.
|
|
125
|
+
"""
|
|
126
|
+
if len(samples) < MIN_SAMPLES:
|
|
127
|
+
raise ValueError(f"a calibration needs at least {MIN_SAMPLES} readings")
|
|
128
|
+
|
|
129
|
+
candidates: list[Fit] = []
|
|
130
|
+
for reverse in (False, True) if allow_reverse else (False,):
|
|
131
|
+
rotations = [
|
|
132
|
+
difference(
|
|
133
|
+
sample.expected_deg,
|
|
134
|
+
(-sample.measured_deg) % 360.0 if reverse else sample.measured_deg,
|
|
135
|
+
)
|
|
136
|
+
for sample in samples
|
|
137
|
+
]
|
|
138
|
+
try:
|
|
139
|
+
offset = circular_mean(rotations)
|
|
140
|
+
except ValueError:
|
|
141
|
+
continue
|
|
142
|
+
calibration = DoaCalibration(angle_offset_deg=offset, reverse=reverse)
|
|
143
|
+
candidates.append(
|
|
144
|
+
Fit(
|
|
145
|
+
calibration=calibration,
|
|
146
|
+
spread_deg=circular_spread(rotations),
|
|
147
|
+
residual_deg=max(abs(sample.residual(calibration)) for sample in samples),
|
|
148
|
+
samples=tuple(samples),
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if not candidates:
|
|
153
|
+
raise ValueError("these readings do not describe a rotation")
|
|
154
|
+
return min(candidates, key=lambda fit: fit.spread_deg)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def sector_angle(sector: int, led_count: int) -> float:
|
|
158
|
+
"""The bearing at the centre of one of the ``2 * led_count`` sectors."""
|
|
159
|
+
sectors = 2 * led_count
|
|
160
|
+
return (sector % sectors) * 360.0 / sectors
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def suggested_sectors(led_count: int, *, count: int = 8) -> list[int]:
|
|
164
|
+
"""Which sectors to walk, spread evenly around the ring.
|
|
165
|
+
|
|
166
|
+
Evenly, because a calibration measured only across one side would fit the
|
|
167
|
+
rotation to that side. Fewer than every sector, because the point is to
|
|
168
|
+
cover the circle rather than to stand in twenty-four places.
|
|
169
|
+
"""
|
|
170
|
+
sectors = 2 * led_count
|
|
171
|
+
step = max(1, sectors // max(1, count))
|
|
172
|
+
return sorted({(index * step) % sectors for index in range(min(count, sectors))})
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
__all__ = [
|
|
176
|
+
"MIN_SAMPLES",
|
|
177
|
+
"Fit",
|
|
178
|
+
"Sample",
|
|
179
|
+
"circular_mean",
|
|
180
|
+
"circular_spread",
|
|
181
|
+
"difference",
|
|
182
|
+
"fit_calibration",
|
|
183
|
+
"sector_angle",
|
|
184
|
+
"suggested_sectors",
|
|
185
|
+
]
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"""The calibration page: light a bearing, speak from it, keep the answer.
|
|
2
|
+
|
|
3
|
+
The measurement is only meaningful against *raw* readings, so while a run is in
|
|
4
|
+
progress the device's calibration is set aside and put back afterwards. The
|
|
5
|
+
alternative — measuring through the calibration being replaced — would converge
|
|
6
|
+
eventually but would never let you see what the device actually reports, which
|
|
7
|
+
is the one number worth looking at while standing next to it.
|
|
8
|
+
|
|
9
|
+
The ring is driven through ``ring_probe``, an ordinary state in the catalogue,
|
|
10
|
+
rather than by writing frames past the engine. There is no back door to the
|
|
11
|
+
device here for the same reason there is none anywhere else: the path the
|
|
12
|
+
calibration is measured over has to be the path everything else uses, or it
|
|
13
|
+
would be calibrating something slightly different from what runs afterwards.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import logging
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from PySide6.QtCore import Qt, QTimer, Signal
|
|
22
|
+
from PySide6.QtWidgets import (
|
|
23
|
+
QCheckBox,
|
|
24
|
+
QDoubleSpinBox,
|
|
25
|
+
QGroupBox,
|
|
26
|
+
QHBoxLayout,
|
|
27
|
+
QLabel,
|
|
28
|
+
QListWidget,
|
|
29
|
+
QMessageBox,
|
|
30
|
+
QProgressBar,
|
|
31
|
+
QPushButton,
|
|
32
|
+
QSpinBox,
|
|
33
|
+
QVBoxLayout,
|
|
34
|
+
QWidget,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
from lefx.sdk import DoaCalibration, calibration_path, save_calibration
|
|
38
|
+
|
|
39
|
+
from .calibrate import Fit, Sample, fit_calibration, sector_angle, suggested_sectors
|
|
40
|
+
from .ring import RingMonitor
|
|
41
|
+
from .session import NULL_OUTPUT, StudioSession
|
|
42
|
+
|
|
43
|
+
logger = logging.getLogger("lefx.effect_creation.studio.calibration")
|
|
44
|
+
|
|
45
|
+
PROBE_EFFECT = "ring_probe"
|
|
46
|
+
SAMPLE_INTERVAL_MS = 100
|
|
47
|
+
"""How often a reading is taken while a sector is being measured."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class CalibrationPage(QWidget):
|
|
51
|
+
"""Walk the ring in half-LED steps and work out how the array is rotated."""
|
|
52
|
+
|
|
53
|
+
frame_received = Signal(tuple)
|
|
54
|
+
|
|
55
|
+
def __init__(self, session: StudioSession, parent: QWidget | None = None) -> None:
|
|
56
|
+
super().__init__(parent)
|
|
57
|
+
self.session = session
|
|
58
|
+
self.samples: list[Sample] = []
|
|
59
|
+
self.queue: list[int] = []
|
|
60
|
+
self.current_sector: int | None = None
|
|
61
|
+
self.readings: list[float] = []
|
|
62
|
+
self.suspended: DoaCalibration | None = None
|
|
63
|
+
self.fit: Fit | None = None
|
|
64
|
+
|
|
65
|
+
self.timer = QTimer(self)
|
|
66
|
+
self.timer.setInterval(SAMPLE_INTERVAL_MS)
|
|
67
|
+
self.timer.timeout.connect(self._take_reading)
|
|
68
|
+
|
|
69
|
+
self._build()
|
|
70
|
+
self.frame_received.connect(self.monitor.set_colors)
|
|
71
|
+
|
|
72
|
+
# -- construction -------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
def _build(self) -> None:
|
|
75
|
+
layout = QHBoxLayout(self)
|
|
76
|
+
|
|
77
|
+
self.monitor = RingMonitor(self.session.led_count)
|
|
78
|
+
self.monitor.set_show_sectors(True)
|
|
79
|
+
layout.addWidget(self.monitor, stretch=3)
|
|
80
|
+
|
|
81
|
+
side = QVBoxLayout()
|
|
82
|
+
side.addWidget(self._build_setup())
|
|
83
|
+
side.addWidget(self._build_run())
|
|
84
|
+
side.addWidget(self._build_result())
|
|
85
|
+
side.addStretch(1)
|
|
86
|
+
layout.addLayout(side, stretch=2)
|
|
87
|
+
|
|
88
|
+
def _build_setup(self) -> QGroupBox:
|
|
89
|
+
box = QGroupBox("Messpunkte")
|
|
90
|
+
layout = QVBoxLayout(box)
|
|
91
|
+
|
|
92
|
+
row = QHBoxLayout()
|
|
93
|
+
row.addWidget(QLabel("Richtungen"))
|
|
94
|
+
self.point_count = QSpinBox()
|
|
95
|
+
self.point_count.setRange(3, 2 * self.session.led_count)
|
|
96
|
+
self.point_count.setValue(8)
|
|
97
|
+
self.point_count.setToolTip(
|
|
98
|
+
"Gleichmäßig über den Ring verteilt. Weniger als der ganze Kreis reicht, "
|
|
99
|
+
"aber nur eine Seite zu messen legt die Drehung auf diese Seite fest."
|
|
100
|
+
)
|
|
101
|
+
row.addWidget(self.point_count)
|
|
102
|
+
layout.addLayout(row)
|
|
103
|
+
|
|
104
|
+
row = QHBoxLayout()
|
|
105
|
+
row.addWidget(QLabel("Messdauer je Punkt"))
|
|
106
|
+
self.dwell = QDoubleSpinBox()
|
|
107
|
+
self.dwell.setRange(1.0, 20.0)
|
|
108
|
+
self.dwell.setValue(4.0)
|
|
109
|
+
self.dwell.setSuffix(" s")
|
|
110
|
+
row.addWidget(self.dwell)
|
|
111
|
+
layout.addLayout(row)
|
|
112
|
+
|
|
113
|
+
self.allow_reverse = QCheckBox("Zählrichtung mitbestimmen")
|
|
114
|
+
self.allow_reverse.setChecked(True)
|
|
115
|
+
self.allow_reverse.setToolTip(
|
|
116
|
+
"Beide Drehsinne durchrechnen und den nehmen, der die Messungen erklärt."
|
|
117
|
+
)
|
|
118
|
+
layout.addWidget(self.allow_reverse)
|
|
119
|
+
return box
|
|
120
|
+
|
|
121
|
+
def _build_run(self) -> QGroupBox:
|
|
122
|
+
box = QGroupBox("Messung")
|
|
123
|
+
layout = QVBoxLayout(box)
|
|
124
|
+
|
|
125
|
+
self.instruction = QLabel("Ausgabe wählen und starten.")
|
|
126
|
+
self.instruction.setWordWrap(True)
|
|
127
|
+
self.instruction.setStyleSheet("font-size: 14px;")
|
|
128
|
+
layout.addWidget(self.instruction)
|
|
129
|
+
|
|
130
|
+
self.progress = QProgressBar()
|
|
131
|
+
self.progress.setTextVisible(True)
|
|
132
|
+
layout.addWidget(self.progress)
|
|
133
|
+
|
|
134
|
+
self.live = QLabel("—")
|
|
135
|
+
self.live.setStyleSheet("color: #888;")
|
|
136
|
+
layout.addWidget(self.live)
|
|
137
|
+
|
|
138
|
+
buttons = QHBoxLayout()
|
|
139
|
+
self.start_button = QPushButton("Start")
|
|
140
|
+
self.start_button.clicked.connect(self._start)
|
|
141
|
+
self.skip_button = QPushButton("Punkt überspringen")
|
|
142
|
+
self.skip_button.clicked.connect(self._finish_sector)
|
|
143
|
+
self.skip_button.setEnabled(False)
|
|
144
|
+
self.cancel_button = QPushButton("Abbrechen")
|
|
145
|
+
self.cancel_button.clicked.connect(self._cancel)
|
|
146
|
+
self.cancel_button.setEnabled(False)
|
|
147
|
+
buttons.addWidget(self.start_button)
|
|
148
|
+
buttons.addWidget(self.skip_button)
|
|
149
|
+
buttons.addWidget(self.cancel_button)
|
|
150
|
+
layout.addLayout(buttons)
|
|
151
|
+
|
|
152
|
+
self.collected = QListWidget()
|
|
153
|
+
self.collected.setMaximumHeight(140)
|
|
154
|
+
layout.addWidget(self.collected)
|
|
155
|
+
return box
|
|
156
|
+
|
|
157
|
+
def _build_result(self) -> QGroupBox:
|
|
158
|
+
box = QGroupBox("Ergebnis")
|
|
159
|
+
layout = QVBoxLayout(box)
|
|
160
|
+
|
|
161
|
+
self.result = QLabel("Noch nichts gemessen.")
|
|
162
|
+
self.result.setWordWrap(True)
|
|
163
|
+
layout.addWidget(self.result)
|
|
164
|
+
|
|
165
|
+
self.current = QLabel("")
|
|
166
|
+
self.current.setStyleSheet("color: #888;")
|
|
167
|
+
self.current.setWordWrap(True)
|
|
168
|
+
layout.addWidget(self.current)
|
|
169
|
+
|
|
170
|
+
buttons = QHBoxLayout()
|
|
171
|
+
self.save_button = QPushButton("Speichern")
|
|
172
|
+
self.save_button.clicked.connect(self._save)
|
|
173
|
+
self.save_button.setEnabled(False)
|
|
174
|
+
self.apply_button = QPushButton("Nur übernehmen")
|
|
175
|
+
self.apply_button.setToolTip("Für diese Sitzung anwenden, ohne sie zu hinterlegen")
|
|
176
|
+
self.apply_button.clicked.connect(lambda: self._apply(persist=False))
|
|
177
|
+
self.apply_button.setEnabled(False)
|
|
178
|
+
buttons.addWidget(self.apply_button)
|
|
179
|
+
buttons.addWidget(self.save_button)
|
|
180
|
+
layout.addLayout(buttons)
|
|
181
|
+
return box
|
|
182
|
+
|
|
183
|
+
# -- what the page knows about the session ------------------------------
|
|
184
|
+
|
|
185
|
+
def refresh(self) -> None:
|
|
186
|
+
"""Called when the page is shown or the output changed."""
|
|
187
|
+
self.monitor.set_led_count(self.session.led_count)
|
|
188
|
+
self.point_count.setMaximum(2 * self.session.led_count)
|
|
189
|
+
provider = self._provider()
|
|
190
|
+
ready = provider is not None and self.session.output_name != NULL_OUTPUT
|
|
191
|
+
self.start_button.setEnabled(ready and not self.timer.isActive())
|
|
192
|
+
if not ready:
|
|
193
|
+
self.instruction.setText(
|
|
194
|
+
"Diese Seite braucht ein Gerät mit Richtungsdaten.\n"
|
|
195
|
+
"Wähle oben eine Ausgabe, die einen doa-Provider mitbringt."
|
|
196
|
+
)
|
|
197
|
+
elif not self.timer.isActive():
|
|
198
|
+
self.instruction.setText(
|
|
199
|
+
f"Bereit. Das Gerät „{self.session.output_name}“ liefert Richtungsdaten."
|
|
200
|
+
)
|
|
201
|
+
self._show_current_calibration()
|
|
202
|
+
|
|
203
|
+
def _provider(self) -> Any | None:
|
|
204
|
+
if self.session.service is None:
|
|
205
|
+
return None
|
|
206
|
+
return self.session.service.providers.get("doa")
|
|
207
|
+
|
|
208
|
+
def _show_current_calibration(self) -> None:
|
|
209
|
+
provider = self._provider()
|
|
210
|
+
if provider is None:
|
|
211
|
+
self.current.setText("")
|
|
212
|
+
return
|
|
213
|
+
calibration = self.suspended or getattr(provider, "calibration", DoaCalibration())
|
|
214
|
+
where = self._calibration_path()
|
|
215
|
+
self.current.setText(
|
|
216
|
+
f"Aktuell am Gerät: {calibration.angle_offset_deg:.1f}°"
|
|
217
|
+
f"{', gespiegelt' if calibration.reverse else ''}\nDatei: {where}"
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
def _calibration_path(self):
|
|
221
|
+
"""The project's calibration file, or the usual place without one."""
|
|
222
|
+
project = getattr(self.session, "project", None)
|
|
223
|
+
return project.calibration_file if project is not None else calibration_path()
|
|
224
|
+
|
|
225
|
+
# -- the run ------------------------------------------------------------
|
|
226
|
+
|
|
227
|
+
def _start(self) -> None:
|
|
228
|
+
provider = self._provider()
|
|
229
|
+
if provider is None:
|
|
230
|
+
return
|
|
231
|
+
if PROBE_EFFECT not in self.session.registry.effects:
|
|
232
|
+
QMessageBox.warning(
|
|
233
|
+
self,
|
|
234
|
+
"Sonde fehlt",
|
|
235
|
+
f"„{PROBE_EFFECT}“ ist nicht geladen. Es gehört zum core-set — "
|
|
236
|
+
"einmal 'uv run python scripts/build_effects.py' und Quellen neu laden.",
|
|
237
|
+
)
|
|
238
|
+
return
|
|
239
|
+
|
|
240
|
+
# Measure what the device actually reports. Calibrating through a
|
|
241
|
+
# calibration would still converge, but you could never read the raw
|
|
242
|
+
# bearing off the screen while standing next to the thing.
|
|
243
|
+
self.suspended = getattr(provider, "calibration", DoaCalibration())
|
|
244
|
+
provider.calibration = DoaCalibration()
|
|
245
|
+
|
|
246
|
+
self.samples.clear()
|
|
247
|
+
self.collected.clear()
|
|
248
|
+
self.fit = None
|
|
249
|
+
self.queue = suggested_sectors(self.session.led_count, count=self.point_count.value())
|
|
250
|
+
self.progress.setRange(0, len(self.queue))
|
|
251
|
+
self.progress.setValue(0)
|
|
252
|
+
self.save_button.setEnabled(False)
|
|
253
|
+
self.apply_button.setEnabled(False)
|
|
254
|
+
self.start_button.setEnabled(False)
|
|
255
|
+
self.skip_button.setEnabled(True)
|
|
256
|
+
self.cancel_button.setEnabled(True)
|
|
257
|
+
self._next_sector()
|
|
258
|
+
|
|
259
|
+
def _next_sector(self) -> None:
|
|
260
|
+
if not self.queue:
|
|
261
|
+
self._conclude()
|
|
262
|
+
return
|
|
263
|
+
self.current_sector = self.queue.pop(0)
|
|
264
|
+
self.readings.clear()
|
|
265
|
+
|
|
266
|
+
angle = sector_angle(self.current_sector, self.session.led_count)
|
|
267
|
+
self.monitor.mark_sector(self.current_sector)
|
|
268
|
+
try:
|
|
269
|
+
self.session.play_state(
|
|
270
|
+
PROBE_EFFECT,
|
|
271
|
+
{"direction_deg": angle, "color": "#FFB000", "brightness": 1.0},
|
|
272
|
+
)
|
|
273
|
+
except Exception as exc:
|
|
274
|
+
logger.warning("could not light the probe: %s", exc)
|
|
275
|
+
|
|
276
|
+
self.instruction.setText(
|
|
277
|
+
f"Sprich aus Richtung {angle:.0f}° — dort, wo der Ring gerade leuchtet."
|
|
278
|
+
)
|
|
279
|
+
self._deadline = self.dwell.value() * 1000.0
|
|
280
|
+
self._elapsed = 0.0
|
|
281
|
+
self.timer.start()
|
|
282
|
+
|
|
283
|
+
def _take_reading(self) -> None:
|
|
284
|
+
provider = self._provider()
|
|
285
|
+
if provider is None:
|
|
286
|
+
self._cancel()
|
|
287
|
+
return
|
|
288
|
+
|
|
289
|
+
reading = provider.sample(_context(self.session))
|
|
290
|
+
if reading is not None and reading.get("direction_deg") is not None:
|
|
291
|
+
# Silence is a valid measurement of the room and a useless one for
|
|
292
|
+
# this: an angle is only a bearing to something while something is
|
|
293
|
+
# making a sound.
|
|
294
|
+
if reading.get("detection_state") in ("sound", "speech"):
|
|
295
|
+
self.readings.append(float(reading["direction_deg"]))
|
|
296
|
+
|
|
297
|
+
self.live.setText(
|
|
298
|
+
f"{len(self.readings)} Messwerte"
|
|
299
|
+
+ (f", zuletzt {self.readings[-1]:.0f}°" if self.readings else ", noch nichts gehört")
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
self._elapsed += SAMPLE_INTERVAL_MS
|
|
303
|
+
if self._elapsed >= self._deadline:
|
|
304
|
+
self._finish_sector()
|
|
305
|
+
|
|
306
|
+
def _finish_sector(self) -> None:
|
|
307
|
+
self.timer.stop()
|
|
308
|
+
if self.current_sector is not None and self.readings:
|
|
309
|
+
from .calibrate import circular_mean, circular_spread
|
|
310
|
+
|
|
311
|
+
expected = sector_angle(self.current_sector, self.session.led_count)
|
|
312
|
+
try:
|
|
313
|
+
measured = circular_mean(self.readings)
|
|
314
|
+
except ValueError:
|
|
315
|
+
measured = None
|
|
316
|
+
if measured is not None:
|
|
317
|
+
self.samples.append(Sample(expected_deg=expected, measured_deg=measured))
|
|
318
|
+
self.collected.addItem(
|
|
319
|
+
f"{expected:6.1f}° → gemessen {measured:6.1f}° "
|
|
320
|
+
f"(±{circular_spread(self.readings):.0f}°, {len(self.readings)} Werte)"
|
|
321
|
+
)
|
|
322
|
+
elif self.current_sector is not None:
|
|
323
|
+
self.collected.addItem(
|
|
324
|
+
f"{sector_angle(self.current_sector, self.session.led_count):6.1f}° → nichts gehört"
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
self.progress.setValue(self.progress.value() + 1)
|
|
328
|
+
self._next_sector()
|
|
329
|
+
|
|
330
|
+
def _conclude(self) -> None:
|
|
331
|
+
self.timer.stop()
|
|
332
|
+
self.monitor.mark_sector(None)
|
|
333
|
+
self.current_sector = None
|
|
334
|
+
self.skip_button.setEnabled(False)
|
|
335
|
+
self.cancel_button.setEnabled(False)
|
|
336
|
+
self.start_button.setEnabled(True)
|
|
337
|
+
self.instruction.setText("Messung beendet.")
|
|
338
|
+
|
|
339
|
+
try:
|
|
340
|
+
self.fit = fit_calibration(self.samples, allow_reverse=self.allow_reverse.isChecked())
|
|
341
|
+
except ValueError as exc:
|
|
342
|
+
self._restore()
|
|
343
|
+
self.result.setText(f"Keine Kalibrierung ableitbar: {exc}")
|
|
344
|
+
return
|
|
345
|
+
|
|
346
|
+
calibration = self.fit.calibration
|
|
347
|
+
verdict = "brauchbar" if self.fit.trustworthy else "zu unruhig — noch einmal messen"
|
|
348
|
+
self.result.setText(
|
|
349
|
+
f"Offset {calibration.angle_offset_deg:.1f}°"
|
|
350
|
+
f"{', gespiegelt' if calibration.reverse else ''}\n"
|
|
351
|
+
f"Streuung {self.fit.spread_deg:.1f}°, größter Restfehler "
|
|
352
|
+
f"{self.fit.residual_deg:.1f}°, {len(self.fit.samples)} Punkte — {verdict}"
|
|
353
|
+
)
|
|
354
|
+
self.save_button.setEnabled(True)
|
|
355
|
+
self.apply_button.setEnabled(True)
|
|
356
|
+
self._restore()
|
|
357
|
+
|
|
358
|
+
def _cancel(self) -> None:
|
|
359
|
+
self.timer.stop()
|
|
360
|
+
self.monitor.mark_sector(None)
|
|
361
|
+
self.current_sector = None
|
|
362
|
+
self.queue.clear()
|
|
363
|
+
self.skip_button.setEnabled(False)
|
|
364
|
+
self.cancel_button.setEnabled(False)
|
|
365
|
+
self.start_button.setEnabled(True)
|
|
366
|
+
self.instruction.setText("Abgebrochen.")
|
|
367
|
+
self._restore()
|
|
368
|
+
|
|
369
|
+
def _restore(self) -> None:
|
|
370
|
+
"""Put back the calibration the device had before the run."""
|
|
371
|
+
provider = self._provider()
|
|
372
|
+
if provider is not None and self.suspended is not None:
|
|
373
|
+
provider.calibration = self.suspended
|
|
374
|
+
self.suspended = None
|
|
375
|
+
self._show_current_calibration()
|
|
376
|
+
|
|
377
|
+
# -- keeping the answer -------------------------------------------------
|
|
378
|
+
|
|
379
|
+
def _apply(self, *, persist: bool) -> None:
|
|
380
|
+
if self.fit is None:
|
|
381
|
+
return
|
|
382
|
+
provider = self._provider()
|
|
383
|
+
if provider is None:
|
|
384
|
+
return
|
|
385
|
+
provider.calibration = self.fit.calibration
|
|
386
|
+
|
|
387
|
+
if persist:
|
|
388
|
+
target = save_calibration(
|
|
389
|
+
self.session.output_name, self.fit.calibration, path=self._calibration_path()
|
|
390
|
+
)
|
|
391
|
+
self.result.setText(f"{self.result.text()}\nGespeichert in {target}")
|
|
392
|
+
self._show_current_calibration()
|
|
393
|
+
|
|
394
|
+
def _save(self) -> None:
|
|
395
|
+
if self.fit is not None and not self.fit.trustworthy:
|
|
396
|
+
answer = QMessageBox.question(
|
|
397
|
+
self,
|
|
398
|
+
"Streuung ist hoch",
|
|
399
|
+
f"Die Messwerte streuen um {self.fit.spread_deg:.0f}°, mehr als eine halbe LED.\n"
|
|
400
|
+
"Trotzdem speichern?",
|
|
401
|
+
)
|
|
402
|
+
if answer != QMessageBox.StandardButton.Yes:
|
|
403
|
+
return
|
|
404
|
+
self._apply(persist=True)
|
|
405
|
+
|
|
406
|
+
def closeEvent(self, event) -> None: # noqa: N802 — Qt's spelling
|
|
407
|
+
self._restore()
|
|
408
|
+
super().closeEvent(event)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _context(session: StudioSession):
|
|
412
|
+
from lefx.sdk import InputContext
|
|
413
|
+
import time
|
|
414
|
+
|
|
415
|
+
return InputContext(
|
|
416
|
+
now=time.monotonic(), led_count=session.led_count, config={}, previous_inputs={}
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
__all__ = ["PROBE_EFFECT", "SAMPLE_INTERVAL_MS", "CalibrationPage"]
|