import numpy as np import pyaudio import argparse def generate_tone(frequency, duration, sample_rate=44100, volume=0.5): # Generate the time axis t = np.linspace(0, duration, int(sample_rate * duration), False) # Generate the sine wave tone = volume * np.sin(2 * np.pi * frequency * t) # Ensure that we have the correct format tone = np.int16(tone * 32767) # Initialize PyAudio p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paInt16, channels=1, rate=sample_rate, output=True) # Play the tone stream.write(tone.tobytes()) # Cleanup stream.stop_stream() stream.close() p.terminate() if __name__ == "__main__": parser = argparse.ArgumentParser(description="Generate a sine wave tone of a specific frequency.") parser.add_argument("frequency", type=float, help="Frequency of the sine wave in Hz.") parser.add_argument("duration", type=float, help="Duration of the tone in seconds.") args = parser.parse_args() generate_tone(args.frequency, args.duration)