function Y=specsynth(S,gen,N,M)
% specsynth - Spectral synthesis by FIR filtering white noise 
%
% specsynth ::
%    seq([[L]])    ~'sequence of L-point power spectra',
%    (N:natural=>[[1,N]]) ~'generator of noise buffers',
%    N:natural     ~'size of buffers of noise signals to take'
%    M:natural     ~'frame overlap'
% -> seq([[1,M]])  ~'sequence of signal buffers'.

% NOTE: this should really take gen to be seq natural -> seq [[1,_]]
% eg
% gen = @(n)rndzip(sampler(gaussian),n,getrndstate)
% this should be  the basic type of a buffered signal generator

if nargin<4, M=N; end
U=rndmap(gen,repeat(N),getrndstate);
Y=unbuffer(zipwith(@conv,map(@(s)spec2fir(sqrt(s)),S),U),M);
