Last active
January 16, 2024 10:45
-
-
Save trueroad/0534d90aacf7e6101f75a47f67af0035 to your computer and use it in GitHub Desktop.
Check parameters of all zipped WAV files as expected.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Check parameters of all zipped WAV files as expected. | |
https://gist.github.com/trueroad/0534d90aacf7e6101f75a47f67af0035 | |
Copyright (C) 2024 Masamichi Hosoda. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions | |
are met: | |
* Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
ARE DISCLAIMED. | |
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
SUCH DAMAGE. | |
""" | |
from pathlib import Path | |
import sys | |
from typing import Final, IO, Optional | |
import wave | |
import zipfile | |
EXPECTED_PARAMETER_NCHANNELS: Final[list[int]] = [1] | |
EXPECTED_PARAMETER_SAMPWIDTH: Final[list[int]] = [2] | |
EXPECTED_PARAMETER_FRAMERATE: Final[list[int]] = [48000, 44100] | |
EXPECTED_PARAMETER_NFRAMES_MIN: Final[Optional[int]] = 0 | |
EXPECTED_PARAMETER_NFRAMES_MAX: Final[Optional[int]] = 0 | |
def main() -> None: | |
"""Do main.""" | |
zip_filename: str | |
for zip_filename in sys.argv[1:]: | |
print(f'{zip_filename}') | |
zf: zipfile.ZipFile | |
with zipfile.ZipFile(zip_filename, 'r') as zf: | |
contents_filename: str | |
for contents_filename in zf.namelist(): | |
if Path(contents_filename).suffix.lower() != '.wav': | |
continue | |
zef: IO[bytes] | |
with zf.open(contents_filename, 'r') as zef: | |
wr: wave.Wave_read | |
with wave.open(zef, 'r') as wr: | |
if wr.getnchannels() not in \ | |
EXPECTED_PARAMETER_NCHANNELS: | |
print(f'Unexpected: {contents_filename}: ' | |
f'nchannels = {wr.getnchannels()}') | |
if wr.getsampwidth() not in \ | |
EXPECTED_PARAMETER_SAMPWIDTH: | |
print(f'Unexpected: {contents_filename}: ' | |
f'sampwidth = {wr.getsampwidth()}') | |
if wr.getframerate() not in \ | |
EXPECTED_PARAMETER_FRAMERATE: | |
print(f'Unexpected: {contents_filename}: ' | |
f'framerate = {wr.getframerate()}') | |
ex_min: Optional[int] = \ | |
EXPECTED_PARAMETER_NFRAMES_MIN | |
ex_max: Optional[int] = \ | |
EXPECTED_PARAMETER_NFRAMES_MAX | |
if ((ex_min is not None and | |
wr.getnframes() < ex_min) or | |
(ex_max is not None and | |
wr.getnframes() > ex_max)): | |
print(f'Unexpected: {contents_filename}: ' | |
f'nframes = {wr.getnframes()}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment