add WAVE_byte_swap.py
This commit is contained in:
parent
c8e2a79999
commit
dbbc9069bc
|
@ -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)
|
||||
|
Loading…
Reference in New Issue