Repository: livekit/agents
File: livekit-agents/livekit/agents/voice/room_io/_input.py (Python)

Code before:
            and self._room._token is not None
            and self._room._server_url is not None
        ):
            self.processor._update_credentials(token=self._room._token, url=self._room._server_url)


class _ParticipantAudioInputStream(_ParticipantInputStream[rtc.AudioFrame], AudioInput):
    def __init__(
        self,
        room: rtc.Room,
        *,
        sample_rate: int,
        num_channels: int,
        noise_cancellation: rtc.NoiseCancellationOptions
        | rtc.FrameProcessor[rtc.AudioFrame]
        | None,
        pre_connect_audio_handler: PreConnectAudioHandler | None,
    ) -> None:
        audio_processor: rtc.FrameProcessor[rtc.AudioFrame] | None = None
        if isinstance(noise_cancellation, rtc.FrameProcessor):
            audio_processor = noise_cancellation

        _ParticipantInputStream.__init__(
            self,
            room=room,
            track_source=rtc.TrackSource.SOURCE_MICROPHONE,
            processor=audio_processor,
        )
        AudioInput.__init__(self, label="RoomIO")
        self._sample_rate = sample_rate
        self._num_channels = num_channels
        self._noise_cancellation = noise_cancellation
        self._pre_connect_audio_handler = pre_connect_audio_handler

    @override
    def _create_stream(self, track: rtc.Track) -> rtc.AudioStream:
        return rtc.AudioStream.from_track(
            track=track,
            sample_rate=self._sample_rate,
            num_channels=self._num_channels,
            noise_cancellation=self._noise_cancellation,
            frame_size_ms=50,
        )

    @override
    async def _forward_task(
        self,
        old_task: asyncio.Task[None] | None,
        stream: rtc.AudioStream,  # type: ignore[override]
        publication: rtc.RemoteTrackPublication,
        participant: rtc.RemoteParticipant,

Review comment (refactor):
python sdk already handles the `noise_cancellation` if is a `FrameProcessor` in https://github.com/livekit/python-sdks/pull/533/files#diff-7e2485e8b1480ff233d810a2d222fe14cb1432d0837ce28a6e6182ae8b2422b6R112-R113, here it's processed duplicately?

Code after:
            self._processor is not None
            and self._room._token is not None
            and self._room._server_url is not None
        ):
            self._processor._on_credentials_updated(
                token=self._room._token, url=self._room._server_url
            )


class _ParticipantAudioInputStream(_ParticipantInputStream[rtc.AudioFrame], AudioInput):
    def __init__(
        self,
        room: rtc.Room,
        *,
        sample_rate: int,
        num_channels: int,
        noise_cancellation: rtc.NoiseCancellationOptions
        | NoiseCancellationSelector
        | rtc.FrameProcessor[rtc.AudioFrame]
        | None,
        pre_connect_audio_handler: PreConnectAudioHandler | None,
        frame_size_ms: int = 50,
    ) -> None:
        audio_processor: rtc.FrameProcessor[rtc.AudioFrame] | None = None
        if isinstance(noise_cancellation, rtc.FrameProcessor):
            audio_processor = noise_cancellation

        _ParticipantInputStream.__init__(
            self,
            room=room,
            track_source=rtc.TrackSource.SOURCE_MICROPHONE,
            processor=audio_processor,
        )
        AudioInput.__init__(self, label="RoomIO")
        if frame_size_ms <= 0:
            raise ValueError("frame_size_ms must be greater than 0")

        self._sample_rate = sample_rate
        self._num_channels = num_channels
        self._frame_size_ms = frame_size_ms
        self._noise_cancellation = noise_cancellation
        self._pre_connect_audio_handler = pre_connect_audio_handler

    @override
    def _create_stream(self, track: rtc.Track, participant: rtc.Participant) -> rtc.AudioStream:
        noise_cancellation = (
            self._noise_cancellation(NoiseCancellationParams(participant, track))
            if callable(self._noise_cancellation)
            else self._noise_cancellation
        )