classdef sigbinop < signal
	properties (GetAccess=private, SetAccess=immutable)
		op
		sig1
		sig2
		chans
	end
	methods
		function s=sigbinop(f,sig1,sig2,chf)
			if isinf(unify_rates(rate(sig1),rate(sig2))),
				error('Sample rate mismatch');
			end
			if nargin<4, 
				chf=@(c1,c2)size(f(zeros(c1,1),zeros(c2,1)),1);
			end
			s.op=f;
			s.sig1=sig1;
			s.sig2=sig2;
			s.chans=chf(channels(sig1),channels(sig2)); 
		end

		function c=channels(s), c=s.chans; end
		function r=rate(s), r=rate(s.sig1); end
		function s=construct(sig)

			s1=construct(sig.sig1);
			s2=construct(sig.sig2);
			op=sig.op;

			s.start   = @start;
			s.stop    = @stop;
			s.dispose = @dispose;
			s.reader = @reader;

			function start, s1.start(); s2.start(); end
			function stop, s1.stop(); s2.stop(); end
			function dispose, s1.dispose(); s2.dispose(); end 
			function r=reader(n)
				r1=s1.reader(n);
				r2=s2.reader(n);
				r =@next;
				function [x,rem]=next
					[x1,rem1]=r1();
					[x2,rem2]=r2();
					x=op(x1,x2);
					rem=max(rem1,rem2);
				end
			end
		end
		function s=tostring(sig)
			s=sprintf('(%s <%s> %s)',tostring(sig.sig1),tostring(sig.op),tostring(sig.sig2));
		end
	end
end
