Created
February 20, 2024 21:36
-
-
Save dmitriykovalev/9140d7b0eccbf7d5ff96b626c4b1247d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import hashlib | |
| import math | |
| import struct | |
| import wave | |
| def rms(samples): | |
| return math.sqrt(sum(s*s for s in samples) / len(samples)) | |
| def md5(data): | |
| return hashlib.md5(data).hexdigest() | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('filename') | |
| args = parser.parse_args() | |
| with wave.open(args.filename, 'rb') as f: | |
| num_channels = f.getnchannels() | |
| sample_width = f.getsampwidth() | |
| sample_rate = f.getframerate() | |
| num_frames = f.getnframes() | |
| print(f'info:') | |
| print(f' num_channels: {num_channels}', ) | |
| print(f' sample_width: {sample_width} bytes' ) | |
| print(f' sample_rate: {sample_rate} Hz') | |
| print(f' num_frames: {num_frames}' ) | |
| print(f' duration: {num_frames / sample_rate:.02f} s') | |
| frames = f.readframes(num_frames) | |
| assert(len(frames) == num_frames * num_channels * sample_width) | |
| fmt = {1: 'b', 2: 'h', 4: 'i', 8: 'q'}[sample_width] | |
| samples = struct.unpack_from(f'<{num_frames * num_channels}{fmt}', frames) | |
| assert(len(samples) == num_frames * num_channels) | |
| print('channels:') | |
| for i in range(num_channels): | |
| channel = samples[i::num_channels] | |
| assert(len(channel) == num_frames) | |
| data = struct.pack(f'<{num_frames}{fmt}', *channel) | |
| print(f' #{i}: min={min(samples)} max={max(samples)} rms={rms(samples):.02f} md5={md5(data)}') | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment