同步操作将从 OpenHarmony-SIG/python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""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-LAWIf 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 thesampling rate (a frame contains a sample for each channel).Function what() calls whathdr(). (It used to also use someheuristics for raw data, but this doesn't work very well.)Finally, the function test() is a simple main program that callswhat() for all files mentioned on the argument list. For directoryarguments it calls what() for all files in that directory. Defaultargument is "." (testing all files in the current directory). Theoption -r tells it to recurse down directories found insideexplicitly given directories."""# The file structure is top-down except that the test program and its# subroutine come last.__all__ = ['what', 'whathdr']from collections import namedtupleSndHeaders = namedtuple('SndHeaders','filetype framerate nchannels nframes sampwidth')SndHeaders.filetype.__doc__ = ("""The value for type indicates the data typeand will be one of the strings 'aifc', 'aiff', 'au','hcom','sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'.""")SndHeaders.framerate.__doc__ = ("""The sampling_rate will be either the actualvalue or 0 if unknown or difficult to decode.""")SndHeaders.nchannels.__doc__ = ("""The number of channels or 0 if it cannot bedetermined or if the value is difficult to decode.""")SndHeaders.nframes.__doc__ = ("""The value for frames will be either the numberof frames or -1.""")SndHeaders.sampwidth.__doc__ = ("""Either the sample size in bits or'A' for A-LAW or 'U' for u-LAW.""")def what(filename):"""Guess the type of a sound file."""res = whathdr(filename)return resdef whathdr(filename):"""Recognize sound headers."""with open(filename, 'rb') as f:h = f.read(512)for tf in tests:res = tf(h, f)if res:return SndHeaders(*res)return None#-----------------------------------## Subroutines per sound header type ##-----------------------------------#tests = []def test_aifc(h, f):import aifcif not h.startswith(b'FORM'):return Noneif h[8:12] == b'AIFC':fmt = 'aifc'elif h[8:12] == b'AIFF':fmt = 'aiff'else:return Nonef.seek(0)try:a = aifc.open(f, 'r')except (EOFError, aifc.Error):return Nonereturn (fmt, a.getframerate(), a.getnchannels(),a.getnframes(), 8 * a.getsampwidth())tests.append(test_aifc)def test_au(h, f):if h.startswith(b'.snd'):func = get_long_beelif h[:4] in (b'0円ds.', b'dns.'):func = get_long_leelse:return Nonefiletype = 'au'hdr_size = func(h[4:8])data_size = func(h[8:12])encoding = func(h[12:16])rate = func(h[16:20])nchannels = func(h[20:24])sample_size = 1 # defaultif encoding == 1:sample_bits = 'U'elif encoding == 2:sample_bits = 8elif encoding == 3:sample_bits = 16sample_size = 2else:sample_bits = '?'frame_size = sample_size * nchannelsif frame_size:nframe = data_size / frame_sizeelse:nframe = -1return filetype, rate, nchannels, nframe, sample_bitstests.append(test_au)def test_hcom(h, f):if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':return Nonedivisor = get_long_be(h[144:148])if divisor:rate = 22050 / divisorelse:rate = 0return 'hcom', rate, 1, -1, 8tests.append(test_hcom)def test_voc(h, f):if not h.startswith(b'Creative Voice File032円'):return Nonesbseek = get_short_le(h[20:22])rate = 0if 0 <= sbseek < 500 and h[sbseek] == 1:ratecode = 256 - h[sbseek+4]if ratecode:rate = int(1000000.0 / ratecode)return 'voc', rate, 1, -1, 8tests.append(test_voc)def test_wav(h, f):import wave# 'RIFF' <len> 'WAVE' 'fmt ' <len>if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':return Nonef.seek(0)try:w = wave.open(f, 'r')except (EOFError, wave.Error):return Nonereturn ('wav', w.getframerate(), w.getnchannels(),w.getnframes(), 8*w.getsampwidth())tests.append(test_wav)def test_8svx(h, f):if not h.startswith(b'FORM') or h[8:12] != b'8SVX':return None# Should decode it to get #channels -- assume always 1return '8svx', 0, 1, 0, 8tests.append(test_8svx)def test_sndt(h, f):if h.startswith(b'SOUND'):nsamples = get_long_le(h[8:12])rate = get_short_le(h[20:22])return 'sndt', rate, 1, nsamples, 8tests.append(test_sndt)def test_sndr(h, f):if h.startswith(b'0円0円'):rate = get_short_le(h[2:4])if 4000 <= rate <= 25000:return 'sndr', rate, 1, -1, 8tests.append(test_sndr)#-------------------------------------------## Subroutines to extract numbers from bytes ##-------------------------------------------#def get_long_be(b):return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]def get_long_le(b):return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]def get_short_be(b):return (b[0] << 8) | b[1]def get_short_le(b):return (b[1] << 8) | b[0]#--------------------## Small test program ##--------------------#def test():import sysrecursive = 0if sys.argv[1:] and sys.argv[1] == '-r':del sys.argv[1:2]recursive = 1try:if sys.argv[1:]:testall(sys.argv[1:], recursive, 1)else:testall(['.'], recursive, 1)except KeyboardInterrupt:sys.stderr.write('\n[Interrupted]\n')sys.exit(1)def testall(list, recursive, toplevel):import sysimport osfor filename in list:if os.path.isdir(filename):print(filename + '/:', end=' ')if recursive or toplevel:print('recursing down:')import globnames = glob.glob(os.path.join(glob.escape(filename), '*'))testall(names, recursive, 0)else:print('*** directory (use -r) ***')else:print(filename + ':', end=' ')sys.stdout.flush()try:print(what(filename))except OSError:print('*** not found ***')if __name__ == '__main__':test()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。