1
0
Fork 0

add WAVE_byte_swap.py

This commit is contained in:
Intel A80486DX2-66 2024-01-21 20:30:39 +03:00
parent c8e2a79999
commit dbbc9069bc
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
1 changed files with 39 additions and 0 deletions

39
src/WAVE_byte_swap.py Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bin/python3
from os.path import basename
from sys import argv
import numpy as np
import wave
def main(argv: list) -> None:
show_help = (argc := len(argv)) != 3
show_help = show_help or (argc >= 2 and argv[1] in ["-h", "--help"])
if show_help:
print("Usage:", basename(argv[0]), "<input file name> "
"<output file name>")
return
input_wave = wave.open(argv[1], "rb")
nchannels, sampwidth, framerate, nframes = input_wave.getparams()[:4]
if sampwidth == 2:
np_type = np.int16
else:
print("Unsupported bit depth:", 8 * sampwidth)
return
audio_data = input_wave.readframes(nframes)
audio_array = np.frombuffer(audio_data, dtype=np_type).byteswap()
output_wave = wave.open(argv[2], "wb")
output_wave.setparams(
(nchannels, sampwidth, framerate, 0, "NONE", "not compressed"))
output_wave.writeframes(audio_array.tobytes())
output_wave.close()
print("Done")
if __name__ == "__main__":
main(argv)