How to add silence in front of a wav file

I did a small script which allows you to prepend your signal with a silence in order to obtain the target duration in seconds. It uses the scipy function for reading of the wav file.

#!/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)

Tags:

Python

Audio

Wav