Ho fatto un piccolo script che consente di anteporre il segnale a un silenzio per ottenere la durata target in secondi. Usa la funzione scipy per leggere il file wav.
#!/usr/bin/env python
from __future__ import print_function, division
import scipy.io.wavfile as wavf
import numpy as np
from sys import argv
def pad_audio(data, fs, T):
# Calculate target number of samples
N_tar = int(fs * T)
# Calculate number of zero samples to append
shape = data.shape
# Create the target shape
N_pad = N_tar - shape[0]
print("Padding with %s seconds of silence" % str(N_pad/fs))
shape = (N_pad,) + shape[1:]
# Stack only if there is something to append
if shape[0] > 0:
if len(shape) > 1:
return np.vstack((np.zeros(shape),
data))
else:
return np.hstack((np.zeros(shape),
data))
else:
return data
if __name__ == "__main__":
if len(argv) != 4:
print("Wrong arguments.")
print("Use: %s in.wav out.wav target_time_s" % argv[0])
else:
in_wav = argv[1]
out_wav = argv[2]
T = float(argv[3])
# Read the wav file
fs, in_data = wavf.read(in_wav)
# Prepend with zeros
out_data = pad_audio(in_data, fs, T)
# Save the output file
wavf.write(out_wav, fs, out_data)
Avrei pensato che fosse meglio mettere il silenzio alla fine, altrimenti l'ascoltatore deve sopportare un periodo di silenzio sconosciuto prima di essere sorpreso dal suono reale. In ogni caso, puoi provare a farlo creando/riproducendo un file WAV che ha un numero sufficiente di campioni alla frequenza di campionamento che stai utilizzando per una durata di (10-L) secondi, dove L è la durata del file audio effettivo. Che cosa hai provato? PS c'è un pacchetto Python Standard Library chiamato _wave_ a cui potresti essere interessato. L'ho trovato cercando: python wav audio. Hai provato a cercare? – barny