openwebrx-clone/csdr/chain/__init__.py

60 lines
1.5 KiB
Python
Raw Normal View History

2021-01-02 02:12:21 +00:00
from pycsdr.modules import Buffer
import logging
logger = logging.getLogger(__name__)
2021-07-16 14:12:16 +00:00
class Chain:
2020-12-16 17:52:00 +00:00
def __init__(self, *workers):
self.input = None
self.output = None
2020-12-16 17:52:00 +00:00
self.workers = workers
for i in range(1, len(self.workers)):
self._connect(self.workers[i - 1], self.workers[i])
def _connect(self, w1, w2):
2021-07-16 14:12:16 +00:00
buffer = Buffer(w1.getOutputFormat())
w1.setOutput(buffer)
w2.setInput(buffer)
2020-12-16 17:52:00 +00:00
def stop(self):
2021-01-17 20:01:54 +00:00
if self.output is not None:
self.output.stop()
2020-12-16 17:52:00 +00:00
for w in self.workers:
w.stop()
def setInput(self, buffer):
if self.input == buffer:
return
self.input = buffer
self.workers[0].setInput(buffer)
def setOutput(self, buffer):
if self.output == buffer:
return
self.output = buffer
self.workers[-1].setOutput(buffer)
2021-07-16 14:12:16 +00:00
def getOutputFormat(self):
return self.workers[-1].getOutputFormat()
def pump(self, write):
if self.output is None:
2021-07-16 14:12:16 +00:00
self.setOutput(Buffer(self.getOutputFormat()))
def copy():
run = True
while run:
data = None
try:
data = self.output.read()
except ValueError:
pass
if data is None or (isinstance(data, bytes) and len(data) == 0):
run = False
else:
write(data)
return copy