import sys import os.path import aifc import sunau import wave import test.audiotests modules = { '.aiff': aifc, '.aifc': aifc, '.au': sunau, '.wav': wave, } byteswaps = { 1: lambda x: x, 2: test.audiotests.byteswap2, 3: test.audiotests.byteswap3, 4: test.audiotests.byteswap4, } byteorders = { aifc: 'big', sunau: 'big', wave: 'little', } def chunks(seq, size): for i in range(0, len(seq), size): yield seq[i: i + size] def tohex(frames, framesize, indent): linesize = (80 - indent - 2) // (framesize * 2 + 1) // 4 * 4 * framesize return '\\\n'.join(' ' * indent + repr(' '.join(''.join('%02X' % b for b in sample) for sample in chunks(line, framesize))) for line in chunks(frames, linesize)) def tohex(frames, framesize, indent): linesize = (80 - indent - 1) // (framesize * 2 + 1) // 4 * 4 * framesize return '"""\\\n%s \\\n%s"""' % (' \\\n'.join(' ' * indent + ' '.join(''.join('%02X' % b for b in sample) for sample in chunks(line, framesize)) for line in chunks(frames, linesize)), ' ' * indent) for fn in sys.argv[1:]: _, ext = os.path.splitext(fn) try: module = modules[ext] except KeyError: continue try: f = module.open(fn, 'r') except module.Error: continue with f: nframes = 48 frames = f.readframes(nframes) sampwidth = f.getsampwidth() framesize = f.getnchannels() * sampwidth t = f.getcomptype() if isinstance(t, bytes): t = t.decode('latin1') if t == 'NONE': t = 'PCM%s' % (sampwidth * 8) byteorder = byteorders[module] if t.lower() in ('alaw', 'ulaw'): byteorder = sys.byteorder if byteorder != 'big': frames = byteswaps[sampwidth](frames) print("""\ class %s%sTest(audiotests.AudioWriteTests, audiotests.AudioTestsWithSourceFile, unittest.TestCase):""" % (module.__name__.title(), t.upper())) print(' module = %s' % module.__name__) print(' sndfilename = %r' % os.path.basename(fn)) print(' sndfilenframes = %s' % f.getnframes()) print(' nchannels = %s' % f.getnchannels()) print(' sampwidth = %s' % sampwidth) print(' framerate = %s' % f.getframerate()) print(' nframes = %s' % nframes) print(' comptype = %r' % f.getcomptype()) print(' compname = %r' % f.getcompname()) print(' frames = bytes.fromhex(%s)' % tohex(frames, framesize, 6)) if t.lower() in ('alaw', 'ulaw'): print(" if sys.byteorder != 'big':") print(' frames = audiotests.byteswap%s(frames)' % sampwidth) elif byteorders[module] != 'big' and sampwidth > 1: print(' frames = audiotests.byteswap%s(frames)' % sampwidth) if module is aifc: print(' close_fd = True') print() print()