Your IP : 216.73.216.85


Current Path : /opt/alt/python35/lib/python3.5/site-packages/captcha/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/captcha/__pycache__/audio.cpython-35.pyc



K�V��@s�dZddlZddlZddlZddlZddlZddlZddlZejddkr�ddl	Z	e	j
Z
dgZdZe
d�Zee�dZejjejjejje��d�Zd	d
�Zddd
�Zdd�Zddd�Zdd�Zddd�Zdd�Zeejjed��Zeed�Zee ed��Z!Gdd�de"�Z#dS)z�
    captcha.audio
    ~~~~~~~~~~~~~

    Generate Audio CAPTCHAs, with built-in digits CAPTCHA.

    This module is totally inspired by https://github.com/dchest/captcha
�N��AudioCaptchai@s(RIFFWAVEfmt @@data��datacCs2tj|�}|jd�}|j�t|�S)N����)�wave�openZ
readframes�close�	bytearray)�filepath�wr�r�>/opt/alt/python35/lib/python3.5/site-packages/captcha/audio.py�_read_wave_file#s
rrcCs�|dkr|Stt|�|�}t|�}d}xa|D]Y}t|�}x:|t||�kr�||kr�|||<|d7}qTW||7}q?W|S)z(Change the voice speed of the wave body.rr)�int�lenr)�body�speed�length�rv�step�v�irrr�change_speed*s
%
rcCs�t|�}||d}t|}tjt�}ttjd|��|dd�<|ttjd|��7}||}||kr�|tdg�}|S)zjPatch header to the given wave body.

    :param body: the wave content body, it should be bytearray.
    rz<Ir�r)r�WAVE_HEADER_LENGTH�copy�WAVE_HEADERr�struct�pack)rrZpadded�total�headerrrrr�patch_wave_header<s
"
r#cCskt|�}dt|d�}d}x>||krftjdd�}|||||<|d7}q)W|S)z!Create white noise for background�rr�r)rr�random�randint)r�level�noiseZadjustrrrrr�create_noiseTsr*cCs=t|�}d}x$||kr8d||<|d7}qW|S)zCreate a piece of silence.rr$r)r)rrrrrr�create_silence`s
r+cCs�|dkr|Stj|�}x�t|�D]�\}}|dkr}|d|d}tt|�d�}t|d�}nB|dkr�dd||}tt|�d�}t|d�}|||<q,W|S)Nrr$r%r)r�	enumerate�maxr�min)rr(rrrrr�change_soundjsr/cCs�t|�t|�kr%||}}xt|�D]q\}}||}|dkr{|dkr{t||d�||<q2td||||dd�||<q2W|S)zMix two wave body into one.r$rr%)rr,r)�src�dstr�svZdvrrr�mix_wave|s

,r3zbeep.wavgffffff�?�c@s�eZdZdZddd�Zedd��Zddd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)raCreate an audio CAPTCHA.

    Create an instance of AudioCaptcha is pretty simple::

        captcha = AudioCaptcha()
        captcha.write('1234', 'out.wav')

    This module has a built-in digits CAPTCHA, but it is suggested that you
    create your own voice data library. A voice data library is a directory
    that contains lots of single charater named directories, for example::

        voices/
            0/
            1/
            2/

    The single charater named directories contain the wave files which pronunce
    the directory name. A charater directory can has many wave files, this
    AudioCaptcha will randomly choose one of them.

    You should always use your own voice library::

        captcha = AudioCaptcha(voicedir='/path/to/voices')
    NcCs1|dkrt}||_i|_g|_dS)N)�DATA_DIR�	_voicedir�_cache�_choices)�selfZvoicedirrrr�__init__�s
		zAudioCaptcha.__init__cCsz|jr|jSx`tj|j�D]L}t|�dkr#tjjtjj|j|��r#|jj|�q#W|jS)z1Available choices for characters to be generated.r)	r8�os�listdirr6r�path�isdir�join�append)r9�nrrr�choices�s	6zAudioCaptcha.choices�cCstj|j|�S)zjGenerate a random string with the given length.

        :param length: the return string length.
        )r&�samplerB)r9rrrrr&�szAudioCaptcha.randomcCs%x|jD]}|j|�q
WdS)zLoad voice data into memory.N)rB�
_load_data)r9�namerrr�load�szAudioCaptcha.loadcCs�tjj|j|�}g}x`tj|�D]O}tjj||�}|jd�r.tjj|�r.|jt|��q.W||j	|<dS)Nz.wav)
r;r=r?r6r<�endswith�isfiler@rr7)r9rF�dirnamer�frrrrrE�s!zAudioCaptcha._load_datacCsdtj|j|�}tjdd�d}t||�}tjdd�d}t||�}|S)N�Z�xgY@�P)r&�choicer7�	randrangerr/)r9�key�voicerr(rrr�_twist_pick�szAudioCaptcha._twist_pickcCs�tj|j�}tj|j|�}tj|�}|j�tjdd�d}t||�}tjdd�d}t||�}|S)Nr�g$@rrC)	r&rOrBr7r�reverserPrr/)r9rQrRrr(rrr�_noise_pick�s
zAudioCaptcha._noise_pickcCs�t|d�}d}xs||kr�|j�}|t|�d}t||||��|||�<|tjdttd��}qW|S)Nrrr�
)r*rVrr3r&r'r�WAVE_SAMPLE_RATE)r9r�charsr)�posZsound�endrrr�create_background_noise�s#$z$AudioCaptcha.create_background_noisecCs+g}g}xG|D]?}|j|j|��tjttd�}|j|�qWtdd�|�}t|�t|�tt	j
|�}|j||�}|d}	x_t|�D]Q\}
}|	t|�d}t
|||	|��||	|�<|||
}	q�Wttttt|tS)N�cSs
t|�S)N)r)�arrr�<lambda>�sz/AudioCaptcha.create_wave_body.<locals>.<lambda>rr)r@rSr&r'rX�mapr-r�reduce�operator�addr\r,r3�BEEP�SILENCE�END_BEEP)r9rYZvoicesZintersrQrZ	durationsr�bgrZrr[rrr�create_wave_body�s
&
#zAudioCaptcha.create_wave_bodycCs,|js|j�|j|�}t|�S)zrGenerate audio CAPTCHA data. The return data is a bytearray.

        :param chars: text to be generated.
        )r7rGrhr#)r9rYrrrr�generates	
zAudioCaptcha.generatec	Cs9|j|�}t|d��}|j|�SWdQRXdS)z�Generate and write audio CAPTCHA data to the output.

        :param chars: text to be generated.
        :param output: output destionation.
        �wbN)rir	�write)r9rY�outputrrKrrrrkszAudioCaptcha.write)�__name__�
__module__�__qualname__�__doc__r:�propertyrBr&rGrErSrVr\rhrirkrrrrr�s
	


)$rpr;rrrr&rb�sys�version_info�	functoolsra�__all__rXrrrrr=r?�abspathrJ�__file__r5rrr#r*r+r/r3rdrfrre�objectrrrrr�<module>	s6			-