encapsulate fft chain in its own class

This commit is contained in:
Jakob Ketterl 2020-12-16 18:52:00 +01:00
parent 4b61192b36
commit 1bd6aa73f3
3 changed files with 43 additions and 13 deletions

16
csdr/chain/__init__.py Normal file
View File

@ -0,0 +1,16 @@
class Chain(object):
def __init__(self, *workers):
self.workers = workers
stage = None
for w in self.workers:
if stage is not None:
w.setInput(stage.getBuffer())
stage = w
self.buffer = stage.getBuffer()
def stop(self):
for w in self.workers:
w.stop()
def getBuffer(self):
return self.buffer

15
csdr/chain/fft.py Normal file
View File

@ -0,0 +1,15 @@
from csdr.chain import Chain
from pycsdr import SocketClient, Fft, LogAveragePower, FftExchangeSides, CompressFftAdpcm
class FftChain(Chain):
def __init__(self, port, fft_size, fft_block_size, fft_averages, fft_compression):
workers = [
SocketClient(port=port),
Fft(size=fft_size, every_n_samples=int(fft_block_size)),
LogAveragePower(add_db=-70, fft_size=fft_size, avg_number=fft_averages),
FftExchangeSides(fft_size=fft_size),
]
if fft_compression == "adpcm":
workers += [CompressFftAdpcm(fft_size=fft_size)]
super().__init__(*workers)

View File

@ -34,8 +34,7 @@ from owrx.js8 import Js8Profiles
from owrx.audio import AudioChopper from owrx.audio import AudioChopper
from csdr.pipe import Pipe from csdr.pipe import Pipe
from csdr.chain.fft import FftChain
from pycsdr import SocketClient, Fft, LogAveragePower, FftExchangeSides
import logging import logging
@ -77,6 +76,7 @@ class output(object):
class dsp(object): class dsp(object):
def __init__(self, output): def __init__(self, output):
self.pycsdr_enabled = True self.pycsdr_enabled = True
self.pycsdr_chain = None
self.samp_rate = 250000 self.samp_rate = 250000
self.output_rate = 11025 self.output_rate = 11025
@ -765,18 +765,15 @@ class dsp(object):
return return
self.running = True self.running = True
nc = SocketClient(self.nc_port) self.pycsdr_chain = FftChain(
port=self.nc_port,
fft_size=self.fft_size,
fft_block_size=self.fft_block_size(),
fft_averages=self.fft_averages,
fft_compression=self.fft_compression
)
fft = Fft(self.fft_size, int(self.fft_block_size())) self.output.send_output("audio", self.pycsdr_chain.getBuffer().read)
fft.setInput(nc.getBuffer())
lap = LogAveragePower(-70, self.fft_size, self.fft_averages)
lap.setInput(fft.getBuffer())
fes = FftExchangeSides(fft_size=self.fft_size)
fes.setInput(lap.getBuffer())
self.output.send_output("audio", fes.getBuffer().read)
return return
with self.modification_lock: with self.modification_lock:
@ -883,6 +880,8 @@ class dsp(object):
def stop(self): def stop(self):
with self.modification_lock: with self.modification_lock:
self.running = False self.running = False
if self.pycsdr_enabled and self.pycsdr_chain is not None:
self.pycsdr_chain.stop()
if self.process is not None: if self.process is not None:
try: try:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM) os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)