
    Nj$                        d Z ddlZddlmc mZ ddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ  e
j"                  d      5 Z ej&                  ej)                  ej+                               ej,                        j/                  ej0                        dz  Zddd       dZ ee      ez  Zded	ej<                  d
dfdZde d
ej<                  fdZ!ddZ"e#dk(  r e"        yy# 1 sw Y   NxY w)a  Acoustic echo test: play Kokoro speech through the real speakers and
measure what each mic path hears.

  RAW mic (sounddevice)      -> should hear it loud (echo problem)
  helper --bypass            -> should hear it loud (capture path alive)
  helper with AEC            -> should hear ~nothing (echo cancelled)

The bypass pass is the aliveness proof: same binary, same capture path,
only voice processing toggled.
    N)VADz/tmp/kokoro-sample.wavdtype      @i]  labelaudio16kreturnc                 d   t               }t        dt        |      dz
  d      D cg c]  }|j                  |||dz           }}|rt	        j
                  |      nt	        j                  d      }t        |dk7  j                               }t        |  dt	        j                  |      j                         dd| d|j                         dd	t        |d
kD  j                                dt        |dkD  j                                dt        |dkD  j                                       y c c}w )Nr   i      z:
    peak=z.4fz nonzero_samples=z	 vad_max=z.2fz frames>0.5=g      ?z >0.75=g      ?z >0.9=g?)r   rangelenprobnparrayzerosintsumprintabsmax)r   r   vadiprobsnzs         7/Users/ahmed/devFolder/Ultron/claude-voice/test_echo.py
vad_reportr      s!   
%C q#h-#-s33A 	!a#g&'3 
   %BHHUO"((1+E	h!m  "	#B	' FF8$((*3//@ E99;s#<U3YOO4E0F/G HeDj%%'()U3YOO4E0F/G	Is   D-bypassc                    dg| rdgng z   }t        j                  |t         j                  t         j                  d      g t	        j
                         fd}t	        j                  |d      }|j                          t        j                  d       j                          t        j                  t        t               t        j                          t        j                  d	       j!                          j#                          |j%                  d
       t'        j(                  dj%                        t&        j*                        }|j-                  t&        j.                        dz  S )Nz./helper/aecmicz--bypassr   )stdoutstderrbufsizec                      j                         sAj                  j                  d      } | sy j                  |        j                         s@y y )Ni   )is_setr   readappend)datachunksprocstops    r   readerz.helper_capture_during_playback.<locals>.reader7   s=    ++-;;##D)DMM$	 ++-    T)targetdaemong      ?皙?   )timeoutr+   r   r   )
subprocessPopenPIPEDEVNULL	threadingEventThreadstarttimesleepclearsdplaySAMPLEPLAY_SRwaitset	terminatejoinr   
frombufferint16astypefloat32)r   argsr*   tpcm16r'   r(   r)   s        @@@r   helper_capture_during_playbackrK   /   s    &:,bADZ__Z-?-?D F??D  	t4AGGIJJsO
LLNGGFGGGIJJsOHHJNNFF1FMM#((6*"((;E<<

#g--r+   c                  Z   t        dt        dd       t        j                  t	        t        dz   dz        ddd      d d df   } t        j                  d	       t        j                  t        t               t        j                          t        j                  d
       t        j                          t        d|        t        d      }t        dt        |      dz  dd       t        d|       t        d      }t        dt        |      dz  dd       t        d|       y )Nzsample: z.1fz%s of Kokoro speech at current volume
r   i>  rG   )
sampleratechannelsr   r   g333333?r.   zRAW mic during playback   T)r   z  (bypass captured zs)zhelper --bypass (no AEC)  Fz  (aec captured zhelper with AEC           )r   DURr<   recr   r9   r:   r=   r>   r?   r@   r)   r   rK   r   )rP   audios     r   mainrR   L   s    	HSIC
DE
&&cAg&'EA ""#Q$(CJJsOGGFGGGIJJsOGGI+S1*$7E	E
5 05R
89+U3*%8E	SZ-c2"
56+U3r+   __main__)r	   N)$__doc__builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr1   r5   r9   wavenumpyr   sounddevicer<   	voice.vadr   openwrD   
readframes
getnframesrE   rF   rG   r>   r?   r   rO   strndarrayr   boolrK   rR   __name__ r+   r   <module>rh      s   	         TYY'(Aall1<<>2"((CJJ2::V
	  )
 	&kGc RZZ D  .4 .BJJ .:4* zF W )(s    AC%%C.