From 3394c9784c648a26707f960a6320bae9501a283e Mon Sep 17 00:00:00 2001 From: Andrei Date: Thu, 6 Apr 2017 23:13:53 -0700 Subject: [PATCH] Add a proxy, allow for more pipin' --- disco/voice/playable.py | 51 ++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/disco/voice/playable.py b/disco/voice/playable.py index 0f438cf..ba55099 100644 --- a/disco/voice/playable.py +++ b/disco/voice/playable.py @@ -36,15 +36,30 @@ class AbstractOpus(object): self.frame_size = self.samples_per_frame * self.sample_size +class BaseUtil(object): + def pipe(self, other, *args, **kwargs): + child = other(self, *args, **kwargs) + setattr(child, 'metadata', self.metadata) + return child + + @property + def metadata(self): + return self._metadata + + @metadata.setter + def metadata(self, value): + self._metadata = value + + @six.add_metaclass(abc.ABCMeta) -class BasePlayable(object): +class BasePlayable(BaseUtil): @abc.abstractmethod def next_frame(self): raise NotImplementedError @six.add_metaclass(abc.ABCMeta) -class BaseInput(object): +class BaseInput(BaseUtil): @abc.abstractmethod def read(self, size): raise NotImplementedError @@ -53,15 +68,13 @@ class BaseInput(object): def fileobj(self): raise NotImplementedError - def pipe(self, other, *args, **kwargs): - return other(self, *args, **kwargs) - -class OpusFilePlayable(BasePlayable): +class OpusFilePlayable(BasePlayable, AbstractOpus): """ An input which reads opus data from a file or file-like object. """ - def __init__(self, fobj): + def __init__(self, fobj, *args, **kwargs): + super(OpusFilePlayable, self).__init__(*args, **kwargs) self.fobj = fobj self.done = False @@ -104,7 +117,7 @@ class FFmpegInput(BaseInput, AbstractOpus): info = info['entries'][0] result = cls(source=info['url'], *args, **kwargs) - result.info = info + result.metadata = info return result def read(self, sz): @@ -221,3 +234,25 @@ class DCADOpusEncoderPlayable(BasePlayable, AbstractOpus, OpusEncoder): return return data + + +class FileProxyPlayable(BasePlayable, AbstractOpus): + def __init__(self, other, output, *args, **kwargs): + self.flush = kwargs.pop('flush', False) + super(FileProxyPlayable, self).__init__(*args, **kwargs) + self.other = other + self.output = output + + def next_frame(self): + frame = self.other.next_frame() + + if frame: + self.output.write(struct.pack('