M17 0.1.0__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.
- m17/__init__.py +13 -0
- m17/__main__.py +27 -0
- m17/address.py +180 -0
- m17/apps.py +350 -0
- m17/audio_test.py +48 -0
- m17/blocks.py +571 -0
- m17/client.py +8 -0
- m17/const.py +23 -0
- m17/framer.py +65 -0
- m17/frames.py +427 -0
- m17/misc.py +131 -0
- m17/network.py +458 -0
- m17/sanity_check.py +18 -0
- m17/voipsim.py +8 -0
- m17-0.1.0.dist-info/METADATA +60 -0
- m17-0.1.0.dist-info/RECORD +17 -0
- m17-0.1.0.dist-info/WHEEL +4 -0
m17/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from m17.address import Address
|
|
2
|
+
from m17.frames import LICHFrame, RegularFrame, IPFrame, M17Payload
|
|
3
|
+
from m17.framer import M17IPFramer, M17RFFramer
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
'Address',
|
|
7
|
+
'LICHFrame',
|
|
8
|
+
'RegularFrame',
|
|
9
|
+
'IPFrame',
|
|
10
|
+
'M17Payload',
|
|
11
|
+
'M17IPFramer',
|
|
12
|
+
'M17RFFramer'
|
|
13
|
+
]
|
m17/__main__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
|
|
2
|
+
helptext="""
|
|
3
|
+
`python -m m17` doesn't do anything on it's own (yet).
|
|
4
|
+
Examples:
|
|
5
|
+
|
|
6
|
+
Without `pip install m17[Codec2]`
|
|
7
|
+
python -m m17.address W2FBI
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
With `pip install m17[Codec2]`
|
|
11
|
+
python -m m17.audio_test 3200
|
|
12
|
+
|
|
13
|
+
Use your microphone mute as a reverse push to talk with these:
|
|
14
|
+
python -m m17.bareclient m17tester.tarxvf.tech 17010 tx srccall dstcall
|
|
15
|
+
python -m m17.bareclient localhost 17000 rx
|
|
16
|
+
python -m m17.bareclient raspi.lan 17000 full srccall dstcall
|
|
17
|
+
|
|
18
|
+
Same microphone mute required:
|
|
19
|
+
connect to M17-M17 A as YOURCALL A
|
|
20
|
+
python -m m17.apps m17ref_client YOURCALL A M17-M17 A
|
|
21
|
+
"""
|
|
22
|
+
def main():
|
|
23
|
+
print(helptext)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if __name__ == "__main__":
|
|
27
|
+
main()
|
m17/address.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""
|
|
2
|
+
M17 Addressing
|
|
3
|
+
"""
|
|
4
|
+
import sys
|
|
5
|
+
import struct
|
|
6
|
+
from typing import Union
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
from m17.const import CALLSIGN_ALPHABET, M17_ADDRESS_LAYOUT_STRUCT
|
|
10
|
+
M17AddressLayout = struct.Struct(M17_ADDRESS_LAYOUT_STRUCT)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
AddressParam = Union[int, bytes]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Address:
|
|
17
|
+
"""
|
|
18
|
+
Call with either "addr" or "callsign" to instantiate, e.g.
|
|
19
|
+
|
|
20
|
+
>>> from m17.address import Address
|
|
21
|
+
>>> Address(callsign="W2FBI").addr
|
|
22
|
+
23178783
|
|
23
|
+
|
|
24
|
+
You can get the hex version using Python's hex()
|
|
25
|
+
>>> hex( 23178783 )
|
|
26
|
+
'0x161ae1f'
|
|
27
|
+
|
|
28
|
+
>>> from m17.address import Address
|
|
29
|
+
>>> Address(addr=23178783).callsign
|
|
30
|
+
'W2FBI'
|
|
31
|
+
|
|
32
|
+
You can also use it directly, e.g.
|
|
33
|
+
>>> from m17.address import Address
|
|
34
|
+
>>> Address.encode("W2FBI")
|
|
35
|
+
23178783
|
|
36
|
+
>>> Address.decode(23178783)
|
|
37
|
+
'W2FBI'
|
|
38
|
+
|
|
39
|
+
Equality tests work like you might hope:
|
|
40
|
+
>>> Address(callsign="W2FBI") == "W2FBI"
|
|
41
|
+
True
|
|
42
|
+
>>> Address(callsign="W2FBI") == 23178783
|
|
43
|
+
True
|
|
44
|
+
>>> Address(callsign="W2FBI") == Address.encode("W2FBI")
|
|
45
|
+
True
|
|
46
|
+
>>> Address(callsign="W2FBI") == Address(addr=23178783)
|
|
47
|
+
True
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def __init__(self, addr: bytes=None, callsign: str=None):
|
|
52
|
+
if addr is None and callsign is None:
|
|
53
|
+
raise ValueError("Must provide either addr or callsign")
|
|
54
|
+
|
|
55
|
+
self.callsign = callsign.upper() if callsign else self.decode(addr)
|
|
56
|
+
self.addr = addr if addr else self.encode(callsign)
|
|
57
|
+
|
|
58
|
+
if isinstance(self.addr, int):
|
|
59
|
+
self.addr = self.addr.to_bytes(6, "big")
|
|
60
|
+
|
|
61
|
+
def __str__(self):
|
|
62
|
+
int_addr = int.from_bytes(self.addr, "big")
|
|
63
|
+
return f"{self.callsign} == 0x{int_addr:06x}"
|
|
64
|
+
|
|
65
|
+
def __bytes__(self):
|
|
66
|
+
return M17AddressLayout.pack(*self.addr)
|
|
67
|
+
|
|
68
|
+
def __index__(self):
|
|
69
|
+
return int.from_bytes(self.addr, "big")
|
|
70
|
+
|
|
71
|
+
def __eq__(self, compareto):
|
|
72
|
+
if isinstance(compareto, str):
|
|
73
|
+
return int(compareto) == self.addr if compareto.isdigit() else compareto.upper() == self.callsign
|
|
74
|
+
|
|
75
|
+
if isinstance(compareto, int):
|
|
76
|
+
return compareto == int(self)
|
|
77
|
+
|
|
78
|
+
if isinstance(compareto, Address):
|
|
79
|
+
return int(self) == int(compareto)
|
|
80
|
+
|
|
81
|
+
return False
|
|
82
|
+
|
|
83
|
+
@staticmethod
|
|
84
|
+
def to_dmr_id(something):
|
|
85
|
+
"""
|
|
86
|
+
Convert a callsign to a DMR ID
|
|
87
|
+
"""
|
|
88
|
+
# if no db:
|
|
89
|
+
# url = "https://database.radioid.net/static/users.json"
|
|
90
|
+
# requests.get()
|
|
91
|
+
# if db but not found, _check once_ using https://database.radioid.net/api/dmr/user/?id=3125404
|
|
92
|
+
# return an Address encoded for DMR using database lookup?
|
|
93
|
+
# or jsut the ID as an int?
|
|
94
|
+
...
|
|
95
|
+
|
|
96
|
+
@staticmethod
|
|
97
|
+
def from_dmr_id(dmr_int):
|
|
98
|
+
"""
|
|
99
|
+
Convert a DMR ID to a callsign
|
|
100
|
+
"""
|
|
101
|
+
# return an Address encoded for callsign using dmr database lookup to get callsign
|
|
102
|
+
...
|
|
103
|
+
|
|
104
|
+
def is_dmr_id(self):
|
|
105
|
+
"""
|
|
106
|
+
Is this a DMR ID?
|
|
107
|
+
"""
|
|
108
|
+
return self.callsign.startswith("D") and self.callsign[1:].isdigit()
|
|
109
|
+
|
|
110
|
+
def is_dmr_talkgroup(self):
|
|
111
|
+
"""
|
|
112
|
+
Is this a DMR talkgroup?
|
|
113
|
+
"""
|
|
114
|
+
return any(
|
|
115
|
+
self.is_brandmeister_tg()
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
def is_brandmeister_tg(self):
|
|
119
|
+
"""
|
|
120
|
+
Is this a Brandmeister talkgroup?
|
|
121
|
+
"""
|
|
122
|
+
return self.callsign.startswith("BM") and self.callsign[1:].isdigit()
|
|
123
|
+
|
|
124
|
+
def is_dstar_reflector(self):
|
|
125
|
+
"""
|
|
126
|
+
Is this a D-Star reflector?
|
|
127
|
+
"""
|
|
128
|
+
return self.callsign.startswith("REF")
|
|
129
|
+
|
|
130
|
+
@staticmethod
|
|
131
|
+
def encode(callsign):
|
|
132
|
+
"""
|
|
133
|
+
Encode a callsign into an address
|
|
134
|
+
"""
|
|
135
|
+
num = 0
|
|
136
|
+
for char in callsign.upper()[::-1]:
|
|
137
|
+
charidx = CALLSIGN_ALPHABET.index(char)
|
|
138
|
+
num *= 40
|
|
139
|
+
num += charidx
|
|
140
|
+
if num >= 40 ** 9:
|
|
141
|
+
raise ValueError("Invalid callsign")
|
|
142
|
+
return num.to_bytes(6, "big")
|
|
143
|
+
|
|
144
|
+
@staticmethod
|
|
145
|
+
def decode(addr: AddressParam):
|
|
146
|
+
"""
|
|
147
|
+
Decode an address into a callsign
|
|
148
|
+
"""
|
|
149
|
+
num = addr
|
|
150
|
+
if isinstance(addr, bytes):
|
|
151
|
+
num = int.from_bytes(addr, "big")
|
|
152
|
+
|
|
153
|
+
if num >= 40 ** 9:
|
|
154
|
+
raise ValueError("Invalid address")
|
|
155
|
+
chars = []
|
|
156
|
+
while num > 0:
|
|
157
|
+
idx = int(num % 40)
|
|
158
|
+
c = CALLSIGN_ALPHABET[idx]
|
|
159
|
+
chars.append(c)
|
|
160
|
+
# print(num,idx,c)
|
|
161
|
+
num //= 40
|
|
162
|
+
callsign = "".join(chars)
|
|
163
|
+
return callsign
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def show_help():
|
|
167
|
+
"""
|
|
168
|
+
Show help
|
|
169
|
+
"""
|
|
170
|
+
print("""
|
|
171
|
+
Provide callsigns on the command line and they will be translated into M17 addresses
|
|
172
|
+
""")
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
if __name__ == "__main__":
|
|
176
|
+
if len(sys.argv) <= 1:
|
|
177
|
+
show_help()
|
|
178
|
+
else:
|
|
179
|
+
for each in sys.argv[1:]:
|
|
180
|
+
print(Address(callsign=each))
|
m17/apps.py
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
import multiprocessing
|
|
3
|
+
import sys
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
import m17.blocks
|
|
7
|
+
import m17.network
|
|
8
|
+
from m17.const import DEFAULT_PORT
|
|
9
|
+
from m17.misc import DictDotAttribute
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def default_config(c2_mode):
|
|
13
|
+
c2, conrate, bitframe = m17.blocks.codec2setup(c2_mode)
|
|
14
|
+
print("conrate, bitframe = [%d,%d]" % (conrate, bitframe))
|
|
15
|
+
|
|
16
|
+
config = DictDotAttribute({
|
|
17
|
+
"m17": {
|
|
18
|
+
"dst": "",
|
|
19
|
+
"src": "",
|
|
20
|
+
},
|
|
21
|
+
"vox": {
|
|
22
|
+
"silence_threshold": 10, # that's measured in queue packets
|
|
23
|
+
},
|
|
24
|
+
"codec2": {
|
|
25
|
+
"c2": c2,
|
|
26
|
+
"conrate": conrate,
|
|
27
|
+
"bitframe": bitframe,
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
return config
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def m17_parrot(refcallsign, port=DEFAULT_PORT):
|
|
34
|
+
# A parrot service for M17 - it's a full client that records and plays back after incoming stream is over PTT is released
|
|
35
|
+
port = int(port)
|
|
36
|
+
# udp_recv and udp_send are needed here, too.
|
|
37
|
+
...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def m17_mirror(refcallsign, port=DEFAULT_PORT):
|
|
41
|
+
# reflects your M17 stream back to you after decoding and encoding
|
|
42
|
+
# can be useful for later transformations, like testing voice stream compatibilities
|
|
43
|
+
port = int(port)
|
|
44
|
+
# udp_recv and udp_send are needed here, too.
|
|
45
|
+
...
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def udp_mirror(refcallsign, port=DEFAULT_PORT):
|
|
49
|
+
# reflects your own UDP packets back to you after a delay
|
|
50
|
+
port = int(port)
|
|
51
|
+
|
|
52
|
+
pkts = {}
|
|
53
|
+
|
|
54
|
+
def packet_handler(sock, active_connections, bs, conn):
|
|
55
|
+
if conn not in pkts:
|
|
56
|
+
pkts[conn] = DictDotAttribute({"packets": [], "lastseen": time.time()})
|
|
57
|
+
else:
|
|
58
|
+
this = pkts[conn]
|
|
59
|
+
pkts[conn].packets.append((time.time() - this.lastseen, bs))
|
|
60
|
+
pkts[conn].lastseen = time.time()
|
|
61
|
+
|
|
62
|
+
def timer(sock):
|
|
63
|
+
def replay(conn, packets):
|
|
64
|
+
for reltime, bs in packets:
|
|
65
|
+
time.sleep(reltime)
|
|
66
|
+
sock.sendto(bs, conn)
|
|
67
|
+
|
|
68
|
+
delthese = []
|
|
69
|
+
for conn in pkts:
|
|
70
|
+
if pkts[conn].lastseen + 10 < time.time():
|
|
71
|
+
# as udp_server is written, this will stop us from recvfrom - and that's okay for now
|
|
72
|
+
# if we have multiple users, we may well timeout on several in a row because of the delays we're seeing here
|
|
73
|
+
# what i wish i had was a setTimeout like in JS, but I'm sure I can do something with asyncio later to get what I want (and actually support multiple udp_mirror users)
|
|
74
|
+
replay(conn, pkts[conn].packets)
|
|
75
|
+
delthese.append(conn)
|
|
76
|
+
for conn in delthese:
|
|
77
|
+
del pkts[conn]
|
|
78
|
+
|
|
79
|
+
srv = m17.blocks.udp_server(port, packet_handler, timer)
|
|
80
|
+
srv()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def udp_reflector(refcallsign, port=DEFAULT_PORT):
|
|
84
|
+
# "Reflects" an incoming stream to all connected users.
|
|
85
|
+
# ✔ So first, we need a way to receive connections and keep track of them, right?
|
|
86
|
+
# We also have our own callsign, but we'll deal with that later.
|
|
87
|
+
|
|
88
|
+
port = int(port)
|
|
89
|
+
|
|
90
|
+
def packet_handler(sock, active_connections, bs, conn):
|
|
91
|
+
others = [c for c in active_connections.keys() if c != conn]
|
|
92
|
+
for c in others:
|
|
93
|
+
sock.sendto(bs, c)
|
|
94
|
+
|
|
95
|
+
srv = m17.blocks.udp_server(port, packet_handler)
|
|
96
|
+
srv()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def m17ref_client(mycall, mymodule, refname, module, port=DEFAULT_PORT, mode=3200):
|
|
100
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
101
|
+
port = int(port)
|
|
102
|
+
if (refname.startswith("M17-") and len(refname) <= 7):
|
|
103
|
+
# should also be able to look up registered port in dns
|
|
104
|
+
host = m17.network.m17ref_name2host(refname)
|
|
105
|
+
print(host)
|
|
106
|
+
# fallback to fetching json if its not in dns already
|
|
107
|
+
else:
|
|
108
|
+
raise (NotImplementedError)
|
|
109
|
+
myrefmod = "%s %s" % (mycall, mymodule)
|
|
110
|
+
c = m17.blocks.M17ReflectorClientBlocks(myrefmod, module, host, port)
|
|
111
|
+
tx_chain = [m17.blocks.mic_audio, m17.blocks.codec2enc, m17.blocks.vox, m17.blocks.m17frame, m17.blocks.tobytes,
|
|
112
|
+
c.sender()]
|
|
113
|
+
rx_chain = [c.receiver(), m17.blocks.m17parse, m17.blocks.payload2codec2, m17.blocks.codec2dec,
|
|
114
|
+
m17.blocks.spkr_audio]
|
|
115
|
+
config = default_config(mode)
|
|
116
|
+
config.m17.dst = "%s %s" % (refname, module)
|
|
117
|
+
config.m17.src = mycall
|
|
118
|
+
print(config)
|
|
119
|
+
c.start()
|
|
120
|
+
modular(config, [tx_chain, rx_chain])
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def voipsim(host="localhost", src="W2FBI", dst="SP5WWP", mode=3200, port=DEFAULT_PORT):
|
|
124
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
125
|
+
port = int(port)
|
|
126
|
+
config = default_config(mode)
|
|
127
|
+
audio_sim = m17.blocks.zeros(size=config.codec2.conrate, dtype="<h", rate=50)
|
|
128
|
+
tx_chain = [audio_sim, m17.blocks.codec2enc, m17.blocks.m17frame, m17.blocks.tobytes,
|
|
129
|
+
m17.blocks.udp_send((host, port))]
|
|
130
|
+
config.m17.dst = dst
|
|
131
|
+
config.m17.src = src
|
|
132
|
+
print(config)
|
|
133
|
+
modular(config, [tx_chain])
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def to_icecast(icecast_url, mode=3200, port=DEFAULT_PORT):
|
|
137
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
138
|
+
port = int(port)
|
|
139
|
+
rx_chain = [m17.blocks.udp_recv(port), m17.blocks.m17parse, m17.blocks.payload2codec2, m17.blocks.codec2dec,
|
|
140
|
+
m17.blocks.ffmpeg(icecast_url)]
|
|
141
|
+
# rx_chain = [udp_recv(port), m17parse, tee('m17'), payload2codec2, codec2dec, ffmpeg(icecast_url)]
|
|
142
|
+
config = default_config(mode)
|
|
143
|
+
modular(config, [rx_chain])
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def to_pcm(mode=3200, port=DEFAULT_PORT):
|
|
147
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
148
|
+
port = int(port)
|
|
149
|
+
rx_chain = [m17.blocks.udp_recv(port), m17.blocks.m17parse, m17.blocks.tee('m17'), m17.blocks.payload2codec2,
|
|
150
|
+
m17.blocks.codec2dec, m17.blocks.teefile('m17.raw'), m17.blocks.null]
|
|
151
|
+
config = default_config(mode)
|
|
152
|
+
modular(config, [rx_chain])
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def recv_dump(mode=3200, port=DEFAULT_PORT):
|
|
156
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
157
|
+
port = int(port)
|
|
158
|
+
rx_chain = [m17.blocks.udp_recv(port), m17.blocks.teefile("rx"), m17.blocks.m17parse, m17.blocks.tee('M17'),
|
|
159
|
+
m17.blocks.payload2codec2, m17.blocks.teefile('out.c2_3200'), m17.blocks.codec2dec,
|
|
160
|
+
m17.blocks.teefile('out.raw'), m17.blocks.spkr_audio]
|
|
161
|
+
config = default_config(mode)
|
|
162
|
+
modular(config, [rx_chain])
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def voip(host="localhost", port=DEFAULT_PORT, voipmode="full", mode=3200, src="W2FBI", dst="SP5WWP"):
|
|
166
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
167
|
+
port = int(port)
|
|
168
|
+
|
|
169
|
+
# this requires remote host to have port forwarded properly and everything - it doesn't
|
|
170
|
+
# reuse the server socket connection (which would support NAT traversal)
|
|
171
|
+
|
|
172
|
+
# this means the tx and rx paths are completely separate, which is,
|
|
173
|
+
# if nothing else, simple to reason about
|
|
174
|
+
|
|
175
|
+
tx_chain = [m17.blocks.mic_audio, m17.blocks.codec2enc, m17.blocks.vox, m17.blocks.m17frame, m17.blocks.tobytes,
|
|
176
|
+
m17.blocks.udp_send((host, port))]
|
|
177
|
+
rx_chain = [m17.blocks.udp_recv(port), m17.blocks.m17parse, m17.blocks.payload2codec2, m17.blocks.codec2dec,
|
|
178
|
+
m17.blocks.spkr_audio]
|
|
179
|
+
if voipmode == "tx":
|
|
180
|
+
# disable the rx chain
|
|
181
|
+
# useful for when something's already bound to listening port
|
|
182
|
+
rx_chain = []
|
|
183
|
+
if voipmode == "rx":
|
|
184
|
+
# disable the tx chain
|
|
185
|
+
# useful for monitoring incoming packets without sending anything
|
|
186
|
+
tx_chain = []
|
|
187
|
+
config = default_config(mode)
|
|
188
|
+
|
|
189
|
+
config.m17.dst = dst
|
|
190
|
+
config.m17.src = src
|
|
191
|
+
print(config)
|
|
192
|
+
|
|
193
|
+
modular(config, [tx_chain, rx_chain])
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def echolink_bridge(mycall, mymodule, refname, refmodule, refport=DEFAULT_PORT, mode=3200):
|
|
197
|
+
mode = int(mode) # so we can call modular_client straight from command line
|
|
198
|
+
refport = int(refport)
|
|
199
|
+
if (refname.startswith("M17-") and len(refname) <= 7):
|
|
200
|
+
# should also be able to look up registered port in dns
|
|
201
|
+
host = m17.network.m17ref_name2host(refname)
|
|
202
|
+
print(host)
|
|
203
|
+
# fallback to fetching json if its not in dns already
|
|
204
|
+
else:
|
|
205
|
+
raise (NotImplementedError)
|
|
206
|
+
myrefmod = "%s %s" % (mycall, mymodule)
|
|
207
|
+
c = m17.blocks.M17ReflectorClientBlocks(myrefmod, refmodule, host, refport)
|
|
208
|
+
echolink_to_m17ref = [m17.blocks.udp_recv(55501), m17.blocks.chunker_b(640), m17.blocks.np_convert("<h"),
|
|
209
|
+
m17.blocks.integer_decimate(2), m17.blocks.codec2enc, m17.blocks.m17frame,
|
|
210
|
+
m17.blocks.tobytes, c.sender()]
|
|
211
|
+
m17ref_to_echolink = [c.receiver(), m17.blocks.m17parse, m17.blocks.payload2codec2, m17.blocks.codec2dec,
|
|
212
|
+
m17.blocks.integer_interpolate(2),
|
|
213
|
+
m17.blocks.udp_send(("127.0.0.1", 55500))]
|
|
214
|
+
config = default_config(mode)
|
|
215
|
+
config.m17.dst = "%s %s" % (refname, refmodule)
|
|
216
|
+
config.m17.src = mycall
|
|
217
|
+
print(config)
|
|
218
|
+
c.start()
|
|
219
|
+
modular(config, [echolink_to_m17ref, m17ref_to_echolink])
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def m17_to_echolink(port=DEFAULT_PORT, echolink_host="localhost", mode=3200, echolink_audio_in_port=55500):
|
|
223
|
+
port = int(port)
|
|
224
|
+
mode = int(mode)
|
|
225
|
+
echolink_audio_in_port = int(echolink_audio_in_port)
|
|
226
|
+
"""
|
|
227
|
+
decode and bridge m17 packets to echolink
|
|
228
|
+
(useful for interopability testing)
|
|
229
|
+
"""
|
|
230
|
+
chain = [
|
|
231
|
+
m17.blocks.udp_recv(port),
|
|
232
|
+
m17.blocks.m17parse, m17.blocks.payload2codec2, m17.blocks.codec2dec,
|
|
233
|
+
m17.blocks.integer_interpolate(2), # echolink wants 16k audio
|
|
234
|
+
m17.blocks.udp_send((echolink_host, echolink_audio_in_port))
|
|
235
|
+
]
|
|
236
|
+
config = default_config(mode)
|
|
237
|
+
config.verbose = 0
|
|
238
|
+
modular(config, [chain])
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _test_chains_example():
|
|
242
|
+
"""
|
|
243
|
+
example playground for testing
|
|
244
|
+
"""
|
|
245
|
+
test_chain = [
|
|
246
|
+
m17.blocks.mic_audio,
|
|
247
|
+
m17.blocks.codec2enc, # .02ms of audio per q element at c2.3200 in this part of chain
|
|
248
|
+
# delay(5/.02), #to delay for 5s, divide 5s by the time-length of a q element in this part of chain (which does change)
|
|
249
|
+
# tee("delayed c2bytes: "),
|
|
250
|
+
# teefile("out.m17"),
|
|
251
|
+
# vox,
|
|
252
|
+
# ptt,
|
|
253
|
+
# m17frame, #.04ms of audio per q element at c2.3200
|
|
254
|
+
# tobytes,
|
|
255
|
+
# udp_send,
|
|
256
|
+
# udp_recv,
|
|
257
|
+
# m17parse,
|
|
258
|
+
# payload2codec2, #back to .02ms per q el
|
|
259
|
+
m17.blocks.codec2dec,
|
|
260
|
+
# null,
|
|
261
|
+
m17.blocks.spkr_audio
|
|
262
|
+
]
|
|
263
|
+
config = default_config(mode)
|
|
264
|
+
config.verbose = 1
|
|
265
|
+
modular(config, [test_chain])
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def modular(config, chains):
|
|
269
|
+
"""
|
|
270
|
+
Take in a global configuration, and a list of lists of queue
|
|
271
|
+
processing functions, and hook them up in a chain, each function in
|
|
272
|
+
its own process
|
|
273
|
+
Fantastic for designing, developing, and debugging new features.
|
|
274
|
+
"""
|
|
275
|
+
# a chain is a series of small functions that share a queue between each pair
|
|
276
|
+
# each small function is its own process - which is absurd, except this
|
|
277
|
+
# is a testing and development environment, so ease of implementation/modification
|
|
278
|
+
# and modularity is the goal
|
|
279
|
+
# this also means we can meet our latency constraint for writing out
|
|
280
|
+
# to the speakers without any effort, even though our total latency
|
|
281
|
+
# from mic->udp is greater than our deadline.
|
|
282
|
+
# As long as each function stays under the deadline individually, all we do is add latency from sampled->delivered
|
|
283
|
+
# (well, as long as we have enough processor cores, but it's current_year, these functions still arent that heavy, and its working excellently given what I needed it to do
|
|
284
|
+
# if a function does get slower than realtime, can I make two in its place writing to the same queues?
|
|
285
|
+
# as long as i have enough cores still, that seems reasonable - but I'll have to think about it
|
|
286
|
+
"""
|
|
287
|
+
queues:
|
|
288
|
+
n -> n2 -> n3 -> n4
|
|
289
|
+
n has no inq
|
|
290
|
+
n4 has no outq
|
|
291
|
+
outq for n is inq for n2, etc
|
|
292
|
+
for each chain:
|
|
293
|
+
0 -> 1 -> 2 -> 3
|
|
294
|
+
0 1 2
|
|
295
|
+
if there's an old outq, inq=
|
|
296
|
+
unless at end of chain, create an outq for each fn, outq=
|
|
297
|
+
|
|
298
|
+
"""
|
|
299
|
+
modules = {
|
|
300
|
+
"chains": chains,
|
|
301
|
+
"queues": [],
|
|
302
|
+
"processes": [],
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
for chainidx, chain in enumerate(modules["chains"]):
|
|
306
|
+
inq = None
|
|
307
|
+
for fnidx, fn in enumerate(chain):
|
|
308
|
+
name = fn.__name__
|
|
309
|
+
if fnidx != len(chain):
|
|
310
|
+
outq = multiprocessing.Queue()
|
|
311
|
+
modules["queues"].append(outq)
|
|
312
|
+
else:
|
|
313
|
+
outq = None
|
|
314
|
+
process = multiprocessing.Process(name="chain_%d/fn_%d/%s" % (chainidx, fnidx, name), target=fn,
|
|
315
|
+
args=(config, inq, outq))
|
|
316
|
+
modules["processes"].append({
|
|
317
|
+
"name": name,
|
|
318
|
+
"inq": inq,
|
|
319
|
+
"outq": outq,
|
|
320
|
+
"process": process
|
|
321
|
+
})
|
|
322
|
+
process.start()
|
|
323
|
+
inq = outq
|
|
324
|
+
try:
|
|
325
|
+
procs = modules['processes']
|
|
326
|
+
while 1:
|
|
327
|
+
if any(not p['process'].is_alive() for p in procs):
|
|
328
|
+
print("lost a client process")
|
|
329
|
+
break
|
|
330
|
+
time.sleep(.05)
|
|
331
|
+
# I can see where this is going to need to change
|
|
332
|
+
# it's fine for now, but a real server will need something different
|
|
333
|
+
except KeyboardInterrupt as e:
|
|
334
|
+
print("Got ^C, ")
|
|
335
|
+
finally:
|
|
336
|
+
print("closing down")
|
|
337
|
+
for proc in procs:
|
|
338
|
+
# messy
|
|
339
|
+
# TODO make a rwlock for indicating shutdown
|
|
340
|
+
proc["process"].terminate()
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
if __name__ == "__main__":
|
|
344
|
+
vars()[sys.argv[1]](*sys.argv[2:])
|
|
345
|
+
|
|
346
|
+
"""
|
|
347
|
+
Good links I found:
|
|
348
|
+
https://www.cloudcity.io/blog/2019/02/27/things-i-wish-they-told-me-about-multiprocessing-in-python/
|
|
349
|
+
|
|
350
|
+
"""
|
m17/audio_test.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Provides a test of the soundcard library. It will record from the default microphone and
|
|
3
|
+
play back to the default speaker.
|
|
4
|
+
"""
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
import soundcard as sc
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
import pycodec2
|
|
11
|
+
except Exception as exc:
|
|
12
|
+
raise ImportError("pycodec2 is required for this test.") from exc
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def audio_test_soundcard(mode):
|
|
16
|
+
"""
|
|
17
|
+
Test the soundcard library.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
mode = int(mode)
|
|
21
|
+
c2 = pycodec2.Codec2(mode)
|
|
22
|
+
conrate = c2.samples_per_frame()
|
|
23
|
+
bitframe = c2.bits_per_frame()
|
|
24
|
+
pa_rate = 8000
|
|
25
|
+
|
|
26
|
+
default_mic = sc.default_microphone()
|
|
27
|
+
default_speaker = sc.default_speaker()
|
|
28
|
+
print(default_mic, default_speaker)
|
|
29
|
+
sc_config = {
|
|
30
|
+
"samplerate": pa_rate,
|
|
31
|
+
"blocksize": conrate
|
|
32
|
+
}
|
|
33
|
+
with default_mic.recorder(**sc_config, channels=1) as mic, \
|
|
34
|
+
default_speaker.player(**sc_config, channels=1) as sp:
|
|
35
|
+
while 1:
|
|
36
|
+
audio = mic.record(numframes=conrate) # .transpose()
|
|
37
|
+
audio = audio.flatten()
|
|
38
|
+
audio = audio * 32767
|
|
39
|
+
audio = audio.astype("<h")
|
|
40
|
+
c2_bits = c2.encode(audio)
|
|
41
|
+
audio = c2.decode(c2_bits)
|
|
42
|
+
audio = audio.astype("float")
|
|
43
|
+
audio = audio / 32767
|
|
44
|
+
sp.play(audio)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
if __name__ == "__main__":
|
|
48
|
+
audio_test_soundcard(int(sys.argv[1]))
|