1999-01-06 09:05:58 -04:00
|
|
|
"""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-LAW
|
|
|
|
|
|
|
|
If the file doesn't have a recognizable type, it returns None.
|
2012-12-25 10:47:37 -04:00
|
|
|
If the file can't be opened, OSError is raised.
|
1999-01-06 09:05:58 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2022-04-22 19:48:03 -03:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings._deprecated(__name__, remove=(3, 13))
|
|
|
|
|
1994-01-14 12:46:14 -04:00
|
|
|
# The file structure is top-down except that the test program and its
|
|
|
|
# subroutine come last.
|
|
|
|
|
2007-08-12 11:37:20 -03:00
|
|
|
__all__ = ['what', 'whathdr']
|
1994-01-14 12:46:14 -04:00
|
|
|
|
2014-10-09 17:59:30 -03:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
SndHeaders = namedtuple('SndHeaders',
|
|
|
|
'filetype framerate nchannels nframes sampwidth')
|
|
|
|
|
2015-08-18 02:04:45 -03:00
|
|
|
SndHeaders.filetype.__doc__ = ("""The 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'.""")
|
|
|
|
SndHeaders.framerate.__doc__ = ("""The sampling_rate will be either the actual
|
|
|
|
value or 0 if unknown or difficult to decode.""")
|
|
|
|
SndHeaders.nchannels.__doc__ = ("""The number of channels or 0 if it cannot be
|
|
|
|
determined or if the value is difficult to decode.""")
|
|
|
|
SndHeaders.nframes.__doc__ = ("""The value for frames will be either the number
|
|
|
|
of frames or -1.""")
|
|
|
|
SndHeaders.sampwidth.__doc__ = ("""Either the sample size in bits or
|
|
|
|
'A' for A-LAW or 'U' for u-LAW.""")
|
|
|
|
|
1994-01-14 12:46:14 -04:00
|
|
|
def what(filename):
|
2007-08-12 11:37:20 -03:00
|
|
|
"""Guess the type of a sound file."""
|
2001-01-14 21:36:40 -04:00
|
|
|
res = whathdr(filename)
|
|
|
|
return res
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def whathdr(filename):
|
2007-08-12 11:37:20 -03:00
|
|
|
"""Recognize sound headers."""
|
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
h = f.read(512)
|
|
|
|
for tf in tests:
|
|
|
|
res = tf(h, f)
|
|
|
|
if res:
|
2014-10-09 17:59:30 -03:00
|
|
|
return SndHeaders(*res)
|
2007-08-12 11:37:20 -03:00
|
|
|
return None
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------#
|
|
|
|
# Subroutines per sound header type #
|
|
|
|
#-----------------------------------#
|
|
|
|
|
|
|
|
tests = []
|
|
|
|
|
|
|
|
def test_aifc(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""AIFC and AIFF files"""
|
2022-04-05 16:05:48 -03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', category=DeprecationWarning)
|
|
|
|
import aifc
|
2010-07-13 20:04:56 -03:00
|
|
|
if not h.startswith(b'FORM'):
|
2001-01-14 21:36:40 -04:00
|
|
|
return None
|
2007-08-12 11:37:20 -03:00
|
|
|
if h[8:12] == b'AIFC':
|
2001-01-14 21:36:40 -04:00
|
|
|
fmt = 'aifc'
|
2007-08-12 11:37:20 -03:00
|
|
|
elif h[8:12] == b'AIFF':
|
2010-07-13 20:04:56 -03:00
|
|
|
fmt = 'aiff'
|
2001-01-14 21:36:40 -04:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
f.seek(0)
|
|
|
|
try:
|
2007-08-13 14:50:00 -03:00
|
|
|
a = aifc.open(f, 'r')
|
2001-01-14 21:36:40 -04:00
|
|
|
except (EOFError, aifc.Error):
|
|
|
|
return None
|
2007-08-12 11:37:20 -03:00
|
|
|
return (fmt, a.getframerate(), a.getnchannels(),
|
|
|
|
a.getnframes(), 8 * a.getsampwidth())
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_aifc)
|
|
|
|
|
|
|
|
|
|
|
|
def test_au(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""AU and SND files"""
|
2007-08-13 14:50:00 -03:00
|
|
|
if h.startswith(b'.snd'):
|
2007-08-12 11:37:20 -03:00
|
|
|
func = get_long_be
|
|
|
|
elif h[:4] in (b'\0ds.', b'dns.'):
|
|
|
|
func = get_long_le
|
2001-01-14 21:36:40 -04:00
|
|
|
else:
|
|
|
|
return None
|
2007-08-12 11:37:20 -03:00
|
|
|
filetype = '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])
|
2001-01-14 21:36:40 -04:00
|
|
|
sample_size = 1 # default
|
|
|
|
if encoding == 1:
|
|
|
|
sample_bits = 'U'
|
|
|
|
elif encoding == 2:
|
|
|
|
sample_bits = 8
|
|
|
|
elif encoding == 3:
|
|
|
|
sample_bits = 16
|
|
|
|
sample_size = 2
|
|
|
|
else:
|
|
|
|
sample_bits = '?'
|
|
|
|
frame_size = sample_size * nchannels
|
2007-08-13 14:50:00 -03:00
|
|
|
if frame_size:
|
|
|
|
nframe = data_size / frame_size
|
|
|
|
else:
|
|
|
|
nframe = -1
|
|
|
|
return filetype, rate, nchannels, nframe, sample_bits
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_au)
|
|
|
|
|
|
|
|
|
|
|
|
def test_hcom(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""HCOM file"""
|
2007-08-12 11:37:20 -03:00
|
|
|
if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':
|
2001-01-14 21:36:40 -04:00
|
|
|
return None
|
2007-08-13 14:50:00 -03:00
|
|
|
divisor = get_long_be(h[144:148])
|
|
|
|
if divisor:
|
|
|
|
rate = 22050 / divisor
|
|
|
|
else:
|
|
|
|
rate = 0
|
|
|
|
return 'hcom', rate, 1, -1, 8
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_hcom)
|
|
|
|
|
|
|
|
|
|
|
|
def test_voc(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""VOC file"""
|
2010-07-13 20:04:56 -03:00
|
|
|
if not h.startswith(b'Creative Voice File\032'):
|
2001-01-14 21:36:40 -04:00
|
|
|
return None
|
|
|
|
sbseek = get_short_le(h[20:22])
|
|
|
|
rate = 0
|
2007-08-13 14:50:00 -03:00
|
|
|
if 0 <= sbseek < 500 and h[sbseek] == 1:
|
|
|
|
ratecode = 256 - h[sbseek+4]
|
|
|
|
if ratecode:
|
|
|
|
rate = int(1000000.0 / ratecode)
|
2001-01-14 21:36:40 -04:00
|
|
|
return 'voc', rate, 1, -1, 8
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_voc)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wav(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""WAV file"""
|
2013-03-18 18:42:42 -03:00
|
|
|
import wave
|
2001-01-14 21:36:40 -04:00
|
|
|
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
|
2007-08-13 14:50:00 -03:00
|
|
|
if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
|
2001-01-14 21:36:40 -04:00
|
|
|
return None
|
2013-03-18 18:42:42 -03:00
|
|
|
f.seek(0)
|
|
|
|
try:
|
2017-11-10 12:38:25 -04:00
|
|
|
w = wave.open(f, 'r')
|
2013-03-18 18:42:42 -03:00
|
|
|
except (EOFError, wave.Error):
|
|
|
|
return None
|
|
|
|
return ('wav', w.getframerate(), w.getnchannels(),
|
|
|
|
w.getnframes(), 8*w.getsampwidth())
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_wav)
|
|
|
|
|
|
|
|
|
|
|
|
def test_8svx(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""8SVX file"""
|
2010-07-13 20:04:56 -03:00
|
|
|
if not h.startswith(b'FORM') or h[8:12] != b'8SVX':
|
2001-01-14 21:36:40 -04:00
|
|
|
return None
|
|
|
|
# Should decode it to get #channels -- assume always 1
|
|
|
|
return '8svx', 0, 1, 0, 8
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_8svx)
|
|
|
|
|
|
|
|
|
|
|
|
def test_sndt(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""SNDT file"""
|
2007-08-13 14:50:00 -03:00
|
|
|
if h.startswith(b'SOUND'):
|
2001-01-14 21:36:40 -04:00
|
|
|
nsamples = get_long_le(h[8:12])
|
|
|
|
rate = get_short_le(h[20:22])
|
|
|
|
return 'sndt', rate, 1, nsamples, 8
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_sndt)
|
|
|
|
|
|
|
|
|
|
|
|
def test_sndr(h, f):
|
2022-10-15 10:30:05 -03:00
|
|
|
"""SNDR file"""
|
2007-08-13 14:50:00 -03:00
|
|
|
if h.startswith(b'\0\0'):
|
2001-01-14 21:36:40 -04:00
|
|
|
rate = get_short_le(h[2:4])
|
|
|
|
if 4000 <= rate <= 25000:
|
|
|
|
return 'sndr', rate, 1, -1, 8
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
tests.append(test_sndr)
|
|
|
|
|
|
|
|
|
2007-08-12 11:37:20 -03:00
|
|
|
#-------------------------------------------#
|
|
|
|
# Subroutines to extract numbers from bytes #
|
|
|
|
#-------------------------------------------#
|
1994-01-14 12:46:14 -04:00
|
|
|
|
2007-08-12 11:37:20 -03:00
|
|
|
def get_long_be(b):
|
|
|
|
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
|
1994-01-14 12:46:14 -04:00
|
|
|
|
2007-08-12 11:37:20 -03:00
|
|
|
def get_long_le(b):
|
|
|
|
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]
|
1994-01-14 12:46:14 -04:00
|
|
|
|
2007-08-12 11:37:20 -03:00
|
|
|
def get_short_be(b):
|
|
|
|
return (b[0] << 8) | b[1]
|
1994-01-14 12:46:14 -04:00
|
|
|
|
2007-08-12 11:37:20 -03:00
|
|
|
def get_short_le(b):
|
|
|
|
return (b[1] << 8) | b[0]
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
#--------------------#
|
|
|
|
# Small test program #
|
|
|
|
#--------------------#
|
|
|
|
|
|
|
|
def test():
|
2001-01-14 21:36:40 -04:00
|
|
|
import sys
|
|
|
|
recursive = 0
|
|
|
|
if sys.argv[1:] and sys.argv[1] == '-r':
|
|
|
|
del sys.argv[1:2]
|
|
|
|
recursive = 1
|
|
|
|
try:
|
|
|
|
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)
|
1994-01-14 12:46:14 -04:00
|
|
|
|
|
|
|
def testall(list, recursive, toplevel):
|
2001-01-14 21:36:40 -04:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
for filename in list:
|
|
|
|
if os.path.isdir(filename):
|
2007-02-09 01:37:30 -04:00
|
|
|
print(filename + '/:', end=' ')
|
2001-01-14 21:36:40 -04:00
|
|
|
if recursive or toplevel:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('recursing down:')
|
2001-01-14 21:36:40 -04:00
|
|
|
import glob
|
2020-06-20 05:10:31 -03:00
|
|
|
names = glob.glob(os.path.join(glob.escape(filename), '*'))
|
2001-01-14 21:36:40 -04:00
|
|
|
testall(names, recursive, 0)
|
|
|
|
else:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('*** directory (use -r) ***')
|
2001-01-14 21:36:40 -04:00
|
|
|
else:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(filename + ':', end=' ')
|
2001-01-14 21:36:40 -04:00
|
|
|
sys.stdout.flush()
|
|
|
|
try:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(what(filename))
|
2012-12-25 10:47:37 -04:00
|
|
|
except OSError:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('*** not found ***')
|
1999-01-06 09:05:58 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2001-01-14 21:36:40 -04:00
|
|
|
test()
|