function [p,next_delay] = sw_source(pitch,Fs,frlen,init_delay)
% SW_SOURCE     Creates source signal from pitch/voicing data
%               Source switches between noise and pulse train
%
%               [p new_delay] = sw_source(pitch,frlen,init_delay)
%               pitch is vector of pitch values in Hz
%               Fs is sampling frequency in Hz
%               frlen is framelength in samples
%               init_delay is initial delay to first pulse in samples

nframes = length(pitch);
p = zeros(frlen*nframes,1);
if nargin < 4
	init_delay = 0;
end

next_delay = init_delay;
for i = 1:length(pitch)
	if pitch(i) > 0
           pitchPeriod = floor(Fs/pitch(i));
	   [source,next_delay] = pulse_train(pitchPeriod,frlen,next_delay);
	else
		source = randn(1,frlen);
	end

	if ~(norm(source) == 0)
		source = source/norm(source);
	else
		source = 0*source;
	end
	p((i-1)*frlen+1:i*frlen) = source;
end


