25 lines
973 B
Python
25 lines
973 B
Python
# tone.py
|
||
import numpy as np
|
||
import sounddevice as sd
|
||
|
||
def play_tone(frequency=440.0, duration=1.0, volume=0.2, sample_rate=44_100):
|
||
"""
|
||
Spielt einen Sinuston ab.
|
||
frequency: Hz
|
||
duration: Sekunden
|
||
volume: 0.0–1.0
|
||
sample_rate: Abtastrate in Hz
|
||
"""
|
||
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
|
||
wave = (volume * np.sin(2 * np.pi * frequency * t)).astype(np.float32)
|
||
sd.play(wave, samplerate=sample_rate, blocking=True)
|
||
|
||
if __name__ == "__main__":
|
||
import argparse
|
||
ap = argparse.ArgumentParser(description="Spiele einen Ton")
|
||
ap.add_argument("-f", "--frequency", type=float, default=440.0, help="Frequenz in Hz (z.B. 440)")
|
||
ap.add_argument("-t", "--time", type=float, default=1.0, help="Dauer in Sekunden (z.B. 1.5)")
|
||
ap.add_argument("-v", "--volume", type=float, default=0.2, help="Lautstärke 0.0–1.0")
|
||
args = ap.parse_args()
|
||
play_tone(args.frequency, args.time, args.volume)
|