# # Band-limited square waves (Python) # # Copyright (c) 2020 Project Nayuki # All rights reserved. Contact Nayuki for licensing. # https://www.nayuki.io/page/band-limited-square-waves # import math, pathlib, struct, sys from typing import List def main(args: List[str]) -> None: # Check number of command line arguments if len(args) not in (5, 6): sys.exit("""Usage: python generate-square-wave.py Frequency DutyCycle SampleRate Duration [BandLimited/Naive] Output.wav Example: python generate-square-wave.py 440.0 0.5 48000 1.0 BandLimited Output.wav""") # Get all command line arguments frequency: float = float(args[0]) dutycycle: float = float(args[1]) samplerate: int = int(args[2]) duration: float = float(args[3]) mode: str outfilepath: str if len(args) == 5: mode = "BandLimited" outfilepath = args[4] elif len(args) == 6: mode = args[4] outfilepath = args[5] else: raise AssertionError() # Check value ranges if frequency <= 0: raise ValueError("Frequency must be positive") if not (0 <= dutycycle <= 1): raise ValueError("Duty cycle must be between 0 and 1") if samplerate <= 0: raise ValueError("Sample rate must be positive") if duration < 0: raise ValueError("Duration must be non-negative") if mode not in ("BandLimited", "Naive"): raise ValueError("Invalid mode specification") # Start writing file data numsamples: int = int(round(duration * samplerate)) with pathlib.Path(outfilepath).open("wb") as fout: # Write WAV header fout.write(b"RIFF") fout.write(struct.pack("