Python Ton Generator
Warning: Undefined array key "title2" in /var/www/vhosts/eygdtiog.host282.checkdomain.de/tillhagenhoffmann.de/projects/sounds.php on line 122
Warning: Undefined array key "section_1" in /var/www/vhosts/eygdtiog.host282.checkdomain.de/tillhagenhoffmann.de/projects/sounds.php on line 132
Warning: Undefined array key "title2" in /var/www/vhosts/eygdtiog.host282.checkdomain.de/tillhagenhoffmann.de/projects/sounds.php on line 122
import numpy as np
SAMPLE_RATE = 44100
def sound():
duration = 2
t = np.linspace(
0,
duration,
int(SAMPLE_RATE * duration),
False
)
freq = 440
signal = (
1.0 * np.sin(2 * np.pi * freq * t)
+ 0.8 * np.sin(2 * np.pi * freq * 2 * t)
+ 0.6 * np.sin(2 * np.pi * freq * 3 * t)
+ 0.4 * np.sin(2 * np.pi * freq * 4 * t)
+ 0.2 * np.sin(2 * np.pi * freq * 5 * t)
)
env = np.ones(len(signal))
fade_in = int(0.02 * SAMPLE_RATE)
fade_out = int(0.20 * SAMPLE_RATE)
env[:fade_in] = np.linspace(
0,
1,
fade_in
)
env[-fade_out:] = np.linspace(
1,
0,
fade_out
)
signal *= env
signal /= np.max(np.abs(signal))
return signal * 0.3
Warning: Undefined array key "section_1" in /var/www/vhosts/eygdtiog.host282.checkdomain.de/tillhagenhoffmann.de/projects/sounds.php on line 132
Warning: Undefined array key "section_2" in /var/www/vhosts/eygdtiog.host282.checkdomain.de/tillhagenhoffmann.de/projects/sounds.php on line 141
import os
import tkinter as tk
from tkinter import ttk, messagebox
import importlib
import numpy as np
from scipy.io.wavfile import write
SAMPLE_RATE = 44100
def create_wav():
sound_name = combo.get()
if not sound_name:
messagebox.showwarning(
"Hinweis",
"Bitte einen Sound auswählen."
)
return
sound_module = importlib.import_module(
f"sounds.{sound_name}"
)
sound = sound_module.sound()
sound /= np.max(np.abs(sound))
audio = (sound * 32767).astype(np.int16)
# Ordner "wav" erstellen, falls er nicht existiert
os.makedirs("wav", exist_ok=True)
base_name = sound_name
output_file = os.path.join(
"wav",
f"{base_name}.wav"
)
counter = 1
while os.path.exists(output_file):
output_file = os.path.join(
"wav",
f"{base_name} ({counter}).wav"
)
counter += 1
# WAV-Datei speichern
write(
output_file,
SAMPLE_RATE,
audio
)
messagebox.showinfo(
"Fertig",
f"{output_file} wurde erstellt."
)
files = sorted([
f[:-3]
for f in os.listdir("sounds")
if f.endswith(".py") and not f.startswith("__")
])
root = tk.Tk()
root.title("WAV Creator")
root.geometry("350x160")
ttk.Label(
root,
text="Sound auswählen:"
).pack(pady=(15,5))
combo = ttk.Combobox(
root,
values=files,
state="readonly"
)
combo.pack(fill="x", padx=20)
if files:
combo.current(0)
ttk.Button(
root,
text="WAV erstellen",
command=create_wav
).pack(pady=20)
root.mainloop()