|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from .FileIdentity import cached_sha256 |
| 5 | + |
| 6 | +PLACEMENTS_RELATIVE = Path(".pinna2hrtf") / "microphone-placements.json" |
| 7 | +PLACEMENTS_FORMAT = "pinna2hrtf-microphone-placements-v1" |
| 8 | +SIDES = ("left", "right") |
| 9 | + |
| 10 | + |
| 11 | +def placement_key(side: str) -> str: |
| 12 | + return "source_position_input_" + side |
| 13 | + |
| 14 | + |
| 15 | +def read_placements(output_dir: Path) -> dict: |
| 16 | + try: |
| 17 | + document = json.loads((Path(output_dir) / PLACEMENTS_RELATIVE).read_text(encoding="utf-8")) |
| 18 | + except (OSError, ValueError): |
| 19 | + return {} |
| 20 | + if not isinstance(document, dict) or document.get("format") != PLACEMENTS_FORMAT: |
| 21 | + return {} |
| 22 | + return {side: document[side] for side in SIDES if isinstance(document.get(side), dict)} |
| 23 | + |
| 24 | + |
| 25 | +def matching_record(placements: dict, side: str, position) -> dict | None: |
| 26 | + record = placements.get(side) |
| 27 | + if record is None or position is None or [float(value) for value in position] != record.get("position_mm"): |
| 28 | + return None |
| 29 | + return record |
| 30 | + |
| 31 | + |
| 32 | +def status(placements: dict, side: str, position, mesh: Path | None) -> str: |
| 33 | + record = matching_record(placements, side, position) |
| 34 | + if record is None: |
| 35 | + return "unrecorded" |
| 36 | + if record.get("mesh_sha256") and mesh is not None and Path(mesh).is_file() and cached_sha256(mesh) == record["mesh_sha256"]: |
| 37 | + return "current" |
| 38 | + return "stale" |
| 39 | + |
| 40 | + |
| 41 | +def is_stale(placements: dict, side: str, position, mesh: Path | None) -> bool: |
| 42 | + return status(placements, side, position, mesh) == "stale" |
| 43 | + |
| 44 | + |
| 45 | +def preprocessing_meshes(config) -> dict: |
| 46 | + from .RunConfig import preprocessing_input_paths |
| 47 | + try: |
| 48 | + return dict(zip(SIDES, preprocessing_input_paths(config, lambda _: None))) |
| 49 | + except (OSError, ValueError, KeyError): |
| 50 | + return {} |
| 51 | + |
| 52 | + |
| 53 | +def statuses(config) -> dict: |
| 54 | + placements = read_placements(config.paths.output_dir) |
| 55 | + positions = {side: getattr(config.preprocessing, placement_key(side)) for side in SIDES} |
| 56 | + meshes = preprocessing_meshes(config) if placements else {} |
| 57 | + return {side: status(placements, side, position, meshes.get(side)) for side, position in positions.items() if position is not None} |
| 58 | + |
| 59 | + |
| 60 | +def record_placements(config, confirmed_sides) -> None: |
| 61 | + previous = read_placements(config.paths.output_dir) |
| 62 | + meshes = preprocessing_meshes(config) if confirmed_sides else {} |
| 63 | + document = {"format": PLACEMENTS_FORMAT} |
| 64 | + for side in SIDES: |
| 65 | + position = getattr(config.preprocessing, placement_key(side)) |
| 66 | + if position is None: |
| 67 | + continue |
| 68 | + position_mm = [float(value) for value in position] |
| 69 | + record = matching_record(previous, side, position) |
| 70 | + if side not in confirmed_sides: |
| 71 | + document[side] = {"position_mm": position_mm, "mesh_sha256": None} |
| 72 | + elif record is not None and record.get("mesh_sha256"): |
| 73 | + document[side] = record |
| 74 | + elif meshes.get(side) is not None and Path(meshes[side]).is_file(): |
| 75 | + document[side] = {"position_mm": position_mm, "mesh_sha256": cached_sha256(meshes[side])} |
| 76 | + path = Path(config.paths.output_dir) / PLACEMENTS_RELATIVE |
| 77 | + if document == {"format": PLACEMENTS_FORMAT, **previous} or (len(document) == 1 and not path.exists()): |
| 78 | + return |
| 79 | + from .Outputs import write_json |
| 80 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 81 | + write_json(path, document) |
| 82 | + |
| 83 | + |
| 84 | +def ignore_stale_placements(config, logger) -> None: |
| 85 | + placements = read_placements(config.paths.output_dir) |
| 86 | + if not placements: |
| 87 | + return |
| 88 | + meshes = preprocessing_meshes(config) |
| 89 | + for side in SIDES: |
| 90 | + position = getattr(config.preprocessing, placement_key(side)) |
| 91 | + if meshes and meshes.get(side) is None: |
| 92 | + continue |
| 93 | + if is_stale(placements, side, position, meshes.get(side)): |
| 94 | + setattr(config.preprocessing, placement_key(side), None) |
| 95 | + logger(stale_message(side)) |
| 96 | + |
| 97 | + |
| 98 | +def stale_message(side: str) -> str: |
| 99 | + return f"Ignoring the saved {side} microphone position because it does not match the current {side} ear mesh. Place the microphone again to use it." |
0 commit comments