From 99c7093a1a264422bde3f480ab3d5a9c3a605ab5 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 25 Jul 2021 19:31:56 +0200 Subject: [PATCH] pack the client audio processing into its own chain --- csdr/__init__.py | 16 ++++++++++++++-- csdr/chain/am.py | 1 - csdr/chain/clientaudio.py | 14 ++++++++++++++ csdr/chain/fm.py | 1 - csdr/chain/ssb.py | 1 - 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 csdr/chain/clientaudio.py diff --git a/csdr/__init__.py b/csdr/__init__.py index 757aead..3f8cbb8 100644 --- a/csdr/__init__.py +++ b/csdr/__init__.py @@ -41,6 +41,7 @@ from csdr.chain.demodulator import DemodulatorChain from csdr.chain.fm import Fm from csdr.chain.am import Am from csdr.chain.ssb import Ssb +from csdr.chain.clientaudio import ClientAudioChain import logging @@ -51,6 +52,7 @@ class Dsp(DirewolfConfigSubscriber): def __init__(self, output: Output): self.pycsdr_enabled = True self.pycsdr_chain = None + self.pycsdr_client_chain = None self.pycsdr_reader = None self.pycsdr_power_reader = None self.buffer = None @@ -732,11 +734,19 @@ class Dsp(DirewolfConfigSubscriber): self.set_squelch_level(self.squelch_level) self.set_bpf(self.low_cut, self.high_cut) self.set_offset_freq(self.offset_freq) + chain.setInput(self.buffer) - outputBuffer = Buffer(chain.getOutputFormat()) - chain.setWriter(outputBuffer) + + self.pycsdr_client_chain = ClientAudioChain(self.get_audio_rate(), self.get_output_rate(), self.audio_compression) + buffer = Buffer(chain.getOutputFormat()) + chain.setWriter(buffer) + self.pycsdr_client_chain.setInput(buffer) + + outputBuffer = Buffer(self.pycsdr_client_chain.getOutputFormat()) + self.pycsdr_client_chain.setWriter(outputBuffer) self.pycsdr_reader = outputBuffer.getReader() self.output.send_output("audio", self.pycsdr_reader.read) + powerBuffer = Buffer(Format.FLOAT) chain.setPowerWriter(powerBuffer) self.pycsdr_power_reader = powerBuffer.getReader() @@ -839,6 +849,8 @@ class Dsp(DirewolfConfigSubscriber): self.pycsdr_reader = None self.pycsdr_power_reader.stop() self.pycsdr_power_reader = None + self.pycsdr_client_chain.stop() + self.pycsdr_client_chain = None if self.process is not None: try: os.killpg(os.getpgid(self.process.pid), signal.SIGTERM) diff --git a/csdr/chain/am.py b/csdr/chain/am.py index e57fa9a..74d40a7 100644 --- a/csdr/chain/am.py +++ b/csdr/chain/am.py @@ -12,7 +12,6 @@ class Am(Chain): AmDemod(), DcBlock(), agc, - Convert(Format.FLOAT, Format.SHORT), ] super().__init__(*workers) diff --git a/csdr/chain/clientaudio.py b/csdr/chain/clientaudio.py new file mode 100644 index 0000000..d331bf8 --- /dev/null +++ b/csdr/chain/clientaudio.py @@ -0,0 +1,14 @@ +from csdr.chain import Chain +from pycsdr.modules import AudioResampler, Convert, AdpcmEncoder +from pycsdr.types import Format + + +class ClientAudioChain(Chain): + def __init__(self, inputRate: int, clientRate: int, compression: str): + workers = [] + if inputRate != clientRate: + workers += [AudioResampler(inputRate, clientRate)] + workers += [Convert(Format.FLOAT, Format.SHORT)] + if compression == "adpcm": + workers += [AdpcmEncoder()] + super().__init__(*workers) diff --git a/csdr/chain/fm.py b/csdr/chain/fm.py index 150cce4..1254fd2 100644 --- a/csdr/chain/fm.py +++ b/csdr/chain/fm.py @@ -13,6 +13,5 @@ class Fm(Chain): Limit(), NfmDeemphasis(sampleRate), agc, - Convert(Format.FLOAT, Format.SHORT), ] super().__init__(*workers) diff --git a/csdr/chain/ssb.py b/csdr/chain/ssb.py index cf94d42..4034281 100644 --- a/csdr/chain/ssb.py +++ b/csdr/chain/ssb.py @@ -10,7 +10,6 @@ class Ssb(Chain): # empty chain as placeholder for the "last decimation" Chain(), Agc(Format.FLOAT), - Convert(Format.FLOAT, Format.SHORT), ] super().__init__(*workers)