2019-12-21 19:58:28 +00:00
|
|
|
from owrx.meta import MetaParser
|
|
|
|
from owrx.wsjt import WsjtParser
|
2020-04-12 11:10:23 +00:00
|
|
|
from owrx.js8 import Js8Parser
|
2019-12-21 19:58:28 +00:00
|
|
|
from owrx.aprs import AprsParser
|
2020-01-09 14:11:53 +00:00
|
|
|
from owrx.pocsag import PocsagParser
|
2021-03-18 18:34:53 +00:00
|
|
|
from owrx.source import SdrSourceEventClient, SdrSourceState, SdrClientClass
|
2021-01-24 21:29:23 +00:00
|
|
|
from owrx.property import PropertyStack, PropertyLayer, PropertyValidator
|
|
|
|
from owrx.property.validators import OrValidator, RegexValidator, BoolValidator
|
2020-04-26 20:46:30 +00:00
|
|
|
from owrx.modes import Modes
|
2021-02-11 18:31:44 +00:00
|
|
|
from owrx.config.core import CoreConfig
|
2021-04-09 16:16:25 +00:00
|
|
|
from csdr.output import Output
|
|
|
|
from csdr import Dsp
|
2019-12-21 19:58:28 +00:00
|
|
|
import threading
|
2021-01-24 21:29:23 +00:00
|
|
|
import re
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-01-24 21:29:23 +00:00
|
|
|
class ModulationValidator(OrValidator):
|
|
|
|
"""
|
|
|
|
This validator only allows alphanumeric characters and numbers, but no spaces or special characters
|
|
|
|
"""
|
2021-01-24 21:54:58 +00:00
|
|
|
|
2021-01-24 21:29:23 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(BoolValidator(), RegexValidator(re.compile("^[a-z0-9]+$")))
|
|
|
|
|
|
|
|
|
2021-04-09 16:16:25 +00:00
|
|
|
class DspManager(Output, SdrSourceEventClient):
|
2019-12-21 19:58:28 +00:00
|
|
|
def __init__(self, handler, sdrSource):
|
|
|
|
self.handler = handler
|
|
|
|
self.sdrSource = sdrSource
|
2020-01-09 14:11:53 +00:00
|
|
|
self.parsers = {
|
|
|
|
"meta": MetaParser(self.handler),
|
|
|
|
"wsjt_demod": WsjtParser(self.handler),
|
|
|
|
"packet_demod": AprsParser(self.handler),
|
|
|
|
"pocsag_demod": PocsagParser(self.handler),
|
2020-04-12 11:10:23 +00:00
|
|
|
"js8_demod": Js8Parser(self.handler),
|
2020-01-09 14:11:53 +00:00
|
|
|
}
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2020-03-24 21:13:42 +00:00
|
|
|
self.props = PropertyStack()
|
2021-01-24 21:29:23 +00:00
|
|
|
|
2020-03-24 21:13:42 +00:00
|
|
|
# local demodulator properties not forwarded to the sdr
|
2021-01-24 21:29:23 +00:00
|
|
|
# ensure strict validation since these can be set from the client
|
2021-01-24 21:47:08 +00:00
|
|
|
# and are used to build executable commands
|
|
|
|
validators = {
|
|
|
|
"output_rate": "int",
|
|
|
|
"hd_output_rate": "int",
|
|
|
|
"squelch_level": "num",
|
|
|
|
"secondary_mod": ModulationValidator(),
|
|
|
|
"low_cut": "num",
|
|
|
|
"high_cut": "num",
|
|
|
|
"offset_freq": "int",
|
|
|
|
"mod": ModulationValidator(),
|
|
|
|
"secondary_offset_freq": "int",
|
|
|
|
"dmr_filter": "int",
|
|
|
|
}
|
|
|
|
self.localProps = PropertyValidator(PropertyLayer().filter(*validators.keys()), validators)
|
|
|
|
|
2021-01-24 19:10:37 +00:00
|
|
|
self.props.addLayer(0, self.localProps)
|
2020-03-24 21:13:42 +00:00
|
|
|
# properties that we inherit from the sdr
|
2020-12-30 16:46:13 +00:00
|
|
|
self.props.addLayer(
|
|
|
|
1,
|
|
|
|
self.sdrSource.getProps().filter(
|
|
|
|
"audio_compression",
|
|
|
|
"fft_compression",
|
|
|
|
"digimodes_fft_size",
|
|
|
|
"samp_rate",
|
|
|
|
"center_freq",
|
|
|
|
"start_mod",
|
|
|
|
"start_freq",
|
|
|
|
"wfm_deemphasis_tau",
|
2021-05-29 16:50:17 +00:00
|
|
|
"digital_voice_codecserver",
|
2020-12-30 16:46:13 +00:00
|
|
|
),
|
|
|
|
)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-04-09 16:16:25 +00:00
|
|
|
self.dsp = Dsp(self)
|
2019-12-21 19:58:28 +00:00
|
|
|
self.dsp.nc_port = self.sdrSource.getPort()
|
|
|
|
|
|
|
|
def set_low_cut(cut):
|
|
|
|
bpf = self.dsp.get_bpf()
|
|
|
|
bpf[0] = cut
|
|
|
|
self.dsp.set_bpf(*bpf)
|
|
|
|
|
|
|
|
def set_high_cut(cut):
|
|
|
|
bpf = self.dsp.get_bpf()
|
|
|
|
bpf[1] = cut
|
|
|
|
self.dsp.set_bpf(*bpf)
|
|
|
|
|
2020-12-30 16:18:46 +00:00
|
|
|
def set_dial_freq(changes):
|
2020-12-30 16:46:13 +00:00
|
|
|
if (
|
|
|
|
"center_freq" not in self.props
|
|
|
|
or self.props["center_freq"] is None
|
|
|
|
or "offset_freq" not in self.props
|
|
|
|
or self.props["offset_freq"] is None
|
|
|
|
):
|
2020-10-10 22:15:09 +00:00
|
|
|
return
|
2020-03-24 21:13:42 +00:00
|
|
|
freq = self.props["center_freq"] + self.props["offset_freq"]
|
2020-01-09 14:11:53 +00:00
|
|
|
for parser in self.parsers.values():
|
|
|
|
parser.setDialFrequency(freq)
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2020-04-26 20:46:30 +00:00
|
|
|
if "start_mod" in self.props:
|
|
|
|
self.dsp.set_demodulator(self.props["start_mod"])
|
|
|
|
mode = Modes.findByModulation(self.props["start_mod"])
|
|
|
|
|
|
|
|
if mode and mode.bandpass:
|
|
|
|
self.dsp.set_bpf(mode.bandpass.low_cut, mode.bandpass.high_cut)
|
|
|
|
else:
|
|
|
|
self.dsp.set_bpf(-4000, 4000)
|
|
|
|
|
|
|
|
if "start_freq" in self.props and "center_freq" in self.props:
|
|
|
|
self.dsp.set_offset_freq(self.props["start_freq"] - self.props["center_freq"])
|
|
|
|
else:
|
|
|
|
self.dsp.set_offset_freq(0)
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
self.subscriptions = [
|
2020-03-24 21:13:42 +00:00
|
|
|
self.props.wireProperty("audio_compression", self.dsp.set_audio_compression),
|
|
|
|
self.props.wireProperty("fft_compression", self.dsp.set_fft_compression),
|
|
|
|
self.props.wireProperty("digimodes_fft_size", self.dsp.set_secondary_fft_size),
|
|
|
|
self.props.wireProperty("samp_rate", self.dsp.set_samp_rate),
|
|
|
|
self.props.wireProperty("output_rate", self.dsp.set_output_rate),
|
2020-08-08 19:29:25 +00:00
|
|
|
self.props.wireProperty("hd_output_rate", self.dsp.set_hd_output_rate),
|
2020-03-24 21:13:42 +00:00
|
|
|
self.props.wireProperty("offset_freq", self.dsp.set_offset_freq),
|
2020-04-05 14:35:46 +00:00
|
|
|
self.props.wireProperty("center_freq", self.dsp.set_center_freq),
|
2020-03-24 21:13:42 +00:00
|
|
|
self.props.wireProperty("squelch_level", self.dsp.set_squelch_level),
|
|
|
|
self.props.wireProperty("low_cut", set_low_cut),
|
|
|
|
self.props.wireProperty("high_cut", set_high_cut),
|
|
|
|
self.props.wireProperty("mod", self.dsp.set_demodulator),
|
|
|
|
self.props.wireProperty("dmr_filter", self.dsp.set_dmr_filter),
|
2020-10-04 19:56:35 +00:00
|
|
|
self.props.wireProperty("wfm_deemphasis_tau", self.dsp.set_wfm_deemphasis_tau),
|
2021-05-29 16:50:17 +00:00
|
|
|
self.props.wireProperty("digital_voice_codecserver", self.dsp.set_codecserver),
|
2020-03-24 21:16:11 +00:00
|
|
|
self.props.filter("center_freq", "offset_freq").wire(set_dial_freq),
|
2019-12-21 19:58:28 +00:00
|
|
|
]
|
|
|
|
|
2021-02-06 20:55:47 +00:00
|
|
|
self.dsp.set_temporary_directory(CoreConfig().get_temporary_directory())
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-04-17 22:50:13 +00:00
|
|
|
def send_secondary_config(*args):
|
|
|
|
self.handler.write_secondary_dsp_config(
|
|
|
|
{
|
|
|
|
"secondary_fft_size": self.props["digimodes_fft_size"],
|
|
|
|
"if_samp_rate": self.dsp.if_samp_rate(),
|
|
|
|
"secondary_bw": self.dsp.secondary_bw(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
def set_secondary_mod(mod):
|
|
|
|
if mod == False:
|
|
|
|
mod = None
|
|
|
|
self.dsp.set_secondary_demodulator(mod)
|
|
|
|
if mod is not None:
|
|
|
|
send_secondary_config()
|
|
|
|
|
|
|
|
self.subscriptions += [
|
|
|
|
self.props.wireProperty("secondary_mod", set_secondary_mod),
|
|
|
|
self.props.wireProperty("digimodes_fft_size", send_secondary_config),
|
|
|
|
self.props.wireProperty("secondary_offset_freq", self.dsp.set_secondary_offset_freq),
|
|
|
|
]
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2020-04-30 20:54:44 +00:00
|
|
|
self.startOnAvailable = False
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
self.sdrSource.addClient(self)
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if self.sdrSource.isAvailable():
|
2021-07-19 17:04:14 +00:00
|
|
|
self.dsp.setBuffer(self.sdrSource.getBuffer())
|
2019-12-21 19:58:28 +00:00
|
|
|
self.dsp.start()
|
2020-04-30 20:54:44 +00:00
|
|
|
else:
|
|
|
|
self.startOnAvailable = True
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
def receive_output(self, t, read_fn):
|
|
|
|
logger.debug("adding new output of type %s", t)
|
|
|
|
writers = {
|
|
|
|
"audio": self.handler.write_dsp_data,
|
2020-08-08 19:29:25 +00:00
|
|
|
"hd_audio": self.handler.write_hd_audio,
|
2019-12-21 19:58:28 +00:00
|
|
|
"smeter": self.handler.write_s_meter_level,
|
|
|
|
"secondary_fft": self.handler.write_secondary_fft,
|
|
|
|
"secondary_demod": self.handler.write_secondary_demod,
|
|
|
|
}
|
2020-01-09 14:11:53 +00:00
|
|
|
for demod, parser in self.parsers.items():
|
|
|
|
writers[demod] = parser.parse
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
write = writers[t]
|
|
|
|
|
2020-08-14 18:22:25 +00:00
|
|
|
threading.Thread(target=self.pump(read_fn, write), name="dsp_pump_{}".format(t)).start()
|
2019-12-21 19:58:28 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.dsp.stop()
|
2020-04-30 20:54:44 +00:00
|
|
|
self.startOnAvailable = False
|
2019-12-21 19:58:28 +00:00
|
|
|
self.sdrSource.removeClient(self)
|
|
|
|
for sub in self.subscriptions:
|
|
|
|
sub.cancel()
|
|
|
|
self.subscriptions = []
|
|
|
|
|
2020-04-30 20:07:19 +00:00
|
|
|
def setProperties(self, props):
|
|
|
|
for k, v in props.items():
|
|
|
|
self.setProperty(k, v)
|
|
|
|
|
2019-12-21 19:58:28 +00:00
|
|
|
def setProperty(self, prop, value):
|
2021-01-24 19:10:37 +00:00
|
|
|
self.localProps[prop] = value
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def getClientClass(self) -> SdrClientClass:
|
|
|
|
return SdrClientClass.USER
|
2019-12-21 19:58:28 +00:00
|
|
|
|
2021-02-20 21:54:07 +00:00
|
|
|
def onStateChange(self, state: SdrSourceState):
|
|
|
|
if state is SdrSourceState.RUNNING:
|
2019-12-21 19:58:28 +00:00
|
|
|
logger.debug("received STATE_RUNNING, attempting DspSource restart")
|
2020-04-30 20:54:44 +00:00
|
|
|
if self.startOnAvailable:
|
2021-07-19 17:04:14 +00:00
|
|
|
self.dsp.setBuffer(self.sdrSource.getBuffer())
|
2020-04-30 20:54:44 +00:00
|
|
|
self.dsp.start()
|
|
|
|
self.startOnAvailable = False
|
2021-02-20 21:54:07 +00:00
|
|
|
elif state is SdrSourceState.STOPPING:
|
2019-12-21 19:58:28 +00:00
|
|
|
logger.debug("received STATE_STOPPING, shutting down DspSource")
|
|
|
|
self.dsp.stop()
|
|
|
|
|
2021-03-18 18:34:53 +00:00
|
|
|
def onFail(self):
|
|
|
|
logger.debug("received onFail(), shutting down DspSource")
|
|
|
|
self.dsp.stop()
|
2021-03-18 21:59:46 +00:00
|
|
|
|
|
|
|
def onShutdown(self):
|
|
|
|
self.dsp.stop()
|