\ddZddlZejedddgZddlmZedd Zd ej_d ej _d ej _d ej _dej _dZ dZgZdZeedZeedZeedZeedZeedZeedZeedZeedZdZdZdZdZdZedkr edSdS) 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.c$t|}|S)zGuess the type of a sound file.)r)filenameress ..\python\lib\sndhdr.pyrr8s (  C Jct|d5}|d}tD]'}|||}|rt|ccdddS( ddddS#1swxYwYdS)zRecognize sound headers.rbiN)openreadtestsr )r fhtfr s r rr>s h   FF3KK ( (B"Q((C (!3'''  ( s7A$A$$A(+A(c:tj5tjdtddl}dddn #1swxYwY|dsdS|dddkrd }n|ddd krd }ndS|d ||d }n#t|j f$rYdSwxYw|| | | d| zfS) zAIFC and AIFF filesignore)categoryrNFORM sAIFCaifcsAIFFaiffr)warningscatch_warnings simplefilterDeprecationWarningr startswithseekrEOFErrorError getframerate getnchannels getnframes getsampwidth)rrrfmtas r test_aifcr.Osm  " "h1CDDDD  << t2w' 1R4G  tFF1III IIa   dj !tt !!1>>#3#3 LLNNA 0 00 22s# AAAB33CCc|drt}n|dddvrt}ndSd}||dd}||dd}||dd}||dd }||d d }d } |d krd } n|d krd} n |dkrd} 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 2A2% % %tHtAacF||HQqtW ItAbeH~~H 4"R%>>DQr"uXIK1}} Q Q   y(JZ' T9fk 99rc|dddks|dddkrdSt|dd }|rd |z }nd }d |d ddfS)z HCOM fileAEsFSSDsHCOMNi"Vrhcomr5r9r)r:)rrdivisorrAs r test_hcomrQsjBx7aCjG33t!CG*%%Gw 4B !!rc|dsdSt|dd}d}d|cxkrdkr1nn.||dkr"d||d zz }|rtd |z }d |dd d fS)zVOC filesCreative Voice FileNr3rir5r0g.Avocr9r)r$ get_short_leint)rrsbseekrAratecodes r test_vocrZs <<2 3 3t !BrE( # #F DFSQvY!^^6!8$  -y8+,,D $2q  rcddl}|dr|dddks|dddkrdS|d ||d }n#t|jf$rYdSwxYwd |||d| zfS) zWAV filerNsRIFFrrsWAVEr2sfmt rwav) waver$r%rr&r'r(r)r*r+)rrr]ws r test_wavr_sKKK << AadGw$6$6!BrE(g:M:MtFF1III IIa   dj !tt 1>>##Q^^%5%5<<>>1Q^^%5%5#5 77sA%%A:9A:cP|dr|dddkrdSdS)z 8SVX filerrrs8SVXN)8svxrr5rr)r$)rrs r test_8svxrbs3 << AadGw$6$6t  rc|dr5t|dd}t|dd}d|d|dfSdS) z SNDT filesSOUNDrrr3rSsndtr5N)r$r;rV)rrnsamplesrAs r test_sndtrfs[||H,q2w''AbeH%%tQ!++,,rc|dr/t|dd}d|cxkrdkr nd Sd|ddd fSd Sd S) z SNDR filesr7r0iiasndrr5r9rN)r$rV)rrrAs r test_sndrrisq||G*AacF## 4 5 4B) )** rcZ|ddz|ddzz|ddzz|dzS)Nrr4r5r2r7rrbs r r:r:3 aDBJ1Q42: &!A$!) 4qt ;;rcZ|ddz|ddzz|ddzz|dzS)Nrr4r7r2r5rrrkrls r r;r;rnrc*|ddz|dzS)Nrrr5rkrls r get_short_berq aDAI1 rc*|ddz|dzS)Nr5rrrkrls r rVrVrrrcnddl}d}|jddr|jddkr |jdd=d} |jddr t|jdd|ddStdg|ddS#t$r3|jd|dYdSwxYw)Nrr5z-rr7.z [Interrupted] )sysargvtestallKeyboardInterruptstderrwriteexit)rv recursives r testr~sJJJI x| t++ HQqSM  8ABB< ) CHQRRL)Q / / / / / SE9a ( ( ( ( (  ,---  s-A7#A779B43B4cFddl}ddl}|D]}|j|rt |dzd|s|rft dddl}||j||d}t||dt dt |dzd|j  t t|#t$rt d YwxYwdS) Nrz/: )endzrecursing down:*z*** directory (use -r) ***:z*** not found ***) rvospathisdirprintglobjoinescaperxstdoutflushrOSError)listr}toplevelrvrr rnamess r rxrxsLJJJ III++ 7== " " + (T/s + + + + 4H 4'(((  "',,t{{8/D/Dc"J"JKKy!,,,,23333 (S.c * * * * J      +d8nn%%%% + + +)***** +++s$DDD__main__)__doc__r _deprecated__name____all__ collectionsrr r= frameraterBnframes sampwidthrrrr.appendrGrQrZr_rbrfrir:r;rqrVr~rxrkrr rsd:Xg.... 9 """""" Z HJJ  > !1 !6  !#   222* Y:::> W " " " Y ! ! ! X 7 7 7 X Y,,, Y*** Y<<<<<<   +++* zDFFFFFr