
    	D,j                        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 d dlm	Z	 ddl
m
Z
  ej        dd            ej        de	           d d
lmZ d dlmZmZ 	 d dlZn# eef$ r dZY nw xY w G d de          Z G d d          Zedk    rD ej        d          Zes ed           e e                                                       dS dS )    N)prompt)litellm   )dumpignorezGCouldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work)message)category)AudioSegment)CouldntDecodeErrorCouldntEncodeErrorc                       e Zd ZdS )SoundDeviceErrorN)__name__
__module____qualname__     Z/home/rurouni/.local/share/uv/tools/aider-chat/lib/python3.11/site-packages/aider/voice.pyr   r      s        Dr   r   c                   @    e Zd ZdZdZdZdZddZd Zd Z	dd	Z
d
 ZdS )Voicer   g     j@g333333?wavNc                    t           t          	 t          d           dd l}|| _        |                                }|rfd }t          |          D ]\  }}||d         v r|} n|!d |D             }t          d| d|           t          d| d| d	           || _        nd | _        n# t          t          f$ r t          w xY w|d
vrt          d|           || _        d S )NzInitializing sound device...r   namec                 6    g | ]}|d          dk    |d         S )max_input_channelsr   r   r   ).0ds     r   
<listcomp>z"Voice.__init__.<locals>.<listcomp>;   s-    'b'b'baaH\F]`aFaFa&	FaFaFar   zDevice 'z&' not found. Available input devices: zUsing input device: z (ID: ))r   mp3webmzUnsupported audio format: )sfr   printsounddevicesdquery_devices	enumerate
ValueError	device_idOSErrorModuleNotFoundErroraudio_format)	selfr,   device_namer%   devicesr)   ideviceavailable_inputss	            r   __init__zVoice.__init__(   sd   :""	#0111$$$$DG&&((G & 	!*7!3!3  IAv"fVn44$%	 5 $'b'b7'b'b'b$$/; / /,/ /  
 L[LL	LLLMMM!*!%,- 	# 	# 	#""	#555H,HHIII(s   BB. .Cc                    ddl }|                    |                    |dz                      }t          | j        |          | _        t          | j        |          | _        | j        | j        z
  }|dk    r|| j        z
  |z  | _        nd| _        | j        	                    |
                                           dS )z=This is called (from a separate thread) for each audio block.r   N   gMbP?g      ?)numpysqrtmeanmaxmax_rmsminmin_rmspctqputcopy)r-   indataframestimestatusnprmsrngs           r   callbackzVoice.callbackM   s    ggbggfai(())4<--4<--lT\);;dl*c1DHHDH

6;;==!!!!!r   c                    d}t          j        | j                  s| j        | j        k     rd}nt	          | j        dz            }d|z  d||z
  z  z   }|d |         }t          j                    | j        z
  }d|dd| S )N
   r   u   ░u   █z$Recording, press ENTER when done... z.1fzsec )mathisnanr=   	thresholdintrC   
start_time)r-   numcntbardurs        r   
get_promptzVoice.get_prompt]   s    :dh 	%48dn#<#<CCdhm$$CckES3Y//$3$iikkDO+HcHHH3HHHr   c                     	 |                      ||          S # t          $ r Y d S t          $ r,}t          d|            t          d           Y d }~d S d }~ww xY w)NzError: zLPlease ensure you have a working audio input device connected and try again.)raw_record_and_transcribeKeyboardInterruptr   r#   )r-   historylanguagees       r   record_and_transcribezVoice.record_and_transcribej   s    	11'8DDD  	 	 	FF 	 	 	-A--   `aaaFFFFF	s    
A	A!AAc                    t          j                    | _        t          j        d          }	 t          | j                            | j        d          d                   }n6# t          t          f$ r d}Y n!| j        j        $ r t          d          w xY wt          j                    | _        	 | j                            |d| j        | j                  5  t#          | j        d	
           d d d            n# 1 swxY w Y   n)# | j        j        $ r}t          d|           d }~ww xY wt'          j        |d|d          5 }| j                                        sE|                    | j                                                   | j                                        Ed d d            n# 1 swxY w Y   | j        }t2          j                            |          }|dk    r| j        dk    rt9          d           d}|}	|dk    r	 t          j        d|           }
t;          j        |          }|                    |
|           t3          j         |           |
}	n# tB          tD          f$ r}t9          d|            Y d }~nWd }~wtF          tH          f$ r}t9          d|            Y d }~n,d }~wtJ          $ r}t9          d|            Y d }~nd }~ww xY wtM          |	d          5 }	 tO          j(        d|||          }n8# tJ          $ r+}t9          d|	 d|            Y d }~d d d            d S d }~ww xY w	 d d d            n# 1 swxY w Y   |	|k    rt3          j         |	           |j)        }|S )Nz.wav)suffixinputdefault_sampleratei>  zONo audio input device detected. Please check your audio settings and try again.r   )
sampleratechannelsrH   r1   g?)refresh_intervalz$Error accessing audio input device: x)moder`   ra   gfffffxAr   z;
Warning: {temp_wav} is too large, switching to mp3 format.r    .)formatzError converting audio: z%File system error during conversion: z*Unexpected error during audio conversion: rbz	whisper-1)modelfiler   rY   zUnable to transcribe z: )*queueQueuer>   tempfilemktemprN   r%   r&   r)   	TypeErrorr(   PortAudioErrorr   rC   rO   InputStreamrH   r   rT   r"   	SoundFileemptywritegetr,   ospathgetsizer#   r
   from_wavexportremover   r   r*   FileNotFoundError	Exceptionopenr   transcriptiontext)r-   rX   rY   temp_wavsample_rateerrri   use_audio_format	file_sizefilenamenew_filenameaudiorZ   fh
transcriptr   s                   r   rV   zVoice.raw_record_and_transcribet   s   ?&111	dg33DNGLLMabccKK:& 	  	  	 KKKw% 	 	 	"a  	
 )++	Q$$&T]SWSa %   > > t====> > > > > > > > > > > > > > > w% 	Q 	Q 	Q"#O##O#OPPP	Q \(qQQQ 	)UYfllnn )

46::<<((( fllnn )	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)  , GOOH--	)))d.?5.H.HPQQQ$u$$H'6L:J6L6LMMM$-h77\2BCCC	(###'&(:; 6 6 644455555555./ C C CAaAABBBBBBBB H H HF1FFGGGGGGGGH (D!! 	R$2%Bw  

    ?h??#??@@@	 	 	 	 	 	 	 	
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 xIhs   3A# #B8B2(D	 C=1D	 =DD	 DD	 	D/D**D/
AF55F9<F9AI+ +K'<JK''J>>K'K""K':M<LM
M
M4MM

MM!M)r   N)NN)r   r   r   r:   r<   r=   rM   r3   rH   rT   r[   rV   r   r   r   r   r   !   s        GG
CI#) #) #) #)J" " " I I I   @ @ @ @ @r   r   __main__OPENAI_API_KEYz3Please set the OPENAI_API_KEY environment variable.)rK   ru   rj   rl   rC   warningsprompt_toolkit.shortcutsr   	aider.llmr   r   filterwarningsSyntaxWarningpydubr
   pydub.exceptionsr   r   	soundfiler"   r*   r+   r|   r   r   r   getenvapi_keyr(   r#   r[   r   r   r   <module>r      s    				     + + + + + +              _     = 9 9 9 9       C C C C C C C C$%   	BBB	 	 	 	 	y 	 	 	S S S S S S S Sl zbi())G PjNOOO	E%%''
'
'
)
)*****	 s   A# #	A/.A/