Skip to content

Instantly share code, notes, and snippets.

@trueroad
Last active November 1, 2023 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trueroad/eb14a5e3714f5f5c6671d74c3b86c079 to your computer and use it in GitHub Desktop.
Save trueroad/eb14a5e3714f5f5c6671d74c3b86c079 to your computer and use it in GitHub Desktop.
Compare WAVs.
#!/usr/bin/env python3
"""
Compare WAVs.
https://gist.github.com/trueroad/eb14a5e3714f5f5c6671d74c3b86c079
Copyright (C) 2023 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.
"""
import sys
import wave
def main() -> None:
"""Do main."""
print('Compare WAVs.\n\n'
'https://gist.github.com/trueroad/'
'eb14a5e3714f5f5c6671d74c3b86c079\n\n'
'Copyright (C) 2023 Masamichi Hosoda.\n'
'All rights reserved.\n')
if len(sys.argv) != 3:
print('Usage: ./compare-wav.py [INPUT1.wav INPUT2.wav]')
sys.exit(1)
filename_in1: str = sys.argv[1]
filename_in2: str = sys.argv[2]
print(f'Input1 filename: {filename_in1}\n'
f'Input2 filename: {filename_in2}\n')
b_diff: bool = False
wf1: wave.Wave_read = wave.open(filename_in1, mode='rb')
wf2: wave.Wave_read = wave.open(filename_in2, mode='rb')
if wf1.getnchannels() != wf2.getnchannels():
print('Different number of channels:'
f' {wf1.getnchannels()} vs {wf2.getnchannels()}')
b_diff = True
if wf1.getsampwidth() != wf2.getsampwidth():
print('Different sample width:'
f' {wf1.getsampwidth()} vs {wf2.getsampwidth()}')
b_diff = True
if wf1.getframerate() != wf2.getframerate():
print('Different frame rate:'
f' {wf1.getframerate()} vs {wf2.getframerate()}')
b_diff = True
if wf1.getnframes() != wf2.getnframes():
print('Different number of frames:'
f' {wf1.getnframes()} vs {wf2.getnframes()}')
b_diff = True
if wf1.getcomptype() != wf2.getcomptype():
print('Different compression type:'
f' {wf1.getcomptype()} vs {wf2.getcomptype()}')
b_diff = True
buf1: bytes = wf1.readframes(-1)
buf2: bytes = wf2.readframes(-1)
if buf1 != buf2:
print('Different frame data.')
b_diff = True
if b_diff:
print('\nDifferent.')
sys.exit(1)
print('\nNo different.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment