From 25ef0cb87badb91d0db3ff451cb4a86b863d2311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Fri, 21 Jun 2024 08:20:25 -0700 Subject: [PATCH] serializers: allow str | bytes | None --- src/pipecat/serializers/base_serializer.py | 4 ++-- src/pipecat/serializers/protobuf.py | 4 ++-- .../serializers/{TwilioFrameSerializer.py => twilio.py} | 0 src/pipecat/utils/audio.py | 4 +++- 4 files changed, 7 insertions(+), 5 deletions(-) rename src/pipecat/serializers/{TwilioFrameSerializer.py => twilio.py} (100%) diff --git a/src/pipecat/serializers/base_serializer.py b/src/pipecat/serializers/base_serializer.py index 71d274cff..83613d9ce 100644 --- a/src/pipecat/serializers/base_serializer.py +++ b/src/pipecat/serializers/base_serializer.py @@ -12,9 +12,9 @@ from pipecat.frames.frames import Frame class FrameSerializer(ABC): @abstractmethod - def serialize(self, frame: Frame) -> bytes: + def serialize(self, frame: Frame) -> str | bytes | None: pass @abstractmethod - def deserialize(self, data: bytes) -> Frame | None: + def deserialize(self, data: str | bytes) -> Frame | None: pass diff --git a/src/pipecat/serializers/protobuf.py b/src/pipecat/serializers/protobuf.py index c51fe8869..0a6dee0b1 100644 --- a/src/pipecat/serializers/protobuf.py +++ b/src/pipecat/serializers/protobuf.py @@ -26,7 +26,7 @@ class ProtobufFrameSerializer(FrameSerializer): def __init__(self): pass - def serialize(self, frame: Frame) -> bytes: + def serialize(self, frame: Frame) -> str | bytes | None: proto_frame = frame_protos.Frame() if type(frame) not in self.SERIALIZABLE_TYPES: raise ValueError( @@ -41,7 +41,7 @@ class ProtobufFrameSerializer(FrameSerializer): result = proto_frame.SerializeToString() return result - def deserialize(self, data: bytes) -> Frame | None: + def deserialize(self, data: str | bytes) -> Frame | None: """Returns a Frame object from a Frame protobuf. Used to convert frames passed over the wire as protobufs to Frame objects used in pipelines and frame processors. diff --git a/src/pipecat/serializers/TwilioFrameSerializer.py b/src/pipecat/serializers/twilio.py similarity index 100% rename from src/pipecat/serializers/TwilioFrameSerializer.py rename to src/pipecat/serializers/twilio.py diff --git a/src/pipecat/utils/audio.py b/src/pipecat/utils/audio.py index e1ee4698b..a3fc1b0ab 100644 --- a/src/pipecat/utils/audio.py +++ b/src/pipecat/utils/audio.py @@ -33,6 +33,7 @@ def calculate_audio_volume(audio: bytes, sample_rate: int) -> float: def exp_smoothing(value: float, prev_value: float, factor: float) -> float: return prev_value + factor * (value - prev_value) + def ulaw_8000_to_pcm_16000(ulaw_8000_bytes): # Convert μ-law to PCM pcm_8000_bytes = audioop.ulaw2lin(ulaw_8000_bytes, 2) @@ -42,6 +43,7 @@ def ulaw_8000_to_pcm_16000(ulaw_8000_bytes): return pcm_16000_bytes + def pcm_16000_to_ulaw_8000(pcm_16000_bytes): # Resample from 16000 Hz to 8000 Hz pcm_8000_bytes = audioop.ratecv(pcm_16000_bytes, 2, 1, 16000, 8000, None)[0] @@ -49,4 +51,4 @@ def pcm_16000_to_ulaw_8000(pcm_16000_bytes): # Convert PCM to μ-law ulaw_8000_bytes = audioop.lin2ulaw(pcm_8000_bytes, 2) - return ulaw_8000_bytes \ No newline at end of file + return ulaw_8000_bytes