open-merou

Open Mérou, a free iOS app for the Oceanic+ underwater iPhone housing
git clone https://git.schurger.org/open-merou.git
Log | Files | Refs | README | LICENSE

make-sounds.py (1933B)


      1 #!/usr/bin/env python3
      2 # SPDX-License-Identifier: GPL-2.0-only
      3 """Generates the alert sounds bundled in the app (OpenMerou/Sounds): plain synthesized tones, so that no
      4 third-party recording is involved.
      5 
      6 Usage: tools/make-sounds.py [output directory]
      7 
      8 - alarm.wav: urgent, three quick two-tone beeps (leak alarm, dive computer alarms);
      9 - notice.wav: softer, two rising notes (dive computer warnings, safety stop cues).
     10 
     11 The frequencies sit where a phone loudspeaker is loudest (2 to 3 kHz for the alarm).
     12 """
     13 import math
     14 import struct
     15 import sys
     16 import wave
     17 
     18 RATE = 22_050
     19 
     20 
     21 def tone(frequency, seconds, level, harmonics=()):
     22     """A sine (plus optional odd harmonics for a harsher sound) with 5 ms fades to avoid clicks."""
     23     count = int(RATE * seconds)
     24     fade = int(RATE * 0.005)
     25     out = []
     26     for i in range(count):
     27         t = i / RATE
     28         value = math.sin(2 * math.pi * frequency * t)
     29         for order, weight in harmonics:
     30             value += weight * math.sin(2 * math.pi * frequency * order * t)
     31         envelope = min(1.0, i / fade, (count - 1 - i) / fade)
     32         out.append(level * envelope * value / (1 + sum(w for _, w in harmonics)))
     33     return out
     34 
     35 
     36 def silence(seconds):
     37     return [0.0] * int(RATE * seconds)
     38 
     39 
     40 def write(path, samples):
     41     with wave.open(path, "wb") as f:
     42         f.setnchannels(1)
     43         f.setsampwidth(2)
     44         f.setframerate(RATE)
     45         f.writeframes(b"".join(struct.pack("<h", int(max(-1.0, min(1.0, s)) * 32767)) for s in samples))
     46 
     47 
     48 def main():
     49     out = sys.argv[1] if len(sys.argv) > 1 else "OpenMerou/Sounds"
     50     harsh = ((3, 0.33), (5, 0.2))
     51     alarm = []
     52     for _ in range(3):
     53         alarm += tone(2_400, 0.11, 0.95, harsh) + tone(3_000, 0.11, 0.95, harsh) + silence(0.05)
     54     write(f"{out}/alarm.wav", alarm)
     55     notice = tone(1_320, 0.14, 0.7) + silence(0.05) + tone(1_760, 0.2, 0.7)
     56     write(f"{out}/notice.wav", notice)
     57 
     58 
     59 if __name__ == "__main__":
     60     main()