Noise Synthesis With SoX

SoX is a command line utility primarily used to convert audio file formats. SoX also includes functionality to apply various effects to audio files and perform basic sound synthesis. The following Perl script generates a series of of WAV files each containing a bandpass filtered white noise signal of randomized (between the specified minimum and maximum) duration, center frequency and bandwidth. The files are then concatenated to create the final output WAV file. After concatenation is complete, the noise segment files are deleted.

#!/usr/bin/perl 
use strict;use warnings;

if( '-h' eq $ARGV[0]){
	print "Usage: noise-seg.pl <output filename> <segs (max 32)> <min dur per seg> <max dur per seg> <min center> <max center> <min bw> <max bw>\n"; exit;
}

my $output_filename = shift or die "Must specify output filename\n";

my $segs = shift || 32;
my $min_dur = shift || 0.025;
my $max_dur = shift || 0.3;
my $min_center = shift || 100;
my $max_center = shift || 9000;
my $min_bandwidth = shift || 10;
my $max_bandwidth = shift || 1000;

# Make a temporary directory to store the segments

my $tmp_dir = '/tmp/noise-seg.'.(int(rand(1000))+1000);
if(! -e $tmp_dir){ mkdir($tmp_dir) }
else { die "$tmp_dir exists. Please clean up." }

my $files_to_be_concatenated = '';

for(my $i=0; $i<$segs; $i++){
	my $dur = rand($max_dur) + $min_dur;
	my $center = int(rand($max_center - $min_center)) + $min_center;
	my $bandwidth = int(rand($max_bandwidth - $min_bandwidth)) + $min_bandwidth;
	my $seg_filename = "$tmp_dir/temp$i.wav";
	$files_to_be_concatenated .= "$seg_filename ";
	`sox -n $seg_filename synth $dur noise bandpass $center $bandwidth`;
}

# Concatenate the segments
`sox -m $files_to_be_concatenated $output_filename`; 	

# Clean up 
`rm -rf $tmp_dir`;	

Installing SoX Under Debian or Ubuntu

To install SoX and support for all available audio formats:
sudo apt-get install sox libsox-fmt-all