o w[e@s4dZddgZddlmZeddZdej_dej_d ej_d ej_d ej _d dZ d dZ gZ ddZ e e ddZe eddZe eddZe eddZe eddZe eddZe eddZe eddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zed*kred+Sd+S),aRoutines 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. whatwhathdr) 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.cCs t|}|S)zGuess the type of a sound file.)r)filenameresr/usr/lib/python3.10/sndhdr.pyr4scCsnt|d(}|d}tD]}|||}|r#t|WdSq WddS1s0wYdS)zRecognize sound headers.rbiN)openreadtestsr)rfhtfrrrr r:s    $c Csddl}|ds dS|dddkrd}n |dddkr!d}ndS|dz||d }Wn t|jfy=YdSw||||d| fS) NrFORM sAIFCaifcsAIFFaiffr) r startswithseekr EOFErrorError getframerate getnchannels getnframes getsampwidth)rrrfmtarrr test_aifcKs"  r!c Cs|drt}n |dddvrt}ndSd}||dd}||dd}||dd}||dd}||dd }d } |d krHd } n|d krOd} n |d krXd} d } nd} | |} | re|| } nd} |||| | fS)Ns.snd)sds.sdns.aurrU?)r get_long_be get_long_le) rrfuncfiletypehdr_size data_sizeencodingrate nchannels sample_size sample_bits frame_sizenframerrr test_au`s2  r:cCsT|dddks|dddkrdSt|dd}|r!d |}nd }d |d d dfS)NAEsFSSDsHCOMi"Vrhcomr'r,r)r-)rrdivisorr4rrr test_hcoms  rCcCst|dsdSt|dd}d}d|krdkr3nn||dkr3d||d}|r3td |}d |dd d fS) NsCreative Voice Filer%rir'r"g.Avocr,r)r get_short_leint)rrsbseekr4ratecoderrr test_vocs $ rKc Csddl}|dr|dddks|dddkrdS|dz||d}Wn t|jfy5YdSwd |||d| fS) NrsRIFFrrsWAVEr$sfmt rwav) waverrr rrrrrr)rrrMwrrr test_wavs* rOcCs"|dr |dddkrdSdS)Nrrrs8SVX)8svxrr'rr)r)rrrrr test_8svxsrQcCs<|drt|dd}t|dd}d|d|dfSdS)NsSOUNDrrr%rDsndtr')rr.rG)rrnsamplesr4rrr test_sndts rTcCsJ|dr!t|dd}d|krdkr#ndSd|ddd fSdSdS) Nsr)r"iiasndrr'r,r)rrG)rrr4rrr test_sndrs rVcC,|dd>|dd>B|dd>B|dBS)Nrr&r'r$r)rr*rbrrr r-,r-cCrW)Nr*r&r)r$r'rrrrXrrr r.rZr.cC|dd>|dBS)Nrrr'rrXrrr get_short_ber\cCr[)Nr'rrrrXrrr rGr]rGcCsddl}d}|jddr|jddkr|jdd=d}z|jddr2t|jdd|dWdStdg|dWdStyP|jd|dYdSw)Nrr'z-rr).z [Interrupted] )sysargvtestallKeyboardInterruptstderrwriteexit)r_ recursiverrr tests   rgc Csddl}ddl}|D]W}|j|r?t|ddd|s|r:tdddl}||j||d}t||dq tdq t|ddd|j ztt |Wq t yatd Yq wdS) Nrz/: )endzrecursing down:*z*** directory (use -r) ***:z*** not found ***) r_ospathisdirprintglobjoinescaperastdoutflushrOSError)listrftoplevelr_rlrrpnamesrrr ras&     ra__main__N)__doc____all__ collectionsrrr0 framerater5nframes sampwidthrrr r!appendr:rCrKrOrQrTrVr-r.r\rGrgra__name__rrrr sN