Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: sndhdr.py
"""Routines to help recognizing sound files.
[0] Fix | Delete
[1] Fix | Delete
Function whathdr() recognizes various types of sound file headers.
[2] Fix | Delete
It understands almost all headers that SOX can decode.
[3] Fix | Delete
[4] Fix | Delete
The return tuple contains the following items, in this order:
[5] Fix | Delete
- file type (as SOX understands it)
[6] Fix | Delete
- sampling rate (0 if unknown or hard to decode)
[7] Fix | Delete
- number of channels (0 if unknown or hard to decode)
[8] Fix | Delete
- number of frames in the file (-1 if unknown or hard to decode)
[9] Fix | Delete
- number of bits/sample, or 'U' for U-LAW, or 'A' for A-LAW
[10] Fix | Delete
[11] Fix | Delete
If the file doesn't have a recognizable type, it returns None.
[12] Fix | Delete
If the file can't be opened, OSError is raised.
[13] Fix | Delete
[14] Fix | Delete
To compute the total time, divide the number of frames by the
[15] Fix | Delete
sampling rate (a frame contains a sample for each channel).
[16] Fix | Delete
[17] Fix | Delete
Function what() calls whathdr(). (It used to also use some
[18] Fix | Delete
heuristics for raw data, but this doesn't work very well.)
[19] Fix | Delete
[20] Fix | Delete
Finally, the function test() is a simple main program that calls
[21] Fix | Delete
what() for all files mentioned on the argument list. For directory
[22] Fix | Delete
arguments it calls what() for all files in that directory. Default
[23] Fix | Delete
argument is "." (testing all files in the current directory). The
[24] Fix | Delete
option -r tells it to recurse down directories found inside
[25] Fix | Delete
explicitly given directories.
[26] Fix | Delete
"""
[27] Fix | Delete
[28] Fix | Delete
# The file structure is top-down except that the test program and its
[29] Fix | Delete
# subroutine come last.
[30] Fix | Delete
[31] Fix | Delete
__all__ = ['what', 'whathdr']
[32] Fix | Delete
[33] Fix | Delete
from collections import namedtuple
[34] Fix | Delete
[35] Fix | Delete
SndHeaders = namedtuple('SndHeaders',
[36] Fix | Delete
'filetype framerate nchannels nframes sampwidth')
[37] Fix | Delete
[38] Fix | Delete
SndHeaders.filetype.__doc__ = ("""The value for type indicates the data type
[39] Fix | Delete
and will be one of the strings 'aifc', 'aiff', 'au','hcom',
[40] Fix | Delete
'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'.""")
[41] Fix | Delete
SndHeaders.framerate.__doc__ = ("""The sampling_rate will be either the actual
[42] Fix | Delete
value or 0 if unknown or difficult to decode.""")
[43] Fix | Delete
SndHeaders.nchannels.__doc__ = ("""The number of channels or 0 if it cannot be
[44] Fix | Delete
determined or if the value is difficult to decode.""")
[45] Fix | Delete
SndHeaders.nframes.__doc__ = ("""The value for frames will be either the number
[46] Fix | Delete
of frames or -1.""")
[47] Fix | Delete
SndHeaders.sampwidth.__doc__ = ("""Either the sample size in bits or
[48] Fix | Delete
'A' for A-LAW or 'U' for u-LAW.""")
[49] Fix | Delete
[50] Fix | Delete
def what(filename):
[51] Fix | Delete
"""Guess the type of a sound file."""
[52] Fix | Delete
res = whathdr(filename)
[53] Fix | Delete
return res
[54] Fix | Delete
[55] Fix | Delete
[56] Fix | Delete
def whathdr(filename):
[57] Fix | Delete
"""Recognize sound headers."""
[58] Fix | Delete
with open(filename, 'rb') as f:
[59] Fix | Delete
h = f.read(512)
[60] Fix | Delete
for tf in tests:
[61] Fix | Delete
res = tf(h, f)
[62] Fix | Delete
if res:
[63] Fix | Delete
return SndHeaders(*res)
[64] Fix | Delete
return None
[65] Fix | Delete
[66] Fix | Delete
[67] Fix | Delete
#-----------------------------------#
[68] Fix | Delete
# Subroutines per sound header type #
[69] Fix | Delete
#-----------------------------------#
[70] Fix | Delete
[71] Fix | Delete
tests = []
[72] Fix | Delete
[73] Fix | Delete
def test_aifc(h, f):
[74] Fix | Delete
import aifc
[75] Fix | Delete
if not h.startswith(b'FORM'):
[76] Fix | Delete
return None
[77] Fix | Delete
if h[8:12] == b'AIFC':
[78] Fix | Delete
fmt = 'aifc'
[79] Fix | Delete
elif h[8:12] == b'AIFF':
[80] Fix | Delete
fmt = 'aiff'
[81] Fix | Delete
else:
[82] Fix | Delete
return None
[83] Fix | Delete
f.seek(0)
[84] Fix | Delete
try:
[85] Fix | Delete
a = aifc.open(f, 'r')
[86] Fix | Delete
except (EOFError, aifc.Error):
[87] Fix | Delete
return None
[88] Fix | Delete
return (fmt, a.getframerate(), a.getnchannels(),
[89] Fix | Delete
a.getnframes(), 8 * a.getsampwidth())
[90] Fix | Delete
[91] Fix | Delete
tests.append(test_aifc)
[92] Fix | Delete
[93] Fix | Delete
[94] Fix | Delete
def test_au(h, f):
[95] Fix | Delete
if h.startswith(b'.snd'):
[96] Fix | Delete
func = get_long_be
[97] Fix | Delete
elif h[:4] in (b'\0ds.', b'dns.'):
[98] Fix | Delete
func = get_long_le
[99] Fix | Delete
else:
[100] Fix | Delete
return None
[101] Fix | Delete
filetype = 'au'
[102] Fix | Delete
hdr_size = func(h[4:8])
[103] Fix | Delete
data_size = func(h[8:12])
[104] Fix | Delete
encoding = func(h[12:16])
[105] Fix | Delete
rate = func(h[16:20])
[106] Fix | Delete
nchannels = func(h[20:24])
[107] Fix | Delete
sample_size = 1 # default
[108] Fix | Delete
if encoding == 1:
[109] Fix | Delete
sample_bits = 'U'
[110] Fix | Delete
elif encoding == 2:
[111] Fix | Delete
sample_bits = 8
[112] Fix | Delete
elif encoding == 3:
[113] Fix | Delete
sample_bits = 16
[114] Fix | Delete
sample_size = 2
[115] Fix | Delete
else:
[116] Fix | Delete
sample_bits = '?'
[117] Fix | Delete
frame_size = sample_size * nchannels
[118] Fix | Delete
if frame_size:
[119] Fix | Delete
nframe = data_size / frame_size
[120] Fix | Delete
else:
[121] Fix | Delete
nframe = -1
[122] Fix | Delete
return filetype, rate, nchannels, nframe, sample_bits
[123] Fix | Delete
[124] Fix | Delete
tests.append(test_au)
[125] Fix | Delete
[126] Fix | Delete
[127] Fix | Delete
def test_hcom(h, f):
[128] Fix | Delete
if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':
[129] Fix | Delete
return None
[130] Fix | Delete
divisor = get_long_be(h[144:148])
[131] Fix | Delete
if divisor:
[132] Fix | Delete
rate = 22050 / divisor
[133] Fix | Delete
else:
[134] Fix | Delete
rate = 0
[135] Fix | Delete
return 'hcom', rate, 1, -1, 8
[136] Fix | Delete
[137] Fix | Delete
tests.append(test_hcom)
[138] Fix | Delete
[139] Fix | Delete
[140] Fix | Delete
def test_voc(h, f):
[141] Fix | Delete
if not h.startswith(b'Creative Voice File\032'):
[142] Fix | Delete
return None
[143] Fix | Delete
sbseek = get_short_le(h[20:22])
[144] Fix | Delete
rate = 0
[145] Fix | Delete
if 0 <= sbseek < 500 and h[sbseek] == 1:
[146] Fix | Delete
ratecode = 256 - h[sbseek+4]
[147] Fix | Delete
if ratecode:
[148] Fix | Delete
rate = int(1000000.0 / ratecode)
[149] Fix | Delete
return 'voc', rate, 1, -1, 8
[150] Fix | Delete
[151] Fix | Delete
tests.append(test_voc)
[152] Fix | Delete
[153] Fix | Delete
[154] Fix | Delete
def test_wav(h, f):
[155] Fix | Delete
import wave
[156] Fix | Delete
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
[157] Fix | Delete
if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
[158] Fix | Delete
return None
[159] Fix | Delete
f.seek(0)
[160] Fix | Delete
try:
[161] Fix | Delete
w = wave.openfp(f, 'r')
[162] Fix | Delete
except (EOFError, wave.Error):
[163] Fix | Delete
return None
[164] Fix | Delete
return ('wav', w.getframerate(), w.getnchannels(),
[165] Fix | Delete
w.getnframes(), 8*w.getsampwidth())
[166] Fix | Delete
[167] Fix | Delete
tests.append(test_wav)
[168] Fix | Delete
[169] Fix | Delete
[170] Fix | Delete
def test_8svx(h, f):
[171] Fix | Delete
if not h.startswith(b'FORM') or h[8:12] != b'8SVX':
[172] Fix | Delete
return None
[173] Fix | Delete
# Should decode it to get #channels -- assume always 1
[174] Fix | Delete
return '8svx', 0, 1, 0, 8
[175] Fix | Delete
[176] Fix | Delete
tests.append(test_8svx)
[177] Fix | Delete
[178] Fix | Delete
[179] Fix | Delete
def test_sndt(h, f):
[180] Fix | Delete
if h.startswith(b'SOUND'):
[181] Fix | Delete
nsamples = get_long_le(h[8:12])
[182] Fix | Delete
rate = get_short_le(h[20:22])
[183] Fix | Delete
return 'sndt', rate, 1, nsamples, 8
[184] Fix | Delete
[185] Fix | Delete
tests.append(test_sndt)
[186] Fix | Delete
[187] Fix | Delete
[188] Fix | Delete
def test_sndr(h, f):
[189] Fix | Delete
if h.startswith(b'\0\0'):
[190] Fix | Delete
rate = get_short_le(h[2:4])
[191] Fix | Delete
if 4000 <= rate <= 25000:
[192] Fix | Delete
return 'sndr', rate, 1, -1, 8
[193] Fix | Delete
[194] Fix | Delete
tests.append(test_sndr)
[195] Fix | Delete
[196] Fix | Delete
[197] Fix | Delete
#-------------------------------------------#
[198] Fix | Delete
# Subroutines to extract numbers from bytes #
[199] Fix | Delete
#-------------------------------------------#
[200] Fix | Delete
[201] Fix | Delete
def get_long_be(b):
[202] Fix | Delete
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
[203] Fix | Delete
[204] Fix | Delete
def get_long_le(b):
[205] Fix | Delete
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]
[206] Fix | Delete
[207] Fix | Delete
def get_short_be(b):
[208] Fix | Delete
return (b[0] << 8) | b[1]
[209] Fix | Delete
[210] Fix | Delete
def get_short_le(b):
[211] Fix | Delete
return (b[1] << 8) | b[0]
[212] Fix | Delete
[213] Fix | Delete
[214] Fix | Delete
#--------------------#
[215] Fix | Delete
# Small test program #
[216] Fix | Delete
#--------------------#
[217] Fix | Delete
[218] Fix | Delete
def test():
[219] Fix | Delete
import sys
[220] Fix | Delete
recursive = 0
[221] Fix | Delete
if sys.argv[1:] and sys.argv[1] == '-r':
[222] Fix | Delete
del sys.argv[1:2]
[223] Fix | Delete
recursive = 1
[224] Fix | Delete
try:
[225] Fix | Delete
if sys.argv[1:]:
[226] Fix | Delete
testall(sys.argv[1:], recursive, 1)
[227] Fix | Delete
else:
[228] Fix | Delete
testall(['.'], recursive, 1)
[229] Fix | Delete
except KeyboardInterrupt:
[230] Fix | Delete
sys.stderr.write('\n[Interrupted]\n')
[231] Fix | Delete
sys.exit(1)
[232] Fix | Delete
[233] Fix | Delete
def testall(list, recursive, toplevel):
[234] Fix | Delete
import sys
[235] Fix | Delete
import os
[236] Fix | Delete
for filename in list:
[237] Fix | Delete
if os.path.isdir(filename):
[238] Fix | Delete
print(filename + '/:', end=' ')
[239] Fix | Delete
if recursive or toplevel:
[240] Fix | Delete
print('recursing down:')
[241] Fix | Delete
import glob
[242] Fix | Delete
names = glob.glob(os.path.join(filename, '*'))
[243] Fix | Delete
testall(names, recursive, 0)
[244] Fix | Delete
else:
[245] Fix | Delete
print('*** directory (use -r) ***')
[246] Fix | Delete
else:
[247] Fix | Delete
print(filename + ':', end=' ')
[248] Fix | Delete
sys.stdout.flush()
[249] Fix | Delete
try:
[250] Fix | Delete
print(what(filename))
[251] Fix | Delete
except OSError:
[252] Fix | Delete
print('*** not found ***')
[253] Fix | Delete
[254] Fix | Delete
if __name__ == '__main__':
[255] Fix | Delete
test()
[256] Fix | Delete
[257] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function