B
    u9a:  ã               @   sÎ   d dl mZ e d¡ d dlmZ e d¡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yd dlmZ W n, ek
r˜   ejdkrŽejZnejZY nX dd	„ ZG d
d„ dejƒZdd„ ZedkrÊeƒ  dS )é    )ÚsupportZaudio)ÚfindfileÚossaudiodevN)ÚAFMT_S16_NEÚlittlec          	   C   sr   t | dƒ8}t  |¡}| ¡ }| ¡ }|j}| d¡ | ¡ }W d Q R X |tjkrZtdƒ‚t	 
|d¡}||d|fS )NÚrbr   z)Expect .au file with 8-bit mu-law samplesé   é   )ÚopenÚsunauZgetframerateZgetnchannelsÚ	_encodingÚseekÚreadZAUDIO_FILE_ENCODING_MULAW_8ÚRuntimeErrorÚaudioopZulaw2lin)ÚpathÚfpZauÚrateÚ	nchannelsÚencodingÚdata© r   ú&/usr/lib/python3.7/test_ossaudiodev.pyÚread_sound_file   s    


r   c               @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚOSSAudioDevTestsc          
   C   s–  yt  d¡}W nL tk
rZ } z.|jd tjtjtjtjfkrHt	 
|¡‚‚ W d d }~X Y nX | ¡  | ¡  | ¡  | ¡  | ¡  |  |j¡ |  |jd¡ |  |jdd|j ¡ xDdD ]<}yt||dƒ W n ttfk
ræ   Y qºX |  d| ¡ qºW tt|ƒƒ|d  | | }| t||¡ |  t|d	 ƒd
k |¡ t  ¡ }	| !|¡ | "¡  t  ¡ }
|
|	 }t|| ƒ| d }|  |dkd||f ¡ d S )NÚwr   z/dev/dspzbad dsp.mode: %r)ÚclosedÚnameÚmodeé*   zdsp.%s not read-onlyé   g®Gáz@g{®Gáz„?éd   g      $@z2elapsed time (%s) > 10%% off of expected time (%s))#r   r
   ÚOSErrorÚargsÚerrnoÚEACCESÚENOENTÚENODEVÚEBUSYÚunittestÚSkipTestÚbufsizeÚ	obufcountÚobuffreeÚgetptrÚfilenoZassertFalser   ÚassertEqualr   r   ÚsetattrÚ	TypeErrorÚAttributeErrorÚfailÚfloatÚlenÚsetparametersr   Ú
assertTrueÚabsÚtimeZ	monotonicÚwriteÚclose)Úselfr   r   Zssizer   ÚdspÚmsgÚattrZexpected_timeZt1Zt2Zelapsed_timeZpercent_diffr   r   r   Úplay_sound_file,   sB    



z OSSAudioDevTests.play_sound_filec       	      C   sÂ   t jddf}tddf}xN||gD ]:}|\}}}| |¡|kr | |¡|kr | |¡|kr P q W tdƒ‚| |||d¡}|  ||||fd||f ¡ | |||d¡}|  ||||fd||f ¡ d S )	Né   i@  r   iD¬  zRunable to set audio sampling parameters: you must have really weird audio hardwareFzsetparameters%r: returned %rT)	r   ZAFMT_U8r   ZsetfmtÚchannelsZspeedr   r7   r0   )	r=   r>   Zconfig1Zconfig2ÚconfigÚfmtrC   r   Úresultr   r   r   Úset_parameters[   s    

zOSSAudioDevTests.set_parametersc             C   s¢   t }d}d}x|d|f|d|f||dfgD ]p}|\}}}| |||d¡}|  ||d¡ y| |||d¡}W n$ tjk
rŽ } zW d d }~X Y q*X |  d	¡ q*W d S )
NiD¬  r   i,  éûÿÿÿiÎÿÿÿFz(unexpectedly got requested configurationTzexpected OSSAudioError)r   r7   ZassertNotEqualr   ZOSSAudioErrorr4   )r=   r>   rE   r   rC   rD   rF   Úerrr   r   r   Úset_bad_parametersy   s    

z#OSSAudioDevTests.set_bad_parametersc             C   s   t tdƒƒ}| j|Ž  d S )Nzaudiotest.au)r   r   rA   )r=   Z
sound_infor   r   r   Útest_playback’   s    zOSSAudioDevTests.test_playbackc          	   C   s4   t  d¡}z|  |¡ W d | ¡  |  |j¡ X d S )Nr   )r   r
   rG   r<   r8   r   )r=   r>   r   r   r   Útest_set_parameters–   s
    
z$OSSAudioDevTests.test_set_parametersc          	   C   s(   t  ¡ }|  | ¡ d¡ W d Q R X d S )Nr   )r   Ú	openmixerZassertGreaterEqualr/   )r=   Úmixerr   r   r   Útest_mixer_methods¢   s    
z#OSSAudioDevTests.test_mixer_methodsc          	   C   s&   t  d¡}W d Q R X |  |j¡ d S )Nr   )r   r
   r8   r   )r=   r>   r   r   r   Ú	test_with¨   s    
zOSSAudioDevTests.test_withc             C   s¸   t  d¡}| ¡  |  t|j¡ |  t|jd¡ |  t|jd¡ |  t|jd¡ |  t|j	¡ |  t|j
¡ |  t|j
¡ |  t|j¡ |  t|j¡ t  ¡ }| ¡  |  t|j¡ d S )Nr   rB   ó   x)r   r
   r<   ZassertRaisesÚ
ValueErrorr/   r   r;   Zwriteallr+   r,   r-   r.   rM   )r=   r>   rN   r   r   r   Útest_on_closed­   s    
zOSSAudioDevTests.test_on_closedN)Ú__name__Ú
__module__Ú__qualname__rA   rG   rJ   rK   rL   rO   rP   rS   r   r   r   r   r   *   s   /r   c           
   C   sx   yt  d¡} W nR t jtfk
r` } z.|jd tjtjtjtj	fkrNt
 |¡‚‚ W d d }~X Y nX |  ¡  t t¡ d S )Nr   r   )r   r
   Úerrorr"   r#   r$   r%   r&   r'   r(   r)   r*   r<   r   Zrun_unittestrT   )r>   r?   r   r   r   Ú	test_main¾   s    
rX   Ú__main__)Ztestr   ZrequiresZtest.supportr   Úimport_moduler   r$   Úsysr   r:   r   r)   r   ÚImportErrorÚ	byteorderZAFMT_S16_LEZAFMT_S16_BEr   ZTestCaser   rX   rT   r   r   r   r   Ú<module>   s*   


 