Last active
August 27, 2018 15:15
-
-
Save aoirint/d8c3402bc2d7a43e85f19c179cad298f 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
| if __name__ == '__main__': | |
| import sys | |
| file = sys.argv[1] | |
| class Wave: | |
| def __init__(self, file): | |
| with open(file, 'rb') as fp: | |
| self.riff = fp.read(4).decode('ascii') | |
| self.size = int.from_bytes(fp.read(4), 'little') | |
| self.identity = fp.read(4).decode('ascii') | |
| self.fmt = fp.read(4).decode('ascii') | |
| self.fmt_bytes = int.from_bytes(fp.read(4), 'little') | |
| self.format_id = int.from_bytes(fp.read(2), 'little') | |
| self.channel_num = int.from_bytes(fp.read(2), 'little') | |
| self.sampling_rate = int.from_bytes(fp.read(4), 'little') | |
| self.velocity = int.from_bytes(fp.read(4), 'little') | |
| self.block_size = int.from_bytes(fp.read(2), 'little') | |
| self.bitpersample = int.from_bytes(fp.read(2), 'little') | |
| if self.format_id == 1: # Linear PCM | |
| self.extsize = 0 | |
| self.ext = b'' | |
| else: | |
| self.extsize = int.from_bytes(fp.read(2), 'little') | |
| self.ext = fp.read(self.extsize) | |
| self.data = fp.read(4).decode('ascii') | |
| self.wavesize = int.from_bytes(fp.read(4), 'little') | |
| self.wave = fp.read(self.wavesize) | |
| def meta(self): | |
| return { | |
| 'RIFF': self.riff, | |
| 'FileSize': self.size, | |
| 'Identity': self.identity, | |
| 'FMT': self.fmt, | |
| 'FMTSize': self.fmt_bytes, | |
| 'FormatID': self.format_id, | |
| 'Channel': self.channel_num, | |
| 'SamplingRate': self.sampling_rate, | |
| 'DataVelocity': self.velocity, | |
| 'BlockSize': self.block_size, | |
| 'BitPerSample': self.bitpersample, | |
| 'ExtensionSize': self.extsize, | |
| 'Data': self.data, | |
| 'WaveSize': self.wavesize | |
| } | |
| wave = Wave(file) | |
| import json | |
| js = json.dumps(wave.meta(), indent=True) | |
| print(js) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment