U
    pd                     @   s2  d Z ddgZddlmZ eddZdej_ dej_ d	ej_ d
ej_ dej	_ dd Z
dd Zg Zdd Zee dd Zee dd Zee dd Zee dd Zee dd Zee dd Zee dd Zee dd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zed*kr.e  d+S ),a  Routines to help recognizing sound files.

Function whathdr() recognizes various types of sound file headers.
It understands almost all headers that SOX can decode.

The return tuple contains the following items, in this order:
- file type (as SOX understands it)
- sampling rate (0 if unknown or hard to decode)
- number of channels (0 if unknown or hard to decode)
- number of frames in the file (-1 if unknown or hard to decode)
- number of bits/sample, or 'U' for U-LAW, or 'A' for A-LAW

If the file doesn't have a recognizable type, it returns None.
If the file can't be opened, OSError is raised.

To compute the total time, divide the number of frames by the
sampling rate (a frame contains a sample for each channel).

Function what() calls whathdr().  (It used to also use some
heuristics for raw data, but this doesn't work very well.)

Finally, the function test() is a simple main program that calls
what() for all files mentioned on the argument list.  For directory
arguments it calls what() for all files in that directory.  Default
argument is "." (testing all files in the current directory).  The
option -r tells it to recurse down directories found inside
explicitly given directories.
whatwhathdr    )
namedtuple
SndHeadersz.filetype framerate nchannels nframes sampwidthzThe value for type indicates the data type
and will be one of the strings 'aifc', 'aiff', 'au','hcom',
'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'.zYThe sampling_rate will be either the actual
value or 0 if unknown or difficult to decode.z^The number of channels or 0 if it cannot be
determined or if the value is difficult to decode.z?The value for frames will be either the number
of frames or -1.zAEither the sample size in bits or
'A' for A-LAW or 'U' for u-LAW.c                 C   s   t | }|S )zGuess the type of a sound file.)r   )filenameres r   /usr/lib/python3.8/sndhdr.pyr   4   s    c              
   C   s^   t | dJ}|d}tD ]*}|||}|rt|   W  5 Q R  S qW 5 Q R  dS Q R X dS )zRecognize sound headers.rbi   N)openreadtestsr   )r   fhZtfr   r   r   r	   r   :   s    

c              	   C   s   dd l }| dsd S | dd dkr,d}n| dd dkrBd}nd S |d z||d	}W n t|jfk
r|   Y d S X || | | d|	  fS )
Nr      FORM      s   AIFCaifcs   AIFFZaiffr)
r   
startswithseekr   EOFErrorErrorgetframerategetnchannels
getnframesgetsampwidth)r   r   r   Zfmtar   r   r	   	test_aifcK   s"    

 
r   c                 C   s   |  drt}n| d d dkr&t}nd S d}|| dd }|| dd }|| dd }|| dd }|| dd	 }d
}	|d
krd}
n$|dkrd}
n|dkrd}
d}	nd}
|	| }|r|| }nd}|||||
fS )Ns   .snd   )s    ds.s   dns.Zaur   r               U      ?)r   get_long_beget_long_le)r   r   funcfiletypeZhdr_size	data_sizeencodingrate	nchannelsZsample_sizeZsample_bitsZ
frame_sizeZnframer   r   r	   test_au`   s2    

r1   c                 C   sT   | dd dks | dd dkr$d S t | dd }|rBd	| }nd
}d|dddfS )NA   E   s   FSSD      s   HCOM      i"V  r   Zhcomr#   r(   r   )r)   )r   r   Zdivisorr/   r   r   r	   	test_hcom   s     
r8   c                 C   st   |  dsd S t| dd }d}d|  kr6dk rfn n,| | dkrfd| |d   }|rftd	| }d
|dddfS )Ns   Creative Voice Filer!      r   i  r#      r   g    .AZvocr(   r   )r   get_short_leint)r   r   Zsbseekr/   Zratecoder   r   r	   test_voc   s    
$r=   c              	   C   s   dd l }| dr2| dd dks2| dd dkr6d S |d z||d}W n t|jfk
rl   Y d S X d	| | | d|	  fS )
Nr   s   RIFFr   r   s   WAVEr    s   fmt r   Zwav)
waver   r   r   r   r   r   r   r   r   )r   r   r>   wr   r   r	   test_wav   s    *
 
r@   c                 C   s"   |  dr| dd dkrd S dS )Nr   r   r   s   8SVX)Z8svxr   r#   r   r   )r   )r   r   r   r   r	   	test_8svx   s    rA   c                 C   s<   |  dr8t| dd }t| dd }d|d|dfS d S )Ns   SOUNDr   r   r!   r9   Zsndtr#   )r   r*   r;   )r   r   Znsamplesr/   r   r   r	   	test_sndt   s    
rB   c                 C   sD   |  dr@t| dd }d|  kr.dkr@n nd|ddd	fS d S )
Ns     r%   r   i  ia  Zsndrr#   r(   r   )r   r;   )r   r   r/   r   r   r	   	test_sndr   s    
rC   c                 C   s,   | d d> | d d> B | d d> B | d B S )Nr   r"   r#   r    r%   r   r&   r   br   r   r	   r)      s    r)   c                 C   s,   | d d> | d d> B | d d> B | d B S )Nr&   r"   r%   r    r#   r   r   r   rD   r   r   r	   r*      s    r*   c                 C   s   | d d> | d B S )Nr   r   r#   r   rD   r   r   r	   get_short_be   s    rF   c                 C   s   | d d> | d B S )Nr#   r   r   r   rD   r   r   r	   r;      s    r;   c                  C   s   dd l } d}| jdd  r8| jd dkr8| jdd= d}z8| jdd  r`t| jdd  |d ntdg|d W n* tk
r   | jd | d Y nX d S )Nr   r#   z-rr%   .z
[Interrupted]
)sysargvtestallKeyboardInterruptstderrwriteexit)rH   	recursiver   r   r	   test   s    rP   c              	   C   s   dd l }dd l}| D ]}|j|r~t|d dd |s<|rttd dd l}||j||d}t||d qtd qt|d dd |j	
  ztt| W q tk
r   td	 Y qX qd S )
Nr   z/: )endzrecursing down:*z*** directory (use -r) ***:z*** not found ***)rH   ospathisdirprintglobjoinescaperJ   stdoutflushr   OSError)listrO   ZtoplevelrH   rU   r   rY   namesr   r   r	   rJ      s"    

rJ   __main__N)__doc____all__collectionsr   r   r,   Z	framerater0   ZnframesZ	sampwidthr   r   r   r   appendr1   r8   r=   r@   rA   rB   rC   r)   r*   rF   r;   rP   rJ   __name__r   r   r   r	   <module>   sJ    









