Files
app/tools/country_tones.py
T
Claude 9b4debf4f5 Comments for every function: the first two batches; doc_check.py stricter
The tests' and shared code's functions in two of eight batches (comment
lines only). doc_check.py skips only lines of annotations alone, so a
function under an annotated property (@Volatile var) needs its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
2026-10-27 08:16:27 +00:00

73 lines
2.7 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (C) 2026 Neo-Desktop
"""The countries' tones for the apps' shared Kotlin, from D-Modem-RE's
table (external/d-modem-re/dsp/tones.cpp: the ITU's list of national
networks' tones, after E.180): each country's ringing, dial, busy and
congestion tones, and the table's other names of them.
tools/country_tones.py > shared/src/commonMain/kotlin/com/neodesktop/dmodem/tones/CountryTonesTable.kt
CountryTonesTest checks the Kotlin against tones.cpp when the submodule
is there.
"""
import os
import re
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
SRC = os.path.join(HERE, "..", "external", "d-modem-re", "dsp", "tones.cpp")
TONE = r"\{\s*(-?\d+),\s*(-?\d+),\s*([\d.]+)f,\s*(\d+),\s*\{([^}]*)\}\s*\}"
COUNTRY = re.compile(
r'\{\s*"([A-Z0-9_]+)",\s*' + TONE + r",\s*(\d+),\s*" + TONE + r",\s*" + TONE + r",\s*" + TONE + r"\s*\}",
re.S,
)
ALIAS = re.compile(r'\{\s*"([A-Z0-9_]+)",\s*"([A-Z0-9_]+)"\s*\}')
def tone(g, i):
"""the Kotlin Tone() of the tone at i in the match g: its frequencies, modulation and cadence (its first n values)"""
f1, f2, mod, n, cad = g[i:i + 5]
vals = [v.strip().rstrip("f") for v in cad.split(",") if v.strip()][: int(n)]
m = float(mod)
cadence = ", ".join(f"{float(v)}f" for v in vals)
return f"Tone({f1}, {f2}, {m}f, floatArrayOf({cadence}))"
def main():
"""tones.cpp's countries and aliases as CountryTonesTable.kt, to stdout"""
text = open(SRC).read()
countries_part, _, aliases_part = text.partition("const Alias ALIASES[]")
countries = COUNTRY.findall(countries_part)
aliases = ALIAS.findall(aliases_part)
out = sys.stdout
out.write("""// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
//
// Generated by tools/country_tones.py from D-Modem-RE's dsp/tones.cpp
// (the ITU's list of the tones of national networks, Annex to ITU
// Operational Bulletin No. 955, after E.180). Don't edit: run it again.
package com.neodesktop.dmodem.tones
internal object CountryTonesTable {
val countries: List<CountryTones> = listOf(
""")
for c in countries:
name = c[0]
ringing = tone(c, 1)
ring_hz = c[6]
dial = tone(c, 7)
busy = tone(c, 12)
congestion = tone(c, 17)
out.write(f' CountryTones(\n "{name}",\n {ringing}, {ring_hz},\n'
f" {dial},\n {busy},\n {congestion},\n ),\n")
out.write(" )\n\n val aliases: Map<String, String> = mapOf(\n")
for a, n in aliases:
out.write(f' "{a}" to "{n}",\n')
out.write(" )\n}\n")
print(f"{len(countries)} countries, {len(aliases)} aliases", file=sys.stderr)
main()