2021-07-25 17:31:56 +00:00
|
|
|
from csdr.chain import Chain
|
2022-06-18 18:23:50 +00:00
|
|
|
from pycsdr.modules import AudioResampler, Convert, AdpcmEncoder, OpusEncoder, Limit
|
2021-07-25 17:31:56 +00:00
|
|
|
from pycsdr.types import Format
|
|
|
|
|
|
|
|
|
2021-08-26 13:58:02 +00:00
|
|
|
class Converter(Chain):
|
|
|
|
def __init__(self, format: Format, inputRate: int, clientRate: int):
|
2021-07-25 17:31:56 +00:00
|
|
|
workers = []
|
|
|
|
if inputRate != clientRate:
|
2021-08-12 16:01:03 +00:00
|
|
|
# we only have an audio resampler for float ATM so if we need to resample, we need to convert
|
|
|
|
if format != Format.FLOAT:
|
|
|
|
workers += [Convert(format, Format.FLOAT)]
|
|
|
|
workers += [AudioResampler(inputRate, clientRate), Limit(), Convert(Format.FLOAT, Format.SHORT)]
|
|
|
|
elif format != Format.SHORT:
|
|
|
|
workers += [Convert(format, Format.SHORT)]
|
2021-08-26 13:58:02 +00:00
|
|
|
super().__init__(workers)
|
|
|
|
|
|
|
|
|
|
|
|
class ClientAudioChain(Chain):
|
|
|
|
def __init__(self, format: Format, inputRate: int, clientRate: int, compression: str):
|
|
|
|
self.format = format
|
|
|
|
self.inputRate = inputRate
|
|
|
|
self.clientRate = clientRate
|
2021-08-27 14:11:03 +00:00
|
|
|
workers = []
|
|
|
|
converter = self._buildConverter()
|
|
|
|
if not converter.empty():
|
|
|
|
workers += [converter]
|
2021-07-25 17:31:56 +00:00
|
|
|
if compression == "adpcm":
|
2021-07-25 18:06:14 +00:00
|
|
|
workers += [AdpcmEncoder(sync=True)]
|
2022-06-18 18:23:50 +00:00
|
|
|
elif compression == "opus":
|
|
|
|
workers += [OpusEncoder()]
|
2021-08-23 12:25:28 +00:00
|
|
|
super().__init__(workers)
|
|
|
|
|
2021-08-26 13:58:02 +00:00
|
|
|
def _buildConverter(self):
|
2022-06-18 21:24:40 +00:00
|
|
|
return Converter(self.format, self.inputRate, 12000)
|
2021-08-26 13:58:02 +00:00
|
|
|
|
2021-08-27 14:11:03 +00:00
|
|
|
def _updateConverter(self):
|
|
|
|
converter = self._buildConverter()
|
|
|
|
index = self.indexOf(lambda x: isinstance(x, Converter))
|
|
|
|
if converter.empty():
|
|
|
|
if index >= 0:
|
|
|
|
self.remove(index)
|
|
|
|
else:
|
|
|
|
if index >= 0:
|
|
|
|
self.replace(index, converter)
|
|
|
|
else:
|
|
|
|
self.insert(converter)
|
|
|
|
|
2021-08-23 12:25:28 +00:00
|
|
|
def setFormat(self, format: Format) -> None:
|
2021-08-26 13:58:02 +00:00
|
|
|
if format == self.format:
|
|
|
|
return
|
|
|
|
self.format = format
|
2021-08-27 14:11:03 +00:00
|
|
|
self._updateConverter()
|
2021-08-23 12:25:28 +00:00
|
|
|
|
|
|
|
def setInputRate(self, inputRate: int) -> None:
|
|
|
|
if inputRate == self.inputRate:
|
|
|
|
return
|
2021-08-26 13:58:02 +00:00
|
|
|
self.inputRate = inputRate
|
2021-08-27 14:11:03 +00:00
|
|
|
self._updateConverter()
|
2021-08-23 12:25:28 +00:00
|
|
|
|
|
|
|
def setClientRate(self, clientRate: int) -> None:
|
|
|
|
if clientRate == self.clientRate:
|
|
|
|
return
|
2021-08-26 13:58:02 +00:00
|
|
|
self.clientRate = clientRate
|
2021-08-27 14:11:03 +00:00
|
|
|
self._updateConverter()
|
2021-08-23 12:25:28 +00:00
|
|
|
|
|
|
|
def setAudioCompression(self, compression: str) -> None:
|
2022-06-18 18:23:50 +00:00
|
|
|
index = self.indexOf(lambda x: isinstance(x, AdpcmEncoder) or isinstance(x, OpusEncoder))
|
|
|
|
newEncoder = None
|
2021-08-23 12:25:28 +00:00
|
|
|
if compression == "adpcm":
|
2022-06-18 18:23:50 +00:00
|
|
|
newEncoder = AdpcmEncoder(sync=True)
|
|
|
|
elif compression == "opus":
|
|
|
|
newEncoder = OpusEncoder()
|
|
|
|
|
|
|
|
if newEncoder:
|
2021-08-23 12:25:28 +00:00
|
|
|
if index < 0:
|
2022-06-18 18:23:50 +00:00
|
|
|
self.append(newEncoder)
|
|
|
|
else:
|
|
|
|
self.replace(index, newEncoder)
|
2021-08-23 12:25:28 +00:00
|
|
|
else:
|
|
|
|
if index >= 0:
|
|
|
|
self.remove(index)
|