Added python impl

This commit is contained in:
Jono Targett 2024-05-24 17:24:04 +09:30
parent 2d5420b4c3
commit 2c1b17fdb2
5 changed files with 77 additions and 0 deletions

View File

@ -13,6 +13,12 @@ cmake ..
make tone-generator make tone-generator
``` ```
## Rules of SELCAL Codes
- Letters must not be duplicated across the whole code
- Within each of the two groups, letters must be sorted alphabetically
- AB-CD and CD-AB are valid.
- AA-CD and BA-CD are not valid.
## Build with system packages? ## Build with system packages?
Requires the following packages (debian) Requires the following packages (debian)

1
python/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.wav

1
python/requirements.txt Normal file
View File

@ -0,0 +1 @@
tones==1.2.0

36
python/tonegen.py Executable file
View File

@ -0,0 +1,36 @@
#! /usr/bin/env python3
from tones import SINE_WAVE, SAWTOOTH_WAVE, SQUARE_WAVE
from tones.mixer import Mixer
import csv
import sys
GROUP_DURATION = 1.0
SILENCE_DURATION = 0.2
CHANNELS = 2
tones = {}
with open('tones.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
tones[row['designator']] = float(row['frequency'])
code = sys.argv[1]
print(f"Generating tone for SELCAL {code}")
# Create mixer, set sample rate and amplitude
mixer = Mixer(44100, 1)
for i in range(CHANNELS):
mixer.create_track(i, SQUARE_WAVE)
groups = code.split("-")
for groupindex,group in enumerate(groups):
for channel,tone in enumerate(group):
mixer.add_tone(channel, frequency=tones[tone], duration=GROUP_DURATION)
if groupindex != len(groups) - 1:
for i in range(CHANNELS):
mixer.add_silence(i, duration=SILENCE_DURATION)
# Mix all tracks into a single list of samples and write to .wav file
mixer.write_wav(f'{code}.wav')

33
python/tones.csv Normal file
View File

@ -0,0 +1,33 @@
designator,frequency
1,680.0
2,754.2
3,836.6
4,927.9
5,1029.2
6,1141.6
7,1266.2
8,1404.4
9,1557.8
A,312.6
B,346.7
C,384.6
D,426.6
E,473.2
F,524.8
G,582.1
H,645.7
J,716.1
K,794.3
L,881.0
M,977.2
P,1083.9
Q,1202.3
R,1333.5
S,1479.1
T,329.2
U,365.2
V,405.0
W,449.3
X,498.3
Y,552.7
Z,613.1
1 designator frequency
2 1 680.0
3 2 754.2
4 3 836.6
5 4 927.9
6 5 1029.2
7 6 1141.6
8 7 1266.2
9 8 1404.4
10 9 1557.8
11 A 312.6
12 B 346.7
13 C 384.6
14 D 426.6
15 E 473.2
16 F 524.8
17 G 582.1
18 H 645.7
19 J 716.1
20 K 794.3
21 L 881.0
22 M 977.2
23 P 1083.9
24 Q 1202.3
25 R 1333.5
26 S 1479.1
27 T 329.2
28 U 365.2
29 V 405.0
30 W 449.3
31 X 498.3
32 Y 552.7
33 Z 613.1