From 38a12738d3d704ce2993503aa1df813d9b42b9b4 Mon Sep 17 00:00:00 2001 From: Maksym Sobolyev Date: Sun, 21 Apr 2024 21:18:26 -0700 Subject: [PATCH] Add test for the python module. --- .github/workflows/build_and_test.yml | 3 ++ test.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test.py diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index d3f6a1c..14afb95 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -86,3 +86,6 @@ jobs: - name: install run: pip install python/ + + - name: test + run: ./scripts/do-test.sh "${PYTHON_CMD} test.py" diff --git a/test.py b/test.py new file mode 100644 index 0000000..50c8b99 --- /dev/null +++ b/test.py @@ -0,0 +1,46 @@ +import argparse +import struct +from G722 import G722 + +def main(): + parser = argparse.ArgumentParser(description='G.722 codec utility') + parser.add_argument('input_file', type=str, help='Input file path') + parser.add_argument('output_file', type=str, help='Output file path') + parser.add_argument('--sln16k', action='store_true', help='Use 16000 Hz sample rate') + parser.add_argument('--encode', action='store_true', help='Encode mode') + parser.add_argument('--bend', action='store_true', help='Use big-endian byte order') + args = parser.parse_args() + + sample_rate = 16000 if args.sln16k else 8000 + bit_rate = 64000 # Example bit rate + + try: + with open(args.input_file, 'rb') as fi, open(args.output_file, 'wb') as fo: + codec = G722(sample_rate, bit_rate) + if args.encode: + while True: + pcm_data = fi.read(2 * 1024) # Read blocks of 2048 bytes + if not pcm_data: + break + # Convert samples to little or big endian based on flag + format_string = '>' if args.bend else '<' + pcm_samples = struct.unpack(format_string + 'h' * (len(pcm_data) // 2), pcm_data) + encoded_data = codec.encode(pcm_samples) + fo.write(encoded_data) + else: + while True: + encoded_data = fi.read(1024) # Read blocks of 1024 bytes + if not encoded_data: + break + decoded_samples = codec.decode(encoded_data) + # Convert samples to little or big endian based on flag + format_string = '>' if args.bend else '<' + output_data = struct.pack(format_string + 'h' * len(decoded_samples), *decoded_samples) + fo.write(output_data) + + except IOError as e: + print(f"File error: {e}", file=sys.stderr) + sys.exit(1) + +if __name__ == "__main__": + main()