function a=audiorec(ch,rate,N,M,varargin)
	if nargin<4, M=N; end
	opts=options('buffer',1,'queue',4,'dspargs',{},varargin{:});

	a=asysobj(@(sz)dsp.AudioRecorder('OutputDataType','double','NumChannels',ch,'SampleRate',rate, ...
					'SamplesPerFrame',M,'BufferSizeSource','Property', ...
					'BufferSize',   opts.buffer*M, ...
					'QueueDuration',opts.queue*M/rate, ...
					opts.dspargs{:}), ...
			0,1,@(sz){[M,ch]});

	if N>M
		OL=M+1:N;
		a=a*loop(@upd,@(sz)zeros(N-M,ch));
	elseif N<M
		BLOCK=M-N+1:M;
		a=a*arr(@(x)x(BLOCK,:));
	end

	function [y,s]=upd(x,s)
		y=[s;x];
		s=y(OL,:);
	end
end

