From 7de2590d0a7324f2caa0dd29ea15082a690ade92 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 13 Sep 2024 10:18:40 +0800 Subject: [PATCH] bugfix --- rtcllm/a2a.py | 4 ++-- rtcllm/aav.py | 35 ++++++++++++++++++++++++++++------- rtcllm/rtc.py | 2 +- rtcllm/vad.py | 4 +++- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/rtcllm/a2a.py b/rtcllm/a2a.py index e37152d..32ca72e 100644 --- a/rtcllm/a2a.py +++ b/rtcllm/a2a.py @@ -74,7 +74,7 @@ class LLMAudioStreamTrack(AudioStreamTrack): if audio_file is None: print(f'*****{self.__class__.__name__}._feed(),{audio_file=}') return - x = self.oi.run_AT_batch_stream(audio_file) - self.audio_iters.append(x) + abiters = self.oi.run_AT_batch_stream(audio_file) + self.audio_iters.append(abiters) return x diff --git a/rtcllm/aav.py b/rtcllm/aav.py index 38f8b3b..4f38b0e 100644 --- a/rtcllm/aav.py +++ b/rtcllm/aav.py @@ -7,8 +7,10 @@ from aiortc.mediastreams import MediaStreamError class MyMediaPlayer(MediaPlayer): pass -class MyTrackBase: +class MyTrackBase(MediaStreamTrack): def __init__(self, source=None): + super().__init__() + self.dumb = None self.source = source self.set_source_track() print(f'{self.kind=}, {self.__class__.__name__}, {dir(self)}') @@ -30,13 +32,31 @@ class MyTrackBase: async def recv(self): print(f'============{self.__class__.__name__}, {self.source.duration=}, {self.source.time=}') if self.source is None: - return None - if self.track.readyState != 'live': - self.source_reload() - f = await self.track.recv() + return await self.dumb.recv() + try: + f = await self.track.recv() + return f + except MediaStreamError: + return await self.dumb.recv() + except Exception as e: + print_exc() + print(f'{e}') + return await self.dumb.recv() return f +class MyAudioStreamTrack(MyTrackBase): + kind = 'audio' + def __init__(self, source=None): + super().__init__() + self.dumb = AudioStreamTrack() + +class MyVideoStreamTrack(MyTrackBase): + kind = 'video' + def __init__(self, source=None): + super().__init__() + self.dumb = AudioStreamTrack() +""" class MyAudioStreamTrack(AudioStreamTrack): def __init__(self, source=None): super().__init__() @@ -116,9 +136,9 @@ class MyVideoStreamTrack(VideoStreamTrack): class LoopingVideoTrack(VideoStreamTrack): - """ + ''' A video track that loops a video file. - """ + ''' def __init__(self, filename): super().__init__() self.player = MyMediaPlayer(filename) @@ -130,4 +150,5 @@ class LoopingVideoTrack(VideoStreamTrack): self.player = MyMediaPlayer(self.player._file_path) frame = await self.player.video.recv() return frame +""" diff --git a/rtcllm/rtc.py b/rtcllm/rtc.py index 685c3ee..c5ce407 100644 --- a/rtcllm/rtc.py +++ b/rtcllm/rtc.py @@ -138,7 +138,7 @@ class RTCLLM: pc = peer.pc if track.kind == 'audio': f = partial(self.vad_voiceend, peer) - vadtrack = AudioTrackVad(track, stage=2, onvoiceend=f) + vadtrack = AudioTrackVad(track, stage=3, onvoiceend=f) peer.vadtrack = vadtrack vadtrack.start_vad() diff --git a/rtcllm/vad.py b/rtcllm/vad.py index e74178c..77c2ea0 100644 --- a/rtcllm/vad.py +++ b/rtcllm/vad.py @@ -20,7 +20,7 @@ class AudioTrackVad(MediaStreamTrack): # self.sample_rate = self.track.getSettings().sampleRate # frameSize = self.track.getSettings().frameSize # self.frame_duration_ms = (1000 * frameSize) / self.sample_rate - self.frame_duration_ms = 0.00002 + self.frame_duration_ms = 0.02 self.num_padding_frames = 20 self.ring_buffer = collections.deque(maxlen=self.num_padding_frames) self.triggered = False @@ -51,6 +51,8 @@ class AudioTrackVad(MediaStreamTrack): async def recv(self): oldf = await self.track.recv() self.sample_rate = oldf.sample_rate + duration = (oldf.samples * 1000) / oldf.sample_rate + print(f'{duration=}, {oldf.samples=}, (oldf.sample_rate=}') frames = self.resample(oldf) for f in frames: if self.debug: