2727from .audio_frame import AudioFrame
2828from .participant import Participant
2929from .track import Track
30+ from .frame_processor import FrameProcessor
3031
3132
3233@dataclass
@@ -62,7 +63,7 @@ def __init__(
6263 sample_rate : int = 48000 ,
6364 num_channels : int = 1 ,
6465 frame_size_ms : int | None = None ,
65- noise_cancellation : Optional [NoiseCancellationOptions ] = None ,
66+ noise_cancellation : Optional [NoiseCancellationOptions | FrameProcessor [ AudioFrame ] ] = None ,
6667 ** kwargs ,
6768 ) -> None :
6869 """Initialize an `AudioStream` instance.
@@ -76,8 +77,8 @@ def __init__(
7677 sample_rate (int, optional): The sample rate for the audio stream in Hz.
7778 Defaults to 48000.
7879 num_channels (int, optional): The number of audio channels. Defaults to 1.
79- noise_cancellation (Optional[NoiseCancellationOptions], optional):
80- If noise cancellation is used, pass a `NoiseCancellationOptions` instance
80+ noise_cancellation (Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame] ], optional):
81+ If noise cancellation is used, pass a `NoiseCancellationOptions` or `FrameProcessor[AudioFrame]` instance
8182 created by the noise cancellation module.
8283
8384 Example:
@@ -105,9 +106,12 @@ def __init__(
105106
106107 self ._audio_filter_module = None
107108 self ._audio_filter_options = None
108- if noise_cancellation is not None :
109+ if isinstance ( noise_cancellation , NoiseCancellationOptions ) :
109110 self ._audio_filter_module = noise_cancellation .module_id
110111 self ._audio_filter_options = noise_cancellation .options
112+ elif isinstance (noise_cancellation , FrameProcessor ):
113+ self ._processor = noise_cancellation
114+
111115 self ._task = self ._loop .create_task (self ._run ())
112116 self ._task .add_done_callback (task_done_logger )
113117
@@ -268,6 +272,8 @@ async def _run(self):
268272 if audio_event .HasField ("frame_received" ):
269273 owned_buffer_info = audio_event .frame_received .frame
270274 frame = AudioFrame ._from_owned_info (owned_buffer_info )
275+ if self ._processor is not None :
276+ frame = self ._processor ._process (frame )
271277 event = AudioFrameEvent (frame )
272278 self ._queue .put (event )
273279 elif audio_event .HasField ("eos" ):
0 commit comments