
    љi                         S SK r S SKrS SKrS SKrS SKJrJrJrJrJ	r	J
r
  SSKJr  SSKJr  \R                  " \5      r " S S\5      r " S S	\5      rg)
    N)AsyncGeneratorCallableOptionalDictList	Awaitable   )SpeechDetector)RecordingSessionBasec                   D   ^  \ rS rSrSS\S\4U 4S jjjrU 4S jrSrU =r	$ )RecordingSession   
session_idpreroll_buffer_countc                 2   > [         TU ]  X5        SU l        g Nr   )super__init__amplitude_threshold)selfr   r   	__class__s      S/home/james-whalen/.local/lib/python3.13/site-packages/aiavatar/sts/vad/standard.pyr   RecordingSession.__init__   s    :*+     c                 "   > [         TU ]  5         g N)r   reset)r   r   s    r   r   RecordingSession.reset   s    r   )r   )   )
__name__
__module____qualname____firstlineno__strintr   r   __static_attributes____classcell__r   s   @r   r   r      s%    ,3 ,c , , r   r   c                     ^  \ rS rSrSSSSSSSS	S	S
SS.S\S\S\S\S\S\S\S\\\/\4      S\\\	/\
S	   4      S\S\4U 4S jjjrS\4S jr\S\4S j5       r\R"                  S\4S j5       rS\S\S \	4S! jrS"\S \	S\4S# jrS$\\S	4   S \	4S% jrS& rS \	4S' jrS \	4S( jrS \	4S) jrS \	S*\	4S+ jrS0S \	S*\	S\S,\4S- jjrS \	S\4S. jrS/rU =r$ )1StandardSpeechDetector   g      Dg      ?g      $@g?i>  r	   r   Ng      ?F)volume_db_thresholdsilence_duration_thresholdmax_durationmin_durationsample_ratechannelsr   to_linear16on_recording_started!on_recording_started_min_durationdebugr,   r-   r.   r/   r0   r1   r   r2   r3   r4   r5   c                  > [         TU ]  UU
S9  Xl        SSU R                  S-  -  -  U l        X l        X0l        X@l        X`l        U	(       a  U R                  R                  U	5        Xl        Xpl        Xl        0 U l        g )N)r0   r4     
         4@)r   r   _volume_db_thresholdr,   r   r-   r.   r/   r1   _on_recording_startedappendr5   r   r2   recording_sessions)r   r,   r-   r.   r/   r0   r1   r   r2   r3   r4   r5   r   s               r   r   StandardSpeechDetector.__init__   s     	#.O 	 	
 %8!#(B43K3Kd3R,S#T *D'(( &&--.BC
$8!&?Ar   returnc           	          U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  S.$ )Nr,   r-   r.   r/   r0   r1   r   r5   rA   r   s    r   
get_config!StandardSpeechDetector.get_config6   sP    #'#;#;*.*I*I -- --++$($=$=ZZ	
 		
r   c                     U R                   $ r   )r:   rB   s    r   r,   *StandardSpeechDetector.volume_db_thresholdB   s    (((r   valuec                 z    Xl         SSUS-  -  -  U l        [        R                  SU SU R                   35        g )Nr7   r8   r9   zUpdated volume_db_threshold to z dB, amplitude_threshold=)r:   r   loggerr5   )r   rG   s     r   r,   rF   F   sA    $)!#(B54<,@#A 6ug=VW[WoWoVpqrr   recorded_datarecorded_durationr   c                 H   #    U R                  US S X#5      I S h  vN   g  N7fr   )_execute_on_speech_detected)r   rJ   rK   r   s       r   execute_on_speech_detected1StandardSpeechDetector.execute_on_speech_detectedL   s      ..}dDJ[hhhs   " "samplesc           	        #    U R                   (       a  U R                  U5      nU R                  U5      nU R                  5       (       a@  UR                  5         UR                  R                  5         [        R                  S5        gUR                  R                  U5        [        [        S [        R                  " SU5       5       5      5      n[        U5      S-  U R                  U R                  -  -  nXCR                   :  nU R                  (       aY  US:  a  S["        R$                  " US-  5      -  nOS	n[        R                  S
US SUR&                  S SUR(                   35        U(       a  U R+                  U5      I S h  vN   UR,                  (       d  U(       au  UR                  5         SUl        UR                   H  nUR.                  R1                  U5        M      UR.                  R1                  U5        U=R&                  U-  sl        UR,                  $ UR.                  R1                  U5        U=R&                  U-  sl        U(       a  SUl        OU=R2                  U-  sl        U R5                  U5      I S h  vN   UR2                  U R6                  :  a  UR&                  UR2                  -
  n	XR8                  :  a+  U R                  (       a  [        R;                  SU	 S35        OoU R                  (       a  [        R;                  SU	 S35        [=        UR.                  5      n
[>        R@                  " U RC                  XUR(                  5      5        UR                  5         UR,                  $ UR&                  U RD                  :  a  U R                  (       a#  [        R;                  SUR&                   S35        [=        UR.                  5      n
[>        R@                  " U RC                  XR&                  UR(                  5      5        UR                  5         UR,                  $  GN GN7f)Nz StandardSpeechDetector is muted.Fc              3   <   #    U  H  u  n[        U5      v   M     g 7fr   )abs).0samples     r   	<genexpr>9StandardSpeechDetector.process_samples.<locals>.<genexpr>]   s     !_=^'&#f++=^s   z<h   r      r7   g      YzdB: z.2fz, duration: z, session: TzRecording too short: z seczRecording finished: z Recording max duration reached: )#r2   get_sessionshould_muter   preroll_bufferclearrI   r5   r<   floatmaxstructiter_unpacklenr0   r1   r   mathlog10record_durationr   _execute_on_voicedis_recordingbufferextendsilence_duration$_check_and_trigger_recording_startedr-   r/   infobytesasynciocreate_taskrN   r.   )r   rP   r   sessionmax_amplitudesample_durationspeech_detected
current_dbfrK   rJ   s              r   process_samples&StandardSpeechDetector.process_samplesO   sd    &&w/G"":.MMO""((*LL;<%%g.c!_V=O=OPTV]=^!__`w<!+0@0@4==0PQ'*E*EE::q $**]U-B"CC
#
LL4
3/|G<S<STW;XXcdkdvdvcwxy))*555##'+$ //ANN))!, 0 %%g.''?:'D ###= NN!!'*##6#+,(((O;( ;;GDDD''4+J+JJ$+$;$;g>V>V$V!$'8'88zz&;<M;Nd$STzz&:;L:MT$RS$)'..$9M''(G(Gjqj|j|(}~ ### ((D,=,==::KK"B7CZCZB[[_ `a %gnn 5##D$C$CMSjSjlsl~l~$  A###] 62 Es&   FQQDQQF6QQinput_streamc                 ,  #    [         R                  S5        U  S h  vN nU(       d    O9U R                  X25      I S h  vN   [        R                  " S5      I S h  vN   MK  U R                  U5        [         R                  S5        g  Nm NM N1
 N/7f)Nz$STSPipeline start processing stream.g-C6?z%STSPipeline finish processing stream.)rI   rl   rv   rn   sleepdelete_session)r   rx   r   datas       r   process_stream%StandardSpeechDetector.process_stream   su     :;& 	($&&t888--'''J';<	( 9'	 'sI   BBBBB BBB-BBBBBc                 .   #    U R                  U5        g 7fr   )r{   r   r   s     r   finalize_session'StandardSpeechDetector.finalize_session   s     J's   c                     U R                   R                  U5      nUc#  [        XR                  5      nX R                   U'   UR                  S:X  a  U R                  Ul        U$ r   )r=   getr   r   r   r   r   rp   s      r   rZ   "StandardSpeechDetector.get_session   s\    ))--j9?&z3L3LMG29##J/&&!+*.*B*BG'r   c                 j    U R                   R                  U5      =n(       a  UR                  5         g g r   )r=   r   r   r   s      r   reset_session$StandardSpeechDetector.reset_session   s,    --11*==7=MMO >r   c                 x    XR                   ;   a+  U R                   U   R                  5         U R                   U	 g g r   )r=   r   r   s     r   r{   %StandardSpeechDetector.delete_session   s8    000##J/557''
3 1r   keyc                 ~    U R                   R                  U5      nU(       a  UR                  R                  U5      $ g r   )r=   r   r|   )r   r   r   rp   s       r   get_session_data'StandardSpeechDetector.get_session_data   s5    ))--j9<<##C(( r   create_sessionc                     U(       a  U R                  U5      nOU R                  R                  U5      nU(       a  X5R                  U'   g g r   )rZ   r=   r   r|   )r   r   r   rG   r   rp   s         r   set_session_data'StandardSpeechDetector.set_session_data   s?    &&z2G--11*=G %LL r   c                 F    U R                  U5      nSSUS-  -  -  Ul        g )Nr7   r8   r9   )rZ   r   )r   r   rG   rp   s       r   set_volume_db_threshold.StandardSpeechDetector.set_volume_db_threshold   s(    "":.&+redl/C&D#r   )
r:   r   r1   r5   r.   r/   r   r=   r-   r2   )F) r    r!   r"   r#   r^   r%   r   r   rm   r$   r   boolr   dictrC   propertyr,   setterrN   rv   r   r}   r   rZ   r   r{   r   anyr   r   r&   r'   r(   s   @r   r*   r*      s    &+,/"! $%:>KO36B #B %*	B
 B B B B "B hw~67B 'xy0F'GHB ,1B B B@

D 

 )U ) ) s s  s
ie iX] ikn iI$U I$ I$ I$V=t1L =Z] =(c  4 4
)3 )S )
&3 &S & &VZ &E# Ee E Er   r*   )rn   loggingrc   r`   typingr   r   r   r   r   r    r
   baser   	getLoggerr    rI   r   r*    r   r   <module>r      sJ        L L  &			8	$+ wE^ wEr   