Skip to content
angelotom56 edited this page Jul 20, 2026 · 1 revision

Welcome to the python-soundfile wiki!#!/usr/bin/env python3 import numpy as np from pydub import AudioSegment from pydub.generators import Sine SR = 44100 DUR = 20.0 # durata totale A = AudioSegment.silent(duration=int(DUR*1000))

Parametri sirena bitonale italiana (di–sol)

F1 = 740.0 F2 = 554.37 CYCLE_S = 1.6 # periodo d’alternanza dei toni START_DIST = 0.6 # livello base (distanza iniziale media) def sine_np(freq, t): return np.sin(2np.pifreqt) def siren_segment(start_s, dur_s, approach=True): n = int(SRdur_s) t = np.linspace(0, dur_s, n, endpoint=False) # Alternanza morbida fra i due toni w = 0.5*(1.0 + np.sin(2np.pi(1.0/CYCLE_S)t)) sig = wsine_np(F1, t) + (1.0-w)sine_np(F2, t) # Invilluppo distanza: avvicinamento o allontanamento if approach: env = np.linspace(START_DIST, 1.0, n) else: env = np.linspace(1.0, 0.3, n) # Lieve tremolo per realismo trem = 0.9 + 0.1np.sin(2np.pi0.8t) x = sigenvtrem0.6 seg = AudioSegment( (x32767).astype(np.int16).tobytes(), frame_rate=SR, sample_width=2, channels=1 ) return int(start_s1000), seg def door_sfx(t_s, open=True): # Effetto porta: breve burst con attacco secco d = 0.11 n = int(SRd) t = np.linspace(0, d, n, endpoint=False) base = np.sin(2np.pi*(180 if open else 150)t) * np.exp(-12t) noise = np.random.randn(n)0.03np.exp(-25t) x = (base + noise) * (1.0 if open else 1.2) seg = AudioSegment( (x32767).astype(np.int16).tobytes(), frame_rate=SR, sample_width=2, channels=1 ).apply_gain(+4 if open else +6) return int(t_s*1000), seg

0–8 s: avvicinamento

pos, seg = siren_segment(0.0, 8.0, approach=True) A = A.overlay(seg, position=pos)

8–12 s: sosta con porte

apertura ~9.0 s, chiusura ~10.5 s

for tt, is_open in [(9.0, True), (10.5, False)]: p, ds = door_sfx(tt, open=is_open) A = A.overlay(ds, position=p)

12–20 s: ripartenza/allontanamento

pos, seg = siren_segment(12.0, 8.0, approach=False) A = A.overlay(seg, position=pos)

Normalizza a picco ~-3 dBFS

peak = A.max_dBFS target_peak = -3.0 A = A.apply_gain(target_peak - peak)

Esporta MP3 a 320 kbps (fallback WAV se manca encoder MP3)

out_mp3 = "ambulanza_20s.mp3" try: A.export(out_mp3, format="mp3", bitrate="320k") print(f"Creato: {out_mp3}") except Exception as e: print("Export MP3 non disponibile, salvo WAV (puoi convertire in MP3 in un secondo momento).") out_wav = "ambulanza_20s.wav" A.export(out_wav, format="wav") print(f"Creato: {out_wav}")