IP : 18.216.21.140Hostname : server86.web-hosting.comKernel : Linux server86.web-hosting.com 4.18.0-513.18.1.lve.el8.x86_64 #1 SMP Thu Feb 22 12:55:50 UTC 2024 x86_64Disable Function : None :) OS : Linux
PATH:
/
home/
servlmvm/
./
../
../
lib64/
python3.6/
sndhdr.py/
/
"""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. 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. """
# The file structure is top-down except that the test program and its # subroutine come last.
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.""")
def what(filename): """Guess the type of a sound file.""" res = whathdr(filename) return res
def 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 # #-----------------------------------#
def test_voc(h, f): if not h.startswith(b'Creative Voice File\032'): return None sbseek = get_short_le(h[20:22]) rate = 0 if 0 <= sbseek < 500 and h[sbseek] == 1: ratecode = 256 - h[sbseek+4] if ratecode: rate = int(1000000.0 / ratecode) return 'voc', rate, 1, -1, 8
tests.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 None f.seek(0) try: w = wave.openfp(f, 'r') except (EOFError, wave.Error): return None return ('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 1 return '8svx', 0, 1, 0, 8