QfdZddlZejedddgZddlmZedd Zd ej_d ej_d ej_d ej_dej_dZ dZgZdZej#edZej#edZej#edZej#edZej#edZej#edZej#edZej#edZdZdZdZdZdZedk(reyy) 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. N) )removewhatwhathdr) 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.ct|}|S)zGuess the type of a sound file.)r)filenameress -/opt/alt/python312/lib64/python3.12/sndhdr.pyrr8s ( C Jct|d5}|jd}tD] }|||}|st|ccdddS dddy#1swYyxYw)zRecognize sound headers.rbiN)openreadtestsr )r fhtfr s r rr>s\ h  FF3KBQ(C!3''    s&A AAActj5tjdtddl}ddd|j dsy|dddk(rd }n |ddd k(rd }ny|j d j|d }||j|j|jd|jzfS#1swYxYw#tjf$rYywxYw) zAIFC and AIFF filesignore)categoryrNFORM sAIFCaifcsAIFFaiffr)warningscatch_warnings simplefilterDeprecationWarningr startswithseekrEOFErrorError getframerate getnchannels getnframes getsampwidth)rrrfmtas r test_aifcr.Os  "h1CD # << 2w' 1RG FF1I IIa  !1>>#3 LLNA 00 22! # " djj !s C 8C CC0/C0c*|jdrt}n|dddvrt}nyd}||dd}||dd}||dd}||dd }||d d }d } |d k(rd } n|d k(rd} n |dk(rd} d } nd} | |z} | r|| z } nd} |||| | fS)zAU and SND filess.sndN)sds.sdns.aurrUr?)r$ get_long_be get_long_le) rrfuncfiletypehdr_size data_sizeencodingrate nchannels sample_size sample_bits frame_sizenframes r test_aurGgs||G 2A% %HAaF|HQqW IAbH~H "R>DQr"XIK1} Q Q   y(JZ' T9fk 99rcb|dddk7s|dddk7ryt|dd }|rd |z }nd }d |d ddfS)z HCOM fileAEsFSSDsHCOMNi"Vrhcomr5r9r)r:)rrdivisorrAs r test_hcomrQsRBx7aCjG3!C*%Gw 4B !!rc|jdsyt|dd}d}d|cxkrdkr&nn#||dk(rd||d zz }|rtd |z }d |dd d fS)zVOC filesCreative Voice FileNr3rir5r0g.Avocr9r)r$ get_short_leint)rrsbseekrAratecodes r test_vocrZss <<2 3 !Br( #F DFSQvY!^6!8$ y8+,D $2q  rcPddl}|jdr|dddk7s|dddk7ry|jd |j|d }d |j |j|jd|jzfS#t|j f$rYywxYw) zWAV filerNsRIFFrrsWAVEr2sfmt rwav) waver$r%rr&r'r(r)r*r+)rrr]ws r test_wavr_s << AaGw$6!Br(g:MFF1I IIa  1>>#Q^^%5<<>1Q^^%5#5 77 djj !sB B%$B%c8|jdr|dddk7ryy)z 8SVX filerrrs8SVXN)8svxrr5rr)r$)rrs r test_8svxrbs! << AaGw$6 rcl|jdr#t|dd}t|dd}d|d|dfSy) z SNDT filesSOUNDrrr3rSsndtr5N)r$r;rV)rrnsamplesrAs r test_sndtrfsD||Hq2w'AbH%tQ!++rcn|jdr$t|dd}d|cxkrdkr ny d|ddd fSy y ) z SNDR filesr7r0iiasndrr5r9rN)r$rV)rrrAs r test_sndrrisF||GAaF# 4 5 4B) ) !rcB|ddz|ddzz|ddzz|dzS)Nrr4r5r2r7rrbs r r:r:3 aDBJ1Q42: &!A$!) 4qt ;;rcB|ddz|ddzz|ddzz|dzS)Nrr4r7r2r5rrrkrls r r;r;rnrc|ddz|dzS)Nrrr5rkrls r get_short_berq aDAI1 rc|ddz|dzS)Nr5rrrkrls r rVrVrrrc\ddl}d}|jddr#|jddk(r|jdd=d} |jddrt|jdd|dytdg|dy#t$r/|jj d|j dYywxYw)Nrr5z-rr7.z [Interrupted] )sysargvtestallKeyboardInterruptstderrwriteexit)rv recursives r testr~sI xx| t+ HHQqSM  88AB< CHHQRL)Q / SE9a (  ,-  s)A3$A335B+*B+cddl}ddl}|D]}|jj|rwt |dzd|s|rWt dddl}|j |jj |j|d}t||dt dt |dzd|jj t t|y#t$rt d YwxYw) Nrz/: )endzrecursing down:*z*** directory (use -r) ***:z*** not found ***) rvospathisdirprintglobjoinescaperxstdoutflushrOSError)listr}toplevelrvrr rnamess r rxrxs  77== " (T/s +H'( "'',,t{{8/Dc"JKy!,23 (S.c * JJ    +d8n% +)* +s C""C98C9__main__)__doc__r _deprecated__name____all__ collectionsrr r= frameraterBnframes sampwidthrrrr.appendrGrQrZr_rbrfrir:r;rqrVr~rxrkrr rsk:Xg. 9 "  HJ  > !1 !6  !#   2* Y:> W " Y ! X 7 X Y, Y* Y<< +* zFr