-
Notifications
You must be signed in to change notification settings - Fork 109
Add frame processor support for audio streams #533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
60ce71f
0d378f2
d786dbd
cb8745c
67802ee
6837888
726936a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| from .audio_frame import AudioFrame | ||
| from .participant import Participant | ||
| from .track import Track | ||
| from .frame_processor import FrameProcessor | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -62,7 +63,7 @@ def __init__( | |
| sample_rate: int = 48000, | ||
| num_channels: int = 1, | ||
| frame_size_ms: int | None = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions] = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None, | ||
| **kwargs, | ||
| ) -> None: | ||
| """Initialize an `AudioStream` instance. | ||
|
|
@@ -76,8 +77,8 @@ def __init__( | |
| sample_rate (int, optional): The sample rate for the audio stream in Hz. | ||
| Defaults to 48000. | ||
| num_channels (int, optional): The number of audio channels. Defaults to 1. | ||
| noise_cancellation (Optional[NoiseCancellationOptions], optional): | ||
| If noise cancellation is used, pass a `NoiseCancellationOptions` instance | ||
| noise_cancellation (Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]], optional): | ||
| If noise cancellation is used, pass a `NoiseCancellationOptions` or `FrameProcessor[AudioFrame]` instance | ||
| created by the noise cancellation module. | ||
|
|
||
| Example: | ||
|
|
@@ -105,9 +106,12 @@ def __init__( | |
|
|
||
| self._audio_filter_module = None | ||
| self._audio_filter_options = None | ||
| if noise_cancellation is not None: | ||
| if isinstance(noise_cancellation, NoiseCancellationOptions): | ||
| self._audio_filter_module = noise_cancellation.module_id | ||
| self._audio_filter_options = noise_cancellation.options | ||
| elif isinstance(noise_cancellation, FrameProcessor): | ||
| self._processor = noise_cancellation | ||
|
|
||
| self._task = self._loop.create_task(self._run()) | ||
| self._task.add_done_callback(task_done_logger) | ||
|
|
||
|
|
@@ -132,7 +136,7 @@ def from_participant( | |
| sample_rate: int = 48000, | ||
| num_channels: int = 1, | ||
| frame_size_ms: int | None = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions] = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None, | ||
| ) -> AudioStream: | ||
| """Create an `AudioStream` from a participant's audio track. | ||
|
|
||
|
|
@@ -182,7 +186,7 @@ def from_track( | |
| sample_rate: int = 48000, | ||
| num_channels: int = 1, | ||
| frame_size_ms: int | None = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions] = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None, | ||
| ) -> AudioStream: | ||
| """Create an `AudioStream` from an existing audio track. | ||
|
|
||
|
|
@@ -268,6 +272,8 @@ async def _run(self): | |
| if audio_event.HasField("frame_received"): | ||
| owned_buffer_info = audio_event.frame_received.frame | ||
| frame = AudioFrame._from_owned_info(owned_buffer_info) | ||
| if self._processor is not None: | ||
| frame = self._processor._process(frame) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat.. this is clean |
||
| event = AudioFrameEvent(frame) | ||
| self._queue.put(event) | ||
| elif audio_event.HasField("eos"): | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||
| from abc import ABC, abstractmethod | ||||||
| from typing import Generic, TypeVar, Union | ||||||
| from .audio_frame import AudioFrame | ||||||
| from .video_frame import VideoFrame | ||||||
|
|
||||||
|
|
||||||
| T = TypeVar("T", bound=Union[AudioFrame, VideoFrame]) | ||||||
|
|
||||||
|
|
||||||
| class FrameProcessor(Generic[T], ABC): | ||||||
lukasIO marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| @property | ||||||
| @abstractmethod | ||||||
| def is_enabled(self) -> bool: ... | ||||||
|
|
||||||
| @abstractmethod | ||||||
| def set_enabled(self, enable: bool): ... | ||||||
|
Comment on lines
+11
to
+16
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know if using a property would work? It seems more python idiomatic
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be used like: processor.enabled = False
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds fine to me! |
||||||
|
|
||||||
| def _update_stream_info( | ||||||
| self, | ||||||
| *, | ||||||
| room_name: str, | ||||||
| participant_identity: str, | ||||||
| publication_sid: str, | ||||||
| ): ... | ||||||
|
|
||||||
| def _update_credentials(self, *, token: str, url: str): ... | ||||||
|
|
||||||
| @abstractmethod | ||||||
| def _process(self, frame: T) -> T: ... | ||||||
|
|
||||||
| @abstractmethod | ||||||
| def _close(self): ... | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's OK for close to be public
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking Users would be expected to disable it with |
||||||
Uh oh!
There was an error while loading. Please reload this page.